[gjs: 1/2] context: Always import byteArray



commit ab1ddf50153f6fb41c64208d0de91ad5ff4aed4b
Author: Philip Chimento <philip chimento gmail com>
Date:   Sun Mar 18 17:34:57 2018 -0700

    context: Always import byteArray
    
    Previously, it could happen in some rare cases that we try to marshal a
    GByteArray into JS before the byteArray module is imported. That would
    cause a crash. To prevent this, always import byteArray.
    
    Closes #39.

 gjs/context.cpp  |  5 +++++
 gjs/importer.cpp | 46 +++++++++++++++++++++-------------------------
 gjs/importer.h   |  4 ++++
 gjs/native.cpp   | 18 +++++++++++-------
 gjs/native.h     |  8 ++++----
 5 files changed, 45 insertions(+), 36 deletions(-)
---
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 70041f1..f4a9110 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -432,6 +432,11 @@ gjs_context_constructed(GObject *object)
         g_error("Failed to define properties on global object");
     }
 
+    /* Pre-import the byteArray module. We depend on this module for some of
+     * our GObject introspection marshalling, so the ByteArray prototype
+     * defined in it needs to be always available. */
+    gjs_import_native_module(cx, importer, "byteArray");
+
     JS_EndRequest(cx);
 
     g_mutex_lock (&contexts_lock);
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index 70f214a..b4ea3c8 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -261,33 +261,29 @@ cancel_import(JSContext       *context,
     }
 }
 
-static bool
-import_native_file(JSContext       *context,
-                   JS::HandleObject obj,
-                   const char      *name)
+/*
+ * gjs_import_native_module:
+ * @cx: the #JSContext
+ * @importer: the root importer
+ * @name: Name under which the module was registered with
+ *  gjs_register_native_module()
+ *
+ * Imports a builtin native-code module so that it is available to JS code as
+ * `imports[name]`.
+ *
+ * Returns: true on success, false if an exception was thrown.
+ */
+bool
+gjs_import_native_module(JSContext       *cx,
+                         JS::HandleObject importer,
+                         const char      *name)
 {
-    JS::RootedObject module_obj(context);
-
     gjs_debug(GJS_DEBUG_IMPORTER, "Importing '%s'", name);
 
-    if (!gjs_import_native_module(context, name, &module_obj))
-        return false;
-
-    if (!define_meta_properties(context, module_obj, NULL, name, obj))
-        return false;
-
-    if (JS_IsExceptionPending(context)) {
-        /* I am not sure whether this can happen, but if it does we want to trap it.
-         */
-        gjs_debug(GJS_DEBUG_IMPORTER,
-                  "Module '%s' reported an exception but gjs_import_native_module() returned true",
-                  name);
-        return false;
-    }
-
-    JS::RootedValue v_module(context, JS::ObjectValue(*module_obj));
-    return JS_DefineProperty(context, obj, name, v_module,
-                             GJS_MODULE_PROP_FLAGS);
+    JS::RootedObject module(cx);
+    return gjs_load_native_module(cx, name, &module) &&
+           define_meta_properties(cx, module, nullptr, name, importer) &&
+           JS_DefineProperty(cx, importer, name, module, GJS_MODULE_PROP_FLAGS);
 }
 
 static bool
@@ -492,7 +488,7 @@ do_import(JSContext       *context,
 
     /* First try importing an internal module like byteArray */
     if (priv->is_root && gjs_is_registered_native_module(context, obj, name)) {
-        if (!import_native_file(context, obj, name))
+        if (!gjs_import_native_module(context, obj, name))
             goto out;
 
         gjs_debug(GJS_DEBUG_IMPORTER,
diff --git a/gjs/importer.h b/gjs/importer.h
index 70d1718..cdc81ab 100644
--- a/gjs/importer.h
+++ b/gjs/importer.h
@@ -33,6 +33,10 @@ G_BEGIN_DECLS
 JSObject *gjs_create_root_importer(JSContext          *cx,
                                    const char * const *search_path);
 
+bool gjs_import_native_module(JSContext       *cx,
+                              JS::HandleObject importer,
+                              const char      *name);
+
 G_END_DECLS
 
 #endif  /* __GJS_IMPORTER_H__ */
diff --git a/gjs/native.cpp b/gjs/native.cpp
index 18bf38c..c7f6dcb 100644
--- a/gjs/native.cpp
+++ b/gjs/native.cpp
@@ -75,16 +75,20 @@ gjs_is_registered_native_module(JSContext  *context,
 }
 
 /**
- * gjs_import_native_module:
- * @context:
- * @module_obj:
+ * gjs_load_native_module:
+ * @context: the #JSContext
+ * @name: Name under which the module was registered with
+ *  gjs_register_native_module()
+ * @module_out: Return location for a #JSObject
+ *
+ * Loads a builtin native-code module called @name into @module_out.
  *
- * Return a native module that's been preloaded.
+ * Returns: true on success, false if an exception was thrown.
  */
 bool
-gjs_import_native_module(JSContext              *context,
-                         const char             *name,
-                         JS::MutableHandleObject module_out)
+gjs_load_native_module(JSContext              *context,
+                       const char             *name,
+                       JS::MutableHandleObject module_out)
 {
     GjsDefineModuleFunc func;
 
diff --git a/gjs/native.h b/gjs/native.h
index b04b7de..b71364e 100644
--- a/gjs/native.h
+++ b/gjs/native.h
@@ -42,10 +42,10 @@ bool     gjs_is_registered_native_module(JSContext  *context,
                                          JSObject   *parent,
                                          const char *name);
 
-/* called by importer.c to load a statically linked native module */
-bool gjs_import_native_module (JSContext              *context,
-                               const char             *name,
-                               JS::MutableHandleObject module_out);
+/* called by importer.cpp to load a statically linked native module */
+bool gjs_load_native_module(JSContext              *cx,
+                            const char             *name,
+                            JS::MutableHandleObject module_out);
 
 G_END_DECLS
 


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