diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2010-12-07 13:56:11 -0800 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2010-12-07 13:56:11 -0800 |
commit | c30f1137121315b0d3641af6dc61e3b047f940e1 (patch) | |
tree | f118eaf670505e6a63f28bc8df845520f67adc55 /deps/v8/src/json.js | |
parent | 5b8c62f7d12c1c5a553e765ba05bbd8a7e17ee47 (diff) | |
download | node-c30f1137121315b0d3641af6dc61e3b047f940e1.tar.gz |
Upgrade V8 to 3.0.0
Diffstat (limited to 'deps/v8/src/json.js')
-rw-r--r-- | deps/v8/src/json.js | 135 |
1 files changed, 99 insertions, 36 deletions
diff --git a/deps/v8/src/json.js b/deps/v8/src/json.js index 5993100f5..e8b732a52 100644 --- a/deps/v8/src/json.js +++ b/deps/v8/src/json.js @@ -66,36 +66,6 @@ function JSONParse(text, reviver) { } } -var characterQuoteCache = { - '\b': '\\b', // ASCII 8, Backspace - '\t': '\\t', // ASCII 9, Tab - '\n': '\\n', // ASCII 10, Newline - '\f': '\\f', // ASCII 12, Formfeed - '\r': '\\r', // ASCII 13, Carriage Return - '\"': '\\"', - '\\': '\\\\' -}; - -function QuoteSingleJSONCharacter(c) { - if (c in characterQuoteCache) { - return characterQuoteCache[c]; - } - var charCode = c.charCodeAt(0); - var result; - if (charCode < 16) result = '\\u000'; - else if (charCode < 256) result = '\\u00'; - else if (charCode < 4096) result = '\\u0'; - else result = '\\u'; - result += charCode.toString(16); - characterQuoteCache[c] = result; - return result; -} - -function QuoteJSONString(str) { - var quotable = /[\\\"\x00-\x1f]/g; - return '"' + str.replace(quotable, QuoteSingleJSONCharacter) + '"'; -} - function StackContains(stack, val) { var length = stack.length; for (var i = 0; i < length; i++) { @@ -152,7 +122,7 @@ function SerializeObject(value, replacer, stack, indent, gap) { var p = replacer[i]; var strP = JSONSerialize(p, value, replacer, stack, indent, gap); if (!IS_UNDEFINED(strP)) { - var member = QuoteJSONString(p) + ":"; + var member = %QuoteJSONString(p) + ":"; if (gap != "") member += " "; member += strP; partial.push(member); @@ -164,7 +134,7 @@ function SerializeObject(value, replacer, stack, indent, gap) { if (ObjectHasOwnProperty.call(value, p)) { var strP = JSONSerialize(p, value, replacer, stack, indent, gap); if (!IS_UNDEFINED(strP)) { - var member = QuoteJSONString(p) + ":"; + var member = %QuoteJSONString(p) + ":"; if (gap != "") member += " "; member += strP; partial.push(member); @@ -209,7 +179,7 @@ function JSONSerialize(key, holder, replacer, stack, indent, gap) { } switch (typeof value) { case "string": - return QuoteJSONString(value); + return %QuoteJSONString(value); case "object": if (!value) { return "null"; @@ -225,9 +195,102 @@ function JSONSerialize(key, holder, replacer, stack, indent, gap) { } } + +function BasicSerializeArray(value, stack, builder) { + if (StackContains(stack, value)) { + throw MakeTypeError('circular_structure', []); + } + stack.push(value); + builder.push("["); + var len = value.length; + for (var i = 0; i < len; i++) { + var before = builder.length; + BasicJSONSerialize(i, value, stack, builder); + if (before == builder.length) builder.push("null"); + builder.push(","); + } + stack.pop(); + if (builder.pop() != ",") { + builder.push("[]"); // Zero length array. Push "[" back on. + } else { + builder.push("]"); + } + +} + + +function BasicSerializeObject(value, stack, builder) { + if (StackContains(stack, value)) { + throw MakeTypeError('circular_structure', []); + } + stack.push(value); + builder.push("{"); + for (var p in value) { + if (%HasLocalProperty(value, p)) { + builder.push(%QuoteJSONString(p)); + builder.push(":"); + var before = builder.length; + BasicJSONSerialize(p, value, stack, builder); + if (before == builder.length) { + builder.pop(); + builder.pop(); + } else { + builder.push(","); + } + } + } + stack.pop(); + if (builder.pop() != ",") { + builder.push("{}"); // Object has no own properties. Push "{" back on. + } else { + builder.push("}"); + } +} + + +function BasicJSONSerialize(key, holder, stack, builder) { + var value = holder[key]; + if (IS_OBJECT(value) && value) { + var toJSON = value.toJSON; + if (IS_FUNCTION(toJSON)) value = toJSON.call(value, $String(key)); + } + if (IS_STRING(value)) { + builder.push(%QuoteJSONString(value)); + } else if (IS_NUMBER(value)) { + builder.push(($isFinite(value) ? %_NumberToString(value) : "null")); + } else if (IS_BOOLEAN(value)) { + builder.push((value ? "true" : "false")); + } else if (IS_OBJECT(value)) { + // Unwrap value if necessary + if (IS_NUMBER_WRAPPER(value)) { + value = %_ValueOf(value); + builder.push(($isFinite(value) ? %_NumberToString(value) : "null")); + } else if (IS_STRING_WRAPPER(value)) { + builder.push(%QuoteJSONString(%_ValueOf(value))); + } else if (IS_BOOLEAN_WRAPPER(value)) { + builder.push((%_ValueOf(value) ? "true" : "false")); + } else { + // Regular non-wrapped object + if (!value) { + builder.push("null"); + } else if (IS_ARRAY(value)) { + BasicSerializeArray(value, stack, builder); + } else { + BasicSerializeObject(value, stack, builder); + } + } + } +} + function JSONStringify(value, replacer, space) { - var stack = []; - var indent = ""; + if (IS_UNDEFINED(replacer) && IS_UNDEFINED(space)) { + var builder = []; + BasicJSONSerialize('', {'': value}, [], builder); + if (builder.length == 0) return; + var result = %_FastAsciiArrayJoin(builder, ""); + if (!IS_UNDEFINED(result)) return result; + return %StringBuilderConcat(builder, builder.length, ""); + } if (IS_OBJECT(space)) { // Unwrap 'space' if it is wrapped if (IS_NUMBER_WRAPPER(space)) { @@ -252,7 +315,7 @@ function JSONStringify(value, replacer, space) { } else { gap = ""; } - return JSONSerialize('', {'': value}, replacer, stack, indent, gap); + return JSONSerialize('', {'': value}, replacer, [], "", gap); } function SetupJSON() { |