Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(997)

Side by Side Diff: src/json.js

Issue 5977006: Introduce NUMBER_IS_FINITE macro to avoid ToNumber() conversion. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/date.js ('k') | src/macros.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 // with the distribution. 11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its 12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived 13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission. 14 // from this software without specific prior written permission.
15 // 15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 var $JSON = global.JSON; 28 var $JSON = global.JSON;
29 29
30 function ParseJSONUnfiltered(text) {
31 var s = $String(text);
32 return %ParseJson(s);
33 }
34
35 function Revive(holder, name, reviver) { 30 function Revive(holder, name, reviver) {
36 var val = holder[name]; 31 var val = holder[name];
37 if (IS_OBJECT(val)) { 32 if (IS_OBJECT(val)) {
38 if (IS_ARRAY(val)) { 33 if (IS_ARRAY(val)) {
39 var length = val.length; 34 var length = val.length;
40 for (var i = 0; i < length; i++) { 35 for (var i = 0; i < length; i++) {
41 var newElement = Revive(val, $String(i), reviver); 36 var newElement = Revive(val, $String(i), reviver);
42 val[i] = newElement; 37 val[i] = newElement;
43 } 38 }
44 } else { 39 } else {
45 for (var p in val) { 40 for (var p in val) {
46 if (ObjectHasOwnProperty.call(val, p)) { 41 if (ObjectHasOwnProperty.call(val, p)) {
47 var newElement = Revive(val, p, reviver); 42 var newElement = Revive(val, p, reviver);
48 if (IS_UNDEFINED(newElement)) { 43 if (IS_UNDEFINED(newElement)) {
49 delete val[p]; 44 delete val[p];
50 } else { 45 } else {
51 val[p] = newElement; 46 val[p] = newElement;
52 } 47 }
53 } 48 }
54 } 49 }
55 } 50 }
56 } 51 }
57 return reviver.call(holder, name, val); 52 return reviver.call(holder, name, val);
58 } 53 }
59 54
60 function JSONParse(text, reviver) { 55 function JSONParse(text, reviver) {
61 var unfiltered = ParseJSONUnfiltered(text); 56 var unfiltered = %ParseJson(TO_STRING_INLINE(text));
62 if (IS_FUNCTION(reviver)) { 57 if (IS_FUNCTION(reviver)) {
63 return Revive({'': unfiltered}, '', reviver); 58 return Revive({'': unfiltered}, '', reviver);
64 } else { 59 } else {
65 return unfiltered; 60 return unfiltered;
66 } 61 }
67 } 62 }
68 63
69 function SerializeArray(value, replacer, stack, indent, gap) { 64 function SerializeArray(value, replacer, stack, indent, gap) {
70 if (!%PushIfAbsent(stack, value)) { 65 if (!%PushIfAbsent(stack, value)) {
71 throw MakeTypeError('circular_structure', []); 66 throw MakeTypeError('circular_structure', []);
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 if (IS_FUNCTION(toJSON)) { 146 if (IS_FUNCTION(toJSON)) {
152 value = %_CallFunction(value, key, toJSON); 147 value = %_CallFunction(value, key, toJSON);
153 } 148 }
154 } 149 }
155 if (IS_FUNCTION(replacer)) { 150 if (IS_FUNCTION(replacer)) {
156 value = %_CallFunction(holder, key, value, replacer); 151 value = %_CallFunction(holder, key, value, replacer);
157 } 152 }
158 if (IS_STRING(value)) { 153 if (IS_STRING(value)) {
159 return %QuoteJSONString(value); 154 return %QuoteJSONString(value);
160 } else if (IS_NUMBER(value)) { 155 } else if (IS_NUMBER(value)) {
161 return $isFinite(value) ? $String(value) : "null"; 156 return NUMBER_IS_FINITE(value) ? $String(value) : "null";
162 } else if (IS_BOOLEAN(value)) { 157 } else if (IS_BOOLEAN(value)) {
163 return value ? "true" : "false"; 158 return value ? "true" : "false";
164 } else if (IS_NULL(value)) { 159 } else if (IS_NULL(value)) {
165 return "null"; 160 return "null";
166 } else if (IS_SPEC_OBJECT(value) && !(typeof value == "function")) { 161 } else if (IS_SPEC_OBJECT(value) && !(typeof value == "function")) {
167 // Non-callable object. If it's a primitive wrapper, it must be unwrapped. 162 // Non-callable object. If it's a primitive wrapper, it must be unwrapped.
168 if (IS_ARRAY(value)) { 163 if (IS_ARRAY(value)) {
169 return SerializeArray(value, replacer, stack, indent, gap); 164 return SerializeArray(value, replacer, stack, indent, gap);
170 } else if (IS_NUMBER_WRAPPER(value)) { 165 } else if (IS_NUMBER_WRAPPER(value)) {
171 value = ToNumber(value); 166 value = ToNumber(value);
172 return $isFinite(value) ? ToString(value) : "null"; 167 return NUMBER_IS_FINITE(value) ? ToString(value) : "null";
173 } else if (IS_STRING_WRAPPER(value)) { 168 } else if (IS_STRING_WRAPPER(value)) {
174 return %QuoteJSONString(ToString(value)); 169 return %QuoteJSONString(ToString(value));
175 } else if (IS_BOOLEAN_WRAPPER(value)) { 170 } else if (IS_BOOLEAN_WRAPPER(value)) {
176 return %_ValueOf(value) ? "true" : "false"; 171 return %_ValueOf(value) ? "true" : "false";
177 } else { 172 } else {
178 return SerializeObject(value, replacer, stack, indent, gap); 173 return SerializeObject(value, replacer, stack, indent, gap);
179 } 174 }
180 } 175 }
181 // Undefined or a callable object. 176 // Undefined or a callable object.
182 return void 0; 177 return void 0;
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 var value = holder[key]; 232 var value = holder[key];
238 if (IS_SPEC_OBJECT(value)) { 233 if (IS_SPEC_OBJECT(value)) {
239 var toJSON = value.toJSON; 234 var toJSON = value.toJSON;
240 if (IS_FUNCTION(toJSON)) { 235 if (IS_FUNCTION(toJSON)) {
241 value = %_CallFunction(value, ToString(key), toJSON); 236 value = %_CallFunction(value, ToString(key), toJSON);
242 } 237 }
243 } 238 }
244 if (IS_STRING(value)) { 239 if (IS_STRING(value)) {
245 builder.push(%QuoteJSONString(value)); 240 builder.push(%QuoteJSONString(value));
246 } else if (IS_NUMBER(value)) { 241 } else if (IS_NUMBER(value)) {
247 builder.push(($isFinite(value) ? %_NumberToString(value) : "null")); 242 builder.push(NUMBER_IS_FINITE(value) ? %_NumberToString(value) : "null");
248 } else if (IS_BOOLEAN(value)) { 243 } else if (IS_BOOLEAN(value)) {
249 builder.push(value ? "true" : "false"); 244 builder.push(value ? "true" : "false");
250 } else if (IS_NULL(value)) { 245 } else if (IS_NULL(value)) {
251 builder.push("null"); 246 builder.push("null");
252 } else if (IS_SPEC_OBJECT(value) && !(typeof value == "function")) { 247 } else if (IS_SPEC_OBJECT(value) && !(typeof value == "function")) {
253 // Value is a non-callable object. 248 // Value is a non-callable object.
254 // Unwrap value if necessary 249 // Unwrap value if necessary
255 if (IS_NUMBER_WRAPPER(value)) { 250 if (IS_NUMBER_WRAPPER(value)) {
256 value = ToNumber(value); 251 value = ToNumber(value);
257 builder.push(($isFinite(value) ? %_NumberToString(value) : "null")); 252 builder.push(NUMBER_IS_FINITE(value) ? %_NumberToString(value) : "null");
258 } else if (IS_STRING_WRAPPER(value)) { 253 } else if (IS_STRING_WRAPPER(value)) {
259 builder.push(%QuoteJSONString(ToString(value))); 254 builder.push(%QuoteJSONString(ToString(value)));
260 } else if (IS_BOOLEAN_WRAPPER(value)) { 255 } else if (IS_BOOLEAN_WRAPPER(value)) {
261 builder.push(%_ValueOf(value) ? "true" : "false"); 256 builder.push(%_ValueOf(value) ? "true" : "false");
262 } else if (IS_ARRAY(value)) { 257 } else if (IS_ARRAY(value)) {
263 BasicSerializeArray(value, stack, builder); 258 BasicSerializeArray(value, stack, builder);
264 } else { 259 } else {
265 BasicSerializeObject(value, stack, builder); 260 BasicSerializeObject(value, stack, builder);
266 } 261 }
267 } 262 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 } 297 }
303 298
304 function SetupJSON() { 299 function SetupJSON() {
305 InstallFunctions($JSON, DONT_ENUM, $Array( 300 InstallFunctions($JSON, DONT_ENUM, $Array(
306 "parse", JSONParse, 301 "parse", JSONParse,
307 "stringify", JSONStringify 302 "stringify", JSONStringify
308 )); 303 ));
309 } 304 }
310 305
311 SetupJSON(); 306 SetupJSON();
OLDNEW
« no previous file with comments | « src/date.js ('k') | src/macros.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698