[gjs/wip/ptomato/mozjs31: 8/9] js: Call JS_Init() and JS_ShutDown()



commit 107e8b04a30bbb4549935b692196231c842d396a
Author: Philip Chimento <philip endlessm com>
Date:   Fri Nov 4 19:02:55 2016 -0700

    js: Call JS_Init() and JS_ShutDown()
    
    Starting with mozjs31, JS_Init() is required. Calling JS_ShutDown() on
    exit is not required, but may become so in the future.
    
    This makes two new public functions in the API which do the necessary
    initialization and shutdown, because gnome-shell will need to call them
    as well.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=751252

 NEWS                         |   13 +++++++++++++
 gjs/console.cpp              |   10 +++++++---
 gjs/context.cpp              |   21 +++++++++++++++++++--
 gjs/context.h                |    3 +++
 installed-tests/gjs-unit.cpp |    4 ++++
 test/gjs-tests.cpp           |    2 ++
 6 files changed, 48 insertions(+), 5 deletions(-)
---
diff --git a/NEWS b/NEWS
index aaaefc2..0d580e2 100644
--- a/NEWS
+++ b/NEWS
@@ -54,6 +54,19 @@ NEXT
   arguments into account for the time being, while still passing them on to the
   script. A warning will be logged if you are using the deprecated behaviour.
 
+- Backwards-incompatible change for GJS embedders such as gnome-shell:
+  You must now call gjs_init() before calling any other GJS functions, and call
+  gjs_shutdown() after you release your last reference to a GjsContext.
+
+  Calling System.exit() from JS code will now not abort the program immediately,
+  but instead will return immediately from gjs_context_eval() so that you can
+  unref your GjsContext and call gjs_shutdown().
+
+  If gjs_context_eval() or gjs_context_eval_file() returns false but the 'error'
+  out parameter is NULL, it means that the JS code called System.exit(). The
+  exit code will be found in the 'exit_status_p' out parameter to
+  gjs_context_eval() or gjs_context_eval_file().
+
 Version 1.46.0
 --------------
 
diff --git a/gjs/console.cpp b/gjs/console.cpp
index d60840b..bfa8d00 100644
--- a/gjs/console.cpp
+++ b/gjs/console.cpp
@@ -242,6 +242,8 @@ main(int argc, char **argv)
     /* This should be removed after a suitable time has passed */
     check_script_args_for_stray_gjs_args(script_argc, script_argv);
 
+    gjs_init();
+
     js_context = (GjsContext*) g_object_new(GJS_TYPE_CONTEXT,
                                             "search-path", include_path,
                                             "program-name", program_name,
@@ -290,9 +292,10 @@ main(int argc, char **argv)
     /* evaluate the script */
     if (!gjs_context_eval(js_context, script, len,
                           filename, &code, &error)) {
-        code = 1;
-        g_printerr("%s\n", error->message);
-        g_clear_error(&error);
+        if (error != NULL) {
+            g_printerr("%s\n", error->message);
+            g_clear_error(&error);
+        }
         goto out;
     }
 
@@ -308,5 +311,6 @@ main(int argc, char **argv)
     g_strfreev(coverage_prefixes);
     g_object_unref(js_context);
     g_free(script);
+    gjs_shutdown();
     exit(code);
 }
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 5c8c267..df3d44d 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -671,8 +671,12 @@ gjs_context_eval(GjsContext   *js_context,
     if (!gjs_eval_with_scope(js_context->context, JS::NullPtr(), script,
                              script_len, filename, &retval)) {
         uint8_t code;
-        if (_gjs_context_should_exit(js_context, &code))
-            exit(code);
+        if (_gjs_context_should_exit(js_context, &code)) {
+            /* exit_status_p is public API so can't be changed, but should be
+             * uint8_t, not int */
+            *exit_status_p = code;
+            goto out;  /* Don't log anything */
+        }
 
         gjs_log_exception(js_context->context);
         g_set_error(error,
@@ -819,3 +823,16 @@ gjs_get_import_global(JSContext *context)
     GjsContext *gjs_context = (GjsContext *) JS_GetContextPrivate(context);
     return gjs_context->global;
 }
+
+void
+gjs_init(void)
+{
+    if (!JS_Init())
+        g_error("Could not initialize Javascript");
+}
+
+void
+gjs_shutdown(void)
+{
+    JS_ShutDown();
+}
diff --git a/gjs/context.h b/gjs/context.h
index a459e5b..cf6c55d 100644
--- a/gjs/context.h
+++ b/gjs/context.h
@@ -76,6 +76,9 @@ void            gjs_context_maybe_gc              (GjsContext  *context);
 
 void            gjs_context_gc                    (GjsContext  *context);
 
+void gjs_init(void);
+void gjs_shutdown(void);
+
 void            gjs_dumpstack                     (void);
 
 G_END_DECLS
diff --git a/installed-tests/gjs-unit.cpp b/installed-tests/gjs-unit.cpp
index 034273f..29b41ae 100644
--- a/installed-tests/gjs-unit.cpp
+++ b/installed-tests/gjs-unit.cpp
@@ -164,6 +164,8 @@ main(int argc, char **argv)
     setlocale(LC_ALL, "");
     g_test_init(&argc, &argv, NULL);
 
+    gjs_init();
+
     /* Make sure to create the GjsContext class first, so we
      * can override the GjsPrivate lookup path.
      */
@@ -218,5 +220,7 @@ main(int argc, char **argv)
 
     g_type_class_unref (context_class);
 
+    gjs_shutdown();
+
     return retval;
 }
diff --git a/test/gjs-tests.cpp b/test/gjs-tests.cpp
index 64e9d96..68a543e 100644
--- a/test/gjs-tests.cpp
+++ b/test/gjs-tests.cpp
@@ -273,7 +273,9 @@ main(int    argc,
     gjs_test_add_tests_for_coverage ();
     gjs_test_add_tests_for_parse_call_args();
 
+    gjs_init();
     g_test_run();
+    gjs_shutdown();
 
     return 0;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]