OLD | NEW |
| (Empty) |
1 // Copyright 2012 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 (function(global, utils) { | |
6 | |
7 "use strict"; | |
8 | |
9 %CheckIsBootstrapping(); | |
10 | |
11 var GlobalObject = global.Object; | |
12 var GlobalWeakMap = global.WeakMap; | |
13 var GlobalWeakSet = global.WeakSet; | |
14 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); | |
15 | |
16 // ------------------------------------------------------------------- | |
17 // Harmony WeakMap | |
18 | |
19 function WeakMapConstructor(iterable) { | |
20 if (!%_IsConstructCall()) { | |
21 throw MakeTypeError(kConstructorNotFunction, "WeakMap"); | |
22 } | |
23 | |
24 %WeakCollectionInitialize(this); | |
25 | |
26 if (!IS_NULL_OR_UNDEFINED(iterable)) { | |
27 var adder = this.set; | |
28 if (!IS_CALLABLE(adder)) { | |
29 throw MakeTypeError(kPropertyNotFunction, 'set', this); | |
30 } | |
31 for (var nextItem of iterable) { | |
32 if (!IS_SPEC_OBJECT(nextItem)) { | |
33 throw MakeTypeError(kIteratorValueNotAnObject, nextItem); | |
34 } | |
35 %_Call(adder, this, nextItem[0], nextItem[1]); | |
36 } | |
37 } | |
38 } | |
39 | |
40 | |
41 function WeakMapGet(key) { | |
42 if (!IS_WEAKMAP(this)) { | |
43 throw MakeTypeError(kIncompatibleMethodReceiver, | |
44 'WeakMap.prototype.get', this); | |
45 } | |
46 if (!IS_SPEC_OBJECT(key)) return UNDEFINED; | |
47 var hash = $getExistingHash(key); | |
48 if (IS_UNDEFINED(hash)) return UNDEFINED; | |
49 return %WeakCollectionGet(this, key, hash); | |
50 } | |
51 | |
52 | |
53 function WeakMapSet(key, value) { | |
54 if (!IS_WEAKMAP(this)) { | |
55 throw MakeTypeError(kIncompatibleMethodReceiver, | |
56 'WeakMap.prototype.set', this); | |
57 } | |
58 if (!IS_SPEC_OBJECT(key)) throw MakeTypeError(kInvalidWeakMapKey); | |
59 return %WeakCollectionSet(this, key, value, $getHash(key)); | |
60 } | |
61 | |
62 | |
63 function WeakMapHas(key) { | |
64 if (!IS_WEAKMAP(this)) { | |
65 throw MakeTypeError(kIncompatibleMethodReceiver, | |
66 'WeakMap.prototype.has', this); | |
67 } | |
68 if (!IS_SPEC_OBJECT(key)) return false; | |
69 var hash = $getExistingHash(key); | |
70 if (IS_UNDEFINED(hash)) return false; | |
71 return %WeakCollectionHas(this, key, hash); | |
72 } | |
73 | |
74 | |
75 function WeakMapDelete(key) { | |
76 if (!IS_WEAKMAP(this)) { | |
77 throw MakeTypeError(kIncompatibleMethodReceiver, | |
78 'WeakMap.prototype.delete', this); | |
79 } | |
80 if (!IS_SPEC_OBJECT(key)) return false; | |
81 var hash = $getExistingHash(key); | |
82 if (IS_UNDEFINED(hash)) return false; | |
83 return %WeakCollectionDelete(this, key, hash); | |
84 } | |
85 | |
86 | |
87 // ------------------------------------------------------------------- | |
88 | |
89 %SetCode(GlobalWeakMap, WeakMapConstructor); | |
90 %FunctionSetLength(GlobalWeakMap, 0); | |
91 %FunctionSetPrototype(GlobalWeakMap, new GlobalObject()); | |
92 %AddNamedProperty(GlobalWeakMap.prototype, "constructor", GlobalWeakMap, | |
93 DONT_ENUM); | |
94 %AddNamedProperty(GlobalWeakMap.prototype, toStringTagSymbol, "WeakMap", | |
95 DONT_ENUM | READ_ONLY); | |
96 | |
97 // Set up the non-enumerable functions on the WeakMap prototype object. | |
98 utils.InstallFunctions(GlobalWeakMap.prototype, DONT_ENUM, [ | |
99 "get", WeakMapGet, | |
100 "set", WeakMapSet, | |
101 "has", WeakMapHas, | |
102 "delete", WeakMapDelete | |
103 ]); | |
104 | |
105 // ------------------------------------------------------------------- | |
106 // Harmony WeakSet | |
107 | |
108 function WeakSetConstructor(iterable) { | |
109 if (!%_IsConstructCall()) { | |
110 throw MakeTypeError(kConstructorNotFunction, "WeakSet"); | |
111 } | |
112 | |
113 %WeakCollectionInitialize(this); | |
114 | |
115 if (!IS_NULL_OR_UNDEFINED(iterable)) { | |
116 var adder = this.add; | |
117 if (!IS_CALLABLE(adder)) { | |
118 throw MakeTypeError(kPropertyNotFunction, 'add', this); | |
119 } | |
120 for (var value of iterable) { | |
121 %_Call(adder, this, value); | |
122 } | |
123 } | |
124 } | |
125 | |
126 | |
127 function WeakSetAdd(value) { | |
128 if (!IS_WEAKSET(this)) { | |
129 throw MakeTypeError(kIncompatibleMethodReceiver, | |
130 'WeakSet.prototype.add', this); | |
131 } | |
132 if (!IS_SPEC_OBJECT(value)) throw MakeTypeError(kInvalidWeakSetValue); | |
133 return %WeakCollectionSet(this, value, true, $getHash(value)); | |
134 } | |
135 | |
136 | |
137 function WeakSetHas(value) { | |
138 if (!IS_WEAKSET(this)) { | |
139 throw MakeTypeError(kIncompatibleMethodReceiver, | |
140 'WeakSet.prototype.has', this); | |
141 } | |
142 if (!IS_SPEC_OBJECT(value)) return false; | |
143 var hash = $getExistingHash(value); | |
144 if (IS_UNDEFINED(hash)) return false; | |
145 return %WeakCollectionHas(this, value, hash); | |
146 } | |
147 | |
148 | |
149 function WeakSetDelete(value) { | |
150 if (!IS_WEAKSET(this)) { | |
151 throw MakeTypeError(kIncompatibleMethodReceiver, | |
152 'WeakSet.prototype.delete', this); | |
153 } | |
154 if (!IS_SPEC_OBJECT(value)) return false; | |
155 var hash = $getExistingHash(value); | |
156 if (IS_UNDEFINED(hash)) return false; | |
157 return %WeakCollectionDelete(this, value, hash); | |
158 } | |
159 | |
160 | |
161 // ------------------------------------------------------------------- | |
162 | |
163 %SetCode(GlobalWeakSet, WeakSetConstructor); | |
164 %FunctionSetLength(GlobalWeakSet, 0); | |
165 %FunctionSetPrototype(GlobalWeakSet, new GlobalObject()); | |
166 %AddNamedProperty(GlobalWeakSet.prototype, "constructor", GlobalWeakSet, | |
167 DONT_ENUM); | |
168 %AddNamedProperty(GlobalWeakSet.prototype, toStringTagSymbol, "WeakSet", | |
169 DONT_ENUM | READ_ONLY); | |
170 | |
171 // Set up the non-enumerable functions on the WeakSet prototype object. | |
172 utils.InstallFunctions(GlobalWeakSet.prototype, DONT_ENUM, [ | |
173 "add", WeakSetAdd, | |
174 "has", WeakSetHas, | |
175 "delete", WeakSetDelete | |
176 ]); | |
177 | |
178 }) | |
OLD | NEW |