[gjs/wip/gdbus-2: 9/13] Convert imported modules from UTF-8 to UTF-16 before executing



commit 48435ab4120ead88cb219e869cf0c5212247f705
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Mon Oct 29 18:45:51 2012 +0100

    Convert imported modules from UTF-8 to UTF-16 before executing
    
    If JS_CStringsAreUTF8 is false, the JSAPI assumes C strings are
    ISO-8859-1. Unfortunately, we rely on that setting for byte arrays,
    so just do the conversion ourselves, like we do in gjs_context_eval().
    
    https://bugzilla.gnome.org/show_bug.cgi?id=687095

 gjs/importer.c |   76 +++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 51 insertions(+), 25 deletions(-)
---
diff --git a/gjs/importer.c b/gjs/importer.c
index 63ee1f3..41b5e80 100644
--- a/gjs/importer.c
+++ b/gjs/importer.c
@@ -262,15 +262,34 @@ import_native_file(JSContext  *context,
     return retval;
 }
 
+static jschar *
+file_get_utf16_contents (const char  *filename,
+                         glong       *length,
+                         GError     **error)
+{
+    gchar *contents;
+    gsize utf8_length;
+
+    if (!g_file_get_contents (filename, &contents,
+                              &utf8_length, error))
+        return NULL;
+
+    /* No cast here, so we get a warning in the
+       (impossible?) case that gunichar2 is different
+       from jschar */
+    return g_utf8_to_utf16 (contents, utf8_length,
+                            NULL, length, error);
+}
 static JSObject *
 load_module_init(JSContext  *context,
                  JSObject   *in_object,
                  const char *full_path)
 {
-    char *script;
-    gsize script_len;
+    jschar *script;
+    glong script_len;
     jsval script_retval;
     JSObject *module_obj;
+    GError *error;
 
     /* First we check if js module has already been loaded  */
     if (gjs_object_has_property(context, in_object, MODULE_INIT_PROPERTY)) {
@@ -301,10 +320,15 @@ load_module_init(JSContext  *context,
                       NULL, NULL,
                       GJS_MODULE_PROP_FLAGS & ~JSPROP_PERMANENT);
 
-    script = NULL;
     script_len = 0;
+    error = NULL;
+
+    if (!(script = file_get_utf16_contents(full_path, &script_len, &error))) {
+        if (!g_error_matches(error, G_FILE_ERROR, G_FILE_ERROR_ISDIR) &&
+            !g_error_matches(error, G_FILE_ERROR, G_FILE_ERROR_NOTDIR) &&
+            !g_error_matches(error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
+            gjs_throw_g_error(context, error);
 
-    if (!g_file_get_contents(full_path, &script, &script_len, NULL)) {
         return NULL;
     }
 
@@ -312,13 +336,13 @@ load_module_init(JSContext  *context,
 
     gjs_debug(GJS_DEBUG_IMPORTER, "Importing %s", full_path);
 
-    if (!JS_EvaluateScript(context,
-                           module_obj,
-                           script,
-                           script_len,
-                           full_path,
-                           1, /* line number */
-                           &script_retval)) {
+    if (!JS_EvaluateUCScript(context,
+                             module_obj,
+                             script,
+                             script_len,
+                             full_path,
+                             1, /* line number */
+                             &script_retval)) {
         g_free(script);
 
         /* If JSOPTION_DONT_REPORT_UNCAUGHT is set then the exception
@@ -388,8 +412,8 @@ import_file(JSContext  *context,
             const char *name,
             const char *full_path)
 {
-    char *script;
-    gsize script_len;
+    jschar *script;
+    glong script_len;
     JSObject *module_obj;
     GError *error;
     jsval script_retval;
@@ -409,25 +433,27 @@ import_file(JSContext  *context,
     if (!define_meta_properties(context, module_obj, name, obj))
         goto out;
 
-    script = NULL;
     script_len = 0;
-
     error = NULL;
-    if (!g_file_get_contents(full_path, &script, &script_len, &error)) {
-        gjs_throw(context, "Could not open %s: %s", full_path, error->message);
-        g_error_free(error);
+
+    if (!(script = file_get_utf16_contents(full_path, &script_len, &error))) {
+        if (!g_error_matches(error, G_FILE_ERROR, G_FILE_ERROR_ISDIR) &&
+            !g_error_matches(error, G_FILE_ERROR, G_FILE_ERROR_NOTDIR) &&
+            !g_error_matches(error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
+            gjs_throw_g_error(context, error);
+
         goto out;
     }
 
     g_assert(script != NULL);
 
-    if (!JS_EvaluateScript(context,
-                           module_obj,
-                           script,
-                           script_len,
-                           full_path,
-                           1, /* line number */
-                           &script_retval)) {
+    if (!JS_EvaluateUCScript(context,
+                             module_obj,
+                             script,
+                             script_len,
+                             full_path,
+                             1, /* line number */
+                             &script_retval)) {
         g_free(script);
 
         /* If JSOPTION_DONT_REPORT_UNCAUGHT is set then the exception



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