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 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 stepback + "}"; | 139 stepback + "}"; |
140 } else { | 140 } else { |
141 final = "{}"; | 141 final = "{}"; |
142 } | 142 } |
143 stack.pop(); | 143 stack.pop(); |
144 return final; | 144 return final; |
145 } | 145 } |
146 | 146 |
147 function JSONSerialize(key, holder, replacer, stack, indent, gap) { | 147 function JSONSerialize(key, holder, replacer, stack, indent, gap) { |
148 var value = holder[key]; | 148 var value = holder[key]; |
149 if (IS_OBJECT(value) && value) { | 149 if (IS_SPEC_OBJECT(value)) { |
150 var toJSON = value.toJSON; | 150 var toJSON = value.toJSON; |
151 if (IS_FUNCTION(toJSON)) { | 151 if (IS_FUNCTION(toJSON)) { |
152 value = toJSON.call(value, key); | 152 value = %_CallFunction(value, key, toJSON); |
153 } | 153 } |
154 } | 154 } |
155 if (IS_FUNCTION(replacer)) { | 155 if (IS_FUNCTION(replacer)) { |
156 value = replacer.call(holder, key, value); | 156 value = %_CallFunction(holder, key, value, replacer); |
157 } | 157 } |
158 // Unwrap value if necessary | 158 if (IS_STRING(value)) { |
159 if (IS_OBJECT(value)) { | 159 return %QuoteJSONString(value); |
160 if (IS_NUMBER_WRAPPER(value)) { | 160 } else if (IS_NUMBER(value)) { |
161 value = $Number(value); | 161 return $isFinite(value) ? $String(value) : "null"; |
| 162 } else if (IS_BOOLEAN(value)) { |
| 163 return value ? "true" : "false"; |
| 164 } else if (IS_NULL(value)) { |
| 165 return "null"; |
| 166 } else if (IS_SPEC_OBJECT(value) && !(typeof value == "function")) { |
| 167 // Non-callable object. If it's a primitive wrapper, it must be unwrapped. |
| 168 if (IS_ARRAY(value)) { |
| 169 return SerializeArray(value, replacer, stack, indent, gap); |
| 170 } else if (IS_NUMBER_WRAPPER(value)) { |
| 171 value = ToNumber(value); |
| 172 return $isFinite(value) ? ToString(value) : "null"; |
162 } else if (IS_STRING_WRAPPER(value)) { | 173 } else if (IS_STRING_WRAPPER(value)) { |
163 value = $String(value); | 174 return %QuoteJSONString(ToString(value)); |
164 } else if (IS_BOOLEAN_WRAPPER(value)) { | 175 } else if (IS_BOOLEAN_WRAPPER(value)) { |
165 value = %_ValueOf(value); | 176 return %_ValueOf(value) ? "true" : "false"; |
| 177 } else { |
| 178 return SerializeObject(value, replacer, stack, indent, gap); |
166 } | 179 } |
167 } | 180 } |
168 switch (typeof value) { | 181 // Undefined or a callable object. |
169 case "string": | 182 return void 0; |
170 return %QuoteJSONString(value); | |
171 case "object": | |
172 if (!value) { | |
173 return "null"; | |
174 } else if (IS_ARRAY(value)) { | |
175 return SerializeArray(value, replacer, stack, indent, gap); | |
176 } else { | |
177 return SerializeObject(value, replacer, stack, indent, gap); | |
178 } | |
179 case "number": | |
180 return $isFinite(value) ? $String(value) : "null"; | |
181 case "boolean": | |
182 return value ? "true" : "false"; | |
183 } | |
184 } | 183 } |
185 | 184 |
186 | 185 |
187 function BasicSerializeArray(value, stack, builder) { | 186 function BasicSerializeArray(value, stack, builder) { |
188 if (!%PushIfAbsent(stack, value)) { | 187 if (!%PushIfAbsent(stack, value)) { |
189 throw MakeTypeError('circular_structure', []); | 188 throw MakeTypeError('circular_structure', []); |
190 } | 189 } |
191 builder.push("["); | 190 builder.push("["); |
192 var len = value.length; | 191 var len = value.length; |
193 for (var i = 0; i < len; i++) { | 192 for (var i = 0; i < len; i++) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 if (builder.pop() != ",") { | 228 if (builder.pop() != ",") { |
230 builder.push("{}"); // Object has no own properties. Push "{" back on. | 229 builder.push("{}"); // Object has no own properties. Push "{" back on. |
231 } else { | 230 } else { |
232 builder.push("}"); | 231 builder.push("}"); |
233 } | 232 } |
234 } | 233 } |
235 | 234 |
236 | 235 |
237 function BasicJSONSerialize(key, holder, stack, builder) { | 236 function BasicJSONSerialize(key, holder, stack, builder) { |
238 var value = holder[key]; | 237 var value = holder[key]; |
239 if (IS_OBJECT(value) && value) { | 238 if (IS_SPEC_OBJECT(value)) { |
240 var toJSON = value.toJSON; | 239 var toJSON = value.toJSON; |
241 if (IS_FUNCTION(toJSON)) value = toJSON.call(value, $String(key)); | 240 if (IS_FUNCTION(toJSON)) { |
| 241 value = %_CallFunction(value, ToString(key), toJSON); |
| 242 } |
242 } | 243 } |
243 if (IS_STRING(value)) { | 244 if (IS_STRING(value)) { |
244 builder.push(%QuoteJSONString(value)); | 245 builder.push(%QuoteJSONString(value)); |
245 } else if (IS_NUMBER(value)) { | 246 } else if (IS_NUMBER(value)) { |
246 builder.push(($isFinite(value) ? %_NumberToString(value) : "null")); | 247 builder.push(($isFinite(value) ? %_NumberToString(value) : "null")); |
247 } else if (IS_BOOLEAN(value)) { | 248 } else if (IS_BOOLEAN(value)) { |
248 builder.push((value ? "true" : "false")); | 249 builder.push(value ? "true" : "false"); |
249 } else if (IS_OBJECT(value)) { | 250 } else if (IS_NULL(value)) { |
| 251 builder.push("null"); |
| 252 } else if (IS_SPEC_OBJECT(value) && !(typeof value == "function")) { |
| 253 // Value is a non-callable object. |
250 // Unwrap value if necessary | 254 // Unwrap value if necessary |
251 if (IS_NUMBER_WRAPPER(value)) { | 255 if (IS_NUMBER_WRAPPER(value)) { |
252 value = %_ValueOf(value); | 256 value = ToNumber(value); |
253 builder.push(($isFinite(value) ? %_NumberToString(value) : "null")); | 257 builder.push(($isFinite(value) ? %_NumberToString(value) : "null")); |
254 } else if (IS_STRING_WRAPPER(value)) { | 258 } else if (IS_STRING_WRAPPER(value)) { |
255 builder.push(%QuoteJSONString(%_ValueOf(value))); | 259 builder.push(%QuoteJSONString(ToString(value))); |
256 } else if (IS_BOOLEAN_WRAPPER(value)) { | 260 } else if (IS_BOOLEAN_WRAPPER(value)) { |
257 builder.push((%_ValueOf(value) ? "true" : "false")); | 261 builder.push(%_ValueOf(value) ? "true" : "false"); |
| 262 } else if (IS_ARRAY(value)) { |
| 263 BasicSerializeArray(value, stack, builder); |
258 } else { | 264 } else { |
259 // Regular non-wrapped object | 265 BasicSerializeObject(value, stack, builder); |
260 if (!value) { | |
261 builder.push("null"); | |
262 } else if (IS_ARRAY(value)) { | |
263 BasicSerializeArray(value, stack, builder); | |
264 } else { | |
265 BasicSerializeObject(value, stack, builder); | |
266 } | |
267 } | 266 } |
268 } | 267 } |
269 } | 268 } |
270 | 269 |
| 270 |
271 function JSONStringify(value, replacer, space) { | 271 function JSONStringify(value, replacer, space) { |
272 if (IS_UNDEFINED(replacer) && IS_UNDEFINED(space)) { | 272 if (%_ArgumentsLength() == 1) { |
273 var builder = []; | 273 var builder = []; |
274 BasicJSONSerialize('', {'': value}, [], builder); | 274 BasicJSONSerialize('', {'': value}, [], builder); |
275 if (builder.length == 0) return; | 275 if (builder.length == 0) return; |
276 var result = %_FastAsciiArrayJoin(builder, ""); | 276 var result = %_FastAsciiArrayJoin(builder, ""); |
277 if (!IS_UNDEFINED(result)) return result; | 277 if (!IS_UNDEFINED(result)) return result; |
278 return %StringBuilderConcat(builder, builder.length, ""); | 278 return %StringBuilderConcat(builder, builder.length, ""); |
279 } | 279 } |
280 if (IS_OBJECT(space)) { | 280 if (IS_OBJECT(space)) { |
281 // Unwrap 'space' if it is wrapped | 281 // Unwrap 'space' if it is wrapped |
282 if (IS_NUMBER_WRAPPER(space)) { | 282 if (IS_NUMBER_WRAPPER(space)) { |
283 space = $Number(space); | 283 space = ToNumber(space); |
284 } else if (IS_STRING_WRAPPER(space)) { | 284 } else if (IS_STRING_WRAPPER(space)) { |
285 space = $String(space); | 285 space = ToString(space); |
286 } | 286 } |
287 } | 287 } |
288 var gap; | 288 var gap; |
289 if (IS_NUMBER(space)) { | 289 if (IS_NUMBER(space)) { |
290 space = $Math.min(ToInteger(space), 10); | 290 space = MathMax(0, MathMin(ToInteger(space), 10)); |
291 gap = ""; | 291 gap = SubString(" ", 0, space); |
292 for (var i = 0; i < space; i++) { | |
293 gap += " "; | |
294 } | |
295 } else if (IS_STRING(space)) { | 292 } else if (IS_STRING(space)) { |
296 if (space.length > 10) { | 293 if (space.length > 10) { |
297 gap = space.substring(0, 10); | 294 gap = SubString(space, 0, 10); |
298 } else { | 295 } else { |
299 gap = space; | 296 gap = space; |
300 } | 297 } |
301 } else { | 298 } else { |
302 gap = ""; | 299 gap = ""; |
303 } | 300 } |
304 return JSONSerialize('', {'': value}, replacer, [], "", gap); | 301 return JSONSerialize('', {'': value}, replacer, [], "", gap); |
305 } | 302 } |
306 | 303 |
307 function SetupJSON() { | 304 function SetupJSON() { |
308 InstallFunctions($JSON, DONT_ENUM, $Array( | 305 InstallFunctions($JSON, DONT_ENUM, $Array( |
309 "parse", JSONParse, | 306 "parse", JSONParse, |
310 "stringify", JSONStringify | 307 "stringify", JSONStringify |
311 )); | 308 )); |
312 } | 309 } |
313 | 310 |
314 SetupJSON(); | 311 SetupJSON(); |
OLD | NEW |