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 // -------------------------------------------------------------------
101 function SetUpWeakMap() {
102 %CheckIsBootstrapping();
104 %SetCode($WeakMap, WeakMapConstructor);
105 %FunctionSetPrototype($WeakMap, new $Object());
106 %AddNamedProperty($WeakMap.prototype, "constructor", $WeakMap, DONT_ENUM);
108 $WeakMap.prototype, symbolToStringTag, "WeakMap", DONT_ENUM | READ_ONLY);
110 // Set up the non-enumerable functions on the WeakMap prototype object.
111 InstallFunctions($WeakMap.prototype, DONT_ENUM, $Array(
115 "delete", WeakMapDelete
122 // -------------------------------------------------------------------
125 function WeakSetConstructor(iterable) {
126 if (!%_IsConstructCall()) {
127 throw MakeTypeError('constructor_not_function', ['WeakSet']);
132 if (!IS_NULL_OR_UNDEFINED(iterable)) {
133 iter = GetIterator(ToObject(iterable));
135 if (!IS_SPEC_FUNCTION(adder)) {
136 throw MakeTypeError('property_not_function', ['add', this]);
140 %WeakCollectionInitialize(this);
142 if (IS_UNDEFINED(iter)) return;
145 while (!(next = iter.next()).done) {
146 if (!IS_SPEC_OBJECT(next)) {
147 throw MakeTypeError('iterator_result_not_an_object', [next]);
149 %_CallFunction(this, next.value, adder);
154 function WeakSetAdd(value) {
155 if (!IS_WEAKSET(this)) {
156 throw MakeTypeError('incompatible_method_receiver',
157 ['WeakSet.prototype.add', this]);
159 if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
160 throw %MakeTypeError('invalid_weakset_value', [this, value]);
162 return %WeakCollectionSet(this, value, true);
166 function WeakSetHas(value) {
167 if (!IS_WEAKSET(this)) {
168 throw MakeTypeError('incompatible_method_receiver',
169 ['WeakSet.prototype.has', this]);
171 if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
172 throw %MakeTypeError('invalid_weakset_value', [this, value]);
174 return %WeakCollectionHas(this, value);
178 function WeakSetDelete(value) {
179 if (!IS_WEAKSET(this)) {
180 throw MakeTypeError('incompatible_method_receiver',
181 ['WeakSet.prototype.delete', this]);
183 if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
184 throw %MakeTypeError('invalid_weakset_value', [this, value]);
186 return %WeakCollectionDelete(this, value);
190 // -------------------------------------------------------------------
192 function SetUpWeakSet() {
193 %CheckIsBootstrapping();
195 %SetCode($WeakSet, WeakSetConstructor);
196 %FunctionSetPrototype($WeakSet, new $Object());
197 %AddNamedProperty($WeakSet.prototype, "constructor", $WeakSet, DONT_ENUM);
199 $WeakSet.prototype, symbolToStringTag, "WeakSet", DONT_ENUM | READ_ONLY);
201 // Set up the non-enumerable functions on the WeakSet prototype object.
202 InstallFunctions($WeakSet.prototype, DONT_ENUM, $Array(
205 "delete", WeakSetDelete