[gjs/wip/ptomato/mozjs45prep: 12/13] js: Evaluate on global object where possible



commit a5704d65410eb52d78783bce50246f1e2ec22c37
Author: Philip Chimento <philip chimento gmail com>
Date:   Mon Apr 17 00:42:36 2017 -0700

    js: Evaluate on global object where possible
    
    There are a few places where it makes sense to switch from using
    gjs_eval_with_scope() to plain old evaluation using the global object as
    the scope. In SpiderMonkey 45, the semantics of evaluation scope change
    quite drastically to match ES6 standards, so the more we can port away
    from it without consequence, the less will be affected after the switch.
    
    We previously evaluated typed-in scripts in the scope of the "this" object
    that was passed to Console.interact(), which would have been the Console
    module object. That is a strange thing to do. Instead, evaluate typed-in
    scripts in the scope of the global object.
    
    Same thing for gjs_run_script_in_coverage_compartment(). Previously the
    scope was the CoverageStatistics singleton, which was unnecessary; instead
    we evaluate using the coverage compartment's global object.
    
    We cannot change the importer (our imported module objects are the scope)
    or gjs_context_eval() (it's public API and gnome-shell uses it to evaluate
    extension code, so we don't want to change what might end up on the global
    object.)

 gjs/coverage.cpp    |   11 +++++++----
 modules/console.cpp |    8 +++++---
 2 files changed, 12 insertions(+), 7 deletions(-)
---
diff --git a/gjs/coverage.cpp b/gjs/coverage.cpp
index 44a11c3..72ac386 100644
--- a/gjs/coverage.cpp
+++ b/gjs/coverage.cpp
@@ -1521,11 +1521,14 @@ gjs_run_script_in_coverage_compartment(GjsCoverage *coverage,
     JSAutoCompartment ac(js_context, priv->coverage_statistics);
     JSAutoRequest ar(js_context);
 
+    JS::CompileOptions options(js_context);
+    options.setUTF8(true);
+
     JS::RootedValue rval(js_context);
-    JS::RootedObject rooted_priv(js_context, priv->coverage_statistics);
-    if (!gjs_eval_with_scope(js_context, rooted_priv, script, strlen(script),
-                             "<coverage_modifier>",
-                             &rval)) {
+    JS::RootedObject global(js_context,
+        JS_GetGlobalForObject(js_context, priv->coverage_statistics));
+    if (!JS::Evaluate(js_context, global, options, script, strlen(script),
+                      &rval)) {
         gjs_log_exception(js_context);
         g_warning("Failed to evaluate <coverage_modifier>");
         return false;
diff --git a/modules/console.cpp b/modules/console.cpp
index 74e410a..8fd3fd4 100644
--- a/modules/console.cpp
+++ b/modules/console.cpp
@@ -159,10 +159,11 @@ gjs_console_interact(JSContext *context,
                      unsigned   argc,
                      JS::Value *vp)
 {
-    GJS_GET_THIS(context, argc, vp, argv, object);
+    JS::CallArgs argv = JS::CallArgsFromVp(argc, vp);
     bool eof = false;
     JS::RootedValue result(context);
     JS::RootedString str(context);
+    JS::RootedObject global(context, gjs_get_import_global(context));
     GString *buffer = NULL;
     char *temp_buf = NULL;
     int lineno;
@@ -191,12 +192,13 @@ gjs_console_interact(JSContext *context,
             g_string_append(buffer, temp_buf);
             g_free(temp_buf);
             lineno++;
-        } while (!JS_BufferIsCompilableUnit(context, object, buffer->str, buffer->len));
+        } while (!JS_BufferIsCompilableUnit(context, global,
+                                            buffer->str, buffer->len));
 
         JS::CompileOptions options(context);
         options.setUTF8(true)
                .setFileAndLine("typein", startline);
-        if (!JS::Evaluate(context, object, options, buffer->str, buffer->len,
+        if (!JS::Evaluate(context, global, options, buffer->str, buffer->len,
                           &result)) {
             /* If this was an uncatchable exception, throw another uncatchable
              * exception on up to the surrounding JS::Evaluate() in main(). This


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