[gjs/wip/ptomato/mozjs52: 42/42] WIP - module: Import lexical environment



commit d2494f95e310433da0cb706ddf756cfc685488dc
Author: Philip Chimento <philip chimento gmail com>
Date:   Fri Jun 23 22:45:05 2017 -0700

    WIP - module: Import lexical environment
    
    Does not work yet.

 gjs/module.cpp |   74 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 67 insertions(+), 7 deletions(-)
---
diff --git a/gjs/module.cpp b/gjs/module.cpp
index 4e44b7a..4e1fd7b 100644
--- a/gjs/module.cpp
+++ b/gjs/module.cpp
@@ -30,6 +30,14 @@
 #include "util/log.h"
 
 class GjsModule {
+    enum Slot {
+        MODULE_OBJ,
+        LEXICAL_ENV,
+        NSLOTS
+    };
+
+    static const int NON_SYNTACTIC_VARIABLES_SLOT = 0;
+
     GjsModule(void) {}
 
     /* Creates a JS module object and sets GjsModule as the private data */
@@ -39,6 +47,15 @@ class GjsModule {
         return JS_NewObject(cx, &GjsModule::klass);
     }
 
+    static inline JSObject *
+    slot(JSObject *module, Slot slot)
+    {
+        JS::Value val = JS_GetReservedSlot(module, slot);
+        g_assert(val.isObject());
+        return &val.toObject();
+    }
+
+    /* Defines the empty module as a property on the importer */
     static bool
     define_import(JSContext       *cx,
                   JS::HandleObject module,
@@ -69,16 +86,21 @@ class GjsModule {
                .setSourceIsLazy(true);
 
         JS::RootedScript compiled_script(cx);
-        if (!JS::Compile(cx, options, script, script_len, &compiled_script))
+        if (!JS::CompileForNonSyntacticScope(cx, options, script, script_len,
+                                             &compiled_script))
             return false;
 
-        JS::AutoObjectVector scope_chain(cx);
-        scope_chain.append(module);
-        JS::RootedValue ignored_retval(cx);
-        if (!JS_ExecuteScript(cx, scope_chain, compiled_script,
-                              &ignored_retval))
+        JS::RootedObject global(cx, JS::CurrentGlobalOrNull(cx));
+        JS::RootedObject lexical(cx);
+        if (!js::ExecuteInGlobalAndReturnScope(cx, global, compiled_script,
+                                               &lexical))
             return false;
 
+        JS::RootedValue non_syntactic_variables(cx,
+            JS_GetReservedSlot(lexical, NON_SYNTACTIC_VARIABLES_SLOT));
+        JS_SetReservedSlot(module, Slot::MODULE_OBJ, non_syntactic_variables);
+        JS_SetReservedSlot(module, Slot::LEXICAL_ENV, JS::ObjectValue(*lexical));
+
         gjs_schedule_gc_if_needed(cx);
 
         gjs_debug(GJS_DEBUG_IMPORTER, "Module loading succeeded");
@@ -116,9 +138,46 @@ class GjsModule {
 
     /* JSClass operations */
 
+    static bool
+    resolve(JSContext       *cx,
+            JS::HandleObject module,
+            JS::HandleId     id,
+            bool            *resolved)
+    {
+        JS::RootedObject module_obj(cx, slot(module, Slot::MODULE_OBJ));
+        if (!JS_HasPropertyById(cx, module_obj, id, resolved))
+            return false;
+        if (resolved) {
+            JS::Rooted<JS::PropertyDescriptor> desc(cx);
+            return JS_GetPropertyDescriptorById(cx, module_obj, id, &desc) &&
+                JS_DefinePropertyById(cx, module, id, desc);
+        }
+
+        JS::RootedObject lexical_env(cx, slot(module, Slot::LEXICAL_ENV));
+        if (!JS_HasPropertyById(cx, lexical_env, id, resolved))
+            return false;
+        if (resolved) {
+            g_warning("Resolving a property from the lexical environment!");
+            JS::Rooted<JS::PropertyDescriptor> desc(cx);
+            return JS_GetPropertyDescriptorById(cx, lexical_env, id, &desc) &&
+                JS_DefinePropertyById(cx, module, id, desc);
+        }
+        return true;
+    }
+
+    static constexpr JSClassOps class_ops = {
+        nullptr,  /* addProperty */
+        nullptr,  /* deleteProperty */
+        nullptr,  /* getProperty */
+        nullptr,  /* setProperty */
+        nullptr,  /* enumerate */
+        &GjsModule::resolve,
+    };
+
     static constexpr JSClass klass = {
         "GjsModule",
-        JSCLASS_BACKGROUND_FINALIZE,
+        JSCLASS_HAS_RESERVED_SLOTS(Slot::NSLOTS),
+        &GjsModule::class_ops,
     };
 
 public:
@@ -151,3 +210,4 @@ gjs_module_import(JSContext       *cx,
 }
 
 decltype(GjsModule::klass) constexpr GjsModule::klass;
+decltype(GjsModule::class_ops) constexpr GjsModule::class_ops;


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