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 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 var newElement = Revive(val, p, reviver); | 42 var newElement = Revive(val, p, reviver); |
43 if (IS_UNDEFINED(newElement)) { | 43 if (IS_UNDEFINED(newElement)) { |
44 delete val[p]; | 44 delete val[p]; |
45 } else { | 45 } else { |
46 val[p] = newElement; | 46 val[p] = newElement; |
47 } | 47 } |
48 } | 48 } |
49 } | 49 } |
50 } | 50 } |
51 } | 51 } |
52 return reviver.call(holder, name, val); | 52 return %_CallFunction(holder, name, val, reviver); |
53 } | 53 } |
54 | 54 |
55 function JSONParse(text, reviver) { | 55 function JSONParse(text, reviver) { |
56 var unfiltered = %ParseJson(TO_STRING_INLINE(text)); | 56 var unfiltered = %ParseJson(TO_STRING_INLINE(text)); |
57 if (IS_FUNCTION(reviver)) { | 57 if (IS_FUNCTION(reviver)) { |
58 return Revive({'': unfiltered}, '', reviver); | 58 return Revive({'': unfiltered}, '', reviver); |
59 } else { | 59 } else { |
60 return unfiltered; | 60 return unfiltered; |
61 } | 61 } |
62 } | 62 } |
63 | 63 |
64 function SerializeArray(value, replacer, stack, indent, gap) { | 64 function SerializeArray(value, replacer, stack, indent, gap) { |
65 if (!%PushIfAbsent(stack, value)) { | 65 if (!%PushIfAbsent(stack, value)) { |
66 throw MakeTypeError('circular_structure', []); | 66 throw MakeTypeError('circular_structure', $Array()); |
67 } | 67 } |
68 var stepback = indent; | 68 var stepback = indent; |
69 indent += gap; | 69 indent += gap; |
70 var partial = []; | 70 var partial = new InternalArray(); |
71 var len = value.length; | 71 var len = value.length; |
72 for (var i = 0; i < len; i++) { | 72 for (var i = 0; i < len; i++) { |
73 var strP = JSONSerialize($String(i), value, replacer, stack, | 73 var strP = JSONSerialize($String(i), value, replacer, stack, |
74 indent, gap); | 74 indent, gap); |
75 if (IS_UNDEFINED(strP)) { | 75 if (IS_UNDEFINED(strP)) { |
76 strP = "null"; | 76 strP = "null"; |
77 } | 77 } |
78 partial.push(strP); | 78 partial.push(strP); |
79 } | 79 } |
80 var final; | 80 var final; |
81 if (gap == "") { | 81 if (gap == "") { |
82 final = "[" + partial.join(",") + "]"; | 82 final = "[" + partial.join(",") + "]"; |
83 } else if (partial.length > 0) { | 83 } else if (partial.length > 0) { |
84 var separator = ",\n" + indent; | 84 var separator = ",\n" + indent; |
85 final = "[\n" + indent + partial.join(separator) + "\n" + | 85 final = "[\n" + indent + partial.join(separator) + "\n" + |
86 stepback + "]"; | 86 stepback + "]"; |
87 } else { | 87 } else { |
88 final = "[]"; | 88 final = "[]"; |
89 } | 89 } |
90 stack.pop(); | 90 stack.pop(); |
91 return final; | 91 return final; |
92 } | 92 } |
93 | 93 |
94 function SerializeObject(value, replacer, stack, indent, gap) { | 94 function SerializeObject(value, replacer, stack, indent, gap) { |
95 if (!%PushIfAbsent(stack, value)) { | 95 if (!%PushIfAbsent(stack, value)) { |
96 throw MakeTypeError('circular_structure', []); | 96 throw MakeTypeError('circular_structure', $Array()); |
97 } | 97 } |
98 var stepback = indent; | 98 var stepback = indent; |
99 indent += gap; | 99 indent += gap; |
100 var partial = []; | 100 var partial = new InternalArray(); |
101 if (IS_ARRAY(replacer)) { | 101 if (IS_ARRAY(replacer)) { |
102 var length = replacer.length; | 102 var length = replacer.length; |
103 for (var i = 0; i < length; i++) { | 103 for (var i = 0; i < length; i++) { |
104 if (%_CallFunction(replacer, i, ObjectHasOwnProperty)) { | 104 if (%_CallFunction(replacer, i, ObjectHasOwnProperty)) { |
105 var p = replacer[i]; | 105 var p = replacer[i]; |
106 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); | 106 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); |
107 if (!IS_UNDEFINED(strP)) { | 107 if (!IS_UNDEFINED(strP)) { |
108 var member = %QuoteJSONString(p) + ":"; | 108 var member = %QuoteJSONString(p) + ":"; |
109 if (gap != "") member += " "; | 109 if (gap != "") member += " "; |
110 member += strP; | 110 member += strP; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 } | 178 } |
179 | 179 |
180 | 180 |
181 function BasicSerializeArray(value, stack, builder) { | 181 function BasicSerializeArray(value, stack, builder) { |
182 var len = value.length; | 182 var len = value.length; |
183 if (len == 0) { | 183 if (len == 0) { |
184 builder.push("[]"); | 184 builder.push("[]"); |
185 return; | 185 return; |
186 } | 186 } |
187 if (!%PushIfAbsent(stack, value)) { | 187 if (!%PushIfAbsent(stack, value)) { |
188 throw MakeTypeError('circular_structure', []); | 188 throw MakeTypeError('circular_structure', $Array()); |
189 } | 189 } |
190 builder.push("["); | 190 builder.push("["); |
191 var val = value[0]; | 191 var val = value[0]; |
192 if (IS_STRING(val)) { | 192 if (IS_STRING(val)) { |
193 // First entry is a string. Remaining entries are likely to be strings too. | 193 // First entry is a string. Remaining entries are likely to be strings too. |
194 builder.push(%QuoteJSONString(val)); | 194 builder.push(%QuoteJSONString(val)); |
195 for (var i = 1; i < len; i++) { | 195 for (var i = 1; i < len; i++) { |
196 val = value[i]; | 196 val = value[i]; |
197 if (IS_STRING(val)) { | 197 if (IS_STRING(val)) { |
198 builder.push(%QuoteJSONStringComma(val)); | 198 builder.push(%QuoteJSONStringComma(val)); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 if (before == builder.length) builder[before - 1] = ",null"; | 231 if (before == builder.length) builder[before - 1] = ",null"; |
232 } | 232 } |
233 } | 233 } |
234 stack.pop(); | 234 stack.pop(); |
235 builder.push("]"); | 235 builder.push("]"); |
236 } | 236 } |
237 | 237 |
238 | 238 |
239 function BasicSerializeObject(value, stack, builder) { | 239 function BasicSerializeObject(value, stack, builder) { |
240 if (!%PushIfAbsent(stack, value)) { | 240 if (!%PushIfAbsent(stack, value)) { |
241 throw MakeTypeError('circular_structure', []); | 241 throw MakeTypeError('circular_structure', $Array()); |
242 } | 242 } |
243 builder.push("{"); | 243 builder.push("{"); |
244 var first = true; | 244 var first = true; |
245 for (var p in value) { | 245 for (var p in value) { |
246 if (%HasLocalProperty(value, p)) { | 246 if (%HasLocalProperty(value, p)) { |
247 if (!first) { | 247 if (!first) { |
248 builder.push(%QuoteJSONStringComma(p)); | 248 builder.push(%QuoteJSONStringComma(p)); |
249 } else { | 249 } else { |
250 builder.push(%QuoteJSONString(p)); | 250 builder.push(%QuoteJSONString(p)); |
251 } | 251 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 BasicSerializeArray(value, stack, builder); | 294 BasicSerializeArray(value, stack, builder); |
295 } else { | 295 } else { |
296 BasicSerializeObject(value, stack, builder); | 296 BasicSerializeObject(value, stack, builder); |
297 } | 297 } |
298 } | 298 } |
299 } | 299 } |
300 | 300 |
301 | 301 |
302 function JSONStringify(value, replacer, space) { | 302 function JSONStringify(value, replacer, space) { |
303 if (%_ArgumentsLength() == 1) { | 303 if (%_ArgumentsLength() == 1) { |
304 var builder = []; | 304 var builder = new InternalArray(); |
305 BasicJSONSerialize('', value, [], builder); | 305 BasicJSONSerialize('', value, new InternalArray(), builder); |
306 if (builder.length == 0) return; | 306 if (builder.length == 0) return; |
307 var result = %_FastAsciiArrayJoin(builder, ""); | 307 var result = %_FastAsciiArrayJoin(builder, ""); |
308 if (!IS_UNDEFINED(result)) return result; | 308 if (!IS_UNDEFINED(result)) return result; |
309 return %StringBuilderConcat(builder, builder.length, ""); | 309 return %StringBuilderConcat(builder, builder.length, ""); |
310 } | 310 } |
311 if (IS_OBJECT(space)) { | 311 if (IS_OBJECT(space)) { |
312 // Unwrap 'space' if it is wrapped | 312 // Unwrap 'space' if it is wrapped |
313 if (IS_NUMBER_WRAPPER(space)) { | 313 if (IS_NUMBER_WRAPPER(space)) { |
314 space = ToNumber(space); | 314 space = ToNumber(space); |
315 } else if (IS_STRING_WRAPPER(space)) { | 315 } else if (IS_STRING_WRAPPER(space)) { |
316 space = ToString(space); | 316 space = ToString(space); |
317 } | 317 } |
318 } | 318 } |
319 var gap; | 319 var gap; |
320 if (IS_NUMBER(space)) { | 320 if (IS_NUMBER(space)) { |
321 space = MathMax(0, MathMin(ToInteger(space), 10)); | 321 space = MathMax(0, MathMin(ToInteger(space), 10)); |
322 gap = SubString(" ", 0, space); | 322 gap = SubString(" ", 0, space); |
323 } else if (IS_STRING(space)) { | 323 } else if (IS_STRING(space)) { |
324 if (space.length > 10) { | 324 if (space.length > 10) { |
325 gap = SubString(space, 0, 10); | 325 gap = SubString(space, 0, 10); |
326 } else { | 326 } else { |
327 gap = space; | 327 gap = space; |
328 } | 328 } |
329 } else { | 329 } else { |
330 gap = ""; | 330 gap = ""; |
331 } | 331 } |
332 return JSONSerialize('', {'': value}, replacer, [], "", gap); | 332 return JSONSerialize('', {'': value}, replacer, new InternalArray(), "", gap); |
333 } | 333 } |
334 | 334 |
335 function SetupJSON() { | 335 function SetupJSON() { |
336 InstallFunctions($JSON, DONT_ENUM, $Array( | 336 InstallFunctions($JSON, DONT_ENUM, $Array( |
337 "parse", JSONParse, | 337 "parse", JSONParse, |
338 "stringify", JSONStringify | 338 "stringify", JSONStringify |
339 )); | 339 )); |
340 } | 340 } |
341 | 341 |
342 SetupJSON(); | 342 SetupJSON(); |
OLD | NEW |