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 // Set up the non-enumerable functions on the WeakMap prototype object.
119 InstallFunctions($WeakMap.prototype, DONT_ENUM, $Array(
123 "delete", WeakMapDelete,
124 "clear", WeakMapClear
131 // -------------------------------------------------------------------
134 function WeakSetConstructor(iterable) {
135 if (!%_IsConstructCall()) {
136 throw MakeTypeError('constructor_not_function', ['WeakSet']);
141 if (!IS_NULL_OR_UNDEFINED(iterable)) {
142 iter = GetIterator(ToObject(iterable));
144 if (!IS_SPEC_FUNCTION(adder)) {
145 throw MakeTypeError('property_not_function', ['add', this]);
149 %WeakCollectionInitialize(this);
151 if (IS_UNDEFINED(iter)) return;
154 while (!(next = iter.next()).done) {
155 if (!IS_SPEC_OBJECT(next)) {
156 throw MakeTypeError('iterator_result_not_an_object', [next]);
158 %_CallFunction(this, next.value, adder);
163 function WeakSetAdd(value) {
164 if (!IS_WEAKSET(this)) {
165 throw MakeTypeError('incompatible_method_receiver',
166 ['WeakSet.prototype.add', this]);
168 if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
169 throw %MakeTypeError('invalid_weakset_value', [this, value]);
171 return %WeakCollectionSet(this, value, true);
175 function WeakSetHas(value) {
176 if (!IS_WEAKSET(this)) {
177 throw MakeTypeError('incompatible_method_receiver',
178 ['WeakSet.prototype.has', this]);
180 if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
181 throw %MakeTypeError('invalid_weakset_value', [this, value]);
183 return %WeakCollectionHas(this, value);
187 function WeakSetDelete(value) {
188 if (!IS_WEAKSET(this)) {
189 throw MakeTypeError('incompatible_method_receiver',
190 ['WeakSet.prototype.delete', this]);
192 if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
193 throw %MakeTypeError('invalid_weakset_value', [this, value]);
195 return %WeakCollectionDelete(this, value);
199 function WeakSetClear() {
200 if (!IS_WEAKSET(this)) {
201 throw MakeTypeError('incompatible_method_receiver',
202 ['WeakSet.prototype.clear', this]);
204 // Replace the internal table with a new empty table.
205 %WeakCollectionInitialize(this);
209 // -------------------------------------------------------------------
211 function SetUpWeakSet() {
212 %CheckIsBootstrapping();
214 %SetCode($WeakSet, WeakSetConstructor);
215 %FunctionSetPrototype($WeakSet, new $Object());
216 %AddNamedProperty($WeakSet.prototype, "constructor", $WeakSet, DONT_ENUM);
218 // Set up the non-enumerable functions on the WeakSet prototype object.
219 InstallFunctions($WeakSet.prototype, DONT_ENUM, $Array(
222 "delete", WeakSetDelete,
223 "clear", WeakSetClear