Bug 1034689 part 1 - Make AndroidBridge JSON parsing work with Latin1 strings. r=Waldo

This commit is contained in:
Jan de Mooij 2014-07-14 15:01:32 +02:00
parent 7cd825c5a3
commit 6072b8f9b3
3 changed files with 31 additions and 10 deletions

View File

@ -5722,13 +5722,17 @@ JS_Stringify(JSContext *cx, MutableHandleValue vp, HandleObject replacer,
}
JS_PUBLIC_API(bool)
JS_ParseJSON(JSContext *cx, const jschar *chars, uint32_t len, JS::MutableHandleValue vp)
JS_ParseJSON(JSContext *cx, const jschar *chars, uint32_t len, MutableHandleValue vp)
{
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
return ParseJSONWithReviver(cx, mozilla::Range<const jschar>(chars, len), NullHandleValue, vp);
}
RootedValue reviver(cx, NullValue());
return ParseJSONWithReviver(cx, mozilla::Range<const jschar>(chars, len), reviver, vp);
JS_PUBLIC_API(bool)
JS_ParseJSON(JSContext *cx, HandleString str, MutableHandleValue vp)
{
return JS_ParseJSONWithReviver(cx, str, NullHandleValue, vp);
}
JS_PUBLIC_API(bool)
@ -5739,6 +5743,22 @@ JS_ParseJSONWithReviver(JSContext *cx, const jschar *chars, uint32_t len, Handle
return ParseJSONWithReviver(cx, mozilla::Range<const jschar>(chars, len), reviver, vp);
}
JS_PUBLIC_API(bool)
JS_ParseJSONWithReviver(JSContext *cx, HandleString str, HandleValue reviver, MutableHandleValue vp)
{
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
assertSameCompartment(cx, str);
AutoStableStringChars stableChars(cx);
if (!stableChars.init(cx, str))
return false;
return stableChars.isLatin1()
? ParseJSONWithReviver(cx, stableChars.latin1Range(), reviver, vp)
: ParseJSONWithReviver(cx, stableChars.twoByteRange(), reviver, vp);
}
/************************************************************************/
JS_PUBLIC_API(void)

View File

@ -4483,10 +4483,17 @@ JS_Stringify(JSContext *cx, JS::MutableHandleValue value, JS::HandleObject repla
JS_PUBLIC_API(bool)
JS_ParseJSON(JSContext *cx, const jschar *chars, uint32_t len, JS::MutableHandleValue vp);
JS_PUBLIC_API(bool)
JS_ParseJSON(JSContext *cx, JS::HandleString str, JS::MutableHandleValue vp);
JS_PUBLIC_API(bool)
JS_ParseJSONWithReviver(JSContext *cx, const jschar *chars, uint32_t len, JS::HandleValue reviver,
JS::MutableHandleValue vp);
JS_PUBLIC_API(bool)
JS_ParseJSONWithReviver(JSContext *cx, JS::HandleString str, JS::HandleValue reviver,
JS::MutableHandleValue vp);
/************************************************************************/
/*

View File

@ -1588,14 +1588,8 @@ NS_IMETHODIMP nsAndroidBridge::HandleGeckoMessage(JS::HandleValue val,
}
JS::RootedString jsonStr(cx, val.toString());
size_t strLen = 0;
const jschar* strChar = JS_GetStringCharsAndLength(cx, jsonStr, &strLen);
if (!strChar) {
return NS_ERROR_UNEXPECTED;
}
JS::RootedValue jsonVal(cx);
if (!JS_ParseJSON(cx, strChar, strLen, &jsonVal) || !jsonVal.isObject()) {
if (!JS_ParseJSON(cx, jsonStr, &jsonVal) || !jsonVal.isObject()) {
return NS_ERROR_INVALID_ARG;
}