OLD | NEW |
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 |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 return final; | 114 return final; |
115 } | 115 } |
116 | 116 |
117 | 117 |
118 function SerializeObject(value, replacer, stack, indent, gap) { | 118 function SerializeObject(value, replacer, stack, indent, gap) { |
119 if (StackHas(stack, value)) throw MakeTypeError(kCircularStructure); | 119 if (StackHas(stack, value)) throw MakeTypeError(kCircularStructure); |
120 StackPush(stack, value); | 120 StackPush(stack, value); |
121 var stepback = indent; | 121 var stepback = indent; |
122 indent += gap; | 122 indent += gap; |
123 var partial = new InternalArray(); | 123 var partial = new InternalArray(); |
124 if (IS_ARRAY(replacer)) { | 124 var keys = %object_keys(value); |
125 var length = replacer.length; | 125 for (var i = 0; i < keys.length; i++) { |
126 for (var i = 0; i < length; i++) { | 126 var p = keys[i]; |
127 var p = replacer[i]; | 127 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); |
128 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); | 128 if (!IS_UNDEFINED(strP)) { |
129 if (!IS_UNDEFINED(strP)) { | 129 var member = %QuoteJSONString(p) + ":"; |
130 var member = %QuoteJSONString(p) + ":"; | 130 if (gap != "") member += " "; |
131 if (gap != "") member += " "; | 131 member += strP; |
132 member += strP; | 132 partial.push(member); |
133 partial.push(member); | |
134 } | |
135 } | |
136 } else { | |
137 var keys = %object_keys(value); | |
138 for (var i = 0; i < keys.length; i++) { | |
139 var p = keys[i]; | |
140 var strP = JSONSerialize(p, value, replacer, stack, indent, gap); | |
141 if (!IS_UNDEFINED(strP)) { | |
142 var member = %QuoteJSONString(p) + ":"; | |
143 if (gap != "") member += " "; | |
144 member += strP; | |
145 partial.push(member); | |
146 } | |
147 } | 133 } |
148 } | 134 } |
149 var final; | 135 var final; |
150 if (gap == "") { | 136 if (gap == "") { |
151 final = "{" + partial.join(",") + "}"; | 137 final = "{" + partial.join(",") + "}"; |
152 } else if (partial.length > 0) { | 138 } else if (partial.length > 0) { |
153 var separator = ",\n" + indent; | 139 var separator = ",\n" + indent; |
154 final = "{\n" + indent + partial.join(separator) + "\n" + | 140 final = "{\n" + indent + partial.join(separator) + "\n" + |
155 stepback + "}"; | 141 stepback + "}"; |
156 } else { | 142 } else { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 } else { | 180 } else { |
195 return SerializeObject(value, replacer, stack, indent, gap); | 181 return SerializeObject(value, replacer, stack, indent, gap); |
196 } | 182 } |
197 } | 183 } |
198 // Undefined or a callable object. | 184 // Undefined or a callable object. |
199 return UNDEFINED; | 185 return UNDEFINED; |
200 } | 186 } |
201 | 187 |
202 | 188 |
203 function JSONStringify(value, replacer, space) { | 189 function JSONStringify(value, replacer, space) { |
204 if (arguments.length === 1) return %BasicJSONStringify(value, ""); | 190 if (arguments.length === 1) return %BasicJSONStringify(value, UNDEFINED, ""); |
205 if (!IS_CALLABLE(replacer) && %is_arraylike(replacer)) { | 191 if (!IS_CALLABLE(replacer)) { |
206 var property_list = new InternalArray(); | 192 return %BasicJSONStringify(value, replacer, space); |
207 var seen_properties = new GlobalSet(); | |
208 var length = TO_LENGTH(replacer.length); | |
209 for (var i = 0; i < length; i++) { | |
210 var v = replacer[i]; | |
211 var item; | |
212 if (IS_STRING(v)) { | |
213 item = v; | |
214 } else if (IS_NUMBER(v)) { | |
215 item = %_NumberToString(v); | |
216 } else if (IS_STRING_WRAPPER(v) || IS_NUMBER_WRAPPER(v)) { | |
217 item = TO_STRING(v); | |
218 } else { | |
219 continue; | |
220 } | |
221 if (!seen_properties.has(item)) { | |
222 property_list.push(item); | |
223 seen_properties.add(item); | |
224 } | |
225 } | |
226 replacer = property_list; | |
227 } | 193 } |
228 if (IS_OBJECT(space)) { | 194 if (IS_OBJECT(space)) { |
229 // Unwrap 'space' if it is wrapped | 195 // Unwrap 'space' if it is wrapped |
230 if (IS_NUMBER_WRAPPER(space)) { | 196 if (IS_NUMBER_WRAPPER(space)) { |
231 space = TO_NUMBER(space); | 197 space = TO_NUMBER(space); |
232 } else if (IS_STRING_WRAPPER(space)) { | 198 } else if (IS_STRING_WRAPPER(space)) { |
233 space = TO_STRING(space); | 199 space = TO_STRING(space); |
234 } | 200 } |
235 } | 201 } |
236 if (!IS_CALLABLE(replacer) && !property_list) { | |
237 return %BasicJSONStringify(value, space); | |
238 } | |
239 var gap; | 202 var gap; |
240 if (IS_NUMBER(space)) { | 203 if (IS_NUMBER(space)) { |
241 space = MaxSimple(0, MinSimple(TO_INTEGER(space), 10)); | 204 space = MaxSimple(0, MinSimple(TO_INTEGER(space), 10)); |
242 gap = %_SubString(" ", 0, space); | 205 gap = %_SubString(" ", 0, space); |
243 } else if (IS_STRING(space)) { | 206 } else if (IS_STRING(space)) { |
244 if (space.length > 10) { | 207 if (space.length > 10) { |
245 gap = %_SubString(space, 0, 10); | 208 gap = %_SubString(space, 0, 10); |
246 } else { | 209 } else { |
247 gap = space; | 210 gap = space; |
248 } | 211 } |
(...skipping 25 matching lines...) Expand all Loading... |
274 } | 237 } |
275 return o.toISOString(); | 238 return o.toISOString(); |
276 } | 239 } |
277 | 240 |
278 // Set up non-enumerable functions of the Date prototype object. | 241 // Set up non-enumerable functions of the Date prototype object. |
279 utils.InstallFunctions(GlobalDate.prototype, DONT_ENUM, [ | 242 utils.InstallFunctions(GlobalDate.prototype, DONT_ENUM, [ |
280 "toJSON", DateToJSON | 243 "toJSON", DateToJSON |
281 ]); | 244 ]); |
282 | 245 |
283 }) | 246 }) |
OLD | NEW |