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