Commit 1feea6bb authored by sandholm@chromium.org's avatar sandholm@chromium.org

Minor JSON cleanup. Also added comment requested for r8086.

Review URL: http://codereview.chromium.org/7086026

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@8095 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 5fd5471a
......@@ -153,7 +153,7 @@ function JSONSerialize(key, holder, replacer, stack, indent, gap) {
if (IS_STRING(value)) {
return %QuoteJSONString(value);
} else if (IS_NUMBER(value)) {
return NUMBER_IS_FINITE(value) ? $String(value) : "null";
return JSON_NUMBER_TO_STRING(value);
} else if (IS_BOOLEAN(value)) {
return value ? "true" : "false";
} else if (IS_NULL(value)) {
......@@ -164,7 +164,7 @@ function JSONSerialize(key, holder, replacer, stack, indent, gap) {
return SerializeArray(value, replacer, stack, indent, gap);
} else if (IS_NUMBER_WRAPPER(value)) {
value = ToNumber(value);
return NUMBER_IS_FINITE(value) ? ToString(value) : "null";
return JSON_NUMBER_TO_STRING(value);
} else if (IS_STRING_WRAPPER(value)) {
return %QuoteJSONString(ToString(value));
} else if (IS_BOOLEAN_WRAPPER(value)) {
......@@ -203,24 +203,22 @@ function BasicSerializeArray(value, stack, builder) {
} else {
builder.push(",");
var before = builder.length;
BasicJSONSerialize(i, value[i], stack, builder);
BasicJSONSerialize(i, val, stack, builder);
if (before == builder.length) builder[before - 1] = ",null";
}
}
}
} else if (IS_NUMBER(val)) {
// First entry is a number. Remaining entries are likely to be numbers too.
builder.push(NUMBER_IS_FINITE(val) ? %_NumberToString(val) : "null");
builder.push(JSON_NUMBER_TO_STRING(val));
for (var i = 1; i < len; i++) {
builder.push(",");
val = value[i];
if (IS_NUMBER(val)) {
builder.push(NUMBER_IS_FINITE(val)
? %_NumberToString(val)
: "null");
builder.push(JSON_NUMBER_TO_STRING(val));
} else {
var before = builder.length;
BasicJSONSerialize(i, value[i], stack, builder);
BasicJSONSerialize(i, val, stack, builder);
if (before == builder.length) builder[before - 1] = ",null";
}
}
......@@ -231,8 +229,7 @@ function BasicSerializeArray(value, stack, builder) {
for (var i = 1; i < len; i++) {
builder.push(",");
before = builder.length;
val = value[i];
BasicJSONSerialize(i, val, stack, builder);
BasicJSONSerialize(i, value[i], stack, builder);
if (before == builder.length) builder[before - 1] = ",null";
}
}
......@@ -280,7 +277,7 @@ function BasicJSONSerialize(key, value, stack, builder) {
if (IS_STRING(value)) {
builder.push(%QuoteJSONString(value));
} else if (IS_NUMBER(value)) {
builder.push(NUMBER_IS_FINITE(value) ? %_NumberToString(value) : "null");
builder.push(JSON_NUMBER_TO_STRING(value));
} else if (IS_BOOLEAN(value)) {
builder.push(value ? "true" : "false");
} else if (IS_NULL(value)) {
......@@ -290,7 +287,7 @@ function BasicJSONSerialize(key, value, stack, builder) {
// Unwrap value if necessary
if (IS_NUMBER_WRAPPER(value)) {
value = ToNumber(value);
builder.push(NUMBER_IS_FINITE(value) ? %_NumberToString(value) : "null");
builder.push(JSON_NUMBER_TO_STRING(value));
} else if (IS_STRING_WRAPPER(value)) {
builder.push(%QuoteJSONString(ToString(value)));
} else if (IS_BOOLEAN_WRAPPER(value)) {
......
......@@ -128,6 +128,7 @@ macro TO_UINT32(arg) = (arg >>> 0);
macro TO_STRING_INLINE(arg) = (IS_STRING(%IS_VAR(arg)) ? arg : NonStringToString(arg));
macro TO_NUMBER_INLINE(arg) = (IS_NUMBER(%IS_VAR(arg)) ? arg : NonNumberToNumber(arg));
macro TO_OBJECT_INLINE(arg) = (IS_SPEC_OBJECT(%IS_VAR(arg)) ? arg : ToObject(arg));
macro JSON_NUMBER_TO_STRING(arg) = ((%_IsSmi(%IS_VAR(arg)) || arg - arg == 0) ? %_NumberToString(arg) : "null");
# Macros implemented in Python.
python macro CHAR_CODE(str) = ord(str[1]);
......
......@@ -5102,6 +5102,8 @@ static inline SinkChar* WriteQuoteJsonString(
Isolate* isolate,
SinkChar* write_cursor,
Vector<const SourceChar> characters) {
// SinkChar is only char if SourceChar is guaranteed to be char.
ASSERT(sizeof(SinkChar) >= sizeof(SourceChar));
const SourceChar* read_cursor = characters.start();
const SourceChar* end = read_cursor + characters.length();
*(write_cursor++) = '"';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment