[gjs/wip/ptomato/mozjs31prep: 1/2] js: Root importer define_meta_properties()



commit 95cdfc25510bd2625a6579c3d3b4cde930b4ec7b
Author: Philip Chimento <philip endlessm com>
Date:   Wed Oct 19 14:07:20 2016 -0700

    js: Root importer define_meta_properties()
    
    Starting out with define_meta_properties() in importer.cpp, we do another
    cascade of GC rooting changes.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=742249

 gi/object.cpp          |   16 ++-----
 gi/repo.cpp            |   12 ++---
 gi/repo.h              |    7 ++-
 gjs/byteArray.cpp      |    8 +--
 gjs/byteArray.h        |    4 +-
 gjs/gi.cpp             |    6 +-
 gjs/gi.h               |    9 ++--
 gjs/importer.cpp       |  111 ++++++++++++++++++++++++-----------------------
 gjs/importer.h         |   11 +++--
 gjs/native.cpp         |    6 +-
 gjs/native.h           |   10 ++--
 modules/cairo-module.h |    4 +-
 modules/cairo.cpp      |    9 +---
 modules/console.cpp    |   21 +++------
 modules/console.h      |    4 +-
 modules/system.cpp     |    9 +---
 modules/system.h       |    4 +-
 17 files changed, 114 insertions(+), 137 deletions(-)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index b977b78..ea39c2c 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -3016,19 +3016,11 @@ static JSFunctionSpec module_funcs[] = {
 };
 
 bool
-gjs_define_private_gi_stuff(JSContext *context,
-                            JSObject **module_out)
+gjs_define_private_gi_stuff(JSContext              *cx,
+                            JS::MutableHandleObject module)
 {
-    JSObject *module;
-
-    module = JS_NewObject (context, NULL, NULL, NULL);
-
-    if (!JS_DefineFunctions(context, module, &module_funcs[0]))
-        return false;
-
-    *module_out = module;
-
-    return true;
+    module.set(JS_NewObject(cx, NULL, NULL, NULL));
+    return JS_DefineFunctions(cx, module, &module_funcs[0]);
 }
 
 bool
diff --git a/gi/repo.cpp b/gi/repo.cpp
index 2c8ade5..78687ba 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -333,15 +333,11 @@ repo_new(JSContext *context)
 }
 
 bool
-gjs_define_repo(JSContext  *context,
-                JSObject  **module_out,
-                const char *name)
+gjs_define_repo(JSContext              *cx,
+                JS::MutableHandleObject repo,
+                const char             *name)
 {
-    JSObject *repo;
-
-    repo = repo_new(context);
-    *module_out = repo;
-
+    repo.set(repo_new(cx));
     return true;
 }
 
diff --git a/gi/repo.h b/gi/repo.h
index 867b8c1..3340b99 100644
--- a/gi/repo.h
+++ b/gi/repo.h
@@ -34,9 +34,10 @@
 
 G_BEGIN_DECLS
 
-bool        gjs_define_repo                     (JSContext      *context,
-                                                 JSObject      **module_out,
-                                                 const char     *name);
+bool gjs_define_repo(JSContext              *cx,
+                     JS::MutableHandleObject repo,
+                     const char             *name);
+
 const char* gjs_info_type_name                  (GIInfoType      type);
 JSObject*   gjs_lookup_private_namespace        (JSContext      *context);
 JSObject*   gjs_lookup_namespace_object         (JSContext      *context,
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index b9de83d..6d29205 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -827,13 +827,12 @@ static JSFunctionSpec gjs_byte_array_module_funcs[] = {
 };
 
 bool
-gjs_define_byte_array_stuff(JSContext  *context,
-                            JSObject  **module_out)
+gjs_define_byte_array_stuff(JSContext              *context,
+                            JS::MutableHandleObject module)
 {
-    JSObject *module;
     JSObject *prototype;
 
-    module = JS_NewObject (context, NULL, NULL, NULL);
+    module.set(JS_NewObject(context, NULL, NULL, NULL));
 
     prototype = JS_InitClass(context, module,
                              NULL,
@@ -852,6 +851,5 @@ gjs_define_byte_array_stuff(JSContext  *context,
     gjs_set_global_slot(context, GJS_GLOBAL_SLOT_BYTE_ARRAY_PROTOTYPE,
                         JS::ObjectOrNullValue(prototype));
 
-    *module_out = module;
     return true;
 }
diff --git a/gjs/byteArray.h b/gjs/byteArray.h
index 0c591a8..e143be3 100644
--- a/gjs/byteArray.h
+++ b/gjs/byteArray.h
@@ -34,8 +34,8 @@ bool        gjs_typecheck_bytearray(JSContext       *context,
                                     JS::HandleObject obj,
                                     bool             throw_error);
 
-bool          gjs_define_byte_array_stuff    (JSContext  *context,
-                                              JSObject  **module_out);
+bool gjs_define_byte_array_stuff(JSContext              *context,
+                                 JS::MutableHandleObject module);
 
 JSObject *    gjs_byte_array_from_byte_array (JSContext  *context,
                                               GByteArray *array);
diff --git a/gjs/gi.cpp b/gjs/gi.cpp
index a960f01..e2316d0 100644
--- a/gjs/gi.cpp
+++ b/gjs/gi.cpp
@@ -32,8 +32,8 @@
 #include "gi/repo.h"
 
 bool
-gjs_define_gi_stuff(JSContext      *context,
-                    JSObject      **module_out)
+gjs_define_gi_stuff(JSContext              *cx,
+                    JS::MutableHandleObject module)
 {
-    return gjs_define_repo(context, module_out, "gi");
+    return gjs_define_repo(cx, module, "gi");
 }
diff --git a/gjs/gi.h b/gjs/gi.h
index 44df221..22d1b70 100644
--- a/gjs/gi.h
+++ b/gjs/gi.h
@@ -30,10 +30,11 @@
 
 G_BEGIN_DECLS
 
-bool          gjs_define_gi_stuff     (JSContext      *context,
-                                       JSObject      **module_out);
-bool          gjs_define_private_gi_stuff   (JSContext     *context,
-                                             JSObject     **module_out);
+bool gjs_define_gi_stuff(JSContext              *cx,
+                         JS::MutableHandleObject module);
+
+bool gjs_define_private_gi_stuff(JSContext              *cx,
+                                 JS::MutableHandleObject module);
 
 G_END_DECLS
 
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index 1b8d72c..056ea0e 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -53,11 +53,11 @@ extern struct JSClass gjs_importer_class;
 GJS_DEFINE_PRIV_FROM_JS(Importer, gjs_importer_class)
 
 static bool
-define_meta_properties(JSContext  *context,
-                       JSObject   *module_obj,
-                       const char *full_path,
-                       const char *module_name,
-                       JSObject   *parent)
+define_meta_properties(JSContext       *context,
+                       JS::HandleObject module_obj,
+                       const char      *full_path,
+                       const char      *module_name,
+                       JS::HandleObject parent)
 {
     bool parent_is_module;
 
@@ -67,12 +67,13 @@ define_meta_properties(JSContext  *context,
     parent_is_module = parent && JS_InstanceOf(context, parent, &gjs_importer_class, NULL);
 
     gjs_debug(GJS_DEBUG_IMPORTER, "Defining parent %p of %p '%s' is mod %d",
-              parent, module_obj, module_name ? module_name : "<root>", parent_is_module);
+              parent.get(), module_obj.get(),
+              module_name ? module_name : "<root>", parent_is_module);
 
     if (full_path != NULL) {
-        if (!JS_DefineProperty(context, module_obj,
-                               "__file__",
-                               JS::StringValue(JS_NewStringCopyZ(context, full_path)),
+        JS::RootedValue file_val(context,
+            JS::StringValue(JS_NewStringCopyZ(context, full_path)));
+        if (!JS_DefineProperty(context, module_obj, "__file__", file_val,
                                NULL, NULL,
                                /* don't set ENUMERATE since we wouldn't want to copy
                                 * this symbol to any other object for example.
@@ -81,11 +82,15 @@ define_meta_properties(JSContext  *context,
             return false;
     }
 
+    JS::RootedValue module_name_val(context, JS::NullValue());
+    JS::RootedValue parent_module_val(context, JS::NullValue());
+    if (parent_is_module) {
+        module_name_val.setString(JS_NewStringCopyZ(context, module_name));
+        parent_module_val.setObject(*parent);
+    }
+
     if (!JS_DefineProperty(context, module_obj,
-                           "__moduleName__",
-                           parent_is_module ?
-                           JS::StringValue(JS_NewStringCopyZ(context, module_name)) :
-                           JS::NullValue(),
+                           "__moduleName__", module_name_val,
                            NULL, NULL,
                            /* don't set ENUMERATE since we wouldn't want to copy
                             * this symbol to any other object for example.
@@ -94,8 +99,7 @@ define_meta_properties(JSContext  *context,
         return false;
 
     if (!JS_DefineProperty(context, module_obj,
-                           "__parentModule__",
-                           parent_is_module ? JS::ObjectValue(*parent) : JS::NullValue(),
+                           "__parentModule__", parent_module_val,
                            NULL, NULL,
                            /* don't set ENUMERATE since we wouldn't want to copy
                             * this symbol to any other object for example.
@@ -107,10 +111,10 @@ define_meta_properties(JSContext  *context,
 }
 
 static bool
-import_directory(JSContext   *context,
-                 JSObject    *obj,
-                 const char  *name,
-                 const char **full_paths)
+import_directory(JSContext       *context,
+                 JS::HandleObject obj,
+                 const char      *name,
+                 const char     **full_paths)
 {
     JSObject *importer;
 
@@ -127,13 +131,13 @@ import_directory(JSContext   *context,
 }
 
 static bool
-define_import(JSContext  *context,
-              JSObject   *obj,
-              JSObject   *module_obj,
-              const char *name)
+define_import(JSContext       *context,
+              JS::HandleObject obj,
+              JS::HandleObject module_obj,
+              const char      *name)
 {
-    if (!JS_DefineProperty(context, obj,
-                           name, JS::ObjectValue(*module_obj),
+    JS::RootedValue module_val(context, JS::ObjectValue(*module_obj));
+    if (!JS_DefineProperty(context, obj, name, module_val,
                            NULL, NULL,
                            GJS_MODULE_PROP_FLAGS & ~JSPROP_PERMANENT)) {
         gjs_debug(GJS_DEBUG_IMPORTER,
@@ -212,11 +216,11 @@ cancel_import(JSContext  *context,
 }
 
 static bool
-import_native_file(JSContext  *context,
-                   JSObject   *obj,
-                   const char *name)
+import_native_file(JSContext       *context,
+                   JS::HandleObject obj,
+                   const char      *name)
 {
-    JSObject *module_obj;
+    JS::RootedObject module_obj(context);
     bool retval = false;
 
     gjs_debug(GJS_DEBUG_IMPORTER, "Importing '%s'", name);
@@ -374,16 +378,15 @@ load_module_elements(JSContext *context,
 }
 
 static bool
-import_file_on_module(JSContext  *context,
-                      JSObject   *obj,
-                      const char *name,
-                      GFile      *file)
+import_file_on_module(JSContext       *context,
+                      JS::HandleObject obj,
+                      const char      *name,
+                      GFile           *file)
 {
-    JSObject *module_obj;
     bool retval = false;
     char *full_path = NULL;
 
-    module_obj = create_module_object (context);
+    JS::RootedObject module_obj(context, create_module_object(context));
 
     if (!define_import(context, obj, module_obj, name))
         goto out;
@@ -409,10 +412,10 @@ import_file_on_module(JSContext  *context,
 }
 
 static bool
-do_import(JSContext  *context,
-          JSObject   *obj,
-          Importer   *priv,
-          const char *name)
+do_import(JSContext       *context,
+          JS::HandleObject obj,
+          Importer        *priv,
+          const char      *name)
 {
     char *filename;
     char *full_path;
@@ -1050,14 +1053,13 @@ gjs_get_search_path(void)
 }
 
 static JSObject*
-gjs_create_importer(JSContext    *context,
-                    const char   *importer_name,
-                    const char  **initial_search_path,
-                    bool          add_standard_search_path,
-                    bool          is_root,
-                    JSObject     *in_object)
+gjs_create_importer(JSContext       *context,
+                    const char      *importer_name,
+                    const char     **initial_search_path,
+                    bool             add_standard_search_path,
+                    bool             is_root,
+                    JS::HandleObject in_object)
 {
-    JSObject *importer;
     char **paths[2] = {0};
     char **search_path;
 
@@ -1069,7 +1071,7 @@ gjs_create_importer(JSContext    *context,
 
     search_path = gjs_g_strv_concat(paths, 2);
 
-    importer = importer_new(context, is_root);
+    JS::RootedObject importer(context, importer_new(context, is_root));
 
     /* API users can replace this property from JS, is the idea */
     if (!gjs_define_string_array(context, importer,
@@ -1087,11 +1089,11 @@ gjs_create_importer(JSContext    *context,
 }
 
 JSObject*
-gjs_define_importer(JSContext    *context,
-                    JSObject     *in_object,
-                    const char   *importer_name,
-                    const char  **initial_search_path,
-                    bool          add_standard_search_path)
+gjs_define_importer(JSContext       *context,
+                    JS::HandleObject in_object,
+                    const char      *importer_name,
+                    const char     **initial_search_path,
+                    bool             add_standard_search_path)
 
 {
     JSObject *importer;
@@ -1106,7 +1108,8 @@ gjs_define_importer(JSContext    *context,
         g_error("no memory to define importer property");
 
     gjs_debug(GJS_DEBUG_IMPORTER,
-              "Defined importer '%s' %p in %p", importer_name, importer, in_object);
+              "Defined importer '%s' %p in %p", importer_name, importer,
+              in_object.get());
 
     return importer;
 }
@@ -1137,7 +1140,7 @@ gjs_create_root_importer(JSContext   *context,
     importer = JS::ObjectValue(*gjs_create_importer(context, "imports",
                                                     initial_search_path,
                                                     add_standard_search_path,
-                                                    true, NULL));
+                                                    true, JS::NullPtr()));
     gjs_set_global_slot(context, GJS_GLOBAL_SLOT_IMPORTS, importer);
 
     JS_EndRequest(context);
diff --git a/gjs/importer.h b/gjs/importer.h
index 825a79a..fd0475d 100644
--- a/gjs/importer.h
+++ b/gjs/importer.h
@@ -38,11 +38,12 @@ bool      gjs_define_root_importer (JSContext   *context,
 bool      gjs_define_root_importer_object(JSContext        *context,
                                           JS::HandleObject  in_object,
                                           JS::HandleObject  root_importer);
-JSObject* gjs_define_importer      (JSContext   *context,
-                                    JSObject    *in_object,
-                                    const char  *importer_name,
-                                    const char **initial_search_path,
-                                    bool         add_standard_search_path);
+
+JSObject *gjs_define_importer(JSContext       *context,
+                              JS::HandleObject in_object,
+                              const char      *importer_name,
+                              const char     **initial_search_path,
+                              bool             add_standard_search_path);
 
 G_END_DECLS
 
diff --git a/gjs/native.cpp b/gjs/native.cpp
index 52adbc0..9f0f7bf 100644
--- a/gjs/native.cpp
+++ b/gjs/native.cpp
@@ -82,9 +82,9 @@ gjs_is_registered_native_module(JSContext  *context,
  * Return a native module that's been preloaded.
  */
 bool
-gjs_import_native_module(JSContext   *context,
-                         const char  *name,
-                         JSObject   **module_out)
+gjs_import_native_module(JSContext              *context,
+                         const char             *name,
+                         JS::MutableHandleObject module_out)
 {
     GjsDefineModuleFunc func;
 
diff --git a/gjs/native.h b/gjs/native.h
index 92a7827..6776a65 100644
--- a/gjs/native.h
+++ b/gjs/native.h
@@ -30,8 +30,8 @@
 
 G_BEGIN_DECLS
 
-typedef bool (* GjsDefineModuleFunc) (JSContext  *context,
-                                      JSObject  **module_out);
+typedef bool (* GjsDefineModuleFunc) (JSContext              *context,
+                                      JS::MutableHandleObject module_out);
 
 /* called on context init */
 void   gjs_register_native_module (const char            *module_id,
@@ -43,9 +43,9 @@ bool     gjs_is_registered_native_module(JSContext  *context,
                                          const char *name);
 
 /* called by importer.c to load a statically linked native module */
-bool     gjs_import_native_module (JSContext  *context,
-                                   const char *name,
-                                   JSObject   **module_out);
+bool gjs_import_native_module (JSContext              *context,
+                               const char             *name,
+                               JS::MutableHandleObject module_out);
 
 G_END_DECLS
 
diff --git a/modules/cairo-module.h b/modules/cairo-module.h
index f2613af..e99692f 100644
--- a/modules/cairo-module.h
+++ b/modules/cairo-module.h
@@ -23,7 +23,7 @@
 #ifndef __CAIRO_MODULE_H__
 #define __CAIRO_MODULE_H__
 
-bool  gjs_js_define_cairo_stuff (JSContext  *context,
-                                 JSObject  **module_out);
+bool gjs_js_define_cairo_stuff(JSContext              *context,
+                               JS::MutableHandleObject module);
 
 #endif /* __CAIRO_MODULE_H__ */
diff --git a/modules/cairo.cpp b/modules/cairo.cpp
index 7e4b915..c9babce 100644
--- a/modules/cairo.cpp
+++ b/modules/cairo.cpp
@@ -55,14 +55,13 @@ gjs_cairo_check_status(JSContext      *context,
 }
 
 bool
-gjs_js_define_cairo_stuff(JSContext *context,
-                          JSObject **module_out)
+gjs_js_define_cairo_stuff(JSContext              *context,
+                          JS::MutableHandleObject module)
 {
     JS::Value obj;
-    JSObject *module;
     JSObject *surface_proto, *pattern_proto, *gradient_proto;
 
-    module = JS_NewObject (context, NULL, NULL, NULL);
+    module.set(JS_NewObject(context, NULL, NULL, NULL));
 
     obj = gjs_cairo_region_create_proto(context, module,
                                         "Region", NULL);
@@ -142,7 +141,5 @@ gjs_js_define_cairo_stuff(JSContext *context,
     if (obj.isNull())
         return false;
 
-    *module_out = module;
-
     return true;
 }
diff --git a/modules/console.cpp b/modules/console.cpp
index 7dde126..24b9352 100644
--- a/modules/console.cpp
+++ b/modules/console.cpp
@@ -153,7 +153,7 @@ gjs_console_readline(JSContext *cx, char **bufp, FILE *file, const char *prompt)
 }
 #endif
 
-static bool
+static JSBool
 gjs_console_interact(JSContext *context,
                      unsigned   argc,
                      JS::Value *vp)
@@ -231,19 +231,10 @@ gjs_console_interact(JSContext *context,
 }
 
 bool
-gjs_define_console_stuff(JSContext  *context,
-                         JSObject  **module_out)
+gjs_define_console_stuff(JSContext              *context,
+                         JS::MutableHandleObject module)
 {
-    JSObject *module;
-
-    module = JS_NewObject (context, NULL, NULL, NULL);
-
-    if (!JS_DefineFunction(context, module,
-                           "interact",
-                           (JSNative) gjs_console_interact,
-                           1, GJS_MODULE_PROP_FLAGS))
-        return false;
-
-    *module_out = module;
-    return true;
+    module.set(JS_NewObject(context, NULL, NULL, NULL));
+    return JS_DefineFunction(context, module, "interact", gjs_console_interact,
+                             1, GJS_MODULE_PROP_FLAGS);
 }
diff --git a/modules/console.h b/modules/console.h
index 5607584..480e055 100644
--- a/modules/console.h
+++ b/modules/console.h
@@ -30,8 +30,8 @@
 
 G_BEGIN_DECLS
 
-bool        gjs_define_console_stuff     (JSContext      *context,
-                                          JSObject      **module_out);
+bool gjs_define_console_stuff(JSContext              *context,
+                              JS::MutableHandleObject module);
 
 G_END_DECLS
 
diff --git a/modules/system.cpp b/modules/system.cpp
index 5d03cc1..db3e143 100644
--- a/modules/system.cpp
+++ b/modules/system.cpp
@@ -150,15 +150,14 @@ static JSFunctionSpec module_funcs[] = {
 };
 
 bool
-gjs_js_define_system_stuff(JSContext  *context,
-                           JSObject  **module_out)
+gjs_js_define_system_stuff(JSContext              *context,
+                           JS::MutableHandleObject module)
 {
     GjsContext *gjs_context;
     char *program_name;
     bool retval;
-    JSObject *module;
 
-    module = JS_NewObject (context, NULL, NULL, NULL);
+    module.set(JS_NewObject(context, NULL, NULL, NULL));
 
     if (!JS_DefineFunctions(context, module, &module_funcs[0]))
         return false;
@@ -197,7 +196,5 @@ gjs_js_define_system_stuff(JSContext  *context,
 
  out:
     g_free(program_name);
-    *module_out = module;
-
     return retval;
 }
diff --git a/modules/system.h b/modules/system.h
index f6984ab..c0358a8 100644
--- a/modules/system.h
+++ b/modules/system.h
@@ -31,8 +31,8 @@
 
 G_BEGIN_DECLS
 
-bool        gjs_js_define_system_stuff     (JSContext      *context,
-                                            JSObject      **module_out);
+bool gjs_js_define_system_stuff(JSContext              *context,
+                                JS::MutableHandleObject module);
 
 G_END_DECLS
 


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