[gjs/mozjs91: 8/21] Use new Script private API




commit 2198046be8748743c272587504dfe46e3e460f76
Author: Evan Welsh <contact evanwelsh com>
Date:   Sat Aug 7 14:27:27 2021 -0700

    Use new Script private API
    
    Setting Script privates has been removed from CompileOptions
    (see changeset below) in favor of a specific API for dynamic
    imports JS::[Set, Get]ScripePrivate (bug 1482153).
    
    Because we must manipulate the Script object itself we can no longer
    use the JS::Evaluate wrapper to set our environment chain and instead
    must use JS_ExecuteScript. Because our environment chain is
    "non-syntactic" (it doesn't come from any JS syntax) we have to specify
    this option. JS::Evaluate previously handled this for us in
    js/src/vm/CompilationAndEvaluation.cpp by passing ScopeKind::NonSyntactic
    in the override we used.
    
    Changeset: https://phabricator.services.mozilla.com/D110459
    See: https://bugzilla.mozilla.org/show_bug.cgi?id=1702278
    See: https://bugzilla.mozilla.org/show_bug.cgi?id=1482153

 gjs/context.cpp | 17 +++++++++++------
 gjs/module.cpp  | 19 +++++++++++++------
 2 files changed, 24 insertions(+), 12 deletions(-)
---
diff --git a/gjs/context.cpp b/gjs/context.cpp
index cbe56ffc..e67ed0a1 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -1421,7 +1421,8 @@ bool gjs_context_eval_module_file(GjsContext* js_context, const char* filename,
  * Otherwise, the global definitions are just discarded.
  */
 bool GjsContextPrivate::eval_with_scope(JS::HandleObject scope_object,
-                                        const char* script, ssize_t script_len,
+                                        const char* script_source,
+                                        ssize_t script_source_len,
                                         const char* filename,
                                         JS::MutableHandleValue retval) {
     /* log and clear exception if it's set (should not be, normally...) */
@@ -1433,8 +1434,8 @@ bool GjsContextPrivate::eval_with_scope(JS::HandleObject scope_object,
     JS::RootedObject eval_obj(m_cx, scope_object);
     if (!eval_obj)
         eval_obj = JS_NewPlainObject(m_cx);
-
-    std::u16string utf16_string = gjs_utf8_script_to_utf16(script, script_len);
+    std::u16string utf16_string =
+        gjs_utf8_script_to_utf16(script_source, script_source_len);
     // COMPAT: This could use JS::SourceText<mozilla::Utf8Unit> directly,
     // but that messes up code coverage. See bug
     // https://bugzilla.mozilla.org/show_bug.cgi?id=1404784
@@ -1450,7 +1451,7 @@ bool GjsContextPrivate::eval_with_scope(JS::HandleObject scope_object,
     }
 
     JS::CompileOptions options(m_cx);
-    options.setFileAndLine(filename, 1);
+    options.setFileAndLine(filename, 1).setNonSyntacticScope(true);
 
     GjsAutoUnref<GFile> file = g_file_new_for_commandline_arg(filename);
     GjsAutoChar uri = g_file_get_uri(file);
@@ -1458,9 +1459,13 @@ bool GjsContextPrivate::eval_with_scope(JS::HandleObject scope_object,
     if (!priv)
         return false;
 
-    options.setPrivateValue(JS::ObjectValue(*priv));
+    JS::RootedScript script(m_cx);
+    script.set(JS::Compile(m_cx, options, buf));
+    if (!script)
+        return false;
 
-    if (!JS::Evaluate(m_cx, scope_chain, options, buf, retval))
+    JS::SetScriptPrivate(script, JS::ObjectValue(*priv));
+    if (!JS_ExecuteScript(m_cx, scope_chain, script, retval))
         return false;
 
     schedule_gc_if_needed();
diff --git a/gjs/module.cpp b/gjs/module.cpp
index 31b7b491..0e05fce0 100644
--- a/gjs/module.cpp
+++ b/gjs/module.cpp
@@ -95,10 +95,10 @@ class GjsScriptModule {
     /* Carries out the actual execution of the module code */
     GJS_JSAPI_RETURN_CONVENTION
     bool evaluate_import(JSContext* cx, JS::HandleObject module,
-                         const char* script, ssize_t script_len,
+                         const char* script_source, ssize_t script_source_len,
                          const char* filename, const char* uri) {
         std::u16string utf16_string =
-            gjs_utf8_script_to_utf16(script, script_len);
+            gjs_utf8_script_to_utf16(script_source, script_source_len);
         // COMPAT: This could use JS::SourceText<mozilla::Utf8Unit> directly,
         // but that messes up code coverage. See bug
         // https://bugzilla.mozilla.org/show_bug.cgi?id=1404784
@@ -114,13 +114,19 @@ class GjsScriptModule {
         }
 
         JS::CompileOptions options(cx);
-        options.setFileAndLine(filename, 1);
+        options.setFileAndLine(filename, 1).setNonSyntacticScope(true);
 
         JS::RootedObject priv(cx, build_private(cx, uri));
-        options.setPrivateValue(JS::ObjectValue(*priv));
+        if (!priv)
+            return false;
+
+        JS::RootedScript script(cx, JS::Compile(cx, options, buf));
+        if (!script)
+            return false;
 
+        JS::SetScriptPrivate(script, JS::ObjectValue(*priv));
         JS::RootedValue ignored_retval(cx);
-        if (!JS::Evaluate(cx, scope_chain, options, buf, &ignored_retval))
+        if (!JS_ExecuteScript(cx, scope_chain, script, &ignored_retval))
             return false;
 
         GjsContextPrivate* gjs = GjsContextPrivate::from_cx(cx);
@@ -222,7 +228,8 @@ class GjsScriptModule {
 
  public:
     /*
-     * Creates a JS object to pass to JS::CompileOptions as a script's private.
+     * Creates a JS object to pass to JS::SetScriptPrivate as a script's
+     * private.
      */
     GJS_JSAPI_RETURN_CONVENTION
     static JSObject* build_private(JSContext* cx, const char* script_uri) {


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