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

Side by Side Diff: src/js/json.js

Issue 1775403008: Replace PushIfAbsent by a Stack object (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 9 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/js/array.js ('k') | src/objects.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 (function(global, utils) { 5 (function(global, utils) {
6 6
7 "use strict"; 7 "use strict";
8 8
9 %CheckIsBootstrapping(); 9 %CheckIsBootstrapping();
10 10
11 // ------------------------------------------------------------------- 11 // -------------------------------------------------------------------
12 // Imports 12 // Imports
13 13
14 var GlobalDate = global.Date; 14 var GlobalDate = global.Date;
15 var GlobalJSON = global.JSON; 15 var GlobalJSON = global.JSON;
16 var GlobalSet = global.Set; 16 var GlobalSet = global.Set;
17 var InternalArray = utils.InternalArray; 17 var InternalArray = utils.InternalArray;
18 var MakeTypeError; 18 var MakeTypeError;
19 var MaxSimple; 19 var MaxSimple;
20 var MinSimple; 20 var MinSimple;
21 var ObjectHasOwnProperty; 21 var ObjectHasOwnProperty;
22 var Stack;
23 var StackHas;
24 var StackPop;
25 var StackPush;
22 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); 26 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
23 27
24 utils.Import(function(from) { 28 utils.Import(function(from) {
25 MakeTypeError = from.MakeTypeError; 29 MakeTypeError = from.MakeTypeError;
26 MaxSimple = from.MaxSimple; 30 MaxSimple = from.MaxSimple;
27 MinSimple = from.MinSimple; 31 MinSimple = from.MinSimple;
28 ObjectHasOwnProperty = from.ObjectHasOwnProperty; 32 ObjectHasOwnProperty = from.ObjectHasOwnProperty;
33 Stack = from.Stack;
34 StackHas = from.StackHas;
35 StackPop = from.StackPop;
36 StackPush = from.StackPush;
29 }); 37 });
30 38
31 // ------------------------------------------------------------------- 39 // -------------------------------------------------------------------
32 40
33 function CreateDataProperty(o, p, v) { 41 function CreateDataProperty(o, p, v) {
34 var desc = {value: v, enumerable: true, writable: true, configurable: true}; 42 var desc = {value: v, enumerable: true, writable: true, configurable: true};
35 return %reflect_define_property(o, p, desc); 43 return %reflect_define_property(o, p, desc);
36 } 44 }
37 45
38 46
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 var unfiltered = %ParseJson(text); 79 var unfiltered = %ParseJson(text);
72 if (IS_CALLABLE(reviver)) { 80 if (IS_CALLABLE(reviver)) {
73 return InternalizeJSONProperty({'': unfiltered}, '', reviver); 81 return InternalizeJSONProperty({'': unfiltered}, '', reviver);
74 } else { 82 } else {
75 return unfiltered; 83 return unfiltered;
76 } 84 }
77 } 85 }
78 86
79 87
80 function SerializeArray(value, replacer, stack, indent, gap) { 88 function SerializeArray(value, replacer, stack, indent, gap) {
81 if (!%PushIfAbsent(stack, value)) throw MakeTypeError(kCircularStructure); 89 if (StackHas(stack, value)) throw MakeTypeError(kCircularStructure);
90 StackPush(stack, value);
82 var stepback = indent; 91 var stepback = indent;
83 indent += gap; 92 indent += gap;
84 var partial = new InternalArray(); 93 var partial = new InternalArray();
85 var len = TO_LENGTH(value.length); 94 var len = TO_LENGTH(value.length);
86 for (var i = 0; i < len; i++) { 95 for (var i = 0; i < len; i++) {
87 var strP = JSONSerialize(%_NumberToString(i), value, replacer, stack, 96 var strP = JSONSerialize(%_NumberToString(i), value, replacer, stack,
88 indent, gap); 97 indent, gap);
89 if (IS_UNDEFINED(strP)) { 98 if (IS_UNDEFINED(strP)) {
90 strP = "null"; 99 strP = "null";
91 } 100 }
92 partial.push(strP); 101 partial.push(strP);
93 } 102 }
94 var final; 103 var final;
95 if (gap == "") { 104 if (gap == "") {
96 final = "[" + partial.join(",") + "]"; 105 final = "[" + partial.join(",") + "]";
97 } else if (partial.length > 0) { 106 } else if (partial.length > 0) {
98 var separator = ",\n" + indent; 107 var separator = ",\n" + indent;
99 final = "[\n" + indent + partial.join(separator) + "\n" + 108 final = "[\n" + indent + partial.join(separator) + "\n" +
100 stepback + "]"; 109 stepback + "]";
101 } else { 110 } else {
102 final = "[]"; 111 final = "[]";
103 } 112 }
104 stack.pop(); 113 StackPop(stack);
105 return final; 114 return final;
106 } 115 }
107 116
108 117
109 function SerializeObject(value, replacer, stack, indent, gap) { 118 function SerializeObject(value, replacer, stack, indent, gap) {
110 if (!%PushIfAbsent(stack, value)) throw MakeTypeError(kCircularStructure); 119 if (StackHas(stack, value)) throw MakeTypeError(kCircularStructure);
120 StackPush(stack, value);
111 var stepback = indent; 121 var stepback = indent;
112 indent += gap; 122 indent += gap;
113 var partial = new InternalArray(); 123 var partial = new InternalArray();
114 if (IS_ARRAY(replacer)) { 124 if (IS_ARRAY(replacer)) {
115 var length = replacer.length; 125 var length = replacer.length;
116 for (var i = 0; i < length; i++) { 126 for (var i = 0; i < length; i++) {
117 var p = replacer[i]; 127 var p = replacer[i];
118 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); 128 var strP = JSONSerialize(p, value, replacer, stack, indent, gap);
119 if (!IS_UNDEFINED(strP)) { 129 if (!IS_UNDEFINED(strP)) {
120 var member = %QuoteJSONString(p) + ":"; 130 var member = %QuoteJSONString(p) + ":";
(...skipping 18 matching lines...) Expand all
139 var final; 149 var final;
140 if (gap == "") { 150 if (gap == "") {
141 final = "{" + partial.join(",") + "}"; 151 final = "{" + partial.join(",") + "}";
142 } else if (partial.length > 0) { 152 } else if (partial.length > 0) {
143 var separator = ",\n" + indent; 153 var separator = ",\n" + indent;
144 final = "{\n" + indent + partial.join(separator) + "\n" + 154 final = "{\n" + indent + partial.join(separator) + "\n" +
145 stepback + "}"; 155 stepback + "}";
146 } else { 156 } else {
147 final = "{}"; 157 final = "{}";
148 } 158 }
149 stack.pop(); 159 StackPop(stack);
150 return final; 160 return final;
151 } 161 }
152 162
153 163
154 function JSONSerialize(key, holder, replacer, stack, indent, gap) { 164 function JSONSerialize(key, holder, replacer, stack, indent, gap) {
155 var value = holder[key]; 165 var value = holder[key];
156 if (IS_RECEIVER(value)) { 166 if (IS_RECEIVER(value)) {
157 var toJSON = value.toJSON; 167 var toJSON = value.toJSON;
158 if (IS_CALLABLE(toJSON)) { 168 if (IS_CALLABLE(toJSON)) {
159 value = %_Call(toJSON, value, key); 169 value = %_Call(toJSON, value, key);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 gap = %_SubString(space, 0, 10); 244 gap = %_SubString(space, 0, 10);
235 } else { 245 } else {
236 gap = space; 246 gap = space;
237 } 247 }
238 } else { 248 } else {
239 gap = ""; 249 gap = "";
240 } 250 }
241 if (!IS_CALLABLE(replacer) && !property_list && !gap && !IS_PROXY(value)) { 251 if (!IS_CALLABLE(replacer) && !property_list && !gap && !IS_PROXY(value)) {
242 return %BasicJSONStringify(value); 252 return %BasicJSONStringify(value);
243 } 253 }
244 return JSONSerialize('', {'': value}, replacer, new InternalArray(), "", gap); 254 return JSONSerialize('', {'': value}, replacer, new Stack(), "", gap);
245 } 255 }
246 256
247 // ------------------------------------------------------------------- 257 // -------------------------------------------------------------------
248 258
249 %AddNamedProperty(GlobalJSON, toStringTagSymbol, "JSON", READ_ONLY | DONT_ENUM); 259 %AddNamedProperty(GlobalJSON, toStringTagSymbol, "JSON", READ_ONLY | DONT_ENUM);
250 260
251 // Set up non-enumerable properties of the JSON object. 261 // Set up non-enumerable properties of the JSON object.
252 utils.InstallFunctions(GlobalJSON, DONT_ENUM, [ 262 utils.InstallFunctions(GlobalJSON, DONT_ENUM, [
253 "parse", JSONParse, 263 "parse", JSONParse,
254 "stringify", JSONStringify 264 "stringify", JSONStringify
(...skipping 17 matching lines...) Expand all
272 "toJSON", DateToJSON 282 "toJSON", DateToJSON
273 ]); 283 ]);
274 284
275 // ------------------------------------------------------------------- 285 // -------------------------------------------------------------------
276 // JSON Builtins 286 // JSON Builtins
277 287
278 function JsonSerializeAdapter(key, object) { 288 function JsonSerializeAdapter(key, object) {
279 var holder = {}; 289 var holder = {};
280 holder[key] = object; 290 holder[key] = object;
281 // No need to pass the actual holder since there is no replacer function. 291 // No need to pass the actual holder since there is no replacer function.
282 return JSONSerialize(key, holder, UNDEFINED, new InternalArray(), "", ""); 292 return JSONSerialize(key, holder, UNDEFINED, new Stack(), "", "");
283 } 293 }
284 294
285 %InstallToContext(["json_serialize_adapter", JsonSerializeAdapter]); 295 %InstallToContext(["json_serialize_adapter", JsonSerializeAdapter]);
286 296
287 }) 297 })
OLDNEW
« no previous file with comments | « src/js/array.js ('k') | src/objects.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698