[gjs/mozjs91] context: Remove workaround for coverage bug




commit 7eaf1beb44fa532a83929bc7d5f5a79affee7a37
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Oct 9 18:00:12 2021 -0700

    context: Remove workaround for coverage bug
    
    There was a bug (https://bugzilla.mozilla.org/show_bug.cgi?id=1404784)
    preventing us from using UTF-8 compilation in GJS, meaning we had to
    convert all JS source text to UTF-16 before compiling. This bug has been
    fixed, so we can remove these workarounds.
    
    One difference is that the UTF-8 compilation APIs don't allow a length
    of -1 to indicate the string is zero-terminated, whereas g_utf8_to_utf16
    did. To prevent negative lengths from being passed accidentally and
    being interpreted as large positive lengths, change the type of the
    length parameter in several methods from ssize_t to size_t.
    
    The public API function gjs_context_eval() still has to accept ssize_t,
    so we take the string length there if a negative number is passed.

 gi/foreign.cpp        |  3 ++-
 gjs/context-private.h |  6 +++---
 gjs/context.cpp       | 28 +++++++++++-----------------
 gjs/module.cpp        | 23 +++++++----------------
 4 files changed, 23 insertions(+), 37 deletions(-)
---
diff --git a/gi/foreign.cpp b/gi/foreign.cpp
index 6c79bcb0..cb8662f7 100644
--- a/gi/foreign.cpp
+++ b/gi/foreign.cpp
@@ -56,7 +56,8 @@ static GHashTable* foreign_structs_table = NULL;
         script = g_strdup_printf("imports.%s;", gi_namespace);
         JS::RootedValue retval(context);
         GjsContextPrivate* gjs = GjsContextPrivate::from_cx(context);
-        if (!gjs->eval_with_scope(nullptr, script, -1, "<internal>", &retval)) {
+        if (!gjs->eval_with_scope(nullptr, script, strlen(script), "<internal>",
+                                  &retval)) {
             g_critical("ERROR importing foreign module %s\n", gi_namespace);
             g_free(script);
             return false;
diff --git a/gjs/context-private.h b/gjs/context-private.h
index bdbd35c7..547fd6fa 100644
--- a/gjs/context-private.h
+++ b/gjs/context-private.h
@@ -7,8 +7,8 @@
 
 #include <config.h>
 
+#include <stddef.h>  // for size_t
 #include <stdint.h>
-#include <sys/types.h>  // for ssize_t
 
 #include <atomic>
 #include <string>
@@ -207,12 +207,12 @@ class GjsContextPrivate : public JS::JobQueue {
         return *(from_cx(cx)->m_atoms);
     }
 
-    [[nodiscard]] bool eval(const char* script, ssize_t script_len,
+    [[nodiscard]] bool eval(const char* script, size_t script_len,
                             const char* filename, int* exit_status_p,
                             GError** error);
     GJS_JSAPI_RETURN_CONVENTION
     bool eval_with_scope(JS::HandleObject scope_object, const char* script,
-                         ssize_t script_len, const char* filename,
+                         size_t script_len, const char* filename,
                          JS::MutableHandleValue retval);
     [[nodiscard]] bool eval_module(const char* identifier, uint8_t* exit_code_p,
                                    GError** error);
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 3d780ef9..6e1d6340 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -88,6 +88,10 @@
 #include "modules/modules.h"
 #include "util/log.h"
 
+namespace mozilla {
+union Utf8Unit;
+}
+
 static void     gjs_context_dispose           (GObject               *object);
 static void     gjs_context_finalize          (GObject               *object);
 static void     gjs_context_constructed       (GObject               *object);
@@ -1191,10 +1195,12 @@ gjs_context_eval(GjsContext   *js_context,
 {
     g_return_val_if_fail(GJS_IS_CONTEXT(js_context), false);
 
+    size_t real_len = script_len < 0 ? strlen(script) : script_len;
+
     GjsAutoUnref<GjsContext> js_context_ref(js_context, GjsAutoTakeOwnership());
 
     GjsContextPrivate* gjs = GjsContextPrivate::from_object(js_context);
-    return gjs->eval(script, script_len, filename, exit_status_p, error);
+    return gjs->eval(script, real_len, filename, exit_status_p, error);
 }
 
 bool gjs_context_eval_module(GjsContext* js_context, const char* identifier,
@@ -1262,7 +1268,7 @@ uint8_t GjsContextPrivate::handle_exit_code(const char* type,
     return 1;
 }
 
-bool GjsContextPrivate::eval(const char* script, ssize_t script_len,
+bool GjsContextPrivate::eval(const char* script, size_t script_len,
                              const char* filename, int* exit_status_p,
                              GError** error) {
     AutoResetExit reset(this);
@@ -1422,7 +1428,7 @@ 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* source, ssize_t source_len,
+                                        const char* source, size_t source_len,
                                         const char* filename,
                                         JS::MutableHandleValue retval) {
     /* log and clear exception if it's set (should not be, normally...) */
@@ -1435,20 +1441,8 @@ bool GjsContextPrivate::eval_with_scope(JS::HandleObject scope_object,
     if (!eval_obj)
         eval_obj = JS_NewPlainObject(m_cx);
 
-    long items_written;  // NOLINT(runtime/int) - this type required by GLib API
-    GError* error;
-    GjsAutoChar16 utf16_string =
-        g_utf8_to_utf16(source, source_len,
-                        /* items_read = */ nullptr, &items_written, &error);
-    if (!utf16_string)
-        return gjs_throw_gerror_message(m_cx, error);
-
-    // 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
-    JS::SourceText<char16_t> buf;
-    if (!buf.init(m_cx, reinterpret_cast<char16_t*>(utf16_string.get()),
-                  items_written, JS::SourceOwnership::Borrowed))
+    JS::SourceText<mozilla::Utf8Unit> buf;
+    if (!buf.init(m_cx, source, source_len, JS::SourceOwnership::Borrowed))
         return false;
 
     JS::RootedObjectVector scope_chain(m_cx);
diff --git a/gjs/module.cpp b/gjs/module.cpp
index fb966690..c4616d99 100644
--- a/gjs/module.cpp
+++ b/gjs/module.cpp
@@ -6,7 +6,6 @@
 
 #include <stddef.h>     // for size_t
 #include <string.h>
-#include <sys/types.h>  // for ssize_t
 
 #include <string>  // for u16string
 
@@ -50,6 +49,10 @@
 #include "util/log.h"
 #include "util/misc.h"
 
+namespace mozilla {
+union Utf8Unit;
+}
+
 class GjsScriptModule {
     char *m_name;
 
@@ -100,22 +103,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* source, ssize_t source_len,
+                         const char* source, size_t source_len,
                          const char* filename, const char* uri) {
-        long items_written;  // NOLINT(runtime/int) - required by GLib API
-        GError* error;
-        GjsAutoChar16 utf16_string =
-            g_utf8_to_utf16(source, source_len,
-                            /* items_read = */ nullptr, &items_written, &error);
-        if (!utf16_string)
-            return gjs_throw_gerror_message(cx, error);
-
-        // 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
-        JS::SourceText<char16_t> buf;
-        if (!buf.init(cx, reinterpret_cast<char16_t*>(utf16_string.get()),
-                      items_written, JS::SourceOwnership::Borrowed))
+        JS::SourceText<mozilla::Utf8Unit> buf;
+        if (!buf.init(cx, source, source_len, JS::SourceOwnership::Borrowed))
             return false;
 
         JS::RootedObjectVector scope_chain(cx);


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