blob: d36fe18fa00dcd5fdae1210e5f9470c53cbc41f3 [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
91function MapConstructor() {
92 if (%_IsConstructCall()) {
93 %MapInitialize(this);
94 } else {
95 return new $Map();
96 }
97}
98
99
100function MapGet(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000101 if (!IS_MAP(this)) {
102 throw MakeTypeError('incompatible_method_receiver',
103 ['Map.prototype.get', this]);
104 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000105 if (IS_UNDEFINED(key)) {
106 key = undefined_sentinel;
107 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000108 return %MapGet(this, key);
109}
110
111
112function MapSet(key, value) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000113 if (!IS_MAP(this)) {
114 throw MakeTypeError('incompatible_method_receiver',
115 ['Map.prototype.set', this]);
116 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000117 if (IS_UNDEFINED(key)) {
118 key = undefined_sentinel;
119 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000120 return %MapSet(this, key, value);
121}
122
123
124function MapHas(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000125 if (!IS_MAP(this)) {
126 throw MakeTypeError('incompatible_method_receiver',
127 ['Map.prototype.has', this]);
128 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000129 if (IS_UNDEFINED(key)) {
130 key = undefined_sentinel;
131 }
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000132 return %MapHas(this, key);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000133}
134
135
136function MapDelete(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000137 if (!IS_MAP(this)) {
138 throw MakeTypeError('incompatible_method_receiver',
139 ['Map.prototype.delete', this]);
140 }
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000141 if (IS_UNDEFINED(key)) {
142 key = undefined_sentinel;
143 }
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000144 return %MapDelete(this, key);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000145}
146
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000147
danno@chromium.orgb6451162011-08-17 14:33:23 +0000148function WeakMapConstructor() {
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000149 if (%_IsConstructCall()) {
150 %WeakMapInitialize(this);
151 } else {
152 return new $WeakMap();
153 }
danno@chromium.orgb6451162011-08-17 14:33:23 +0000154}
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000155
156
157function WeakMapGet(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000158 if (!IS_WEAKMAP(this)) {
159 throw MakeTypeError('incompatible_method_receiver',
160 ['WeakMap.prototype.get', this]);
161 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000162 if (!IS_SPEC_OBJECT(key)) {
163 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
164 }
165 return %WeakMapGet(this, key);
166}
167
168
169function WeakMapSet(key, value) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000170 if (!IS_WEAKMAP(this)) {
171 throw MakeTypeError('incompatible_method_receiver',
172 ['WeakMap.prototype.set', this]);
173 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000174 if (!IS_SPEC_OBJECT(key)) {
175 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
176 }
177 return %WeakMapSet(this, key, value);
178}
179
180
181function WeakMapHas(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000182 if (!IS_WEAKMAP(this)) {
183 throw MakeTypeError('incompatible_method_receiver',
184 ['WeakMap.prototype.has', this]);
185 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000186 if (!IS_SPEC_OBJECT(key)) {
187 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
188 }
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000189 return %WeakMapHas(this, key);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000190}
191
192
193function WeakMapDelete(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000194 if (!IS_WEAKMAP(this)) {
195 throw MakeTypeError('incompatible_method_receiver',
196 ['WeakMap.prototype.delete', this]);
197 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000198 if (!IS_SPEC_OBJECT(key)) {
199 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
200 }
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +0000201 return %WeakMapDelete(this, key);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000202}
203
204// -------------------------------------------------------------------
205
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000206(function () {
207 %CheckIsBootstrapping();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000208
209 // Set up the Set and Map constructor function.
210 %SetCode($Set, SetConstructor);
211 %SetCode($Map, MapConstructor);
212
213 // Set up the constructor property on the Set and Map prototype object.
214 %SetProperty($Set.prototype, "constructor", $Set, DONT_ENUM);
215 %SetProperty($Map.prototype, "constructor", $Map, DONT_ENUM);
216
217 // Set up the non-enumerable functions on the Set prototype object.
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000218 InstallFunctions($Set.prototype, DONT_ENUM, $Array(
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000219 "add", SetAdd,
220 "has", SetHas,
221 "delete", SetDelete
222 ));
223
224 // Set up the non-enumerable functions on the Map prototype object.
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000225 InstallFunctions($Map.prototype, DONT_ENUM, $Array(
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000226 "get", MapGet,
227 "set", MapSet,
228 "has", MapHas,
229 "delete", MapDelete
230 ));
231
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000232 // Set up the WeakMap constructor function.
danno@chromium.orgb6451162011-08-17 14:33:23 +0000233 %SetCode($WeakMap, WeakMapConstructor);
234
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000235 // Set up the constructor property on the WeakMap prototype object.
236 %SetProperty($WeakMap.prototype, "constructor", $WeakMap, DONT_ENUM);
237
238 // Set up the non-enumerable functions on the WeakMap prototype object.
ricow@chromium.org27bf2882011-11-17 08:34:43 +0000239 InstallFunctions($WeakMap.prototype, DONT_ENUM, $Array(
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000240 "get", WeakMapGet,
241 "set", WeakMapSet,
242 "has", WeakMapHas,
243 "delete", WeakMapDelete
244 ));
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000245})();