コード例 #1
0
ファイル: jsapi-util-string.cpp プロジェクト: victoryang/gjs
gboolean
gjs_string_to_utf8 (JSContext  *context,
                    const jsval value,
                    char      **utf8_string_p)
{
    JSString *str;
    gsize len;
    char *bytes;

    JS_BeginRequest(context);

    if (!JSVAL_IS_STRING(value)) {
        gjs_throw(context,
                  "Value is not a string, cannot convert to UTF-8");
        JS_EndRequest(context);
        return JS_FALSE;
    }

    str = JSVAL_TO_STRING(value);

    len = JS_GetStringEncodingLength(context, str);
    if (len == (gsize)(-1)) {
        JS_EndRequest(context);
        return JS_FALSE;
    }

    if (utf8_string_p) {
        bytes = JS_EncodeStringToUTF8(context, str);
        *utf8_string_p = bytes;
    }

    JS_EndRequest(context);

    return JS_TRUE;
}
コード例 #2
0
ファイル: jsapi-util-string.cpp プロジェクト: GNOME/gjs
bool
gjs_string_to_utf8 (JSContext      *context,
                    const JS::Value value,
                    char          **utf8_string_p)
{
    gsize len;
    char *bytes;

    JS_BeginRequest(context);

    if (!value.isString()) {
        gjs_throw(context,
                  "Value is not a string, cannot convert to UTF-8");
        JS_EndRequest(context);
        return false;
    }

    JS::RootedString str(context, value.toString());

    len = JS_GetStringEncodingLength(context, str);
    if (len == (gsize)(-1)) {
        JS_EndRequest(context);
        return false;
    }

    if (utf8_string_p) {
        bytes = JS_EncodeStringToUTF8(context, str);
        *utf8_string_p = bytes;
    }

    JS_EndRequest(context);

    return true;
}
コード例 #3
0
/**
 * gjs_string_to_utf8:
 * @cx: JSContext
 * @value: a JS::Value containing a string
 * @utf8_string_p: return location for a unique JS chars pointer
 *
 * Converts the JSString in @value to UTF-8 and puts it in @utf8_string_p.
 *
 * This function is a convenience wrapper around JS_EncodeStringToUTF8() that
 * typechecks the JS::Value and throws an exception if it's the wrong type.
 * Don't use this function if you already have a JS::RootedString, or if you
 * know the value already holds a string; use JS_EncodeStringToUTF8() instead.
 */
bool
gjs_string_to_utf8(JSContext      *cx,
                   const JS::Value value,
                   GjsAutoJSChar  *utf8_string_p)
{
    JSAutoRequest ar(cx);

    if (!value.isString()) {
        gjs_throw(cx, "Value is not a string, cannot convert to UTF-8");
        return false;
    }

    JS::RootedString str(cx, value.toString());
    utf8_string_p->reset(JS_EncodeStringToUTF8(cx, str));
    return !!*utf8_string_p;
}
コード例 #4
0
/**
 * gjs_get_string_id:
 * @context: a #JSContext
 * @id: a jsid that is an object hash key (could be an int or string)
 * @name_p place to store ASCII string version of key
 *
 * If the id is not a string ID, return false and set *name_p to %NULL.
 * Otherwise, return true and fill in *name_p with ASCII name of id.
 *
 * Returns: true if *name_p is non-%NULL
 **/
bool
gjs_get_string_id (JSContext       *context,
                   jsid             id,
                   GjsAutoJSChar   *name_p)
{
    JS::RootedValue id_val(context);

    if (!JS_IdToValue(context, id, &id_val))
        return false;

    if (id_val.isString()) {
        JS::RootedString str(context, id_val.toString());
        name_p->reset(JS_EncodeStringToUTF8(context, str));
        return !!*name_p;
    } else {
        return false;
    }
}