| 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 |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 } | 176 } |
| 177 // Undefined or a callable object. | 177 // Undefined or a callable object. |
| 178 return UNDEFINED; | 178 return UNDEFINED; |
| 179 } | 179 } |
| 180 | 180 |
| 181 | 181 |
| 182 function JSONStringify(value, replacer, space) { | 182 function JSONStringify(value, replacer, space) { |
| 183 if (%_ArgumentsLength() == 1) { | 183 if (%_ArgumentsLength() == 1) { |
| 184 return %BasicJSONStringify(value); | 184 return %BasicJSONStringify(value); |
| 185 } | 185 } |
| 186 if (IS_OBJECT(space)) { | |
| 187 // Unwrap 'space' if it is wrapped | |
| 188 if (IS_NUMBER_WRAPPER(space)) { | |
| 189 space = $toNumber(space); | |
| 190 } else if (IS_STRING_WRAPPER(space)) { | |
| 191 space = $toString(space); | |
| 192 } | |
| 193 } | |
| 194 var gap; | |
| 195 if (IS_NUMBER(space)) { | |
| 196 space = MathMax(0, MathMin($toInteger(space), 10)); | |
| 197 gap = %_SubString(" ", 0, space); | |
| 198 } else if (IS_STRING(space)) { | |
| 199 if (space.length > 10) { | |
| 200 gap = %_SubString(space, 0, 10); | |
| 201 } else { | |
| 202 gap = space; | |
| 203 } | |
| 204 } else { | |
| 205 gap = ""; | |
| 206 } | |
| 207 if (IS_ARRAY(replacer)) { | 186 if (IS_ARRAY(replacer)) { |
| 208 // Deduplicate replacer array items. | 187 // Deduplicate replacer array items. |
| 209 var property_list = new InternalArray(); | 188 var property_list = new InternalArray(); |
| 210 var seen_properties = { __proto__: null }; | 189 var seen_properties = { __proto__: null }; |
| 211 var length = replacer.length; | 190 var length = replacer.length; |
| 212 for (var i = 0; i < length; i++) { | 191 for (var i = 0; i < length; i++) { |
| 213 var v = replacer[i]; | 192 var v = replacer[i]; |
| 214 var item; | 193 var item; |
| 215 if (IS_STRING(v)) { | 194 if (IS_STRING(v)) { |
| 216 item = v; | 195 item = v; |
| 217 } else if (IS_NUMBER(v)) { | 196 } else if (IS_NUMBER(v)) { |
| 218 item = %_NumberToString(v); | 197 item = %_NumberToString(v); |
| 219 } else if (IS_STRING_WRAPPER(v) || IS_NUMBER_WRAPPER(v)) { | 198 } else if (IS_STRING_WRAPPER(v) || IS_NUMBER_WRAPPER(v)) { |
| 220 item = $toString(v); | 199 item = $toString(v); |
| 221 } else { | 200 } else { |
| 222 continue; | 201 continue; |
| 223 } | 202 } |
| 224 if (!seen_properties[item]) { | 203 if (!seen_properties[item]) { |
| 225 property_list.push(item); | 204 property_list.push(item); |
| 226 seen_properties[item] = true; | 205 seen_properties[item] = true; |
| 227 } | 206 } |
| 228 } | 207 } |
| 229 replacer = property_list; | 208 replacer = property_list; |
| 230 } | 209 } |
| 210 if (IS_OBJECT(space)) { |
| 211 // Unwrap 'space' if it is wrapped |
| 212 if (IS_NUMBER_WRAPPER(space)) { |
| 213 space = $toNumber(space); |
| 214 } else if (IS_STRING_WRAPPER(space)) { |
| 215 space = $toString(space); |
| 216 } |
| 217 } |
| 218 var gap; |
| 219 if (IS_NUMBER(space)) { |
| 220 space = MathMax(0, MathMin($toInteger(space), 10)); |
| 221 gap = %_SubString(" ", 0, space); |
| 222 } else if (IS_STRING(space)) { |
| 223 if (space.length > 10) { |
| 224 gap = %_SubString(space, 0, 10); |
| 225 } else { |
| 226 gap = space; |
| 227 } |
| 228 } else { |
| 229 gap = ""; |
| 230 } |
| 231 return JSONSerialize('', {'': value}, replacer, new InternalArray(), "", gap); | 231 return JSONSerialize('', {'': value}, replacer, new InternalArray(), "", gap); |
| 232 } | 232 } |
| 233 | 233 |
| 234 // ------------------------------------------------------------------- | 234 // ------------------------------------------------------------------- |
| 235 | 235 |
| 236 %AddNamedProperty(GlobalJSON, symbolToStringTag, "JSON", READ_ONLY | DONT_ENUM); | 236 %AddNamedProperty(GlobalJSON, symbolToStringTag, "JSON", READ_ONLY | DONT_ENUM); |
| 237 | 237 |
| 238 // Set up non-enumerable properties of the JSON object. | 238 // Set up non-enumerable properties of the JSON object. |
| 239 utils.InstallFunctions(GlobalJSON, DONT_ENUM, [ | 239 utils.InstallFunctions(GlobalJSON, DONT_ENUM, [ |
| 240 "parse", JSONParse, | 240 "parse", JSONParse, |
| 241 "stringify", JSONStringify | 241 "stringify", JSONStringify |
| 242 ]); | 242 ]); |
| 243 | 243 |
| 244 // ------------------------------------------------------------------- | 244 // ------------------------------------------------------------------- |
| 245 // JSON Builtins | 245 // JSON Builtins |
| 246 | 246 |
| 247 $jsonSerializeAdapter = function(key, object) { | 247 $jsonSerializeAdapter = function(key, object) { |
| 248 var holder = {}; | 248 var holder = {}; |
| 249 holder[key] = object; | 249 holder[key] = object; |
| 250 // 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. |
| 251 return JSONSerialize(key, holder, UNDEFINED, new InternalArray(), "", ""); | 251 return JSONSerialize(key, holder, UNDEFINED, new InternalArray(), "", ""); |
| 252 } | 252 } |
| 253 | 253 |
| 254 }) | 254 }) |
| OLD | NEW |