OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var $jsonSerializeAdapter; | 5 var $jsonSerializeAdapter; |
6 | 6 |
7 (function(global, utils) { | 7 (function(global, utils) { |
8 | 8 |
9 "use strict"; | 9 "use strict"; |
10 | 10 |
11 %CheckIsBootstrapping(); | 11 %CheckIsBootstrapping(); |
12 | 12 |
13 // ------------------------------------------------------------------- | 13 // ------------------------------------------------------------------- |
14 // Imports | 14 // Imports |
15 | 15 |
16 var GlobalJSON = global.JSON; | 16 var GlobalJSON = global.JSON; |
17 var InternalArray = utils.InternalArray; | 17 var InternalArray = utils.InternalArray; |
18 | 18 |
19 var MathMax; | 19 var MathMax; |
20 var MathMin; | 20 var MathMin; |
| 21 var ObjectHasOwnProperty; |
21 | 22 |
22 utils.Import(function(from) { | 23 utils.Import(function(from) { |
23 MathMax = from.MathMax; | 24 MathMax = from.MathMax; |
24 MathMin = from.MathMin; | 25 MathMin = from.MathMin; |
| 26 ObjectHasOwnProperty = from.ObjectHasOwnProperty; |
25 }); | 27 }); |
26 | 28 |
27 // ------------------------------------------------------------------- | 29 // ------------------------------------------------------------------- |
28 | 30 |
29 function Revive(holder, name, reviver) { | 31 function Revive(holder, name, reviver) { |
30 var val = holder[name]; | 32 var val = holder[name]; |
31 if (IS_OBJECT(val)) { | 33 if (IS_OBJECT(val)) { |
32 if (IS_ARRAY(val)) { | 34 if (IS_ARRAY(val)) { |
33 var length = val.length; | 35 var length = val.length; |
34 for (var i = 0; i < length; i++) { | 36 for (var i = 0; i < length; i++) { |
35 var newElement = Revive(val, %_NumberToString(i), reviver); | 37 var newElement = Revive(val, %_NumberToString(i), reviver); |
36 val[i] = newElement; | 38 val[i] = newElement; |
37 } | 39 } |
38 } else { | 40 } else { |
39 for (var p in val) { | 41 for (var p in val) { |
40 if (%_CallFunction(val, p, $objectHasOwnProperty)) { | 42 if (HAS_OWN_PROPERTY(val, p)) { |
41 var newElement = Revive(val, p, reviver); | 43 var newElement = Revive(val, p, reviver); |
42 if (IS_UNDEFINED(newElement)) { | 44 if (IS_UNDEFINED(newElement)) { |
43 delete val[p]; | 45 delete val[p]; |
44 } else { | 46 } else { |
45 val[p] = newElement; | 47 val[p] = newElement; |
46 } | 48 } |
47 } | 49 } |
48 } | 50 } |
49 } | 51 } |
50 } | 52 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 | 94 |
93 | 95 |
94 function SerializeObject(value, replacer, stack, indent, gap) { | 96 function SerializeObject(value, replacer, stack, indent, gap) { |
95 if (!%PushIfAbsent(stack, value)) throw MakeTypeError(kCircularStructure); | 97 if (!%PushIfAbsent(stack, value)) throw MakeTypeError(kCircularStructure); |
96 var stepback = indent; | 98 var stepback = indent; |
97 indent += gap; | 99 indent += gap; |
98 var partial = new InternalArray(); | 100 var partial = new InternalArray(); |
99 if (IS_ARRAY(replacer)) { | 101 if (IS_ARRAY(replacer)) { |
100 var length = replacer.length; | 102 var length = replacer.length; |
101 for (var i = 0; i < length; i++) { | 103 for (var i = 0; i < length; i++) { |
102 if (%_CallFunction(replacer, i, $objectHasOwnProperty)) { | 104 if (HAS_OWN_PROPERTY(replacer, i)) { |
103 var p = replacer[i]; | 105 var p = replacer[i]; |
104 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); | 106 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); |
105 if (!IS_UNDEFINED(strP)) { | 107 if (!IS_UNDEFINED(strP)) { |
106 var member = %QuoteJSONString(p) + ":"; | 108 var member = %QuoteJSONString(p) + ":"; |
107 if (gap != "") member += " "; | 109 if (gap != "") member += " "; |
108 member += strP; | 110 member += strP; |
109 partial.push(member); | 111 partial.push(member); |
110 } | 112 } |
111 } | 113 } |
112 } | 114 } |
113 } else { | 115 } else { |
114 for (var p in value) { | 116 for (var p in value) { |
115 if (%_CallFunction(value, p, $objectHasOwnProperty)) { | 117 if (HAS_OWN_PROPERTY(value, p)) { |
116 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); | 118 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); |
117 if (!IS_UNDEFINED(strP)) { | 119 if (!IS_UNDEFINED(strP)) { |
118 var member = %QuoteJSONString(p) + ":"; | 120 var member = %QuoteJSONString(p) + ":"; |
119 if (gap != "") member += " "; | 121 if (gap != "") member += " "; |
120 member += strP; | 122 member += strP; |
121 partial.push(member); | 123 partial.push(member); |
122 } | 124 } |
123 } | 125 } |
124 } | 126 } |
125 } | 127 } |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 replacer = property_list; | 227 replacer = property_list; |
226 } | 228 } |
227 return JSONSerialize('', {'': value}, replacer, new InternalArray(), "", gap); | 229 return JSONSerialize('', {'': value}, replacer, new InternalArray(), "", gap); |
228 } | 230 } |
229 | 231 |
230 // ------------------------------------------------------------------- | 232 // ------------------------------------------------------------------- |
231 | 233 |
232 %AddNamedProperty(GlobalJSON, symbolToStringTag, "JSON", READ_ONLY | DONT_ENUM); | 234 %AddNamedProperty(GlobalJSON, symbolToStringTag, "JSON", READ_ONLY | DONT_ENUM); |
233 | 235 |
234 // Set up non-enumerable properties of the JSON object. | 236 // Set up non-enumerable properties of the JSON object. |
235 $installFunctions(GlobalJSON, DONT_ENUM, [ | 237 utils.InstallFunctions(GlobalJSON, DONT_ENUM, [ |
236 "parse", JSONParse, | 238 "parse", JSONParse, |
237 "stringify", JSONStringify | 239 "stringify", JSONStringify |
238 ]); | 240 ]); |
239 | 241 |
240 // ------------------------------------------------------------------- | 242 // ------------------------------------------------------------------- |
241 // JSON Builtins | 243 // JSON Builtins |
242 | 244 |
243 $jsonSerializeAdapter = function(key, object) { | 245 $jsonSerializeAdapter = function(key, object) { |
244 var holder = {}; | 246 var holder = {}; |
245 holder[key] = object; | 247 holder[key] = object; |
246 // No need to pass the actual holder since there is no replacer function. | 248 // No need to pass the actual holder since there is no replacer function. |
247 return JSONSerialize(key, holder, UNDEFINED, new InternalArray(), "", ""); | 249 return JSONSerialize(key, holder, UNDEFINED, new InternalArray(), "", ""); |
248 } | 250 } |
249 | 251 |
250 }) | 252 }) |
OLD | NEW |