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

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

Issue 1506933003: JSON.parse: properly deal with reviver result (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Rebase Created 5 years 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 | « no previous file | src/js/v8natives.js » ('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 GlobalJSON = global.JSON; 14 var GlobalJSON = global.JSON;
15 var InternalArray = utils.InternalArray; 15 var InternalArray = utils.InternalArray;
16 var MakeTypeError; 16 var MakeTypeError;
17 var MaxSimple; 17 var MaxSimple;
18 var MinSimple; 18 var MinSimple;
19 var ObjectHasOwnProperty; 19 var ObjectHasOwnProperty;
20 var ObjectKeys;
20 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); 21 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
21 22
22 utils.Import(function(from) { 23 utils.Import(function(from) {
23 MakeTypeError = from.MakeTypeError; 24 MakeTypeError = from.MakeTypeError;
24 MaxSimple = from.MaxSimple; 25 MaxSimple = from.MaxSimple;
25 MinSimple = from.MinSimple; 26 MinSimple = from.MinSimple;
26 ObjectHasOwnProperty = from.ObjectHasOwnProperty; 27 ObjectHasOwnProperty = from.ObjectHasOwnProperty;
28 ObjectKeys = from.ObjectKeys;
27 }); 29 });
28 30
29 // ------------------------------------------------------------------- 31 // -------------------------------------------------------------------
30 32
31 function Revive(holder, name, reviver) { 33 function InternalizeJSONProperty(holder, name, reviver) {
32 var val = holder[name]; 34 var val = holder[name];
33 if (IS_OBJECT(val)) { 35 if (IS_OBJECT(val) && val !== null) {
34 if (IS_ARRAY(val)) { 36 if (IS_ARRAY(val)) {
35 var length = val.length; 37 var length = val.length;
36 for (var i = 0; i < length; i++) { 38 for (var i = 0; i < length; i++) {
37 var newElement = Revive(val, %_NumberToString(i), reviver); 39 var newElement =
38 val[i] = newElement; 40 InternalizeJSONProperty(val, %_NumberToString(i), reviver);
41 if (IS_UNDEFINED(newElement)) {
42 delete val[i];
43 } else {
44 val[i] = newElement;
45 }
39 } 46 }
40 } else { 47 } else {
41 for (var p in val) { 48 for (var p of ObjectKeys(val)) {
42 if (HAS_OWN_PROPERTY(val, p)) { 49 var newElement = InternalizeJSONProperty(val, p, reviver);
43 var newElement = Revive(val, p, reviver); 50 if (IS_UNDEFINED(newElement)) {
44 if (IS_UNDEFINED(newElement)) { 51 delete val[p];
45 delete val[p]; 52 } else {
46 } else { 53 val[p] = newElement;
47 val[p] = newElement;
48 }
49 } 54 }
50 } 55 }
51 } 56 }
52 } 57 }
53 return %_Call(reviver, holder, name, val); 58 return %_Call(reviver, holder, name, val);
54 } 59 }
55 60
56 61
57 function JSONParse(text, reviver) { 62 function JSONParse(text, reviver) {
58 var unfiltered = %ParseJson(text); 63 var unfiltered = %ParseJson(text);
59 if (IS_CALLABLE(reviver)) { 64 if (IS_CALLABLE(reviver)) {
60 return Revive({'': unfiltered}, '', reviver); 65 return InternalizeJSONProperty({'': unfiltered}, '', reviver);
61 } else { 66 } else {
62 return unfiltered; 67 return unfiltered;
63 } 68 }
64 } 69 }
65 70
66 71
67 function SerializeArray(value, replacer, stack, indent, gap) { 72 function SerializeArray(value, replacer, stack, indent, gap) {
68 if (!%PushIfAbsent(stack, value)) throw MakeTypeError(kCircularStructure); 73 if (!%PushIfAbsent(stack, value)) throw MakeTypeError(kCircularStructure);
69 var stepback = indent; 74 var stepback = indent;
70 indent += gap; 75 indent += gap;
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 function JsonSerializeAdapter(key, object) { 252 function JsonSerializeAdapter(key, object) {
248 var holder = {}; 253 var holder = {};
249 holder[key] = object; 254 holder[key] = object;
250 // No need to pass the actual holder since there is no replacer function. 255 // No need to pass the actual holder since there is no replacer function.
251 return JSONSerialize(key, holder, UNDEFINED, new InternalArray(), "", ""); 256 return JSONSerialize(key, holder, UNDEFINED, new InternalArray(), "", "");
252 } 257 }
253 258
254 %InstallToContext(["json_serialize_adapter", JsonSerializeAdapter]); 259 %InstallToContext(["json_serialize_adapter", JsonSerializeAdapter]);
255 260
256 }) 261 })
OLDNEW
« no previous file with comments | « no previous file | src/js/v8natives.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698