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 GlobalJSON = global.JSON; | 14 var GlobalJSON = global.JSON; |
15 var InternalArray = utils.InternalArray; | 15 var InternalArray = utils.InternalArray; |
16 var MathMax; | 16 var MathMax; |
17 var MathMin; | 17 var MathMin; |
18 var ObjectHasOwnProperty; | 18 var ObjectHasOwnProperty; |
19 var ToNumber; | 19 var ToNumber; |
20 var ToString; | |
21 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); | 20 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); |
22 | 21 |
23 utils.Import(function(from) { | 22 utils.Import(function(from) { |
24 MathMax = from.MathMax; | 23 MathMax = from.MathMax; |
25 MathMin = from.MathMin; | 24 MathMin = from.MathMin; |
26 ObjectHasOwnProperty = from.ObjectHasOwnProperty; | 25 ObjectHasOwnProperty = from.ObjectHasOwnProperty; |
27 ToNumber = from.ToNumber; | 26 ToNumber = from.ToNumber; |
28 ToString = from.ToString; | |
29 }); | 27 }); |
30 | 28 |
31 // ------------------------------------------------------------------- | 29 // ------------------------------------------------------------------- |
32 | 30 |
33 function Revive(holder, name, reviver) { | 31 function Revive(holder, name, reviver) { |
34 var val = holder[name]; | 32 var val = holder[name]; |
35 if (IS_OBJECT(val)) { | 33 if (IS_OBJECT(val)) { |
36 if (IS_ARRAY(val)) { | 34 if (IS_ARRAY(val)) { |
37 var length = val.length; | 35 var length = val.length; |
38 for (var i = 0; i < length; i++) { | 36 for (var i = 0; i < length; i++) { |
(...skipping 11 matching lines...) Expand all Loading... |
50 } | 48 } |
51 } | 49 } |
52 } | 50 } |
53 } | 51 } |
54 } | 52 } |
55 return %_Call(reviver, holder, name, val); | 53 return %_Call(reviver, holder, name, val); |
56 } | 54 } |
57 | 55 |
58 | 56 |
59 function JSONParse(text, reviver) { | 57 function JSONParse(text, reviver) { |
60 var unfiltered = %ParseJson(TO_STRING_INLINE(text)); | 58 var unfiltered = %ParseJson(text); |
61 if (IS_CALLABLE(reviver)) { | 59 if (IS_CALLABLE(reviver)) { |
62 return Revive({'': unfiltered}, '', reviver); | 60 return Revive({'': unfiltered}, '', reviver); |
63 } else { | 61 } else { |
64 return unfiltered; | 62 return unfiltered; |
65 } | 63 } |
66 } | 64 } |
67 | 65 |
68 | 66 |
69 function SerializeArray(value, replacer, stack, indent, gap) { | 67 function SerializeArray(value, replacer, stack, indent, gap) { |
70 if (!%PushIfAbsent(stack, value)) throw MakeTypeError(kCircularStructure); | 68 if (!%PushIfAbsent(stack, value)) throw MakeTypeError(kCircularStructure); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 value = %_Call(replacer, holder, key, value); | 152 value = %_Call(replacer, holder, key, value); |
155 } | 153 } |
156 if (IS_STRING(value)) { | 154 if (IS_STRING(value)) { |
157 return %QuoteJSONString(value); | 155 return %QuoteJSONString(value); |
158 } else if (IS_NUMBER(value)) { | 156 } else if (IS_NUMBER(value)) { |
159 return JSON_NUMBER_TO_STRING(value); | 157 return JSON_NUMBER_TO_STRING(value); |
160 } else if (IS_BOOLEAN(value)) { | 158 } else if (IS_BOOLEAN(value)) { |
161 return value ? "true" : "false"; | 159 return value ? "true" : "false"; |
162 } else if (IS_NULL(value)) { | 160 } else if (IS_NULL(value)) { |
163 return "null"; | 161 return "null"; |
164 } else if (IS_SPEC_OBJECT(value) && !(typeof value == "function")) { | 162 } else if (IS_SPEC_OBJECT(value) && !IS_CALLABLE(value)) { |
165 // Non-callable object. If it's a primitive wrapper, it must be unwrapped. | 163 // Non-callable object. If it's a primitive wrapper, it must be unwrapped. |
166 if (IS_ARRAY(value)) { | 164 if (IS_ARRAY(value)) { |
167 return SerializeArray(value, replacer, stack, indent, gap); | 165 return SerializeArray(value, replacer, stack, indent, gap); |
168 } else if (IS_NUMBER_WRAPPER(value)) { | 166 } else if (IS_NUMBER_WRAPPER(value)) { |
169 value = ToNumber(value); | 167 value = ToNumber(value); |
170 return JSON_NUMBER_TO_STRING(value); | 168 return JSON_NUMBER_TO_STRING(value); |
171 } else if (IS_STRING_WRAPPER(value)) { | 169 } else if (IS_STRING_WRAPPER(value)) { |
172 return %QuoteJSONString(ToString(value)); | 170 return %QuoteJSONString(TO_STRING(value)); |
173 } else if (IS_BOOLEAN_WRAPPER(value)) { | 171 } else if (IS_BOOLEAN_WRAPPER(value)) { |
174 return %_ValueOf(value) ? "true" : "false"; | 172 return %_ValueOf(value) ? "true" : "false"; |
175 } else { | 173 } else { |
176 return SerializeObject(value, replacer, stack, indent, gap); | 174 return SerializeObject(value, replacer, stack, indent, gap); |
177 } | 175 } |
178 } | 176 } |
179 // Undefined or a callable object. | 177 // Undefined or a callable object. |
180 return UNDEFINED; | 178 return UNDEFINED; |
181 } | 179 } |
182 | 180 |
183 | 181 |
184 function JSONStringify(value, replacer, space) { | 182 function JSONStringify(value, replacer, space) { |
185 if (%_ArgumentsLength() == 1) { | 183 if (%_ArgumentsLength() == 1) { |
186 return %BasicJSONStringify(value); | 184 return %BasicJSONStringify(value); |
187 } | 185 } |
188 if (IS_ARRAY(replacer)) { | 186 if (IS_ARRAY(replacer)) { |
189 // Deduplicate replacer array items. | 187 // Deduplicate replacer array items. |
190 var property_list = new InternalArray(); | 188 var property_list = new InternalArray(); |
191 var seen_properties = { __proto__: null }; | 189 var seen_properties = { __proto__: null }; |
192 var length = replacer.length; | 190 var length = replacer.length; |
193 for (var i = 0; i < length; i++) { | 191 for (var i = 0; i < length; i++) { |
194 var v = replacer[i]; | 192 var v = replacer[i]; |
195 var item; | 193 var item; |
196 if (IS_STRING(v)) { | 194 if (IS_STRING(v)) { |
197 item = v; | 195 item = v; |
198 } else if (IS_NUMBER(v)) { | 196 } else if (IS_NUMBER(v)) { |
199 item = %_NumberToString(v); | 197 item = %_NumberToString(v); |
200 } else if (IS_STRING_WRAPPER(v) || IS_NUMBER_WRAPPER(v)) { | 198 } else if (IS_STRING_WRAPPER(v) || IS_NUMBER_WRAPPER(v)) { |
201 item = ToString(v); | 199 item = TO_STRING(v); |
202 } else { | 200 } else { |
203 continue; | 201 continue; |
204 } | 202 } |
205 if (!seen_properties[item]) { | 203 if (!seen_properties[item]) { |
206 property_list.push(item); | 204 property_list.push(item); |
207 seen_properties[item] = true; | 205 seen_properties[item] = true; |
208 } | 206 } |
209 } | 207 } |
210 replacer = property_list; | 208 replacer = property_list; |
211 } | 209 } |
212 if (IS_OBJECT(space)) { | 210 if (IS_OBJECT(space)) { |
213 // Unwrap 'space' if it is wrapped | 211 // Unwrap 'space' if it is wrapped |
214 if (IS_NUMBER_WRAPPER(space)) { | 212 if (IS_NUMBER_WRAPPER(space)) { |
215 space = ToNumber(space); | 213 space = ToNumber(space); |
216 } else if (IS_STRING_WRAPPER(space)) { | 214 } else if (IS_STRING_WRAPPER(space)) { |
217 space = ToString(space); | 215 space = TO_STRING(space); |
218 } | 216 } |
219 } | 217 } |
220 var gap; | 218 var gap; |
221 if (IS_NUMBER(space)) { | 219 if (IS_NUMBER(space)) { |
222 space = MathMax(0, MathMin($toInteger(space), 10)); | 220 space = MathMax(0, MathMin($toInteger(space), 10)); |
223 gap = %_SubString(" ", 0, space); | 221 gap = %_SubString(" ", 0, space); |
224 } else if (IS_STRING(space)) { | 222 } else if (IS_STRING(space)) { |
225 if (space.length > 10) { | 223 if (space.length > 10) { |
226 gap = %_SubString(space, 0, 10); | 224 gap = %_SubString(space, 0, 10); |
227 } else { | 225 } else { |
(...skipping 21 matching lines...) Expand all Loading... |
249 function JsonSerializeAdapter(key, object) { | 247 function JsonSerializeAdapter(key, object) { |
250 var holder = {}; | 248 var holder = {}; |
251 holder[key] = object; | 249 holder[key] = object; |
252 // No need to pass the actual holder since there is no replacer function. | 250 // No need to pass the actual holder since there is no replacer function. |
253 return JSONSerialize(key, holder, UNDEFINED, new InternalArray(), "", ""); | 251 return JSONSerialize(key, holder, UNDEFINED, new InternalArray(), "", ""); |
254 } | 252 } |
255 | 253 |
256 %InstallToContext(["json_serialize_adapter", JsonSerializeAdapter]); | 254 %InstallToContext(["json_serialize_adapter", JsonSerializeAdapter]); |
257 | 255 |
258 }) | 256 }) |
OLD | NEW |