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']);
23 %WeakCollectionInitialize(this);
25 if (!IS_NULL_OR_UNDEFINED(iterable)) {
27 if (!IS_SPEC_FUNCTION(adder)) {
28 throw MakeTypeError('property_not_function', ['set', this]);
30 for (var nextItem of iterable) {
31 if (!IS_SPEC_OBJECT(nextItem)) {
32 throw MakeTypeError('iterator_value_not_an_object', [nextItem]);
34 %_CallFunction(this, nextItem[0], nextItem[1], adder);
40 function WeakMapGet(key) {
41 if (!IS_WEAKMAP(this)) {
42 throw MakeTypeError('incompatible_method_receiver',
43 ['WeakMap.prototype.get', this]);
45 if (!IS_SPEC_OBJECT(key)) return UNDEFINED;
46 return %WeakCollectionGet(this, key);
50 function WeakMapSet(key, value) {
51 if (!IS_WEAKMAP(this)) {
52 throw MakeTypeError('incompatible_method_receiver',
53 ['WeakMap.prototype.set', this]);
55 if (!IS_SPEC_OBJECT(key)) {
56 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
58 return %WeakCollectionSet(this, key, value);
62 function WeakMapHas(key) {
63 if (!IS_WEAKMAP(this)) {
64 throw MakeTypeError('incompatible_method_receiver',
65 ['WeakMap.prototype.has', this]);
67 if (!IS_SPEC_OBJECT(key)) return false;
68 return %WeakCollectionHas(this, key);
72 function WeakMapDelete(key) {
73 if (!IS_WEAKMAP(this)) {
74 throw MakeTypeError('incompatible_method_receiver',
75 ['WeakMap.prototype.delete', this]);
77 if (!IS_SPEC_OBJECT(key)) return false;
78 return %WeakCollectionDelete(this, key);
82 // -------------------------------------------------------------------
84 function SetUpWeakMap() {
85 %CheckIsBootstrapping();
87 %SetCode($WeakMap, WeakMapConstructor);
88 %FunctionSetPrototype($WeakMap, new $Object());
89 %AddNamedProperty($WeakMap.prototype, "constructor", $WeakMap, DONT_ENUM);
91 $WeakMap.prototype, symbolToStringTag, "WeakMap", DONT_ENUM | READ_ONLY);
93 // Set up the non-enumerable functions on the WeakMap prototype object.
94 InstallFunctions($WeakMap.prototype, DONT_ENUM, $Array(
98 "delete", WeakMapDelete
105 // -------------------------------------------------------------------
108 function WeakSetConstructor(iterable) {
109 if (!%_IsConstructCall()) {
110 throw MakeTypeError('constructor_not_function', ['WeakSet']);
113 %WeakCollectionInitialize(this);
115 if (!IS_NULL_OR_UNDEFINED(iterable)) {
116 var adder = this.add;
117 if (!IS_SPEC_FUNCTION(adder)) {
118 throw MakeTypeError('property_not_function', ['add', this]);
120 for (var value of iterable) {
121 %_CallFunction(this, value, adder);
127 function WeakSetAdd(value) {
128 if (!IS_WEAKSET(this)) {
129 throw MakeTypeError('incompatible_method_receiver',
130 ['WeakSet.prototype.add', this]);
132 if (!IS_SPEC_OBJECT(value)) {
133 throw %MakeTypeError('invalid_weakset_value', [this, value]);
135 return %WeakCollectionSet(this, value, true);
139 function WeakSetHas(value) {
140 if (!IS_WEAKSET(this)) {
141 throw MakeTypeError('incompatible_method_receiver',
142 ['WeakSet.prototype.has', this]);
144 if (!IS_SPEC_OBJECT(value)) return false;
145 return %WeakCollectionHas(this, value);
149 function WeakSetDelete(value) {
150 if (!IS_WEAKSET(this)) {
151 throw MakeTypeError('incompatible_method_receiver',
152 ['WeakSet.prototype.delete', this]);
154 if (!IS_SPEC_OBJECT(value)) return false;
155 return %WeakCollectionDelete(this, value);
159 // -------------------------------------------------------------------
161 function SetUpWeakSet() {
162 %CheckIsBootstrapping();
164 %SetCode($WeakSet, WeakSetConstructor);
165 %FunctionSetPrototype($WeakSet, new $Object());
166 %AddNamedProperty($WeakSet.prototype, "constructor", $WeakSet, DONT_ENUM);
168 $WeakSet.prototype, symbolToStringTag, "WeakSet", DONT_ENUM | READ_ONLY);
170 // Set up the non-enumerable functions on the WeakSet prototype object.
171 InstallFunctions($WeakSet.prototype, DONT_ENUM, $Array(
174 "delete", WeakSetDelete