[gjs: 2/6] js: Convert scripts to UTF-16 before evaluating



commit bc36f39ff09629e1b4c5c54f334028d2b2f8c545
Author: Philip Chimento <philip chimento gmail com>
Date:   Mon Oct 2 11:14:20 2017 -0700

    js: Convert scripts to UTF-16 before evaluating
    
    This works around a bug in SpiderMonkey where coverage data is generated
    twice when using separate compile/execute steps with an object on the
    environment chain.
    
    Unfortunately there is no overload of JS::Evaluate() which handles both
    an environment chain and UTF-8 source.
    
    See upstream bug https://bugzilla.mozilla.org/show_bug.cgi?id=1404784
    
    https://bugzilla.gnome.org/show_bug.cgi?id=788166

 gjs/jsapi-util.cpp | 15 +++++++++------
 gjs/module.cpp     | 15 +++++++++------
 2 files changed, 18 insertions(+), 12 deletions(-)
---
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 94ceb82..90bd56b 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -24,6 +24,9 @@
 
 #include <config.h>
 
+#include <codecvt>
+#include <locale>
+
 #include <util/log.h>
 #include <util/glib.h>
 #include <util/misc.h>
@@ -860,19 +863,19 @@ gjs_eval_with_scope(JSContext             *context,
         eval_obj = JS_NewPlainObject(context);
 
     JS::CompileOptions options(context);
-    options.setUTF8(true)
-           .setFileAndLine(filename, start_line_number)
+    options.setFileAndLine(filename, start_line_number)
            .setSourceIsLazy(true);
 
-    JS::RootedScript compiled_script(context);
-    if (!JS::Compile(context, options, script, real_len, &compiled_script))
-        return false;
+    std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
+    std::u16string utf16_string = convert.from_bytes(script);
+    JS::SourceBufferHolder buf(utf16_string.c_str(), utf16_string.size(),
+                               JS::SourceBufferHolder::NoOwnership);
 
     JS::AutoObjectVector scope_chain(context);
     if (!scope_chain.append(eval_obj))
         g_error("Unable to append to vector");
 
-    if (!JS_ExecuteScript(context, scope_chain, compiled_script, retval))
+    if (!JS::Evaluate(context, scope_chain, options, buf, retval))
         return false;
 
     gjs_schedule_gc_if_needed(context);
diff --git a/gjs/module.cpp b/gjs/module.cpp
index d0d4a01..f25507b 100644
--- a/gjs/module.cpp
+++ b/gjs/module.cpp
@@ -21,6 +21,9 @@
  * IN THE SOFTWARE.
  */
 
+#include <codecvt>
+#include <locale>
+
 #include <gio/gio.h>
 
 #include "jsapi-util.h"
@@ -86,20 +89,20 @@ class GjsModule {
                     int              line_number)
     {
         JS::CompileOptions options(cx);
-        options.setUTF8(true)
-               .setFileAndLine(filename, line_number)
+        options.setFileAndLine(filename, line_number)
                .setSourceIsLazy(true);
 
-        JS::RootedScript compiled_script(cx);
-        if (!JS::Compile(cx, options, script, script_len, &compiled_script))
-            return false;
+        std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
+        std::u16string utf16_string = convert.from_bytes(script);
+        JS::SourceBufferHolder buf(utf16_string.c_str(), utf16_string.size(),
+                                   JS::SourceBufferHolder::NoOwnership);
 
         JS::AutoObjectVector scope_chain(cx);
         if (!scope_chain.append(module))
             g_error("Unable to append to vector");
 
         JS::RootedValue ignored_retval(cx);
-        if (!JS_ExecuteScript(cx, scope_chain, compiled_script, &ignored_retval))
+        if (!JS::Evaluate(cx, scope_chain, options, buf, &ignored_retval))
             return false;
 
         gjs_schedule_gc_if_needed(cx);


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