[gjs/wip/ptomato/mozjs45prep: 26/32] js: Use JS::CloneAndExecuteScript for cross-compartment



commit f442b50f46ae914bcf2ce87d9cb6dd42eccf0c98
Author: Philip Chimento <philip chimento gmail com>
Date:   Wed Mar 22 10:42:52 2017 +0000

    js: Use JS::CloneAndExecuteScript for cross-compartment
    
    For evaluating a script in another compartment, we need to use
    JS::CloneAndExecuteScript(), since JS::Evaluate() does not now take a
    global object.
    
    We have to duplicate the compile options and stripping the shebang line
    in gjs_context_eval_file_in_compartment(), and change
    gjs_strip_unix_shebang() to take an unsigned length. Not great, but I
    think the alternative would cause even more churn.

 gjs/coverage.cpp   |   20 ++++++++++++++------
 gjs/jsapi-util.cpp |    9 +++++----
 gjs/jsapi-util.h   |    2 +-
 test/gjs-tests.cpp |   12 ++++++------
 4 files changed, 26 insertions(+), 17 deletions(-)
---
diff --git a/gjs/coverage.cpp b/gjs/coverage.cpp
index bb5883e..adc48e7 100644
--- a/gjs/coverage.cpp
+++ b/gjs/coverage.cpp
@@ -1300,18 +1300,26 @@ gjs_context_eval_file_in_compartment(GjsContext      *context,
 
     g_object_unref(file);
 
+    int start_line_number = 1;
+    const char *stripped_script = gjs_strip_unix_shebang(script, &script_len,
+                                                         &start_line_number);
+
     JSContext *js_context = (JSContext *) gjs_context_get_native_context(context);
 
+    JS::CompileOptions options(js_context);
+    options.setUTF8(true)
+           .setFileAndLine(filename, start_line_number)
+           .setSourceIsLazy(true);
+    JS::RootedScript compiled_script(js_context);
+    if (!JS::Compile(js_context, options, stripped_script, script_len,
+                     &compiled_script))
+        return false;
+
     JSAutoCompartment compartment(js_context, compartment_object);
 
-    JS::RootedValue return_value(js_context);
-    if (!gjs_eval_with_scope(js_context,
-                             compartment_object,
-                             script, script_len, filename,
-                             &return_value)) {
+    if (!JS::CloneAndExecuteScript(js_context, compiled_script)) {
         g_free(script);
         gjs_log_exception(js_context);
-        g_free(script);
         g_set_error(error, GJS_ERROR, GJS_ERROR_FAILED, "Failed to evaluate %s", filename);
         return false;
     }
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 2a69f26..8174f9a 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -866,7 +866,7 @@ gjs_schedule_gc_if_needed (JSContext *context)
  */
 const char *
 gjs_strip_unix_shebang(const char  *script,
-                       gssize      *script_len,
+                       size_t      *script_len,
                        int         *start_line_number_out)
 {
     g_assert(script_len);
@@ -912,12 +912,13 @@ gjs_eval_with_scope(JSContext             *context,
 {
     int start_line_number = 1;
     JSAutoRequest ar(context);
+    size_t real_len = script_len;
 
     if (script_len < 0)
-        script_len = strlen(script);
+        real_len = strlen(script);
 
     script = gjs_strip_unix_shebang(script,
-                                    &script_len,
+                                    &real_len,
                                     &start_line_number);
 
     /* log and clear exception if it's set (should not be, normally...) */
@@ -935,7 +936,7 @@ gjs_eval_with_scope(JSContext             *context,
            .setFileAndLine(filename, start_line_number)
            .setSourceIsLazy(true);
 
-    if (!JS::Evaluate(context, eval_obj, options, script, script_len, retval))
+    if (!JS::Evaluate(context, eval_obj, options, script, real_len, retval))
         return false;
 
     gjs_schedule_gc_if_needed(context);
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index 718a82f..e05e12b 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -272,7 +272,7 @@ typedef enum {
 } GjsConstString;
 
 const char * gjs_strip_unix_shebang(const char *script,
-                                    gssize     *script_len,
+                                    size_t     *script_len,
                                     int        *new_start_line_number);
 
 /* These four functions wrap JS_GetPropertyById(), etc., but with a
diff --git a/test/gjs-tests.cpp b/test/gjs-tests.cpp
index febfe0e..f5119e5 100644
--- a/test/gjs-tests.cpp
+++ b/test/gjs-tests.cpp
@@ -330,8 +330,8 @@ static void
 gjstest_test_strip_shebang_no_advance_for_no_shebang(void)
 {
     const char *script = "foo\nbar";
-    gssize     script_len_original = strlen(script);
-    gssize     script_len = script_len_original;
+    size_t script_len_original = strlen(script);
+    size_t script_len = script_len_original;
     int        line_number = 1;
 
     const char *stripped = gjs_strip_unix_shebang(script,
@@ -347,8 +347,8 @@ static void
 gjstest_test_strip_shebang_advance_for_shebang(void)
 {
     const char *script = "#!foo\nbar";
-    gssize     script_len_original = strlen(script);
-    gssize     script_len = script_len_original;
+    size_t script_len_original = strlen(script);
+    size_t script_len = script_len_original;
     int        line_number = 1;
 
     const char *stripped = gjs_strip_unix_shebang(script,
@@ -364,8 +364,8 @@ static void
 gjstest_test_strip_shebang_return_null_for_just_shebang(void)
 {
     const char *script = "#!foo";
-    gssize     script_len_original = strlen(script);
-    gssize     script_len = script_len_original;
+    size_t script_len_original = strlen(script);
+    size_t script_len = script_len_original;
     int        line_number = 1;
 
     const char *stripped = gjs_strip_unix_shebang(script,


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