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 (function(global, utils) { | 5 (function(global, utils) { |
6 | 6 |
7 "use strict"; | 7 "use strict"; |
8 | 8 |
9 %CheckIsBootstrapping(); | 9 %CheckIsBootstrapping(); |
10 | 10 |
11 // ------------------------------------------------------------------- | 11 // ------------------------------------------------------------------- |
12 // Imports | 12 // Imports |
13 | 13 |
14 var GlobalDate = global.Date; | 14 var GlobalDate = global.Date; |
15 var GlobalJSON = global.JSON; | 15 var GlobalJSON = global.JSON; |
16 var GlobalSet = global.Set; | 16 var GlobalSet = global.Set; |
17 var InternalArray = utils.InternalArray; | 17 var InternalArray = utils.InternalArray; |
18 var MakeTypeError; | 18 var MakeTypeError; |
19 var MaxSimple; | 19 var MaxSimple; |
20 var MinSimple; | 20 var MinSimple; |
21 var ObjectHasOwnProperty; | 21 var ObjectHasOwnProperty; |
22 var Stack; | |
23 var StackHas; | |
24 var StackPop; | |
25 var StackPush; | |
26 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); | 22 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); |
27 | 23 |
28 utils.Import(function(from) { | 24 utils.Import(function(from) { |
29 MakeTypeError = from.MakeTypeError; | 25 MakeTypeError = from.MakeTypeError; |
30 MaxSimple = from.MaxSimple; | 26 MaxSimple = from.MaxSimple; |
31 MinSimple = from.MinSimple; | 27 MinSimple = from.MinSimple; |
32 ObjectHasOwnProperty = from.ObjectHasOwnProperty; | 28 ObjectHasOwnProperty = from.ObjectHasOwnProperty; |
33 Stack = from.Stack; | |
34 StackHas = from.StackHas; | |
35 StackPop = from.StackPop; | |
36 StackPush = from.StackPush; | |
37 }); | 29 }); |
38 | 30 |
39 // ------------------------------------------------------------------- | 31 // ------------------------------------------------------------------- |
40 | 32 |
41 function CreateDataProperty(o, p, v) { | 33 function CreateDataProperty(o, p, v) { |
42 var desc = {value: v, enumerable: true, writable: true, configurable: true}; | 34 var desc = {value: v, enumerable: true, writable: true, configurable: true}; |
43 return %reflect_define_property(o, p, desc); | 35 return %reflect_define_property(o, p, desc); |
44 } | 36 } |
45 | 37 |
46 | 38 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 var unfiltered = %ParseJson(text); | 71 var unfiltered = %ParseJson(text); |
80 if (IS_CALLABLE(reviver)) { | 72 if (IS_CALLABLE(reviver)) { |
81 return InternalizeJSONProperty({'': unfiltered}, '', reviver); | 73 return InternalizeJSONProperty({'': unfiltered}, '', reviver); |
82 } else { | 74 } else { |
83 return unfiltered; | 75 return unfiltered; |
84 } | 76 } |
85 } | 77 } |
86 | 78 |
87 | 79 |
88 function SerializeArray(value, replacer, stack, indent, gap) { | 80 function SerializeArray(value, replacer, stack, indent, gap) { |
89 if (StackHas(stack, value)) throw MakeTypeError(kCircularStructure); | 81 if (!%PushIfAbsent(stack, value)) throw MakeTypeError(kCircularStructure); |
90 StackPush(stack, value); | |
91 var stepback = indent; | 82 var stepback = indent; |
92 indent += gap; | 83 indent += gap; |
93 var partial = new InternalArray(); | 84 var partial = new InternalArray(); |
94 var len = TO_LENGTH(value.length); | 85 var len = TO_LENGTH(value.length); |
95 for (var i = 0; i < len; i++) { | 86 for (var i = 0; i < len; i++) { |
96 var strP = JSONSerialize(%_NumberToString(i), value, replacer, stack, | 87 var strP = JSONSerialize(%_NumberToString(i), value, replacer, stack, |
97 indent, gap); | 88 indent, gap); |
98 if (IS_UNDEFINED(strP)) { | 89 if (IS_UNDEFINED(strP)) { |
99 strP = "null"; | 90 strP = "null"; |
100 } | 91 } |
101 partial.push(strP); | 92 partial.push(strP); |
102 } | 93 } |
103 var final; | 94 var final; |
104 if (gap == "") { | 95 if (gap == "") { |
105 final = "[" + partial.join(",") + "]"; | 96 final = "[" + partial.join(",") + "]"; |
106 } else if (partial.length > 0) { | 97 } else if (partial.length > 0) { |
107 var separator = ",\n" + indent; | 98 var separator = ",\n" + indent; |
108 final = "[\n" + indent + partial.join(separator) + "\n" + | 99 final = "[\n" + indent + partial.join(separator) + "\n" + |
109 stepback + "]"; | 100 stepback + "]"; |
110 } else { | 101 } else { |
111 final = "[]"; | 102 final = "[]"; |
112 } | 103 } |
113 StackPop(stack); | 104 stack.pop(); |
114 return final; | 105 return final; |
115 } | 106 } |
116 | 107 |
117 | 108 |
118 function SerializeObject(value, replacer, stack, indent, gap) { | 109 function SerializeObject(value, replacer, stack, indent, gap) { |
119 if (StackHas(stack, value)) throw MakeTypeError(kCircularStructure); | 110 if (!%PushIfAbsent(stack, value)) throw MakeTypeError(kCircularStructure); |
120 StackPush(stack, value); | |
121 var stepback = indent; | 111 var stepback = indent; |
122 indent += gap; | 112 indent += gap; |
123 var partial = new InternalArray(); | 113 var partial = new InternalArray(); |
124 if (IS_ARRAY(replacer)) { | 114 if (IS_ARRAY(replacer)) { |
125 var length = replacer.length; | 115 var length = replacer.length; |
126 for (var i = 0; i < length; i++) { | 116 for (var i = 0; i < length; i++) { |
127 var p = replacer[i]; | 117 var p = replacer[i]; |
128 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); | 118 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); |
129 if (!IS_UNDEFINED(strP)) { | 119 if (!IS_UNDEFINED(strP)) { |
130 var member = %QuoteJSONString(p) + ":"; | 120 var member = %QuoteJSONString(p) + ":"; |
(...skipping 18 matching lines...) Expand all Loading... |
149 var final; | 139 var final; |
150 if (gap == "") { | 140 if (gap == "") { |
151 final = "{" + partial.join(",") + "}"; | 141 final = "{" + partial.join(",") + "}"; |
152 } else if (partial.length > 0) { | 142 } else if (partial.length > 0) { |
153 var separator = ",\n" + indent; | 143 var separator = ",\n" + indent; |
154 final = "{\n" + indent + partial.join(separator) + "\n" + | 144 final = "{\n" + indent + partial.join(separator) + "\n" + |
155 stepback + "}"; | 145 stepback + "}"; |
156 } else { | 146 } else { |
157 final = "{}"; | 147 final = "{}"; |
158 } | 148 } |
159 StackPop(stack); | 149 stack.pop(); |
160 return final; | 150 return final; |
161 } | 151 } |
162 | 152 |
163 | 153 |
164 function JSONSerialize(key, holder, replacer, stack, indent, gap) { | 154 function JSONSerialize(key, holder, replacer, stack, indent, gap) { |
165 var value = holder[key]; | 155 var value = holder[key]; |
166 if (IS_RECEIVER(value)) { | 156 if (IS_RECEIVER(value)) { |
167 var toJSON = value.toJSON; | 157 var toJSON = value.toJSON; |
168 if (IS_CALLABLE(toJSON)) { | 158 if (IS_CALLABLE(toJSON)) { |
169 value = %_Call(toJSON, value, key); | 159 value = %_Call(toJSON, value, key); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 gap = %_SubString(space, 0, 10); | 234 gap = %_SubString(space, 0, 10); |
245 } else { | 235 } else { |
246 gap = space; | 236 gap = space; |
247 } | 237 } |
248 } else { | 238 } else { |
249 gap = ""; | 239 gap = ""; |
250 } | 240 } |
251 if (!IS_CALLABLE(replacer) && !property_list && !gap && !IS_PROXY(value)) { | 241 if (!IS_CALLABLE(replacer) && !property_list && !gap && !IS_PROXY(value)) { |
252 return %BasicJSONStringify(value); | 242 return %BasicJSONStringify(value); |
253 } | 243 } |
254 return JSONSerialize('', {'': value}, replacer, new Stack(), "", gap); | 244 return JSONSerialize('', {'': value}, replacer, new InternalArray(), "", gap); |
255 } | 245 } |
256 | 246 |
257 // ------------------------------------------------------------------- | 247 // ------------------------------------------------------------------- |
258 | 248 |
259 %AddNamedProperty(GlobalJSON, toStringTagSymbol, "JSON", READ_ONLY | DONT_ENUM); | 249 %AddNamedProperty(GlobalJSON, toStringTagSymbol, "JSON", READ_ONLY | DONT_ENUM); |
260 | 250 |
261 // Set up non-enumerable properties of the JSON object. | 251 // Set up non-enumerable properties of the JSON object. |
262 utils.InstallFunctions(GlobalJSON, DONT_ENUM, [ | 252 utils.InstallFunctions(GlobalJSON, DONT_ENUM, [ |
263 "parse", JSONParse, | 253 "parse", JSONParse, |
264 "stringify", JSONStringify | 254 "stringify", JSONStringify |
(...skipping 17 matching lines...) Expand all Loading... |
282 "toJSON", DateToJSON | 272 "toJSON", DateToJSON |
283 ]); | 273 ]); |
284 | 274 |
285 // ------------------------------------------------------------------- | 275 // ------------------------------------------------------------------- |
286 // JSON Builtins | 276 // JSON Builtins |
287 | 277 |
288 function JsonSerializeAdapter(key, object) { | 278 function JsonSerializeAdapter(key, object) { |
289 var holder = {}; | 279 var holder = {}; |
290 holder[key] = object; | 280 holder[key] = object; |
291 // No need to pass the actual holder since there is no replacer function. | 281 // No need to pass the actual holder since there is no replacer function. |
292 return JSONSerialize(key, holder, UNDEFINED, new Stack(), "", ""); | 282 return JSONSerialize(key, holder, UNDEFINED, new InternalArray(), "", ""); |
293 } | 283 } |
294 | 284 |
295 %InstallToContext(["json_serialize_adapter", JsonSerializeAdapter]); | 285 %InstallToContext(["json_serialize_adapter", JsonSerializeAdapter]); |
296 | 286 |
297 }) | 287 }) |
OLD | NEW |