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

Side by Side Diff: src/json.js

Issue 6580038: [Isolates] Merge from bleeding_edge, revisions 5934-6100. (Closed) Base URL: http://v8.googlecode.com/svn/branches/experimental/isolates/
Patch Set: '' Created 9 years, 10 months 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/ic.cc ('k') | src/lithium-allocator.h » ('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
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 59
60 function JSONParse(text, reviver) { 60 function JSONParse(text, reviver) {
61 var unfiltered = ParseJSONUnfiltered(text); 61 var unfiltered = ParseJSONUnfiltered(text);
62 if (IS_FUNCTION(reviver)) { 62 if (IS_FUNCTION(reviver)) {
63 return Revive({'': unfiltered}, '', reviver); 63 return Revive({'': unfiltered}, '', reviver);
64 } else { 64 } else {
65 return unfiltered; 65 return unfiltered;
66 } 66 }
67 } 67 }
68 68
69 function StackContains(stack, val) {
70 var length = stack.length;
71 for (var i = 0; i < length; i++) {
72 if (stack[i] === val) {
73 return true;
74 }
75 }
76 return false;
77 }
78
79 function SerializeArray(value, replacer, stack, indent, gap) { 69 function SerializeArray(value, replacer, stack, indent, gap) {
80 if (StackContains(stack, value)) { 70 if (!%PushIfAbsent(stack, value)) {
81 throw MakeTypeError('circular_structure', []); 71 throw MakeTypeError('circular_structure', []);
82 } 72 }
83 stack.push(value);
84 var stepback = indent; 73 var stepback = indent;
85 indent += gap; 74 indent += gap;
86 var partial = []; 75 var partial = [];
87 var len = value.length; 76 var len = value.length;
88 for (var i = 0; i < len; i++) { 77 for (var i = 0; i < len; i++) {
89 var strP = JSONSerialize($String(i), value, replacer, stack, 78 var strP = JSONSerialize($String(i), value, replacer, stack,
90 indent, gap); 79 indent, gap);
91 if (IS_UNDEFINED(strP)) { 80 if (IS_UNDEFINED(strP)) {
92 strP = "null"; 81 strP = "null";
93 } 82 }
94 partial.push(strP); 83 partial.push(strP);
95 } 84 }
96 var final; 85 var final;
97 if (gap == "") { 86 if (gap == "") {
98 final = "[" + partial.join(",") + "]"; 87 final = "[" + partial.join(",") + "]";
99 } else if (partial.length > 0) { 88 } else if (partial.length > 0) {
100 var separator = ",\n" + indent; 89 var separator = ",\n" + indent;
101 final = "[\n" + indent + partial.join(separator) + "\n" + 90 final = "[\n" + indent + partial.join(separator) + "\n" +
102 stepback + "]"; 91 stepback + "]";
103 } else { 92 } else {
104 final = "[]"; 93 final = "[]";
105 } 94 }
106 stack.pop(); 95 stack.pop();
107 return final; 96 return final;
108 } 97 }
109 98
110 function SerializeObject(value, replacer, stack, indent, gap) { 99 function SerializeObject(value, replacer, stack, indent, gap) {
111 if (StackContains(stack, value)) { 100 if (!%PushIfAbsent(stack, value)) {
112 throw MakeTypeError('circular_structure', []); 101 throw MakeTypeError('circular_structure', []);
113 } 102 }
114 stack.push(value);
115 var stepback = indent; 103 var stepback = indent;
116 indent += gap; 104 indent += gap;
117 var partial = []; 105 var partial = [];
118 if (IS_ARRAY(replacer)) { 106 if (IS_ARRAY(replacer)) {
119 var length = replacer.length; 107 var length = replacer.length;
120 for (var i = 0; i < length; i++) { 108 for (var i = 0; i < length; i++) {
121 if (ObjectHasOwnProperty.call(replacer, i)) { 109 if (ObjectHasOwnProperty.call(replacer, i)) {
122 var p = replacer[i]; 110 var p = replacer[i];
123 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); 111 var strP = JSONSerialize(p, value, replacer, stack, indent, gap);
124 if (!IS_UNDEFINED(strP)) { 112 if (!IS_UNDEFINED(strP)) {
(...skipping 26 matching lines...) Expand all
151 stepback + "}"; 139 stepback + "}";
152 } else { 140 } else {
153 final = "{}"; 141 final = "{}";
154 } 142 }
155 stack.pop(); 143 stack.pop();
156 return final; 144 return final;
157 } 145 }
158 146
159 function JSONSerialize(key, holder, replacer, stack, indent, gap) { 147 function JSONSerialize(key, holder, replacer, stack, indent, gap) {
160 var value = holder[key]; 148 var value = holder[key];
161 if (IS_OBJECT(value) && value) { 149 if (IS_SPEC_OBJECT(value)) {
162 var toJSON = value.toJSON; 150 var toJSON = value.toJSON;
163 if (IS_FUNCTION(toJSON)) { 151 if (IS_FUNCTION(toJSON)) {
164 value = toJSON.call(value, key); 152 value = %_CallFunction(value, key, toJSON);
165 } 153 }
166 } 154 }
167 if (IS_FUNCTION(replacer)) { 155 if (IS_FUNCTION(replacer)) {
168 value = replacer.call(holder, key, value); 156 value = %_CallFunction(holder, key, value, replacer);
169 } 157 }
170 // Unwrap value if necessary 158 if (IS_STRING(value)) {
171 if (IS_OBJECT(value)) { 159 return %QuoteJSONString(value);
172 if (IS_NUMBER_WRAPPER(value)) { 160 } else if (IS_NUMBER(value)) {
173 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";
174 } else if (IS_STRING_WRAPPER(value)) { 173 } else if (IS_STRING_WRAPPER(value)) {
175 value = $String(value); 174 return %QuoteJSONString(ToString(value));
176 } else if (IS_BOOLEAN_WRAPPER(value)) { 175 } else if (IS_BOOLEAN_WRAPPER(value)) {
177 value = %_ValueOf(value); 176 return %_ValueOf(value) ? "true" : "false";
177 } else {
178 return SerializeObject(value, replacer, stack, indent, gap);
178 } 179 }
179 } 180 }
180 switch (typeof value) { 181 // Undefined or a callable object.
181 case "string": 182 return void 0;
182 return %QuoteJSONString(value);
183 case "object":
184 if (!value) {
185 return "null";
186 } else if (IS_ARRAY(value)) {
187 return SerializeArray(value, replacer, stack, indent, gap);
188 } else {
189 return SerializeObject(value, replacer, stack, indent, gap);
190 }
191 case "number":
192 return $isFinite(value) ? $String(value) : "null";
193 case "boolean":
194 return value ? "true" : "false";
195 }
196 } 183 }
197 184
198 185
199 function BasicSerializeArray(value, stack, builder) { 186 function BasicSerializeArray(value, stack, builder) {
200 if (StackContains(stack, value)) { 187 if (!%PushIfAbsent(stack, value)) {
201 throw MakeTypeError('circular_structure', []); 188 throw MakeTypeError('circular_structure', []);
202 } 189 }
203 stack.push(value);
204 builder.push("["); 190 builder.push("[");
205 var len = value.length; 191 var len = value.length;
206 for (var i = 0; i < len; i++) { 192 for (var i = 0; i < len; i++) {
207 var before = builder.length; 193 var before = builder.length;
208 BasicJSONSerialize(i, value, stack, builder); 194 BasicJSONSerialize(i, value, stack, builder);
209 if (before == builder.length) builder.push("null"); 195 if (before == builder.length) builder.push("null");
210 builder.push(","); 196 builder.push(",");
211 } 197 }
212 stack.pop(); 198 stack.pop();
213 if (builder.pop() != ",") { 199 if (builder.pop() != ",") {
214 builder.push("[]"); // Zero length array. Push "[" back on. 200 builder.push("[]"); // Zero length array. Push "[" back on.
215 } else { 201 } else {
216 builder.push("]"); 202 builder.push("]");
217 } 203 }
218 204
219 } 205 }
220 206
221 207
222 function BasicSerializeObject(value, stack, builder) { 208 function BasicSerializeObject(value, stack, builder) {
223 if (StackContains(stack, value)) { 209 if (!%PushIfAbsent(stack, value)) {
224 throw MakeTypeError('circular_structure', []); 210 throw MakeTypeError('circular_structure', []);
225 } 211 }
226 stack.push(value);
227 builder.push("{"); 212 builder.push("{");
228 for (var p in value) { 213 for (var p in value) {
229 if (%HasLocalProperty(value, p)) { 214 if (%HasLocalProperty(value, p)) {
230 builder.push(%QuoteJSONString(p)); 215 builder.push(%QuoteJSONString(p));
231 builder.push(":"); 216 builder.push(":");
232 var before = builder.length; 217 var before = builder.length;
233 BasicJSONSerialize(p, value, stack, builder); 218 BasicJSONSerialize(p, value, stack, builder);
234 if (before == builder.length) { 219 if (before == builder.length) {
235 builder.pop(); 220 builder.pop();
236 builder.pop(); 221 builder.pop();
237 } else { 222 } else {
238 builder.push(","); 223 builder.push(",");
239 } 224 }
240 } 225 }
241 } 226 }
242 stack.pop(); 227 stack.pop();
243 if (builder.pop() != ",") { 228 if (builder.pop() != ",") {
244 builder.push("{}"); // Object has no own properties. Push "{" back on. 229 builder.push("{}"); // Object has no own properties. Push "{" back on.
245 } else { 230 } else {
246 builder.push("}"); 231 builder.push("}");
247 } 232 }
248 } 233 }
249 234
250 235
251 function BasicJSONSerialize(key, holder, stack, builder) { 236 function BasicJSONSerialize(key, holder, stack, builder) {
252 var value = holder[key]; 237 var value = holder[key];
253 if (IS_OBJECT(value) && value) { 238 if (IS_SPEC_OBJECT(value)) {
254 var toJSON = value.toJSON; 239 var toJSON = value.toJSON;
255 if (IS_FUNCTION(toJSON)) value = toJSON.call(value, $String(key)); 240 if (IS_FUNCTION(toJSON)) {
241 value = %_CallFunction(value, ToString(key), toJSON);
242 }
256 } 243 }
257 if (IS_STRING(value)) { 244 if (IS_STRING(value)) {
258 builder.push(%QuoteJSONString(value)); 245 builder.push(%QuoteJSONString(value));
259 } else if (IS_NUMBER(value)) { 246 } else if (IS_NUMBER(value)) {
260 builder.push(($isFinite(value) ? %_NumberToString(value) : "null")); 247 builder.push(($isFinite(value) ? %_NumberToString(value) : "null"));
261 } else if (IS_BOOLEAN(value)) { 248 } else if (IS_BOOLEAN(value)) {
262 builder.push((value ? "true" : "false")); 249 builder.push(value ? "true" : "false");
263 } 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.
264 // Unwrap value if necessary 254 // Unwrap value if necessary
265 if (IS_NUMBER_WRAPPER(value)) { 255 if (IS_NUMBER_WRAPPER(value)) {
266 value = %_ValueOf(value); 256 value = ToNumber(value);
267 builder.push(($isFinite(value) ? %_NumberToString(value) : "null")); 257 builder.push(($isFinite(value) ? %_NumberToString(value) : "null"));
268 } else if (IS_STRING_WRAPPER(value)) { 258 } else if (IS_STRING_WRAPPER(value)) {
269 builder.push(%QuoteJSONString(%_ValueOf(value))); 259 builder.push(%QuoteJSONString(ToString(value)));
270 } else if (IS_BOOLEAN_WRAPPER(value)) { 260 } else if (IS_BOOLEAN_WRAPPER(value)) {
271 builder.push((%_ValueOf(value) ? "true" : "false")); 261 builder.push(%_ValueOf(value) ? "true" : "false");
262 } else if (IS_ARRAY(value)) {
263 BasicSerializeArray(value, stack, builder);
272 } else { 264 } else {
273 // Regular non-wrapped object 265 BasicSerializeObject(value, stack, builder);
274 if (!value) {
275 builder.push("null");
276 } else if (IS_ARRAY(value)) {
277 BasicSerializeArray(value, stack, builder);
278 } else {
279 BasicSerializeObject(value, stack, builder);
280 }
281 } 266 }
282 } 267 }
283 } 268 }
284 269
270
285 function JSONStringify(value, replacer, space) { 271 function JSONStringify(value, replacer, space) {
286 if (IS_UNDEFINED(replacer) && IS_UNDEFINED(space)) { 272 if (%_ArgumentsLength() == 1) {
287 var builder = []; 273 var builder = [];
288 BasicJSONSerialize('', {'': value}, [], builder); 274 BasicJSONSerialize('', {'': value}, [], builder);
289 if (builder.length == 0) return; 275 if (builder.length == 0) return;
290 var result = %_FastAsciiArrayJoin(builder, ""); 276 var result = %_FastAsciiArrayJoin(builder, "");
291 if (!IS_UNDEFINED(result)) return result; 277 if (!IS_UNDEFINED(result)) return result;
292 return %StringBuilderConcat(builder, builder.length, ""); 278 return %StringBuilderConcat(builder, builder.length, "");
293 } 279 }
294 if (IS_OBJECT(space)) { 280 if (IS_OBJECT(space)) {
295 // Unwrap 'space' if it is wrapped 281 // Unwrap 'space' if it is wrapped
296 if (IS_NUMBER_WRAPPER(space)) { 282 if (IS_NUMBER_WRAPPER(space)) {
297 space = $Number(space); 283 space = ToNumber(space);
298 } else if (IS_STRING_WRAPPER(space)) { 284 } else if (IS_STRING_WRAPPER(space)) {
299 space = $String(space); 285 space = ToString(space);
300 } 286 }
301 } 287 }
302 var gap; 288 var gap;
303 if (IS_NUMBER(space)) { 289 if (IS_NUMBER(space)) {
304 space = $Math.min(ToInteger(space), 10); 290 space = MathMax(0, MathMin(ToInteger(space), 10));
305 gap = ""; 291 gap = SubString(" ", 0, space);
306 for (var i = 0; i < space; i++) {
307 gap += " ";
308 }
309 } else if (IS_STRING(space)) { 292 } else if (IS_STRING(space)) {
310 if (space.length > 10) { 293 if (space.length > 10) {
311 gap = space.substring(0, 10); 294 gap = SubString(space, 0, 10);
312 } else { 295 } else {
313 gap = space; 296 gap = space;
314 } 297 }
315 } else { 298 } else {
316 gap = ""; 299 gap = "";
317 } 300 }
318 return JSONSerialize('', {'': value}, replacer, [], "", gap); 301 return JSONSerialize('', {'': value}, replacer, [], "", gap);
319 } 302 }
320 303
321 function SetupJSON() { 304 function SetupJSON() {
322 InstallFunctions($JSON, DONT_ENUM, $Array( 305 InstallFunctions($JSON, DONT_ENUM, $Array(
323 "parse", JSONParse, 306 "parse", JSONParse,
324 "stringify", JSONStringify 307 "stringify", JSONStringify
325 )); 308 ));
326 } 309 }
327 310
328 SetupJSON(); 311 SetupJSON();
OLDNEW
« no previous file with comments | « src/ic.cc ('k') | src/lithium-allocator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698