[gjs/wip/js24: 8/12] Adapt to utf8 changes



commit 287637223bf8600d49af412a7c7905a60dac9225
Author: Tim Lunn <tim feathertop org>
Date:   Sat Sep 28 15:10:54 2013 +1000

    Adapt to utf8 changes
    
    JS_SetCStringsAreUTF8 has been removed and internally spidermonkey no longer handles
    utf8 strings. There is a compiletime option setUTF8 that needs to be set, this change
    requires using JS::Evaluate. Additionally we need to handle utf8 encoding in the
    gjs_string functions. Finally JS_BufferIsCompilableUnit no longer has a utf8 argument
    
    https://bugzilla.gnome.org/show_bug.cgi?id=711046

 gjs/byteArray.cpp         |   10 +++++++---
 gjs/context.cpp           |   22 +++++++++++++---------
 gjs/importer.cpp          |   22 +++++++++++++++-------
 gjs/jsapi-util-string.cpp |   38 ++++++++++++++++++++++++++++++++------
 modules/console.cpp       |    9 ++++++---
 5 files changed, 73 insertions(+), 28 deletions(-)
---
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index 57d27a5..4c5113b 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -771,9 +771,13 @@ byte_array_ensure_initialized (JSContext *context)
 
     if (g_once_init_enter (&initialized)) {
         jsval rval;
-        JS_EvaluateScript(context, JS_GetGlobalObject(context),
-                          "imports.byteArray.ByteArray;", 27,
-                          "<internal>", 1, &rval);
+        JS::CompileOptions options(context);
+        options.setUTF8(true)
+               .setFileAndLine("<internal>", 1);
+        js::RootedObject rootedObj(context, JS_GetGlobalObject(context));
+        JS::Evaluate(context, rootedObj, options,
+                     "imports.byteArray.ByteArray;", 27,
+                     &rval);
         g_once_init_leave (&initialized, 1);
     }
 }
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 0136370..9ff080c 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -345,8 +345,6 @@ gjs_context_class_init(GjsContextClass *klass)
                                       NULL,
                                       G_TYPE_NONE, 0);
 
-    JS_SetCStringsAreUTF8();
-
     /* For GjsPrivate */
     {
         char *priv_typelib_dir = g_build_filename (PKGLIBDIR, "girepository-1.0", NULL);
@@ -1017,13 +1015,19 @@ gjs_context_eval(GjsContext *js_context,
     retval = JSVAL_VOID;
     if (script_len < 0)
         script_len = strlen(script);
-    if (!JS_EvaluateScript(js_context->context,
-                           js_context->global,
-                           script,
-                           script_len,
-                           filename,
-                           line_number,
-                           &retval)) {
+
+    JS::CompileOptions options(js_context->context);
+    options.setUTF8(true)
+           .setFileAndLine(filename, line_number)
+           .setSourcePolicy(JS::CompileOptions::LAZY_SOURCE);
+    js::RootedObject rootedObj(js_context->context, js_context->global);
+
+    if (!JS::Evaluate(js_context->context,
+                      rootedObj,
+                      options,
+                      script,
+                      script_len,
+                      &retval)) {
         gjs_debug(GJS_DEBUG_CONTEXT,
                   "Script evaluation failed");
 
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index 3941d18..d15c8bc 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -269,6 +269,9 @@ import_file(JSContext  *context,
     jsval script_retval;
     GError *error = NULL;
 
+    JS::CompileOptions options(context);
+    js::RootedObject rootedObj(context, module_obj);
+
     if (!(g_file_load_contents(file, NULL, &script, &script_len, NULL, &error))) {
         if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY) &&
             !g_error_matches(error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY) &&
@@ -284,13 +287,18 @@ import_file(JSContext  *context,
 
     full_path = g_file_get_parse_name (file);
 
-    if (!JS_EvaluateScript(context,
-                           module_obj,
-                           script,
-                           script_len,
-                           full_path,
-                           1, /* line number */
-                           &script_retval)) {
+
+    options.setUTF8(true)
+           .setFileAndLine(full_path, 1)
+           .setSourcePolicy(JS::CompileOptions::LAZY_SOURCE);
+
+    if (!JS::Evaluate(context,
+                      rootedObj,
+                      options,
+                      script,
+                      script_len,
+                      &script_retval)) {
+
 
         /* If JSOPTION_DONT_REPORT_UNCAUGHT is set then the exception
          * would be left set after the evaluate and not go to the error
diff --git a/gjs/jsapi-util-string.cpp b/gjs/jsapi-util-string.cpp
index af58564..5ddc32d 100644
--- a/gjs/jsapi-util-string.cpp
+++ b/gjs/jsapi-util-string.cpp
@@ -55,9 +55,7 @@ gjs_string_to_utf8 (JSContext  *context,
     }
 
     if (utf8_string_p) {
-        bytes = (char*) g_malloc((len + 1) * sizeof(char));
-        JS_EncodeStringToBuffer(context, str, bytes, len);
-        bytes[len] = '\0';
+        bytes = JS_EncodeStringToUTF8(context, str);
         *utf8_string_p = bytes;
     }
 
@@ -72,13 +70,41 @@ gjs_string_from_utf8(JSContext  *context,
                      gssize      n_bytes,
                      jsval      *value_p)
 {
+    jschar *u16_string;
+    glong u16_string_length;
     JSString *str;
+    GError *error;
+
+    /* intentionally using n_bytes even though glib api suggests n_chars; with
+    * n_chars (from g_utf8_strlen()) the result appears truncated
+    */
+
+    error = NULL;
+    u16_string = g_utf8_to_utf16(utf8_string,
+                                 n_bytes,
+                                 NULL,
+                                 &u16_string_length,
+                                 &error);
+    if (!u16_string) {
+        gjs_throw(context,
+                  "Failed to convert UTF-8 string to "
+                  "JS string: %s",
+                  error->message);
+                  g_error_free(error);
+        return JS_FALSE;
+    }
 
     JS_BeginRequest(context);
 
-    if (n_bytes < 0)
-        n_bytes = strlen(utf8_string);
-    str = JS_NewStringCopyN(context, utf8_string, n_bytes);
+    if (g_mem_is_system_malloc()) {
+        /* Avoid a copy - assumes that g_malloc == js_malloc == malloc */
+        str = JS_NewUCString(context, u16_string, u16_string_length);
+    } else {
+        str = JS_NewUCStringCopyN(context,
+                                (jschar*)u16_string,
+                                u16_string_length);
+        g_free(u16_string);
+    }
 
     if (str && value_p)
         *value_p = STRING_TO_JSVAL(str);
diff --git a/modules/console.cpp b/modules/console.cpp
index 1424b68..3ea0c64 100644
--- a/modules/console.cpp
+++ b/modules/console.cpp
@@ -190,10 +190,13 @@ gjs_console_interact(JSContext *context,
             g_string_append(buffer, temp_buf);
             g_free(temp_buf);
             lineno++;
-        } while (!JS_BufferIsCompilableUnit(context, JS_TRUE, object, buffer->str, buffer->len));
+        } while (!JS_BufferIsCompilableUnit(context, object, buffer->str, buffer->len));
 
-        JS_EvaluateScript(context, object, buffer->str, buffer->len, "typein",
-                          startline, &result);
+        JS::CompileOptions options(context);
+        options.setUTF8(true)
+               .setFileAndLine("typein", startline);
+        js::RootedObject rootedObj(context, object);
+        JS::Evaluate(context, rootedObj, options, buffer->str, buffer->len,  &result);
 
         if (JS_GetPendingException(context, &result)) {
             str = JS_ValueToString(context, result);


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