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 |
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 toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); | 18 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); |
19 | 19 |
20 // ------------------------------------------------------------------- | 20 // ------------------------------------------------------------------- |
21 | 21 |
22 function CreateDataProperty(o, p, v) { | |
23 var desc = {value: v, enumerable: true, writable: true, configurable: true}; | |
24 return %reflect_define_property(o, p, desc); | |
25 } | |
26 | |
27 | |
28 function InternalizeJSONProperty(holder, name, reviver) { | |
29 var val = holder[name]; | |
30 if (IS_RECEIVER(val)) { | |
31 if (%is_arraylike(val)) { | |
32 var length = TO_LENGTH(val.length); | |
33 for (var i = 0; i < length; i++) { | |
34 var newElement = | |
35 InternalizeJSONProperty(val, %_NumberToString(i), reviver); | |
36 if (IS_UNDEFINED(newElement)) { | |
37 %reflect_delete_property(val, i); | |
38 } else { | |
39 CreateDataProperty(val, i, newElement); | |
40 } | |
41 } | |
42 } else { | |
43 var keys = %object_keys(val); | |
44 for (var i = 0; i < keys.length; i++) { | |
45 var p = keys[i]; | |
46 var newElement = InternalizeJSONProperty(val, p, reviver); | |
47 if (IS_UNDEFINED(newElement)) { | |
48 %reflect_delete_property(val, p); | |
49 } else { | |
50 CreateDataProperty(val, p, newElement); | |
51 } | |
52 } | |
53 } | |
54 } | |
55 return %_Call(reviver, holder, name, val); | |
56 } | |
57 | |
58 | |
59 function JSONParse(text, reviver) { | 22 function JSONParse(text, reviver) { |
60 var unfiltered = %ParseJson(text); | 23 return %ParseJson(text, reviver); |
61 if (IS_CALLABLE(reviver)) { | |
62 return InternalizeJSONProperty({'': unfiltered}, '', reviver); | |
63 } else { | |
64 return unfiltered; | |
65 } | |
66 } | 24 } |
67 | 25 |
68 // ------------------------------------------------------------------- | 26 // ------------------------------------------------------------------- |
69 | 27 |
70 %AddNamedProperty(GlobalJSON, toStringTagSymbol, "JSON", READ_ONLY | DONT_ENUM); | 28 %AddNamedProperty(GlobalJSON, toStringTagSymbol, "JSON", READ_ONLY | DONT_ENUM); |
71 | 29 |
72 // Set up non-enumerable properties of the JSON object. | 30 // Set up non-enumerable properties of the JSON object. |
73 utils.InstallFunctions(GlobalJSON, DONT_ENUM, [ | 31 utils.InstallFunctions(GlobalJSON, DONT_ENUM, [ |
74 "parse", JSONParse, | 32 "parse", JSONParse, |
75 ]); | 33 ]); |
(...skipping 10 matching lines...) Expand all Loading... |
86 } | 44 } |
87 return o.toISOString(); | 45 return o.toISOString(); |
88 } | 46 } |
89 | 47 |
90 // Set up non-enumerable functions of the Date prototype object. | 48 // Set up non-enumerable functions of the Date prototype object. |
91 utils.InstallFunctions(GlobalDate.prototype, DONT_ENUM, [ | 49 utils.InstallFunctions(GlobalDate.prototype, DONT_ENUM, [ |
92 "toJSON", DateToJSON | 50 "toJSON", DateToJSON |
93 ]); | 51 ]); |
94 | 52 |
95 }) | 53 }) |
OLD | NEW |