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

Side by Side Diff: src/json.js

Issue 6529032: Merge 6168:6800 from bleeding_edge to experimental/gc branch. (Closed) Base URL: http://v8.googlecode.com/svn/branches/experimental/gc/
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/inspector.cc ('k') | src/jsregexp.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Modified: svn:mergeinfo
Merged /branches/bleeding_edge/src/json.js:r6169-6800
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 20 matching lines...) Expand all
31 var val = holder[name]; 31 var val = holder[name];
32 if (IS_OBJECT(val)) { 32 if (IS_OBJECT(val)) {
33 if (IS_ARRAY(val)) { 33 if (IS_ARRAY(val)) {
34 var length = val.length; 34 var length = val.length;
35 for (var i = 0; i < length; i++) { 35 for (var i = 0; i < length; i++) {
36 var newElement = Revive(val, $String(i), reviver); 36 var newElement = Revive(val, $String(i), reviver);
37 val[i] = newElement; 37 val[i] = newElement;
38 } 38 }
39 } else { 39 } else {
40 for (var p in val) { 40 for (var p in val) {
41 if (ObjectHasOwnProperty.call(val, p)) { 41 if (%_CallFunction(val, p, ObjectHasOwnProperty)) {
42 var newElement = Revive(val, p, reviver); 42 var newElement = Revive(val, p, reviver);
43 if (IS_UNDEFINED(newElement)) { 43 if (IS_UNDEFINED(newElement)) {
44 delete val[p]; 44 delete val[p];
45 } else { 45 } else {
46 val[p] = newElement; 46 val[p] = newElement;
47 } 47 }
48 } 48 }
49 } 49 }
50 } 50 }
51 } 51 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 function SerializeObject(value, replacer, stack, indent, gap) { 94 function SerializeObject(value, replacer, stack, indent, gap) {
95 if (!%PushIfAbsent(stack, value)) { 95 if (!%PushIfAbsent(stack, value)) {
96 throw MakeTypeError('circular_structure', []); 96 throw MakeTypeError('circular_structure', []);
97 } 97 }
98 var stepback = indent; 98 var stepback = indent;
99 indent += gap; 99 indent += gap;
100 var partial = []; 100 var partial = [];
101 if (IS_ARRAY(replacer)) { 101 if (IS_ARRAY(replacer)) {
102 var length = replacer.length; 102 var length = replacer.length;
103 for (var i = 0; i < length; i++) { 103 for (var i = 0; i < length; i++) {
104 if (ObjectHasOwnProperty.call(replacer, i)) { 104 if (%_CallFunction(replacer, i, ObjectHasOwnProperty)) {
105 var p = replacer[i]; 105 var p = replacer[i];
106 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); 106 var strP = JSONSerialize(p, value, replacer, stack, indent, gap);
107 if (!IS_UNDEFINED(strP)) { 107 if (!IS_UNDEFINED(strP)) {
108 var member = %QuoteJSONString(p) + ":"; 108 var member = %QuoteJSONString(p) + ":";
109 if (gap != "") member += " "; 109 if (gap != "") member += " ";
110 member += strP; 110 member += strP;
111 partial.push(member); 111 partial.push(member);
112 } 112 }
113 } 113 }
114 } 114 }
115 } else { 115 } else {
116 for (var p in value) { 116 for (var p in value) {
117 if (ObjectHasOwnProperty.call(value, p)) { 117 if (%_CallFunction(value, p, ObjectHasOwnProperty)) {
118 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); 118 var strP = JSONSerialize(p, value, replacer, stack, indent, gap);
119 if (!IS_UNDEFINED(strP)) { 119 if (!IS_UNDEFINED(strP)) {
120 var member = %QuoteJSONString(p) + ":"; 120 var member = %QuoteJSONString(p) + ":";
121 if (gap != "") member += " "; 121 if (gap != "") member += " ";
122 member += strP; 122 member += strP;
123 partial.push(member); 123 partial.push(member);
124 } 124 }
125 } 125 }
126 } 126 }
127 } 127 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 } else { 172 } else {
173 return SerializeObject(value, replacer, stack, indent, gap); 173 return SerializeObject(value, replacer, stack, indent, gap);
174 } 174 }
175 } 175 }
176 // Undefined or a callable object. 176 // Undefined or a callable object.
177 return void 0; 177 return void 0;
178 } 178 }
179 179
180 180
181 function BasicSerializeArray(value, stack, builder) { 181 function BasicSerializeArray(value, stack, builder) {
182 var len = value.length;
183 if (len == 0) {
184 builder.push("[]");
185 return;
186 }
182 if (!%PushIfAbsent(stack, value)) { 187 if (!%PushIfAbsent(stack, value)) {
183 throw MakeTypeError('circular_structure', []); 188 throw MakeTypeError('circular_structure', []);
184 } 189 }
185 builder.push("["); 190 builder.push("[");
186 var len = value.length; 191 var val = value[0];
187 for (var i = 0; i < len; i++) { 192 if (IS_STRING(val)) {
193 // First entry is a string. Remaining entries are likely to be strings too.
194 builder.push(%QuoteJSONString(val));
195 for (var i = 1; i < len; i++) {
196 val = value[i];
197 if (IS_STRING(val)) {
198 builder.push(%QuoteJSONStringComma(val));
199 } else {
200 builder.push(",");
201 var before = builder.length;
202 BasicJSONSerialize(i, value[i], stack, builder);
203 if (before == builder.length) builder[before - 1] = ",null";
204 }
205 }
206 } else if (IS_NUMBER(val)) {
207 // First entry is a number. Remaining entries are likely to be numbers too.
208 builder.push(NUMBER_IS_FINITE(val) ? %_NumberToString(val) : "null");
209 for (var i = 1; i < len; i++) {
210 builder.push(",");
211 val = value[i];
212 if (IS_NUMBER(val)) {
213 builder.push(NUMBER_IS_FINITE(val)
214 ? %_NumberToString(val)
215 : "null");
216 } else {
217 var before = builder.length;
218 BasicJSONSerialize(i, value[i], stack, builder);
219 if (before == builder.length) builder[before - 1] = ",null";
220 }
221 }
222 } else {
188 var before = builder.length; 223 var before = builder.length;
189 BasicJSONSerialize(i, value, stack, builder); 224 BasicJSONSerialize(0, val, stack, builder);
190 if (before == builder.length) builder.push("null"); 225 if (before == builder.length) builder.push("null");
191 builder.push(","); 226 for (var i = 1; i < len; i++) {
227 builder.push(",");
228 before = builder.length;
229 val = value[i];
230 BasicJSONSerialize(i, val, stack, builder);
231 if (before == builder.length) builder[before - 1] = ",null";
232 }
192 } 233 }
193 stack.pop(); 234 stack.pop();
194 if (builder.pop() != ",") { 235 builder.push("]");
195 builder.push("[]"); // Zero length array. Push "[" back on.
196 } else {
197 builder.push("]");
198 }
199
200 } 236 }
201 237
202 238
203 function BasicSerializeObject(value, stack, builder) { 239 function BasicSerializeObject(value, stack, builder) {
204 if (!%PushIfAbsent(stack, value)) { 240 if (!%PushIfAbsent(stack, value)) {
205 throw MakeTypeError('circular_structure', []); 241 throw MakeTypeError('circular_structure', []);
206 } 242 }
207 builder.push("{"); 243 builder.push("{");
244 var first = true;
208 for (var p in value) { 245 for (var p in value) {
209 if (%HasLocalProperty(value, p)) { 246 if (%HasLocalProperty(value, p)) {
210 builder.push(%QuoteJSONString(p)); 247 if (!first) {
248 builder.push(%QuoteJSONStringComma(p));
249 } else {
250 builder.push(%QuoteJSONString(p));
251 }
211 builder.push(":"); 252 builder.push(":");
212 var before = builder.length; 253 var before = builder.length;
213 BasicJSONSerialize(p, value, stack, builder); 254 BasicJSONSerialize(p, value[p], stack, builder);
214 if (before == builder.length) { 255 if (before == builder.length) {
215 builder.pop(); 256 builder.pop();
216 builder.pop(); 257 builder.pop();
217 } else { 258 } else {
218 builder.push(","); 259 first = false;
219 } 260 }
220 } 261 }
221 } 262 }
222 stack.pop(); 263 stack.pop();
223 if (builder.pop() != ",") { 264 builder.push("}");
224 builder.push("{}"); // Object has no own properties. Push "{" back on.
225 } else {
226 builder.push("}");
227 }
228 } 265 }
229 266
230 267
231 function BasicJSONSerialize(key, holder, stack, builder) { 268 function BasicJSONSerialize(key, value, stack, builder) {
232 var value = holder[key];
233 if (IS_SPEC_OBJECT(value)) { 269 if (IS_SPEC_OBJECT(value)) {
234 var toJSON = value.toJSON; 270 var toJSON = value.toJSON;
235 if (IS_FUNCTION(toJSON)) { 271 if (IS_FUNCTION(toJSON)) {
236 value = %_CallFunction(value, ToString(key), toJSON); 272 value = %_CallFunction(value, ToString(key), toJSON);
237 } 273 }
238 } 274 }
239 if (IS_STRING(value)) { 275 if (IS_STRING(value)) {
240 builder.push(%QuoteJSONString(value)); 276 builder.push(%QuoteJSONString(value));
241 } else if (IS_NUMBER(value)) { 277 } else if (IS_NUMBER(value)) {
242 builder.push(NUMBER_IS_FINITE(value) ? %_NumberToString(value) : "null"); 278 builder.push(NUMBER_IS_FINITE(value) ? %_NumberToString(value) : "null");
(...skipping 16 matching lines...) Expand all
259 } else { 295 } else {
260 BasicSerializeObject(value, stack, builder); 296 BasicSerializeObject(value, stack, builder);
261 } 297 }
262 } 298 }
263 } 299 }
264 300
265 301
266 function JSONStringify(value, replacer, space) { 302 function JSONStringify(value, replacer, space) {
267 if (%_ArgumentsLength() == 1) { 303 if (%_ArgumentsLength() == 1) {
268 var builder = []; 304 var builder = [];
269 BasicJSONSerialize('', {'': value}, [], builder); 305 BasicJSONSerialize('', value, [], builder);
270 if (builder.length == 0) return; 306 if (builder.length == 0) return;
271 var result = %_FastAsciiArrayJoin(builder, ""); 307 var result = %_FastAsciiArrayJoin(builder, "");
272 if (!IS_UNDEFINED(result)) return result; 308 if (!IS_UNDEFINED(result)) return result;
273 return %StringBuilderConcat(builder, builder.length, ""); 309 return %StringBuilderConcat(builder, builder.length, "");
274 } 310 }
275 if (IS_OBJECT(space)) { 311 if (IS_OBJECT(space)) {
276 // Unwrap 'space' if it is wrapped 312 // Unwrap 'space' if it is wrapped
277 if (IS_NUMBER_WRAPPER(space)) { 313 if (IS_NUMBER_WRAPPER(space)) {
278 space = ToNumber(space); 314 space = ToNumber(space);
279 } else if (IS_STRING_WRAPPER(space)) { 315 } else if (IS_STRING_WRAPPER(space)) {
(...skipping 17 matching lines...) Expand all
297 } 333 }
298 334
299 function SetupJSON() { 335 function SetupJSON() {
300 InstallFunctions($JSON, DONT_ENUM, $Array( 336 InstallFunctions($JSON, DONT_ENUM, $Array(
301 "parse", JSONParse, 337 "parse", JSONParse,
302 "stringify", JSONStringify 338 "stringify", JSONStringify
303 )); 339 ));
304 } 340 }
305 341
306 SetupJSON(); 342 SetupJSON();
OLDNEW
« no previous file with comments | « src/inspector.cc ('k') | src/jsregexp.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698