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.
7 // This file relies on the fact that the following declaration has been made
9 // var $Array = global.Array;
11 var $WeakMap = global.WeakMap;
12 var $WeakSet = global.WeakSet;
15 // -------------------------------------------------------------------
18 function WeakMapConstructor(iterable) {
19 if (!%_IsConstructCall()) {
20 throw MakeTypeError('constructor_not_function', ['WeakMap']);
25 if (!IS_NULL_OR_UNDEFINED(iterable)) {
26 iter = GetIterator(ToObject(iterable));
28 if (!IS_SPEC_FUNCTION(adder)) {
29 throw MakeTypeError('property_not_function', ['set', this]);
33 %WeakCollectionInitialize(this);
35 if (IS_UNDEFINED(iter)) return;
37 var next, done, nextItem;
38 while (!(next = iter.next()).done) {
39 if (!IS_SPEC_OBJECT(next)) {
40 throw MakeTypeError('iterator_result_not_an_object', [next]);
42 nextItem = next.value;
43 if (!IS_SPEC_OBJECT(nextItem)) {
44 throw MakeTypeError('iterator_value_not_an_object', [nextItem]);
46 %_CallFunction(this, nextItem[0], nextItem[1], adder);
51 function WeakMapGet(key) {
52 if (!IS_WEAKMAP(this)) {
53 throw MakeTypeError('incompatible_method_receiver',
54 ['WeakMap.prototype.get', this]);
56 if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
57 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
59 return %WeakCollectionGet(this, key);
63 function WeakMapSet(key, value) {
64 if (!IS_WEAKMAP(this)) {
65 throw MakeTypeError('incompatible_method_receiver',
66 ['WeakMap.prototype.set', this]);
68 if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
69 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
71 return %WeakCollectionSet(this, key, value);
75 function WeakMapHas(key) {
76 if (!IS_WEAKMAP(this)) {
77 throw MakeTypeError('incompatible_method_receiver',
78 ['WeakMap.prototype.has', this]);
80 if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
81 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
83 return %WeakCollectionHas(this, key);
87 function WeakMapDelete(key) {
88 if (!IS_WEAKMAP(this)) {
89 throw MakeTypeError('incompatible_method_receiver',
90 ['WeakMap.prototype.delete', this]);
92 if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
93 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
95 return %WeakCollectionDelete(this, key);
99 function WeakMapClear() {
100 if (!IS_WEAKMAP(this)) {
101 throw MakeTypeError('incompatible_method_receiver',
102 ['WeakMap.prototype.clear', this]);
104 // Replace the internal table with a new empty table.
105 %WeakCollectionInitialize(this);
109 // -------------------------------------------------------------------
111 function SetUpWeakMap() {
112 %CheckIsBootstrapping();
114 %SetCode($WeakMap, WeakMapConstructor);
115 %FunctionSetPrototype($WeakMap, new $Object());
116 %AddNamedProperty($WeakMap.prototype, "constructor", $WeakMap, DONT_ENUM);
118 $WeakMap.prototype, symbolToStringTag, "WeakMap", DONT_ENUM | READ_ONLY);
120 // Set up the non-enumerable functions on the WeakMap prototype object.
121 InstallFunctions($WeakMap.prototype, DONT_ENUM, $Array(
125 "delete", WeakMapDelete,
126 "clear", WeakMapClear
133 // -------------------------------------------------------------------
136 function WeakSetConstructor(iterable) {
137 if (!%_IsConstructCall()) {
138 throw MakeTypeError('constructor_not_function', ['WeakSet']);
143 if (!IS_NULL_OR_UNDEFINED(iterable)) {
144 iter = GetIterator(ToObject(iterable));
146 if (!IS_SPEC_FUNCTION(adder)) {
147 throw MakeTypeError('property_not_function', ['add', this]);
151 %WeakCollectionInitialize(this);
153 if (IS_UNDEFINED(iter)) return;
156 while (!(next = iter.next()).done) {
157 if (!IS_SPEC_OBJECT(next)) {
158 throw MakeTypeError('iterator_result_not_an_object', [next]);
160 %_CallFunction(this, next.value, adder);
165 function WeakSetAdd(value) {
166 if (!IS_WEAKSET(this)) {
167 throw MakeTypeError('incompatible_method_receiver',
168 ['WeakSet.prototype.add', this]);
170 if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
171 throw %MakeTypeError('invalid_weakset_value', [this, value]);
173 return %WeakCollectionSet(this, value, true);
177 function WeakSetHas(value) {
178 if (!IS_WEAKSET(this)) {
179 throw MakeTypeError('incompatible_method_receiver',
180 ['WeakSet.prototype.has', this]);
182 if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
183 throw %MakeTypeError('invalid_weakset_value', [this, value]);
185 return %WeakCollectionHas(this, value);
189 function WeakSetDelete(value) {
190 if (!IS_WEAKSET(this)) {
191 throw MakeTypeError('incompatible_method_receiver',
192 ['WeakSet.prototype.delete', this]);
194 if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
195 throw %MakeTypeError('invalid_weakset_value', [this, value]);
197 return %WeakCollectionDelete(this, value);
201 function WeakSetClear() {
202 if (!IS_WEAKSET(this)) {
203 throw MakeTypeError('incompatible_method_receiver',
204 ['WeakSet.prototype.clear', this]);
206 // Replace the internal table with a new empty table.
207 %WeakCollectionInitialize(this);
211 // -------------------------------------------------------------------
213 function SetUpWeakSet() {
214 %CheckIsBootstrapping();
216 %SetCode($WeakSet, WeakSetConstructor);
217 %FunctionSetPrototype($WeakSet, new $Object());
218 %AddNamedProperty($WeakSet.prototype, "constructor", $WeakSet, DONT_ENUM);
220 $WeakSet.prototype, symbolToStringTag, "WeakSet", DONT_ENUM | READ_ONLY);
222 // Set up the non-enumerable functions on the WeakSet prototype object.
223 InstallFunctions($WeakSet.prototype, DONT_ENUM, $Array(
226 "delete", WeakSetDelete,
227 "clear", WeakSetClear