[gjs: 1/2] importer: Add backwards-compatible GjsFileImporter global object




commit 81e0c866cd5d6c9e2df73a6946b13cc9685d3bef
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Jan 30 14:56:53 2021 -0800

    importer: Add backwards-compatible GjsFileImporter global object
    
    JS_InitClass() used to do this for unknown reasons, and it was exposed to
    user code, so continue to do it even after discontinuing use of
    JS_InitClass().
    
    Closes: #372

 gjs/importer.cpp                   | 17 ++++++++++++++++-
 installed-tests/js/testImporter.js |  8 ++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)
---
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index f69d36fe..5076ce9a 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -821,9 +821,16 @@ JSFunctionSpec gjs_importer_proto_funcs[] = {
     return gjs_search_path;
 }
 
+GJS_JSAPI_RETURN_CONVENTION
+static bool no_construct(JSContext* cx, unsigned argc, JS::Value* vp) {
+    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
+    gjs_throw_abstract_constructor_error(cx, args);
+    return false;
+}
+
 GJS_JSAPI_RETURN_CONVENTION
 static JSObject* gjs_importer_define_proto(JSContext* cx) {
-    JSObject* global = JS::CurrentGlobalOrNull(cx);
+    JS::RootedObject global(cx, JS::CurrentGlobalOrNull(cx));
     g_assert(global && "Must enter a realm before defining importer");
 
     // If we've been here more than once, we already have the proto
@@ -842,6 +849,14 @@ static JSObject* gjs_importer_define_proto(JSContext* cx) {
     gjs_set_global_slot(global, GjsGlobalSlot::PROTOTYPE_importer,
                         JS::ObjectValue(*proto));
 
+    // For backwards compatibility
+    JSFunction* constructor = JS_NewFunction(
+        cx, no_construct, 0, JSFUN_CONSTRUCTOR, "GjsFileImporter");
+    JS::RootedObject ctor_obj(cx, JS_GetFunctionObject(constructor));
+    if (!JS_LinkConstructorAndPrototype(cx, ctor_obj, proto) ||
+        !JS_DefineProperty(cx, global, "GjsFileImporter", ctor_obj, 0))
+        return nullptr;
+
     gjs_debug(GJS_DEBUG_CONTEXT, "Initialized class %s prototype %p",
               gjs_importer_class.name, proto.get());
     return proto;
diff --git a/installed-tests/js/testImporter.js b/installed-tests/js/testImporter.js
index b4bad76b..b9c519c5 100644
--- a/installed-tests/js/testImporter.js
+++ b/installed-tests/js/testImporter.js
@@ -56,6 +56,14 @@ describe('Importer', function () {
         imports.searchPath = oldSearchPath;
     });
 
+    it('is on the global object (backwards compatibility)', function () {
+        expect(imports instanceof globalThis.GjsFileImporter).toBeTruthy();
+    });
+
+    it('is abstract', function () {
+        expect(() => new globalThis.GjsFileImporter()).toThrow();
+    });
+
     it('exists', function () {
         expect(imports).toBeDefined();
     });


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