[gjs] Remove obsolete handling of binary data and move to CStringsAreUTF8



commit 822046043fadb2d5adeb710f93faf960c98c462e
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Thu Dec 27 23:57:34 2012 +0100

    Remove obsolete handling of binary data and move to CStringsAreUTF8
    
    All C strings are UTF8 in the glib world, so that setting make sense
    for gjs, and the reason it was previously off was to allow a broken
    way to represent binary data.
    Now that we don't do that any more, we can remove a lot of useless code
    and just let Spidermonkey do the right thing.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=680730

 gi/object.c             |    7 +-
 gi/repo.c               |    2 +-
 gjs/byteArray.c         |   28 ++--
 gjs/context.c           |    1 +
 gjs/jsapi-util-array.c  |    2 +-
 gjs/jsapi-util-error.c  |    2 +-
 gjs/jsapi-util-string.c |  428 ++++++-----------------------------------------
 gjs/jsapi-util.c        |   21 ++-
 gjs/jsapi-util.h        |   20 ---
 gjs/native.c            |    7 +-
 gjs/profiler.c          |    5 +-
 gjs/stack.c             |    2 +-
 gjs/unit-test-utils.c   |    1 +
 test/js/testLocale.js   |   13 --
 14 files changed, 88 insertions(+), 451 deletions(-)
---
diff --git a/gi/object.c b/gi/object.c
index 3ffcba3..0ec3987 100644
--- a/gi/object.c
+++ b/gi/object.c
@@ -1174,8 +1174,7 @@ real_connect_func(JSContext *context,
         return JS_FALSE;
     }
 
-    signal_name = gjs_string_get_ascii(context, argv[0]);
-    if (signal_name == NULL) {
+    if (!gjs_string_to_utf8(context, argv[0], &signal_name)) {
         return JS_FALSE;
     }
 
@@ -1325,9 +1324,7 @@ emit_func(JSContext *context,
         return JS_FALSE;
     }
 
-    signal_name = gjs_string_get_ascii(context,
-                                                  argv[0]);
-    if (signal_name == NULL)
+    if (!gjs_string_to_utf8(context, argv[0], &signal_name))
         return JS_FALSE;
 
     if (!g_signal_parse_name(signal_name,
diff --git a/gi/repo.c b/gi/repo.c
index caf163b..06e4163 100644
--- a/gi/repo.c
+++ b/gi/repo.c
@@ -87,7 +87,7 @@ resolve_namespace_object(JSContext  *context,
     version = NULL;
     if (JS_GetProperty(context, versions, ns_name, &version_val) &&
         JSVAL_IS_STRING(version_val)) {
-        version = gjs_string_get_ascii(context, version_val);
+        gjs_string_to_utf8(context, version_val, &version);
     }
 
     repo = g_irepository_get_default();
diff --git a/gjs/byteArray.c b/gjs/byteArray.c
index 3bdc689..38ab65d 100644
--- a/gjs/byteArray.c
+++ b/gjs/byteArray.c
@@ -528,11 +528,9 @@ to_string_func(JSContext *context,
 
     byte_array_ensure_array(priv);
 
-    encoding_is_utf8 = TRUE;
     if (argc >= 1 &&
         JSVAL_IS_STRING(argv[0])) {
-        encoding = gjs_string_get_ascii(context, argv[0]);
-        if (encoding == NULL)
+        if (!gjs_string_to_utf8(context, argv[0], &encoding))
             return JS_FALSE;
 
         /* maybe we should be smarter about utf8 synonyms here.
@@ -540,13 +538,14 @@ to_string_func(JSContext *context,
          * just an optimization anyway.
          */
         if (strcmp(encoding, "UTF-8") == 0) {
-            g_free(encoding);
             encoding_is_utf8 = TRUE;
+            g_free(encoding);
+            encoding = NULL;
         } else {
             encoding_is_utf8 = FALSE;
         }
     } else {
-        encoding = "UTF-8";
+        encoding_is_utf8 = TRUE;
     }
 
     if (priv->array->len == 0)
@@ -557,7 +556,7 @@ to_string_func(JSContext *context,
 
     if (encoding_is_utf8) {
         /* optimization, avoids iconv overhead and runs
-         * glib's hardwired utf8-to-utf16
+         * libmozjs hardwired utf8-to-utf16
          */
         jsval retval;
         JSBool ok;
@@ -681,11 +680,9 @@ from_string_func(JSContext *context,
         goto out;
     }
 
-    encoding_is_utf8 = TRUE;
     if (argc > 1 &&
         JSVAL_IS_STRING(argv[1])) {
-        encoding = gjs_string_get_ascii(context, argv[1]);
-        if (encoding == NULL)
+        if (!gjs_string_to_utf8(context, argv[1], &encoding))
             goto out;
 
         /* maybe we should be smarter about utf8 synonyms here.
@@ -693,22 +690,19 @@ from_string_func(JSContext *context,
          * just an optimization anyway.
          */
         if (strcmp(encoding, "UTF-8") == 0) {
-            g_free(encoding);
             encoding_is_utf8 = TRUE;
+            g_free(encoding);
+            encoding = NULL;
         } else {
             encoding_is_utf8 = FALSE;
         }
     } else {
-        encoding = "UTF-8";
+        encoding_is_utf8 = TRUE;
     }
 
     if (encoding_is_utf8) {
         /* optimization? avoids iconv overhead and runs
-         * glib's hardwired utf16-to-utf8.
-         * Does a gratuitous copy/strlen, but
-         * the generic path below also has
-         * gratuitous copy. Could be fixed for this path,
-         * if it ever turns out to matter.
+         * libmozjs hardwired utf16-to-utf8.
          */
         char *utf8 = NULL;
         if (!gjs_string_to_utf8(context,
@@ -750,7 +744,7 @@ from_string_func(JSContext *context,
 
         g_free(encoded);
     }
-    
+
     JS_SET_RVAL(context, vp, OBJECT_TO_JSVAL(obj));
 
     retval = JS_TRUE;
diff --git a/gjs/context.c b/gjs/context.c
index 6fff636..afe1470 100644
--- a/gjs/context.c
+++ b/gjs/context.c
@@ -558,6 +558,7 @@ gjs_context_constructor (GType                  type,
     js_context = GJS_CONTEXT(object);
 
     if (js_context->runtime == NULL) {
+        JS_SetCStringsAreUTF8();
         js_context->runtime = JS_NewRuntime(32*1024*1024 /* max bytes */);
         if (js_context->runtime == NULL)
             gjs_fatal("Failed to create javascript runtime");
diff --git a/gjs/jsapi-util-array.c b/gjs/jsapi-util-array.c
index cd74afa..a5553a6 100644
--- a/gjs/jsapi-util-array.c
+++ b/gjs/jsapi-util-array.c
@@ -318,7 +318,7 @@ gjstest_test_func_gjs_jsapi_util_array(void)
 
         value = gjs_rooted_array_get(context, array, i);
         g_assert(JSVAL_IS_STRING(value));
-        ascii = gjs_string_get_ascii(context, value);
+        gjs_string_to_utf8(context, value, &ascii);
         g_assert(strcmp(ascii, "abcdefghijk") == 0);
         g_free(ascii);
     }
diff --git a/gjs/jsapi-util-error.c b/gjs/jsapi-util-error.c
index 39f32e3..a4b1c61 100644
--- a/gjs/jsapi-util-error.c
+++ b/gjs/jsapi-util-error.c
@@ -219,7 +219,7 @@ gjstest_test_func_gjs_jsapi_util_error_throw(void)
 
     g_assert(JSVAL_IS_STRING(value));
 
-    gjs_string_get_binary_data (context, value, &s, NULL);
+    gjs_string_to_utf8(context, value, &s);
     g_assert(s != NULL);
     strcmp_result = strcmp(s, "This is an exception 42");
     free(s);
diff --git a/gjs/jsapi-util-string.c b/gjs/jsapi-util-string.c
index 60bbebb..e627da0 100644
--- a/gjs/jsapi-util-string.c
+++ b/gjs/jsapi-util-string.c
@@ -29,89 +29,39 @@
 #include "compat.h"
 
 gboolean
-gjs_try_string_to_utf8 (JSContext  *context,
-                        const jsval string_val,
-                        char      **utf8_string_p,
-                        GError    **error)
+gjs_string_to_utf8 (JSContext  *context,
+                    const jsval value,
+                    char      **utf8_string_p)
 {
-    const jschar *s;
-    size_t s_length;
-    char *utf8_string;
-    long read_items;
-    long utf8_length;
-    GError *convert_error = NULL;
+    JSString *str;
+    gsize len;
+    char *bytes;
 
     JS_BeginRequest(context);
 
-    if (!JSVAL_IS_STRING(string_val)) {
-        g_set_error_literal(error, GJS_UTIL_ERROR, GJS_UTIL_ERROR_ARGUMENT_TYPE_MISMATCH,
-                            "Object is not a string, cannot convert to UTF-8");
-        JS_EndRequest(context);
-        return FALSE;
-    }
-
-    s = JS_GetStringCharsAndLength(context, JSVAL_TO_STRING(string_val), &s_length);
-    if (s == NULL) {
+    if (!JSVAL_IS_STRING(value)) {
+        gjs_throw(context,
+                  "Value is not a string, cannot convert to UTF-8");
         JS_EndRequest(context);
-        return FALSE;
+        return JS_FALSE;
     }
 
-    utf8_string = g_utf16_to_utf8(s,
-                                  (glong)s_length,
-                                  &read_items, &utf8_length,
-                                  &convert_error);
-
-    /* ENDING REQUEST - no JSAPI after this point */
-    JS_EndRequest(context);
-
-    if (!utf8_string) {
-        g_set_error(error, GJS_UTIL_ERROR, GJS_UTIL_ERROR_ARGUMENT_INVALID,
-                    "Failed to convert JS string to UTF-8: %s",
-                    convert_error->message);
-        g_error_free(convert_error);
-        return FALSE;
-    }
+    str = JSVAL_TO_STRING(value);
 
-    if ((size_t)read_items != s_length) {
-        g_set_error_literal(error, GJS_UTIL_ERROR, GJS_UTIL_ERROR_ARGUMENT_INVALID,
-                            "JS string contains embedded NULs");
-        g_free(utf8_string);
-        return FALSE;
-    }
+    len = JS_GetStringEncodingLength(context, str);
+    if (len == (gsize)(-1))
+        return JS_FALSE;
 
-    /* Our assumption is that the string is being converted to UTF-8
-     * in order to use with GLib-style APIs; Javascript has a looser
-     * sense of validate-Unicode than GLib, so validate here to
-     * prevent problems later on. Given the success of the above,
-     * the only thing that could really be wrong here is including
-     * non-characters like a byte-reversed BOM. If the validation
-     * ever becomes a bottleneck, we could do an inline-special
-     * case of all-ASCII.
-     */
-    if (!g_utf8_validate (utf8_string, utf8_length, NULL)) {
-        g_set_error_literal(error, GJS_UTIL_ERROR, GJS_UTIL_ERROR_ARGUMENT_INVALID,
-                            "JS string contains invalid Unicode characters");
-        g_free(utf8_string);
-        return FALSE;
+    if (utf8_string_p) {
+        bytes = g_malloc((len + 1) * sizeof(char));
+        JS_EncodeStringToBuffer(str, bytes, len);
+        bytes[len] = '\0';
+        *utf8_string_p = bytes;
     }
 
-    *utf8_string_p = utf8_string;
-    return TRUE;
-}
-
-JSBool
-gjs_string_to_utf8 (JSContext  *context,
-                    const jsval string_val,
-                    char      **utf8_string_p)
-{
-  GError *error = NULL;
+    JS_EndRequest(context);
 
-  if (!gjs_try_string_to_utf8(context, string_val, utf8_string_p, &error))
-    {
-      gjs_throw_g_error(context, error);
-      return JS_FALSE;
-    }
-  return JS_TRUE;
+    return JS_TRUE;
 }
 
 JSBool
@@ -120,96 +70,47 @@ gjs_string_from_utf8(JSContext  *context,
                      gssize      n_bytes,
                      jsval      *value_p)
 {
-    jschar *u16_string;
-    glong u16_string_length;
-    JSString *s;
-    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;
-    }
+    JSString *str;
 
     JS_BeginRequest(context);
 
-    if (g_mem_is_system_malloc()) {
-        /* Avoid a copy - assumes that g_malloc == js_malloc == malloc */
-        s = JS_NewUCString(context, u16_string, u16_string_length);
-    } else {
-        s = JS_NewUCStringCopyN(context,
-                                (jschar*)u16_string,
-                                u16_string_length);
-        g_free(u16_string);
-    }
+    if (n_bytes < 0)
+        n_bytes = strlen(utf8_string);
+    str = JS_NewStringCopyN(context, utf8_string, n_bytes);
 
-    if (!s) {
-        JS_EndRequest(context);
-        return JS_FALSE;
-    }
+    if (str && value_p)
+        *value_p = STRING_TO_JSVAL(str);
 
-    *value_p = STRING_TO_JSVAL(s);
     JS_EndRequest(context);
-    return JS_TRUE;
+    return str != NULL;
 }
 
 gboolean
-gjs_try_string_to_filename(JSContext    *context,
-                           const jsval   filename_val,
-                           char        **filename_string_p,
-                           GError      **error)
+gjs_string_to_filename(JSContext    *context,
+                       const jsval   filename_val,
+                       char        **filename_string_p)
 {
-  gchar *tmp, *filename_string;
-
-  /* gjs_string_to_filename verifies that filename_val is a string */
-
-  if (!gjs_try_string_to_utf8(context, filename_val, &tmp, error)) {
-      /* exception already set */
-      return JS_FALSE;
-  }
+    GError *error;
+    gchar *tmp, *filename_string;
 
-  error = NULL;
-  filename_string = g_filename_from_utf8(tmp, -1, NULL, NULL, error);
-  if (!filename_string) {
-    g_free(tmp);
-    return FALSE;
-  }
+    /* gjs_string_to_filename verifies that filename_val is a string */
 
-  *filename_string_p = filename_string;
+    if (!gjs_string_to_utf8(context, filename_val, &tmp)) {
+        /* exception already set */
+        return JS_FALSE;
+    }
 
-  g_free(tmp);
-  return TRUE;
-}
+    error = NULL;
+    filename_string = g_filename_from_utf8(tmp, -1, NULL, NULL, &error);
+    if (!filename_string) {
+        gjs_throw_g_error(context, error);
+        g_free(tmp);
+        return FALSE;
+    }
 
-JSBool
-gjs_string_to_filename(JSContext    *context,
-                       const jsval   filename_val,
-                       char        **filename_string_p)
-{
-  GError *error = NULL;
-
-  if (!gjs_try_string_to_filename(context, filename_val, filename_string_p, &error)) {
-      gjs_throw(context,
-                "Could not convert filename to UTF8: '%s'",
-                error->message);
-      g_error_free(error);
-      return JS_FALSE;
-  }
-  return JS_TRUE;
+    *filename_string_p = filename_string;
+    g_free(tmp);
+    return TRUE;
 }
 
 JSBool
@@ -243,148 +144,6 @@ gjs_string_from_filename(JSContext  *context,
     return JS_TRUE;
 }
 
-
-/**
- * gjs_string_get_ascii:
- * @context: a JSContext
- * @value: a jsval
- *
- * If the given value is not a string, throw an exception and return %NULL.
- * Otherwise, return the ascii bytes of the string. If the string is not
- * ASCII, you will get corrupted garbage.
- *
- * Returns: an ASCII C string or %NULL on error
- **/
-char*
-gjs_string_get_ascii(JSContext       *context,
-                     jsval            value)
-{
-    char *ascii;
-
-    if (!JSVAL_IS_STRING(value)) {
-        gjs_throw(context, "A string was expected, but value was not a string");
-        return NULL;
-    }
-
-    gjs_string_get_binary_data(context, value, &ascii, NULL);
-
-    return ascii;
-}
-
-static JSBool
-throw_if_binary_strings_broken(JSContext *context)
-{
-    /* JS_GetStringBytes() returns low byte of each jschar,
-     * unless JS_CStringsAreUTF8() in which case we're screwed.
-     */
-    if (JS_CStringsAreUTF8()) {
-        gjs_throw(context, "If JS_CStringsAreUTF8(), gjs doesn't know how to do binary strings");
-        return JS_TRUE;
-    }
-
-    return JS_FALSE;
-}
-
-/**
- * gjs_string_get_binary_data:
- * @context: js context
- * @value: a jsval
- * @data_p: address to return allocated data buffer
- * @len_p: address to return length of data
- *
- * Get the binary data in the JSString contained in @value.
- * Throws a JS exception if value is not a string.
- *
- * Returns: JS_FALSE if exception thrown
- **/
-JSBool
-gjs_string_get_binary_data(JSContext       *context,
-                           jsval            value,
-                           char           **data_p,
-                           gsize           *len_p)
-{
-    JSString *str;
-    gsize len;
-    char *bytes;
-
-    JS_BeginRequest(context);
-
-    if (!JSVAL_IS_STRING(value)) {
-        gjs_throw(context,
-                  "Value is not a string, can't return binary data from it");
-        JS_EndRequest(context);
-        return JS_FALSE;
-    }
-
-    if (throw_if_binary_strings_broken(context)) {
-        JS_EndRequest(context);
-        return JS_FALSE;
-    }
-
-    str = JSVAL_TO_STRING(value);
-
-    len = JS_GetStringEncodingLength(context, str);
-    if (len == (gsize)(-1))
-        return JS_FALSE;
-
-    if (data_p) {
-        bytes = g_malloc((len + 1) * sizeof(char));
-        JS_EncodeStringToBuffer(str, bytes, len);
-        bytes[len] = '\0';
-        *data_p = bytes;
-    }
-
-    if (len_p)
-        *len_p = len;
-
-    JS_EndRequest(context);
-
-    return JS_TRUE;
-}
-
-/**
- * gjs_string_from_binary_data:
- * @context: js context
- * @data: binary data buffer
- * @len: length of data
- * @value_p: a jsval location, should be GC rooted
- *
- * Gets a string representing the passed-in binary data.
- *
- * Returns: JS_FALSE if exception thrown
- **/
-JSBool
-gjs_string_from_binary_data(JSContext       *context,
-                            const char      *data,
-                            gsize            len,
-                            jsval           *value_p)
-{
-    JSString *s;
-
-    JS_BeginRequest(context);
-
-    if (throw_if_binary_strings_broken(context)) {
-        JS_EndRequest(context);
-        return JS_FALSE;
-    }
-
-    /* store each byte in a 16-bit jschar so all high bytes are 0;
-     * we can't put two bytes per jschar because then we'd lose
-     * track of the string length if it was an odd number.
-     */
-    s = JS_NewStringCopyN(context, data, len);
-    if (s == NULL) {
-        /* gjs_throw() does nothing if exception already set */
-        gjs_throw(context, "Failed to allocate binary string");
-        JS_EndRequest(context);
-        return JS_FALSE;
-    }
-    *value_p = STRING_TO_JSVAL(s);
-
-    JS_EndRequest(context);
-    return JS_TRUE;
-}
-
 /**
  * gjs_string_get_uint16_data:
  * @context: js context
@@ -449,8 +208,7 @@ gjs_get_string_id (JSContext       *context,
         return JS_FALSE;
 
     if (JSVAL_IS_STRING(id_val)) {
-        gjs_string_get_binary_data(context, id_val, name_p, NULL);
-        return JS_TRUE;
+        return gjs_string_to_utf8(context, id_val, name_p);
     } else {
         *name_p = NULL;
         return JS_FALSE;
@@ -487,7 +245,6 @@ gjs_unichar_from_string (JSContext *context,
 #include "unit-test-utils.h"
 #include <string.h>
 
-
 void
 gjstest_test_func_gjs_jsapi_util_string_js_string_utf8(void)
 {
@@ -511,91 +268,4 @@ gjstest_test_func_gjs_jsapi_util_string_js_string_utf8(void)
     g_free(utf8_result);
 }
 
-void
-gjstest_test_func_gjs_jsapi_util_string_get_ascii(void)
-{
-    GjsUnitTestFixture fixture;
-    JSContext *context;
-    const char *ascii_string = "Hello, world";
-    JSString  *js_string;
-    jsval      void_value;
-    char *test;
-
-    _gjs_unit_test_fixture_begin(&fixture);
-    context = fixture.context;
-
-    js_string = JS_NewStringCopyZ(context, ascii_string);
-    test = gjs_string_get_ascii(context, STRING_TO_JSVAL(js_string));
-    g_assert(g_str_equal(test, ascii_string));
-    g_free(test);
-    void_value = JSVAL_VOID;
-    test = gjs_string_get_ascii(context, void_value);
-    g_assert(test == NULL);
-    g_free(test);
-    g_assert(JS_IsExceptionPending(context));
-
-    _gjs_unit_test_fixture_finish(&fixture);
-}
-
-void
-gjstest_test_func_gjs_jsapi_util_string_get_binary(void)
-{
-    GjsUnitTestFixture fixture;
-    JSContext *context;
-    const char binary_string[] = "foo\0bar\0baz";
-    const char binary_string_odd[] = "foo\0bar\0baz123";
-    jsval js_string;
-    jsval void_value;
-    char *data;
-    gsize len;
-
-    g_assert_cmpuint(sizeof(binary_string), ==, 12);
-    g_assert_cmpuint(sizeof(binary_string_odd), ==, 15);
-
-    _gjs_unit_test_fixture_begin(&fixture);
-    context = fixture.context;
-
-    js_string = JSVAL_VOID;
-    JS_AddValueRoot(context, &js_string);
-
-    /* Even-length string (maps nicely to len/2 jschar) */
-    if (!gjs_string_from_binary_data(context,
-                                     binary_string, sizeof(binary_string),
-                                     &js_string))
-        g_error("Failed to create binary data string");
-
-    if (!gjs_string_get_binary_data(context,
-                                    js_string,
-                                    &data, &len))
-        g_error("Failed to get binary data from string");
-    g_assert_cmpuint(len, ==, sizeof(binary_string));
-    g_assert(memcmp(data, binary_string, len) == 0);
-    g_free(data);
-
-
-    /* Odd-length string (does not map nicely to len/2 jschar) */
-    if (!gjs_string_from_binary_data(context,
-                                     binary_string_odd, sizeof(binary_string_odd),
-                                     &js_string))
-        g_error("Failed to create odd-length binary data string");
-
-    if (!gjs_string_get_binary_data(context,
-                                    js_string,
-                                    &data, &len))
-        g_error("Failed to get binary data from string");
-    g_assert_cmpuint(len, ==, sizeof(binary_string_odd));
-    g_assert(memcmp(data, binary_string_odd, len) == 0);
-    g_free(data);
-
-    JS_RemoveValueRoot(context, &js_string);
-
-    void_value = JSVAL_VOID;
-    g_assert(!JS_IsExceptionPending(context));
-    g_assert(!gjs_string_get_binary_data(context, void_value,
-                                        &data, &len));
-    g_assert(JS_IsExceptionPending(context));
-
-    _gjs_unit_test_fixture_finish(&fixture);
-}
-
 #endif /* GJS_BUILD_TESTS */
diff --git a/gjs/jsapi-util.c b/gjs/jsapi-util.c
index 5768ad3..8373262 100644
--- a/gjs/jsapi-util.c
+++ b/gjs/jsapi-util.c
@@ -509,7 +509,7 @@ gjs_string_readable (JSContext   *context,
 
     g_string_append_c(buf, '"');
 
-    if (!gjs_try_string_to_utf8(context, STRING_TO_JSVAL(string), &chars, NULL)) {
+    if (!gjs_string_to_utf8(context, STRING_TO_JSVAL(string), &chars)) {
         size_t i, len;
         const jschar *uchars;
 
@@ -939,7 +939,7 @@ log_prop(JSContext  *context,
     if (JSVAL_IS_STRING(id)) {
         char *name;
 
-        name = gjs_string_get_ascii(context, id);
+        gjs_string_to_utf8(context, id, &name);
         gjs_debug(GJS_DEBUG_PROPS,
                   "prop %s: %s",
                   name, what);
@@ -1142,7 +1142,6 @@ gjs_parse_args (JSContext  *context,
     guint i;
     const char *fmt_iter;
     va_list args;
-    GError *arg_error = NULL;
     guint n_unwind = 0;
 #define MAX_UNWIND_STRINGS 16
     gpointer unwind_strings[MAX_UNWIND_STRINGS];
@@ -1237,9 +1236,13 @@ gjs_parse_args (JSContext  *context,
             if (*fmt_iter == 'z' && JSVAL_IS_NULL(js_value)) {
                 *arg = NULL;
             } else {
-                if (gjs_try_string_to_utf8 (context, js_value, arg, &arg_error)) {
+                if (gjs_string_to_utf8 (context, js_value, arg)) {
                     unwind_strings[n_unwind++] = *arg;
                     g_assert(n_unwind < MAX_UNWIND_STRINGS);
+                } else {
+                    /* Our error message is going to be more useful */
+                    JS_ClearPendingException(context);
+                    arg_error_message = "Couldn't convert to string";
                 }
             }
         }
@@ -1247,9 +1250,13 @@ gjs_parse_args (JSContext  *context,
         case 'F': {
             char **arg = arg_location;
 
-            if (gjs_try_string_to_filename (context, js_value, arg, &arg_error)) {
+            if (gjs_string_to_filename (context, js_value, arg)) {
                 unwind_strings[n_unwind++] = *arg;
                 g_assert(n_unwind < MAX_UNWIND_STRINGS);
+            } else {
+                /* Our error message is going to be more useful */
+                JS_ClearPendingException(context);
+                arg_error_message = "Couldn't convert to filename";
             }
         }
             break;
@@ -1297,13 +1304,9 @@ gjs_parse_args (JSContext  *context,
             g_assert_not_reached ();
         }
 
-        if (arg_error != NULL)
-            arg_error_message = arg_error->message;
-
         if (arg_error_message != NULL) {
             gjs_throw(context, "Error invoking %s, at argument %d (%s): %s", function_name,
                       consumed_args+1, argname, arg_error_message);
-            g_clear_error (&arg_error);
             goto error_unwind;
         }
 
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index f49bd5b..e3c10c8 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -306,16 +306,6 @@ JSBool      gjs_string_from_filename         (JSContext       *context,
                                               const char      *filename_string,
                                               gssize           n_bytes,
                                               jsval           *value_p);
-char*       gjs_string_get_ascii             (JSContext       *context,
-                                              jsval            value);
-JSBool      gjs_string_get_binary_data       (JSContext       *context,
-                                              jsval            value,
-                                              char           **data_p,
-                                              gsize           *len_p);
-JSBool      gjs_string_from_binary_data      (JSContext       *context,
-                                              const char      *data,
-                                              gsize            len,
-                                              jsval           *value_p);
 JSBool      gjs_string_get_uint16_data       (JSContext       *context,
                                               jsval            value,
                                               guint16        **data_p,
@@ -371,16 +361,6 @@ void              gjs_unroot_value_locations  (JSContext        *context,
 
 /* Functions intended for more "internal" use */
 
-gboolean gjs_try_string_to_filename           (JSContext    *context,
-                                               const jsval   filename_val,
-                                               char        **filename_string_p,
-                                               GError      **error);
-
-gboolean    gjs_try_string_to_utf8            (JSContext       *context,
-                                               const            jsval string_val,
-                                               char           **utf8_string_p,
-                                               GError         **error);
-
 void gjs_maybe_gc (JSContext *context);
 
 G_END_DECLS
diff --git a/gjs/native.c b/gjs/native.c
index fce6d58..b7aa3d9 100644
--- a/gjs/native.c
+++ b/gjs/native.c
@@ -95,9 +95,14 @@ get_module_name(JSContext *context,
                 JSObject  *module_obj,
                 jsval     *tmp)
 {
+    char *name;
+
     if (gjs_object_get_property(context, module_obj, "__moduleName__", tmp) &&
         JSVAL_IS_STRING(*tmp))
-        return gjs_string_get_ascii(context, *tmp);
+        if (gjs_string_to_utf8(context, *tmp, &name))
+            return name;
+        else
+            return NULL;
     else
         return NULL;
 }
diff --git a/gjs/profiler.c b/gjs/profiler.c
index ac726cb..51b80f1 100644
--- a/gjs/profiler.c
+++ b/gjs/profiler.c
@@ -154,9 +154,8 @@ gjs_profile_function_key_from_js(JSContext             *cx,
      * figure out the name of the called function.
      */
     function_name = JS_GetFunctionId(function);
-    if (function_name)
-        key->function_name = gjs_string_get_ascii(cx, STRING_TO_JSVAL(function_name));
-    else
+    if (!function_name ||
+        !gjs_string_to_utf8(cx, STRING_TO_JSVAL(function_name), &key->function_name))
         key->function_name = g_strdup("(unknown)");
 
     g_assert(key->filename != NULL);
diff --git a/gjs/stack.c b/gjs/stack.c
index 53f0359..230fb38 100644
--- a/gjs/stack.c
+++ b/gjs/stack.c
@@ -117,7 +117,7 @@ format_frame(JSContext* cx, JSStackFrame* fp,
         if (fun) {
 	    JSString* funname = JS_GetFunctionId(fun);
             if (funname)
-                funname_str = gjs_string_get_ascii(cx, STRING_TO_JSVAL(funname));
+              gjs_string_to_utf8(cx, STRING_TO_JSVAL(funname), &funname_str);
 	}
 
         call_obj = JS_GetFrameCallObject(cx, fp);
diff --git a/gjs/unit-test-utils.c b/gjs/unit-test-utils.c
index 86490f5..97afcf9 100644
--- a/gjs/unit-test-utils.c
+++ b/gjs/unit-test-utils.c
@@ -35,6 +35,7 @@ test_error_reporter(JSContext     *context,
 void
 _gjs_unit_test_fixture_begin (GjsUnitTestFixture *fixture)
 {
+    JS_SetCStringsAreUTF8();
     fixture->runtime = JS_NewRuntime(1024*1024 /* max bytes */);
     fixture->context = JS_NewContext(fixture->runtime, 8192);
     JS_BeginRequest(fixture->context);
diff --git a/test/js/testLocale.js b/test/js/testLocale.js
index 48ab4f9..1b8f271 100644
--- a/test/js/testLocale.js
+++ b/test/js/testLocale.js
@@ -42,17 +42,4 @@ function testToLocaleCompare() {
     assertRaises(function() { "a".localeCompare("\ud800"); });
 }
 
-function testInvalidStrings() {
-    // Not really related to locale handling - here we are testing
-    // gjs_string_to_utf8() to properly catch things we'll choke
-    // on later.
-
-    // Unpaired surrogate
-    assertRaises(function() { "\ud800".toLocaleLowerCase(); });
-    // Embedded NUL
-    assertRaises(function() { "\u0000".toLocaleLowerCase(); });
-    // Byte-reversed BOM (an example of a non-character)
-    assertRaises(function() { "\ufffe".toLocaleLowerCase(); });
-}
-
 gjstestRun();



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