| OLD | NEW |
| 1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 var $JSON = global.JSON; | 28 var $JSON = global.JSON; |
| 29 | 29 |
| 30 function ParseJSONUnfiltered(text) { | |
| 31 var s = $String(text); | |
| 32 return %ParseJson(s); | |
| 33 } | |
| 34 | |
| 35 function Revive(holder, name, reviver) { | 30 function Revive(holder, name, reviver) { |
| 36 var val = holder[name]; | 31 var val = holder[name]; |
| 37 if (IS_OBJECT(val)) { | 32 if (IS_OBJECT(val)) { |
| 38 if (IS_ARRAY(val)) { | 33 if (IS_ARRAY(val)) { |
| 39 var length = val.length; | 34 var length = val.length; |
| 40 for (var i = 0; i < length; i++) { | 35 for (var i = 0; i < length; i++) { |
| 41 var newElement = Revive(val, $String(i), reviver); | 36 var newElement = Revive(val, $String(i), reviver); |
| 42 val[i] = newElement; | 37 val[i] = newElement; |
| 43 } | 38 } |
| 44 } else { | 39 } else { |
| 45 for (var p in val) { | 40 for (var p in val) { |
| 46 if (ObjectHasOwnProperty.call(val, p)) { | 41 if (ObjectHasOwnProperty.call(val, p)) { |
| 47 var newElement = Revive(val, p, reviver); | 42 var newElement = Revive(val, p, reviver); |
| 48 if (IS_UNDEFINED(newElement)) { | 43 if (IS_UNDEFINED(newElement)) { |
| 49 delete val[p]; | 44 delete val[p]; |
| 50 } else { | 45 } else { |
| 51 val[p] = newElement; | 46 val[p] = newElement; |
| 52 } | 47 } |
| 53 } | 48 } |
| 54 } | 49 } |
| 55 } | 50 } |
| 56 } | 51 } |
| 57 return reviver.call(holder, name, val); | 52 return reviver.call(holder, name, val); |
| 58 } | 53 } |
| 59 | 54 |
| 60 function JSONParse(text, reviver) { | 55 function JSONParse(text, reviver) { |
| 61 var unfiltered = ParseJSONUnfiltered(text); | 56 var unfiltered = %ParseJson(TO_STRING_INLINE(text)); |
| 62 if (IS_FUNCTION(reviver)) { | 57 if (IS_FUNCTION(reviver)) { |
| 63 return Revive({'': unfiltered}, '', reviver); | 58 return Revive({'': unfiltered}, '', reviver); |
| 64 } else { | 59 } else { |
| 65 return unfiltered; | 60 return unfiltered; |
| 66 } | 61 } |
| 67 } | 62 } |
| 68 | 63 |
| 69 function SerializeArray(value, replacer, stack, indent, gap) { | 64 function SerializeArray(value, replacer, stack, indent, gap) { |
| 70 if (!%PushIfAbsent(stack, value)) { | 65 if (!%PushIfAbsent(stack, value)) { |
| 71 throw MakeTypeError('circular_structure', []); | 66 throw MakeTypeError('circular_structure', []); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 if (IS_FUNCTION(toJSON)) { | 146 if (IS_FUNCTION(toJSON)) { |
| 152 value = %_CallFunction(value, key, toJSON); | 147 value = %_CallFunction(value, key, toJSON); |
| 153 } | 148 } |
| 154 } | 149 } |
| 155 if (IS_FUNCTION(replacer)) { | 150 if (IS_FUNCTION(replacer)) { |
| 156 value = %_CallFunction(holder, key, value, replacer); | 151 value = %_CallFunction(holder, key, value, replacer); |
| 157 } | 152 } |
| 158 if (IS_STRING(value)) { | 153 if (IS_STRING(value)) { |
| 159 return %QuoteJSONString(value); | 154 return %QuoteJSONString(value); |
| 160 } else if (IS_NUMBER(value)) { | 155 } else if (IS_NUMBER(value)) { |
| 161 return $isFinite(value) ? $String(value) : "null"; | 156 return NUMBER_IS_FINITE(value) ? $String(value) : "null"; |
| 162 } else if (IS_BOOLEAN(value)) { | 157 } else if (IS_BOOLEAN(value)) { |
| 163 return value ? "true" : "false"; | 158 return value ? "true" : "false"; |
| 164 } else if (IS_NULL(value)) { | 159 } else if (IS_NULL(value)) { |
| 165 return "null"; | 160 return "null"; |
| 166 } else if (IS_SPEC_OBJECT(value) && !(typeof value == "function")) { | 161 } else if (IS_SPEC_OBJECT(value) && !(typeof value == "function")) { |
| 167 // Non-callable object. If it's a primitive wrapper, it must be unwrapped. | 162 // Non-callable object. If it's a primitive wrapper, it must be unwrapped. |
| 168 if (IS_ARRAY(value)) { | 163 if (IS_ARRAY(value)) { |
| 169 return SerializeArray(value, replacer, stack, indent, gap); | 164 return SerializeArray(value, replacer, stack, indent, gap); |
| 170 } else if (IS_NUMBER_WRAPPER(value)) { | 165 } else if (IS_NUMBER_WRAPPER(value)) { |
| 171 value = ToNumber(value); | 166 value = ToNumber(value); |
| 172 return $isFinite(value) ? ToString(value) : "null"; | 167 return NUMBER_IS_FINITE(value) ? ToString(value) : "null"; |
| 173 } else if (IS_STRING_WRAPPER(value)) { | 168 } else if (IS_STRING_WRAPPER(value)) { |
| 174 return %QuoteJSONString(ToString(value)); | 169 return %QuoteJSONString(ToString(value)); |
| 175 } else if (IS_BOOLEAN_WRAPPER(value)) { | 170 } else if (IS_BOOLEAN_WRAPPER(value)) { |
| 176 return %_ValueOf(value) ? "true" : "false"; | 171 return %_ValueOf(value) ? "true" : "false"; |
| 177 } else { | 172 } else { |
| 178 return SerializeObject(value, replacer, stack, indent, gap); | 173 return SerializeObject(value, replacer, stack, indent, gap); |
| 179 } | 174 } |
| 180 } | 175 } |
| 181 // Undefined or a callable object. | 176 // Undefined or a callable object. |
| 182 return void 0; | 177 return void 0; |
| 183 } | 178 } |
| 184 | 179 |
| 185 | 180 |
| 186 function BasicSerializeArray(value, stack, builder) { | 181 function BasicSerializeArray(value, stack, builder) { |
| 182 var len = value.length; |
| 183 if (len == 0) { |
| 184 builder.push("[]"); |
| 185 return; |
| 186 } |
| 187 if (!%PushIfAbsent(stack, value)) { | 187 if (!%PushIfAbsent(stack, value)) { |
| 188 throw MakeTypeError('circular_structure', []); | 188 throw MakeTypeError('circular_structure', []); |
| 189 } | 189 } |
| 190 builder.push("["); | 190 builder.push("["); |
| 191 var len = value.length; | 191 var val = value[0]; |
| 192 for (var i = 0; i < len; i++) { | 192 if (IS_STRING(val)) { |
| 193 // First entry is a string. Remaining entries are likely to be strings too. |
| 194 builder.push(%QuoteJSONString(val)); |
| 195 for (var i = 1; i < len; i++) { |
| 196 builder.push(","); |
| 197 val = value[i]; |
| 198 if (IS_STRING(val)) { |
| 199 builder.push(%QuoteJSONString(val)); |
| 200 } else { |
| 201 var before = builder.length; |
| 202 BasicJSONSerialize(i, value[i], stack, builder); |
| 203 if (before == builder.length) builder.push("null"); |
| 204 } |
| 205 } |
| 206 } else if (IS_NUMBER(val)) { |
| 207 // First entry is a number. Remaining entries are likely to be numbers too. |
| 208 builder.push(NUMBER_IS_FINITE(val) ? %_NumberToString(val) : "null"); |
| 209 for (var i = 1; i < len; i++) { |
| 210 builder.push(","); |
| 211 val = value[i]; |
| 212 if (IS_NUMBER(val)) { |
| 213 builder.push(NUMBER_IS_FINITE(val) |
| 214 ? %_NumberToString(val) |
| 215 : "null"); |
| 216 } else { |
| 217 var before = builder.length; |
| 218 BasicJSONSerialize(i, value[i], stack, builder); |
| 219 if (before == builder.length) builder.push("null"); |
| 220 } |
| 221 } |
| 222 } else { |
| 193 var before = builder.length; | 223 var before = builder.length; |
| 194 BasicJSONSerialize(i, value, stack, builder); | 224 BasicJSONSerialize(0, val, stack, builder); |
| 195 if (before == builder.length) builder.push("null"); | 225 if (before == builder.length) builder.push("null"); |
| 196 builder.push(","); | 226 for (var i = 1; i < len; i++) { |
| 227 builder.push(","); |
| 228 before = builder.length; |
| 229 val = value[i]; |
| 230 BasicJSONSerialize(i, val, stack, builder); |
| 231 if (before == builder.length) builder.push("null"); |
| 232 } |
| 197 } | 233 } |
| 198 stack.pop(); | 234 stack.pop(); |
| 199 if (builder.pop() != ",") { | 235 builder.push("]"); |
| 200 builder.push("[]"); // Zero length array. Push "[" back on. | |
| 201 } else { | |
| 202 builder.push("]"); | |
| 203 } | |
| 204 | |
| 205 } | 236 } |
| 206 | 237 |
| 207 | 238 |
| 208 function BasicSerializeObject(value, stack, builder) { | 239 function BasicSerializeObject(value, stack, builder) { |
| 209 if (!%PushIfAbsent(stack, value)) { | 240 if (!%PushIfAbsent(stack, value)) { |
| 210 throw MakeTypeError('circular_structure', []); | 241 throw MakeTypeError('circular_structure', []); |
| 211 } | 242 } |
| 212 builder.push("{"); | 243 builder.push("{"); |
| 213 for (var p in value) { | 244 for (var p in value) { |
| 214 if (%HasLocalProperty(value, p)) { | 245 if (%HasLocalProperty(value, p)) { |
| 215 builder.push(%QuoteJSONString(p)); | 246 builder.push(%QuoteJSONString(p)); |
| 216 builder.push(":"); | 247 builder.push(":"); |
| 217 var before = builder.length; | 248 var before = builder.length; |
| 218 BasicJSONSerialize(p, value, stack, builder); | 249 BasicJSONSerialize(p, value[p], stack, builder); |
| 219 if (before == builder.length) { | 250 if (before == builder.length) { |
| 220 builder.pop(); | 251 builder.pop(); |
| 221 builder.pop(); | 252 builder.pop(); |
| 222 } else { | 253 } else { |
| 223 builder.push(","); | 254 builder.push(","); |
| 224 } | 255 } |
| 225 } | 256 } |
| 226 } | 257 } |
| 227 stack.pop(); | 258 stack.pop(); |
| 228 if (builder.pop() != ",") { | 259 if (builder.pop() != ",") { |
| 229 builder.push("{}"); // Object has no own properties. Push "{" back on. | 260 builder.push("{}"); // Object has no own properties. Push "{" back on. |
| 230 } else { | 261 } else { |
| 231 builder.push("}"); | 262 builder.push("}"); |
| 232 } | 263 } |
| 233 } | 264 } |
| 234 | 265 |
| 235 | 266 |
| 236 function BasicJSONSerialize(key, holder, stack, builder) { | 267 function BasicJSONSerialize(key, value, stack, builder) { |
| 237 var value = holder[key]; | |
| 238 if (IS_SPEC_OBJECT(value)) { | 268 if (IS_SPEC_OBJECT(value)) { |
| 239 var toJSON = value.toJSON; | 269 var toJSON = value.toJSON; |
| 240 if (IS_FUNCTION(toJSON)) { | 270 if (IS_FUNCTION(toJSON)) { |
| 241 value = %_CallFunction(value, ToString(key), toJSON); | 271 value = %_CallFunction(value, ToString(key), toJSON); |
| 242 } | 272 } |
| 243 } | 273 } |
| 244 if (IS_STRING(value)) { | 274 if (IS_STRING(value)) { |
| 245 builder.push(%QuoteJSONString(value)); | 275 builder.push(%QuoteJSONString(value)); |
| 246 } else if (IS_NUMBER(value)) { | 276 } else if (IS_NUMBER(value)) { |
| 247 builder.push(($isFinite(value) ? %_NumberToString(value) : "null")); | 277 builder.push(NUMBER_IS_FINITE(value) ? %_NumberToString(value) : "null"); |
| 248 } else if (IS_BOOLEAN(value)) { | 278 } else if (IS_BOOLEAN(value)) { |
| 249 builder.push(value ? "true" : "false"); | 279 builder.push(value ? "true" : "false"); |
| 250 } else if (IS_NULL(value)) { | 280 } else if (IS_NULL(value)) { |
| 251 builder.push("null"); | 281 builder.push("null"); |
| 252 } else if (IS_SPEC_OBJECT(value) && !(typeof value == "function")) { | 282 } else if (IS_SPEC_OBJECT(value) && !(typeof value == "function")) { |
| 253 // Value is a non-callable object. | 283 // Value is a non-callable object. |
| 254 // Unwrap value if necessary | 284 // Unwrap value if necessary |
| 255 if (IS_NUMBER_WRAPPER(value)) { | 285 if (IS_NUMBER_WRAPPER(value)) { |
| 256 value = ToNumber(value); | 286 value = ToNumber(value); |
| 257 builder.push(($isFinite(value) ? %_NumberToString(value) : "null")); | 287 builder.push(NUMBER_IS_FINITE(value) ? %_NumberToString(value) : "null"); |
| 258 } else if (IS_STRING_WRAPPER(value)) { | 288 } else if (IS_STRING_WRAPPER(value)) { |
| 259 builder.push(%QuoteJSONString(ToString(value))); | 289 builder.push(%QuoteJSONString(ToString(value))); |
| 260 } else if (IS_BOOLEAN_WRAPPER(value)) { | 290 } else if (IS_BOOLEAN_WRAPPER(value)) { |
| 261 builder.push(%_ValueOf(value) ? "true" : "false"); | 291 builder.push(%_ValueOf(value) ? "true" : "false"); |
| 262 } else if (IS_ARRAY(value)) { | 292 } else if (IS_ARRAY(value)) { |
| 263 BasicSerializeArray(value, stack, builder); | 293 BasicSerializeArray(value, stack, builder); |
| 264 } else { | 294 } else { |
| 265 BasicSerializeObject(value, stack, builder); | 295 BasicSerializeObject(value, stack, builder); |
| 266 } | 296 } |
| 267 } | 297 } |
| 268 } | 298 } |
| 269 | 299 |
| 270 | 300 |
| 271 function JSONStringify(value, replacer, space) { | 301 function JSONStringify(value, replacer, space) { |
| 272 if (%_ArgumentsLength() == 1) { | 302 if (%_ArgumentsLength() == 1) { |
| 273 var builder = []; | 303 var builder = []; |
| 274 BasicJSONSerialize('', {'': value}, [], builder); | 304 BasicJSONSerialize('', value, [], builder); |
| 275 if (builder.length == 0) return; | 305 if (builder.length == 0) return; |
| 276 var result = %_FastAsciiArrayJoin(builder, ""); | 306 var result = %_FastAsciiArrayJoin(builder, ""); |
| 277 if (!IS_UNDEFINED(result)) return result; | 307 if (!IS_UNDEFINED(result)) return result; |
| 278 return %StringBuilderConcat(builder, builder.length, ""); | 308 return %StringBuilderConcat(builder, builder.length, ""); |
| 279 } | 309 } |
| 280 if (IS_OBJECT(space)) { | 310 if (IS_OBJECT(space)) { |
| 281 // Unwrap 'space' if it is wrapped | 311 // Unwrap 'space' if it is wrapped |
| 282 if (IS_NUMBER_WRAPPER(space)) { | 312 if (IS_NUMBER_WRAPPER(space)) { |
| 283 space = ToNumber(space); | 313 space = ToNumber(space); |
| 284 } else if (IS_STRING_WRAPPER(space)) { | 314 } else if (IS_STRING_WRAPPER(space)) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 302 } | 332 } |
| 303 | 333 |
| 304 function SetupJSON() { | 334 function SetupJSON() { |
| 305 InstallFunctions($JSON, DONT_ENUM, $Array( | 335 InstallFunctions($JSON, DONT_ENUM, $Array( |
| 306 "parse", JSONParse, | 336 "parse", JSONParse, |
| 307 "stringify", JSONStringify | 337 "stringify", JSONStringify |
| 308 )); | 338 )); |
| 309 } | 339 } |
| 310 | 340 |
| 311 SetupJSON(); | 341 SetupJSON(); |
| OLD | NEW |