blob: a44c3d7cd7caeb2d7eb835b002332fc8df30a679 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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"use strict";
6
7// This file relies on the fact that the following declaration has been made
8// in runtime.js:
9// var $Array = global.Array;
10
11var $WeakMap = global.WeakMap;
12var $WeakSet = global.WeakSet;
13
14
15// -------------------------------------------------------------------
16// Harmony WeakMap
17
18function WeakMapConstructor(iterable) {
19 if (!%_IsConstructCall()) {
20 throw MakeTypeError('constructor_not_function', ['WeakMap']);
21 }
22
23 var iter, adder;
24
25 if (!IS_NULL_OR_UNDEFINED(iterable)) {
26 iter = GetIterator(ToObject(iterable));
27 adder = this.set;
28 if (!IS_SPEC_FUNCTION(adder)) {
29 throw MakeTypeError('property_not_function', ['set', this]);
30 }
31 }
32
33 %WeakCollectionInitialize(this);
34
35 if (IS_UNDEFINED(iter)) return;
36
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]);
41 }
42 nextItem = next.value;
43 if (!IS_SPEC_OBJECT(nextItem)) {
44 throw MakeTypeError('iterator_value_not_an_object', [nextItem]);
45 }
46 %_CallFunction(this, nextItem[0], nextItem[1], adder);
47 }
48}
49
50
51function WeakMapGet(key) {
52 if (!IS_WEAKMAP(this)) {
53 throw MakeTypeError('incompatible_method_receiver',
54 ['WeakMap.prototype.get', this]);
55 }
56 if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
57 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
58 }
59 return %WeakCollectionGet(this, key);
60}
61
62
63function WeakMapSet(key, value) {
64 if (!IS_WEAKMAP(this)) {
65 throw MakeTypeError('incompatible_method_receiver',
66 ['WeakMap.prototype.set', this]);
67 }
68 if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
69 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
70 }
71 return %WeakCollectionSet(this, key, value);
72}
73
74
75function WeakMapHas(key) {
76 if (!IS_WEAKMAP(this)) {
77 throw MakeTypeError('incompatible_method_receiver',
78 ['WeakMap.prototype.has', this]);
79 }
80 if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
81 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
82 }
83 return %WeakCollectionHas(this, key);
84}
85
86
87function WeakMapDelete(key) {
88 if (!IS_WEAKMAP(this)) {
89 throw MakeTypeError('incompatible_method_receiver',
90 ['WeakMap.prototype.delete', this]);
91 }
92 if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
93 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
94 }
95 return %WeakCollectionDelete(this, key);
96}
97
98
Ben Murdochb8a8cc12014-11-26 15:28:44 +000099// -------------------------------------------------------------------
100
101function SetUpWeakMap() {
102 %CheckIsBootstrapping();
103
104 %SetCode($WeakMap, WeakMapConstructor);
105 %FunctionSetPrototype($WeakMap, new $Object());
106 %AddNamedProperty($WeakMap.prototype, "constructor", $WeakMap, DONT_ENUM);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400107 %AddNamedProperty(
108 $WeakMap.prototype, symbolToStringTag, "WeakMap", DONT_ENUM | READ_ONLY);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109
110 // Set up the non-enumerable functions on the WeakMap prototype object.
111 InstallFunctions($WeakMap.prototype, DONT_ENUM, $Array(
112 "get", WeakMapGet,
113 "set", WeakMapSet,
114 "has", WeakMapHas,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400115 "delete", WeakMapDelete
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000116 ));
117}
118
119SetUpWeakMap();
120
121
122// -------------------------------------------------------------------
123// Harmony WeakSet
124
125function WeakSetConstructor(iterable) {
126 if (!%_IsConstructCall()) {
127 throw MakeTypeError('constructor_not_function', ['WeakSet']);
128 }
129
130 var iter, adder;
131
132 if (!IS_NULL_OR_UNDEFINED(iterable)) {
133 iter = GetIterator(ToObject(iterable));
134 adder = this.add;
135 if (!IS_SPEC_FUNCTION(adder)) {
136 throw MakeTypeError('property_not_function', ['add', this]);
137 }
138 }
139
140 %WeakCollectionInitialize(this);
141
142 if (IS_UNDEFINED(iter)) return;
143
144 var next, done;
145 while (!(next = iter.next()).done) {
146 if (!IS_SPEC_OBJECT(next)) {
147 throw MakeTypeError('iterator_result_not_an_object', [next]);
148 }
149 %_CallFunction(this, next.value, adder);
150 }
151}
152
153
154function WeakSetAdd(value) {
155 if (!IS_WEAKSET(this)) {
156 throw MakeTypeError('incompatible_method_receiver',
157 ['WeakSet.prototype.add', this]);
158 }
159 if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
160 throw %MakeTypeError('invalid_weakset_value', [this, value]);
161 }
162 return %WeakCollectionSet(this, value, true);
163}
164
165
166function WeakSetHas(value) {
167 if (!IS_WEAKSET(this)) {
168 throw MakeTypeError('incompatible_method_receiver',
169 ['WeakSet.prototype.has', this]);
170 }
171 if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
172 throw %MakeTypeError('invalid_weakset_value', [this, value]);
173 }
174 return %WeakCollectionHas(this, value);
175}
176
177
178function WeakSetDelete(value) {
179 if (!IS_WEAKSET(this)) {
180 throw MakeTypeError('incompatible_method_receiver',
181 ['WeakSet.prototype.delete', this]);
182 }
183 if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
184 throw %MakeTypeError('invalid_weakset_value', [this, value]);
185 }
186 return %WeakCollectionDelete(this, value);
187}
188
189
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000190// -------------------------------------------------------------------
191
192function SetUpWeakSet() {
193 %CheckIsBootstrapping();
194
195 %SetCode($WeakSet, WeakSetConstructor);
196 %FunctionSetPrototype($WeakSet, new $Object());
197 %AddNamedProperty($WeakSet.prototype, "constructor", $WeakSet, DONT_ENUM);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400198 %AddNamedProperty(
199 $WeakSet.prototype, symbolToStringTag, "WeakSet", DONT_ENUM | READ_ONLY);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000200
201 // Set up the non-enumerable functions on the WeakSet prototype object.
202 InstallFunctions($WeakSet.prototype, DONT_ENUM, $Array(
203 "add", WeakSetAdd,
204 "has", WeakSetHas,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400205 "delete", WeakSetDelete
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000206 ));
207}
208
209SetUpWeakSet();