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

Side by Side Diff: src/json.js

Issue 1097703002: Wrap JSON and generator implementation in functions. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 8 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
« no previous file with comments | « src/generator.js ('k') | src/json-stringifier.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 // 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;
6
7 (function() {
8
5 "use strict"; 9 "use strict";
6 10
7 // This file relies on the fact that the following declarations have been made 11 %CheckIsBootstrapping();
8 // in runtime.js:
9 // var $Array = global.Array;
10 // var $String = global.String;
11 12
12 var $JSON = global.JSON; 13 var GlobalJSON = global.JSON;
13 14
14 // ------------------------------------------------------------------- 15 // -------------------------------------------------------------------
15 16
16 function Revive(holder, name, reviver) { 17 function Revive(holder, name, reviver) {
17 var val = holder[name]; 18 var val = holder[name];
18 if (IS_OBJECT(val)) { 19 if (IS_OBJECT(val)) {
19 if (IS_ARRAY(val)) { 20 if (IS_ARRAY(val)) {
20 var length = val.length; 21 var length = val.length;
21 for (var i = 0; i < length; i++) { 22 for (var i = 0; i < length; i++) {
22 var newElement = Revive(val, $String(i), reviver); 23 var newElement = Revive(val, %_NumberToString(i), reviver);
23 val[i] = newElement; 24 val[i] = newElement;
24 } 25 }
25 } else { 26 } else {
26 for (var p in val) { 27 for (var p in val) {
27 if (%_CallFunction(val, p, ObjectHasOwnProperty)) { 28 if (%_CallFunction(val, p, ObjectHasOwnProperty)) {
28 var newElement = Revive(val, p, reviver); 29 var newElement = Revive(val, p, reviver);
29 if (IS_UNDEFINED(newElement)) { 30 if (IS_UNDEFINED(newElement)) {
30 delete val[p]; 31 delete val[p];
31 } else { 32 } else {
32 val[p] = newElement; 33 val[p] = newElement;
33 } 34 }
34 } 35 }
35 } 36 }
36 } 37 }
37 } 38 }
38 return %_CallFunction(holder, name, val, reviver); 39 return %_CallFunction(holder, name, val, reviver);
39 } 40 }
40 41
42
41 function JSONParse(text, reviver) { 43 function JSONParse(text, reviver) {
42 var unfiltered = %ParseJson(TO_STRING_INLINE(text)); 44 var unfiltered = %ParseJson(TO_STRING_INLINE(text));
43 if (IS_SPEC_FUNCTION(reviver)) { 45 if (IS_SPEC_FUNCTION(reviver)) {
44 return Revive({'': unfiltered}, '', reviver); 46 return Revive({'': unfiltered}, '', reviver);
45 } else { 47 } else {
46 return unfiltered; 48 return unfiltered;
47 } 49 }
48 } 50 }
49 51
52
50 function SerializeArray(value, replacer, stack, indent, gap) { 53 function SerializeArray(value, replacer, stack, indent, gap) {
51 if (!%PushIfAbsent(stack, value)) { 54 if (!%PushIfAbsent(stack, value)) {
52 throw MakeTypeError('circular_structure', []); 55 throw MakeTypeError('circular_structure', []);
53 } 56 }
54 var stepback = indent; 57 var stepback = indent;
55 indent += gap; 58 indent += gap;
56 var partial = new InternalArray(); 59 var partial = new InternalArray();
57 var len = value.length; 60 var len = value.length;
58 for (var i = 0; i < len; i++) { 61 for (var i = 0; i < len; i++) {
59 var strP = JSONSerialize($String(i), value, replacer, stack, 62 var strP = JSONSerialize(%_NumberToString(i), value, replacer, stack,
60 indent, gap); 63 indent, gap);
61 if (IS_UNDEFINED(strP)) { 64 if (IS_UNDEFINED(strP)) {
62 strP = "null"; 65 strP = "null";
63 } 66 }
64 partial.push(strP); 67 partial.push(strP);
65 } 68 }
66 var final; 69 var final;
67 if (gap == "") { 70 if (gap == "") {
68 final = "[" + partial.join(",") + "]"; 71 final = "[" + partial.join(",") + "]";
69 } else if (partial.length > 0) { 72 } else if (partial.length > 0) {
70 var separator = ",\n" + indent; 73 var separator = ",\n" + indent;
71 final = "[\n" + indent + partial.join(separator) + "\n" + 74 final = "[\n" + indent + partial.join(separator) + "\n" +
72 stepback + "]"; 75 stepback + "]";
73 } else { 76 } else {
74 final = "[]"; 77 final = "[]";
75 } 78 }
76 stack.pop(); 79 stack.pop();
77 return final; 80 return final;
78 } 81 }
79 82
83
80 function SerializeObject(value, replacer, stack, indent, gap) { 84 function SerializeObject(value, replacer, stack, indent, gap) {
81 if (!%PushIfAbsent(stack, value)) { 85 if (!%PushIfAbsent(stack, value)) {
82 throw MakeTypeError('circular_structure', []); 86 throw MakeTypeError('circular_structure', []);
83 } 87 }
84 var stepback = indent; 88 var stepback = indent;
85 indent += gap; 89 indent += gap;
86 var partial = new InternalArray(); 90 var partial = new InternalArray();
87 if (IS_ARRAY(replacer)) { 91 if (IS_ARRAY(replacer)) {
88 var length = replacer.length; 92 var length = replacer.length;
89 for (var i = 0; i < length; i++) { 93 for (var i = 0; i < length; i++) {
(...skipping 28 matching lines...) Expand all
118 var separator = ",\n" + indent; 122 var separator = ",\n" + indent;
119 final = "{\n" + indent + partial.join(separator) + "\n" + 123 final = "{\n" + indent + partial.join(separator) + "\n" +
120 stepback + "}"; 124 stepback + "}";
121 } else { 125 } else {
122 final = "{}"; 126 final = "{}";
123 } 127 }
124 stack.pop(); 128 stack.pop();
125 return final; 129 return final;
126 } 130 }
127 131
132
128 function JSONSerialize(key, holder, replacer, stack, indent, gap) { 133 function JSONSerialize(key, holder, replacer, stack, indent, gap) {
129 var value = holder[key]; 134 var value = holder[key];
130 if (IS_SPEC_OBJECT(value)) { 135 if (IS_SPEC_OBJECT(value)) {
131 var toJSON = value.toJSON; 136 var toJSON = value.toJSON;
132 if (IS_SPEC_FUNCTION(toJSON)) { 137 if (IS_SPEC_FUNCTION(toJSON)) {
133 value = %_CallFunction(value, key, toJSON); 138 value = %_CallFunction(value, key, toJSON);
134 } 139 }
135 } 140 }
136 if (IS_SPEC_FUNCTION(replacer)) { 141 if (IS_SPEC_FUNCTION(replacer)) {
137 value = %_CallFunction(holder, key, value, replacer); 142 value = %_CallFunction(holder, key, value, replacer);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 property_list.push(item); 212 property_list.push(item);
208 // We cannot use true here because __proto__ needs to be an object. 213 // We cannot use true here because __proto__ needs to be an object.
209 seen_properties[item] = seen_sentinel; 214 seen_properties[item] = seen_sentinel;
210 } 215 }
211 } 216 }
212 replacer = property_list; 217 replacer = property_list;
213 } 218 }
214 return JSONSerialize('', {'': value}, replacer, new InternalArray(), "", gap); 219 return JSONSerialize('', {'': value}, replacer, new InternalArray(), "", gap);
215 } 220 }
216 221
217
218 // ------------------------------------------------------------------- 222 // -------------------------------------------------------------------
219 223
220 function SetUpJSON() { 224 %AddNamedProperty(GlobalJSON, symbolToStringTag, "JSON", READ_ONLY | DONT_ENUM);
221 %CheckIsBootstrapping();
222 225
223 %AddNamedProperty($JSON, symbolToStringTag, "JSON", READ_ONLY | DONT_ENUM); 226 // Set up non-enumerable properties of the JSON object.
224 227 InstallFunctions(GlobalJSON, DONT_ENUM, [
225 // Set up non-enumerable properties of the JSON object. 228 "parse", JSONParse,
226 InstallFunctions($JSON, DONT_ENUM, [ 229 "stringify", JSONStringify
227 "parse", JSONParse, 230 ]);
228 "stringify", JSONStringify
229 ]);
230 }
231
232 SetUpJSON();
233
234 231
235 // ------------------------------------------------------------------- 232 // -------------------------------------------------------------------
236 // JSON Builtins 233 // JSON Builtins
237 234
238 function JSONSerializeAdapter(key, object) { 235 $jsonSerializeAdapter = function(key, object) {
239 var holder = {}; 236 var holder = {};
240 holder[key] = object; 237 holder[key] = object;
241 // No need to pass the actual holder since there is no replacer function. 238 // No need to pass the actual holder since there is no replacer function.
242 return JSONSerialize(key, holder, UNDEFINED, new InternalArray(), "", ""); 239 return JSONSerialize(key, holder, UNDEFINED, new InternalArray(), "", "");
243 } 240 }
241
242 })();
OLDNEW
« no previous file with comments | « src/generator.js ('k') | src/json-stringifier.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698