blob: b3c2db72d792732be2ae8f8caf9554df8c34ffaa [file] [log] [blame]
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00001// Copyright 2012 the V8 project authors. All rights reserved.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +000028"use strict";
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000029
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +000030var $Set = global.Set;
31var $Map = global.Map;
32var $WeakMap = global.WeakMap;
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000033
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000034//-------------------------------------------------------------------
35
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000036// Global sentinel to be used instead of undefined keys, which are not
37// supported internally but required for Harmony sets and maps.
38var undefined_sentinel = {};
39
40
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000041function SetConstructor() {
42 if (%_IsConstructCall()) {
43 %SetInitialize(this);
44 } else {
45 return new $Set();
46 }
47}
48
49
50function SetAdd(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +000051 if (!IS_SET(this)) {
52 throw MakeTypeError('incompatible_method_receiver',
53 ['Set.prototype.add', this]);
54 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000055 if (IS_UNDEFINED(key)) {
56 key = undefined_sentinel;
57 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000058 return %SetAdd(this, key);
59}
60
61
62function SetHas(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +000063 if (!IS_SET(this)) {
64 throw MakeTypeError('incompatible_method_receiver',
65 ['Set.prototype.has', this]);
66 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000067 if (IS_UNDEFINED(key)) {
68 key = undefined_sentinel;
69 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000070 return %SetHas(this, key);
71}
72
73
74function SetDelete(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +000075 if (!IS_SET(this)) {
76 throw MakeTypeError('incompatible_method_receiver',
77 ['Set.prototype.delete', this]);
78 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000079 if (IS_UNDEFINED(key)) {
80 key = undefined_sentinel;
81 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000082 if (%SetHas(this, key)) {
83 %SetDelete(this, key);
84 return true;
85 } else {
86 return false;
87 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000088}
89
90
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +000091function SetGetSize() {
92 if (!IS_SET(this)) {
93 throw MakeTypeError('incompatible_method_receiver',
94 ['Set.prototype.size', this]);
95 }
96 return %SetGetSize(this);
97}
98
99
100function SetClear() {
101 if (!IS_SET(this)) {
102 throw MakeTypeError('incompatible_method_receiver',
103 ['Set.prototype.clear', this]);
104 }
105 // Replace the internal table with a new empty table.
106 %SetInitialize(this);
107}
108
109
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000110function MapConstructor() {
111 if (%_IsConstructCall()) {
112 %MapInitialize(this);
113 } else {
114 return new $Map();
115 }
116}
117
118
119function MapGet(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000120 if (!IS_MAP(this)) {
121 throw MakeTypeError('incompatible_method_receiver',
122 ['Map.prototype.get', this]);
123 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000124 if (IS_UNDEFINED(key)) {
125 key = undefined_sentinel;
126 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000127 return %MapGet(this, key);
128}
129
130
131function MapSet(key, value) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000132 if (!IS_MAP(this)) {
133 throw MakeTypeError('incompatible_method_receiver',
134 ['Map.prototype.set', this]);
135 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000136 if (IS_UNDEFINED(key)) {
137 key = undefined_sentinel;
138 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000139 return %MapSet(this, key, value);
140}
141
142
143function MapHas(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000144 if (!IS_MAP(this)) {
145 throw MakeTypeError('incompatible_method_receiver',
146 ['Map.prototype.has', this]);
147 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000148 if (IS_UNDEFINED(key)) {
149 key = undefined_sentinel;
150 }
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000151 return %MapHas(this, key);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000152}
153
154
155function MapDelete(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000156 if (!IS_MAP(this)) {
157 throw MakeTypeError('incompatible_method_receiver',
158 ['Map.prototype.delete', this]);
159 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000160 if (IS_UNDEFINED(key)) {
161 key = undefined_sentinel;
162 }
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000163 return %MapDelete(this, key);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000164}
165
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000166
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000167function MapGetSize() {
168 if (!IS_MAP(this)) {
169 throw MakeTypeError('incompatible_method_receiver',
170 ['Map.prototype.size', this]);
171 }
172 return %MapGetSize(this);
173}
174
175
176function MapClear() {
177 if (!IS_MAP(this)) {
178 throw MakeTypeError('incompatible_method_receiver',
179 ['Map.prototype.clear', this]);
180 }
181 // Replace the internal table with a new empty table.
182 %MapInitialize(this);
183}
184
185
danno@chromium.orgb6451162011-08-17 14:33:23 +0000186function WeakMapConstructor() {
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000187 if (%_IsConstructCall()) {
188 %WeakMapInitialize(this);
189 } else {
190 return new $WeakMap();
191 }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000192}
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000193
194
195function WeakMapGet(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000196 if (!IS_WEAKMAP(this)) {
197 throw MakeTypeError('incompatible_method_receiver',
198 ['WeakMap.prototype.get', this]);
199 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000200 if (!IS_SPEC_OBJECT(key)) {
201 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
202 }
203 return %WeakMapGet(this, key);
204}
205
206
207function WeakMapSet(key, value) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000208 if (!IS_WEAKMAP(this)) {
209 throw MakeTypeError('incompatible_method_receiver',
210 ['WeakMap.prototype.set', this]);
211 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000212 if (!IS_SPEC_OBJECT(key)) {
213 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
214 }
215 return %WeakMapSet(this, key, value);
216}
217
218
219function WeakMapHas(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000220 if (!IS_WEAKMAP(this)) {
221 throw MakeTypeError('incompatible_method_receiver',
222 ['WeakMap.prototype.has', this]);
223 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000224 if (!IS_SPEC_OBJECT(key)) {
225 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
226 }
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000227 return %WeakMapHas(this, key);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000228}
229
230
231function WeakMapDelete(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000232 if (!IS_WEAKMAP(this)) {
233 throw MakeTypeError('incompatible_method_receiver',
234 ['WeakMap.prototype.delete', this]);
235 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000236 if (!IS_SPEC_OBJECT(key)) {
237 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
238 }
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000239 return %WeakMapDelete(this, key);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000240}
241
242// -------------------------------------------------------------------
243
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000244(function () {
245 %CheckIsBootstrapping();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000246
247 // Set up the Set and Map constructor function.
248 %SetCode($Set, SetConstructor);
249 %SetCode($Map, MapConstructor);
250
251 // Set up the constructor property on the Set and Map prototype object.
252 %SetProperty($Set.prototype, "constructor", $Set, DONT_ENUM);
253 %SetProperty($Map.prototype, "constructor", $Map, DONT_ENUM);
254
255 // Set up the non-enumerable functions on the Set prototype object.
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000256 InstallGetter($Set.prototype, "size", SetGetSize);
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000257 InstallFunctions($Set.prototype, DONT_ENUM, $Array(
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000258 "add", SetAdd,
259 "has", SetHas,
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000260 "delete", SetDelete,
261 "clear", SetClear
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000262 ));
263
264 // Set up the non-enumerable functions on the Map prototype object.
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000265 InstallGetter($Map.prototype, "size", MapGetSize);
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000266 InstallFunctions($Map.prototype, DONT_ENUM, $Array(
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000267 "get", MapGet,
268 "set", MapSet,
269 "has", MapHas,
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000270 "delete", MapDelete,
271 "clear", MapClear
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000272 ));
273
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000274 // Set up the WeakMap constructor function.
danno@chromium.orgb6451162011-08-17 14:33:23 +0000275 %SetCode($WeakMap, WeakMapConstructor);
276
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000277 // Set up the constructor property on the WeakMap prototype object.
278 %SetProperty($WeakMap.prototype, "constructor", $WeakMap, DONT_ENUM);
279
280 // Set up the non-enumerable functions on the WeakMap prototype object.
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000281 InstallFunctions($WeakMap.prototype, DONT_ENUM, $Array(
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000282 "get", WeakMapGet,
283 "set", WeakMapSet,
284 "has", WeakMapHas,
285 "delete", WeakMapDelete
286 ));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000287})();