Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / v8 / src / collection.js
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // 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
28 "use strict";
29
30 // This file relies on the fact that the following declaration has been made
31 // in runtime.js:
32 // var $Array = global.Array;
33
34 var $Set = global.Set;
35 var $Map = global.Map;
36 var $WeakMap = global.WeakMap;
37 var $WeakSet = global.WeakSet;
38
39 // Global sentinel to be used instead of undefined keys, which are not
40 // supported internally but required for Harmony sets and maps.
41 var undefined_sentinel = {};
42
43
44 // Map and Set uses SameValueZero which means that +0 and -0 should be treated
45 // as the same value.
46 function NormalizeKey(key) {
47   if (IS_UNDEFINED(key)) {
48     return undefined_sentinel;
49   }
50
51   if (key === 0) {
52     return 0;
53   }
54
55   return key;
56 }
57
58
59 // -------------------------------------------------------------------
60 // Harmony Set
61
62 function SetConstructor() {
63   if (%_IsConstructCall()) {
64     %SetInitialize(this);
65   } else {
66     throw MakeTypeError('constructor_not_function', ['Set']);
67   }
68 }
69
70
71 function SetAdd(key) {
72   if (!IS_SET(this)) {
73     throw MakeTypeError('incompatible_method_receiver',
74                         ['Set.prototype.add', this]);
75   }
76   return %SetAdd(this, NormalizeKey(key));
77 }
78
79
80 function SetHas(key) {
81   if (!IS_SET(this)) {
82     throw MakeTypeError('incompatible_method_receiver',
83                         ['Set.prototype.has', this]);
84   }
85   return %SetHas(this, NormalizeKey(key));
86 }
87
88
89 function SetDelete(key) {
90   if (!IS_SET(this)) {
91     throw MakeTypeError('incompatible_method_receiver',
92                         ['Set.prototype.delete', this]);
93   }
94   key = NormalizeKey(key);
95   if (%SetHas(this, key)) {
96     %SetDelete(this, key);
97     return true;
98   } else {
99     return false;
100   }
101 }
102
103
104 function SetGetSize() {
105   if (!IS_SET(this)) {
106     throw MakeTypeError('incompatible_method_receiver',
107                         ['Set.prototype.size', this]);
108   }
109   return %SetGetSize(this);
110 }
111
112
113 function SetClear() {
114   if (!IS_SET(this)) {
115     throw MakeTypeError('incompatible_method_receiver',
116                         ['Set.prototype.clear', this]);
117   }
118   // Replace the internal table with a new empty table.
119   %SetInitialize(this);
120 }
121
122
123 // -------------------------------------------------------------------
124
125 function SetUpSet() {
126   %CheckIsBootstrapping();
127
128   %SetCode($Set, SetConstructor);
129   %FunctionSetPrototype($Set, new $Object());
130   %SetProperty($Set.prototype, "constructor", $Set, DONT_ENUM);
131
132   // Set up the non-enumerable functions on the Set prototype object.
133   InstallGetter($Set.prototype, "size", SetGetSize);
134   InstallFunctions($Set.prototype, DONT_ENUM, $Array(
135     "add", SetAdd,
136     "has", SetHas,
137     "delete", SetDelete,
138     "clear", SetClear
139   ));
140 }
141
142 SetUpSet();
143
144
145 // -------------------------------------------------------------------
146 // Harmony Map
147
148 function MapConstructor() {
149   if (%_IsConstructCall()) {
150     %MapInitialize(this);
151   } else {
152     throw MakeTypeError('constructor_not_function', ['Map']);
153   }
154 }
155
156
157 function MapGet(key) {
158   if (!IS_MAP(this)) {
159     throw MakeTypeError('incompatible_method_receiver',
160                         ['Map.prototype.get', this]);
161   }
162   return %MapGet(this, NormalizeKey(key));
163 }
164
165
166 function MapSet(key, value) {
167   if (!IS_MAP(this)) {
168     throw MakeTypeError('incompatible_method_receiver',
169                         ['Map.prototype.set', this]);
170   }
171   return %MapSet(this, NormalizeKey(key), value);
172 }
173
174
175 function MapHas(key) {
176   if (!IS_MAP(this)) {
177     throw MakeTypeError('incompatible_method_receiver',
178                         ['Map.prototype.has', this]);
179   }
180   return %MapHas(this, NormalizeKey(key));
181 }
182
183
184 function MapDelete(key) {
185   if (!IS_MAP(this)) {
186     throw MakeTypeError('incompatible_method_receiver',
187                         ['Map.prototype.delete', this]);
188   }
189   return %MapDelete(this, NormalizeKey(key));
190 }
191
192
193 function MapGetSize() {
194   if (!IS_MAP(this)) {
195     throw MakeTypeError('incompatible_method_receiver',
196                         ['Map.prototype.size', this]);
197   }
198   return %MapGetSize(this);
199 }
200
201
202 function MapClear() {
203   if (!IS_MAP(this)) {
204     throw MakeTypeError('incompatible_method_receiver',
205                         ['Map.prototype.clear', this]);
206   }
207   // Replace the internal table with a new empty table.
208   %MapInitialize(this);
209 }
210
211
212 // -------------------------------------------------------------------
213
214 function SetUpMap() {
215   %CheckIsBootstrapping();
216
217   %SetCode($Map, MapConstructor);
218   %FunctionSetPrototype($Map, new $Object());
219   %SetProperty($Map.prototype, "constructor", $Map, DONT_ENUM);
220
221   // Set up the non-enumerable functions on the Map prototype object.
222   InstallGetter($Map.prototype, "size", MapGetSize);
223   InstallFunctions($Map.prototype, DONT_ENUM, $Array(
224     "get", MapGet,
225     "set", MapSet,
226     "has", MapHas,
227     "delete", MapDelete,
228     "clear", MapClear
229   ));
230 }
231
232 SetUpMap();
233
234
235 // -------------------------------------------------------------------
236 // Harmony WeakMap
237
238 function WeakMapConstructor() {
239   if (%_IsConstructCall()) {
240     %WeakCollectionInitialize(this);
241   } else {
242     throw MakeTypeError('constructor_not_function', ['WeakMap']);
243   }
244 }
245
246
247 function WeakMapGet(key) {
248   if (!IS_WEAKMAP(this)) {
249     throw MakeTypeError('incompatible_method_receiver',
250                         ['WeakMap.prototype.get', this]);
251   }
252   if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
253     throw %MakeTypeError('invalid_weakmap_key', [this, key]);
254   }
255   return %WeakCollectionGet(this, key);
256 }
257
258
259 function WeakMapSet(key, value) {
260   if (!IS_WEAKMAP(this)) {
261     throw MakeTypeError('incompatible_method_receiver',
262                         ['WeakMap.prototype.set', this]);
263   }
264   if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
265     throw %MakeTypeError('invalid_weakmap_key', [this, key]);
266   }
267   return %WeakCollectionSet(this, key, value);
268 }
269
270
271 function WeakMapHas(key) {
272   if (!IS_WEAKMAP(this)) {
273     throw MakeTypeError('incompatible_method_receiver',
274                         ['WeakMap.prototype.has', this]);
275   }
276   if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
277     throw %MakeTypeError('invalid_weakmap_key', [this, key]);
278   }
279   return %WeakCollectionHas(this, key);
280 }
281
282
283 function WeakMapDelete(key) {
284   if (!IS_WEAKMAP(this)) {
285     throw MakeTypeError('incompatible_method_receiver',
286                         ['WeakMap.prototype.delete', this]);
287   }
288   if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
289     throw %MakeTypeError('invalid_weakmap_key', [this, key]);
290   }
291   return %WeakCollectionDelete(this, key);
292 }
293
294
295 function WeakMapClear() {
296   if (!IS_WEAKMAP(this)) {
297     throw MakeTypeError('incompatible_method_receiver',
298                         ['WeakMap.prototype.clear', this]);
299   }
300   // Replace the internal table with a new empty table.
301   %WeakCollectionInitialize(this);
302 }
303
304
305 // -------------------------------------------------------------------
306
307 function SetUpWeakMap() {
308   %CheckIsBootstrapping();
309
310   %SetCode($WeakMap, WeakMapConstructor);
311   %FunctionSetPrototype($WeakMap, new $Object());
312   %SetProperty($WeakMap.prototype, "constructor", $WeakMap, DONT_ENUM);
313
314   // Set up the non-enumerable functions on the WeakMap prototype object.
315   InstallFunctions($WeakMap.prototype, DONT_ENUM, $Array(
316     "get", WeakMapGet,
317     "set", WeakMapSet,
318     "has", WeakMapHas,
319     "delete", WeakMapDelete,
320     "clear", WeakMapClear
321   ));
322 }
323
324 SetUpWeakMap();
325
326
327 // -------------------------------------------------------------------
328 // Harmony WeakSet
329
330 function WeakSetConstructor() {
331   if (%_IsConstructCall()) {
332     %WeakCollectionInitialize(this);
333   } else {
334     throw MakeTypeError('constructor_not_function', ['WeakSet']);
335   }
336 }
337
338
339 function WeakSetAdd(value) {
340   if (!IS_WEAKSET(this)) {
341     throw MakeTypeError('incompatible_method_receiver',
342                         ['WeakSet.prototype.add', this]);
343   }
344   if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
345     throw %MakeTypeError('invalid_weakset_value', [this, value]);
346   }
347   return %WeakCollectionSet(this, value, true);
348 }
349
350
351 function WeakSetHas(value) {
352   if (!IS_WEAKSET(this)) {
353     throw MakeTypeError('incompatible_method_receiver',
354                         ['WeakSet.prototype.has', this]);
355   }
356   if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
357     throw %MakeTypeError('invalid_weakset_value', [this, value]);
358   }
359   return %WeakCollectionHas(this, value);
360 }
361
362
363 function WeakSetDelete(value) {
364   if (!IS_WEAKSET(this)) {
365     throw MakeTypeError('incompatible_method_receiver',
366                         ['WeakSet.prototype.delete', this]);
367   }
368   if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
369     throw %MakeTypeError('invalid_weakset_value', [this, value]);
370   }
371   return %WeakCollectionDelete(this, value);
372 }
373
374
375 function WeakSetClear() {
376   if (!IS_WEAKSET(this)) {
377     throw MakeTypeError('incompatible_method_receiver',
378                         ['WeakSet.prototype.clear', this]);
379   }
380   // Replace the internal table with a new empty table.
381   %WeakCollectionInitialize(this);
382 }
383
384
385 // -------------------------------------------------------------------
386
387 function SetUpWeakSet() {
388   %CheckIsBootstrapping();
389
390   %SetCode($WeakSet, WeakSetConstructor);
391   %FunctionSetPrototype($WeakSet, new $Object());
392   %SetProperty($WeakSet.prototype, "constructor", $WeakSet, DONT_ENUM);
393
394   // Set up the non-enumerable functions on the WeakSet prototype object.
395   InstallFunctions($WeakSet.prototype, DONT_ENUM, $Array(
396     "add", WeakSetAdd,
397     "has", WeakSetHas,
398     "delete", WeakSetDelete,
399     "clear", WeakSetClear
400   ));
401 }
402
403 SetUpWeakSet();