Bug 1027528 part 4 - Make JSON Quote function handle Latin1 strings. r=Waldo

This commit is contained in:
Jan de Mooij 2014-06-20 12:39:46 +02:00
parent a2a4ffc44c
commit a34828f171
3 changed files with 38 additions and 12 deletions

View File

@ -62,3 +62,14 @@ function testEvalHackNotJSON() {
}
}
testEvalHackNotJSON();
function testQuote() {
// Latin1
var s = toLatin1("abc--\x05-'\"-\n-\u00ff++");
assertEq(JSON.stringify(s), '"abc--\\u0005-\'\\"-\\n-\xFF++"');
// TwoByte
s += "\uAAAA";
assertEq(JSON.stringify(s), '"abc--\\u0005-\'\\"-\\n-\xFF++\uAAAA"');
}
testQuote();

View File

@ -7,6 +7,7 @@
#include "json.h"
#include "mozilla/FloatingPoint.h"
#include "mozilla/Range.h"
#include "jsarray.h"
#include "jsatom.h"
@ -31,6 +32,8 @@ using namespace js::types;
using mozilla::IsFinite;
using mozilla::Maybe;
using mozilla::Range;
using mozilla::RangedPtr;
const Class js::JSONClass = {
js_JSON_str,
@ -55,20 +58,19 @@ static inline bool IsQuoteSpecialCharacter(jschar c)
}
/* ES5 15.12.3 Quote. */
template <typename CharT>
static bool
Quote(JSContext *cx, StringBuffer &sb, JSString *str)
Quote(StringBuffer &sb, JSLinearString *str)
{
JS::Anchor<JSString *> anchor(str);
size_t len = str->length();
const jschar *buf = str->getChars(cx);
if (!buf)
return false;
/* Step 1. */
if (!sb.append('"'))
return false;
/* Step 2. */
JS::AutoCheckCannotGC nogc;
const RangedPtr<const CharT> buf(str->chars<CharT>(nogc), len);
for (size_t i = 0; i < len; ++i) {
/* Batch-append maximal character sequences containing no escapes. */
size_t mark = i;
@ -117,6 +119,19 @@ Quote(JSContext *cx, StringBuffer &sb, JSString *str)
return sb.append('"');
}
static bool
Quote(JSContext *cx, StringBuffer &sb, JSString *str)
{
JS::Anchor<JSString *> anchor(str);
JSLinearString *linear = str->ensureLinear(cx);
if (!linear)
return false;
return linear->hasLatin1Chars()
? Quote<Latin1Char>(sb, linear)
: Quote<jschar>(sb, linear);
}
namespace {
class StringifyContext
@ -774,8 +789,8 @@ Revive(JSContext *cx, HandleValue reviver, MutableHandleValue vp)
template <typename CharT>
bool
js::ParseJSONWithReviver(JSContext *cx, mozilla::Range<const CharT> chars,
HandleValue reviver, MutableHandleValue vp)
js::ParseJSONWithReviver(JSContext *cx, const Range<const CharT> chars, HandleValue reviver,
MutableHandleValue vp)
{
/* 15.12.2 steps 2-3. */
JSONParser<CharT> parser(cx, chars);
@ -789,12 +804,12 @@ js::ParseJSONWithReviver(JSContext *cx, mozilla::Range<const CharT> chars,
}
template bool
js::ParseJSONWithReviver(JSContext *cx, mozilla::Range<const Latin1Char> chars,
HandleValue reviver, MutableHandleValue vp);
js::ParseJSONWithReviver(JSContext *cx, const Range<const Latin1Char> chars, HandleValue reviver,
MutableHandleValue vp);
template bool
js::ParseJSONWithReviver(JSContext *cx, mozilla::Range<const jschar> chars,
HandleValue reviver, MutableHandleValue vp);
js::ParseJSONWithReviver(JSContext *cx, const Range<const jschar> chars, HandleValue reviver,
MutableHandleValue vp);
#if JS_HAS_TOSOURCE
static bool

View File

@ -28,7 +28,7 @@ namespace js {
template <typename CharT>
extern bool
ParseJSONWithReviver(JSContext *cx, mozilla::Range<const CharT> chars,
ParseJSONWithReviver(JSContext *cx, const mozilla::Range<const CharT> chars,
HandleValue reviver, MutableHandleValue vp);
} // namespace js