[gjs/mozjs91] context: Remove workaround for coverage bug
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/mozjs91] context: Remove workaround for coverage bug
- Date: Sun, 10 Oct 2021 01:01:32 +0000 (UTC)
commit f970547ba7cf176371bd5c704ab9a267c71ba0e2
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 | 4 ++--
gjs/context.cpp | 24 +++++++-----------------
gjs/module.cpp | 18 +++---------------
4 files changed, 14 insertions(+), 35 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..a1c8ea55 100644
--- a/gjs/context-private.h
+++ b/gjs/context-private.h
@@ -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..0553a4e0 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -1191,10 +1191,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 +1264,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 +1424,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 +1437,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..77156381 100644
--- a/gjs/module.cpp
+++ b/gjs/module.cpp
@@ -100,22 +100,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]