Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / v8 / src / weak-collection.js
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.
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
11 var $WeakMap = global.WeakMap;
12 var $WeakSet = global.WeakSet;
13
14
15 // -------------------------------------------------------------------
16 // Harmony WeakMap
17
18 function 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
51 function 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
63 function 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
75 function 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
87 function 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
99 function WeakMapClear() {
100   if (!IS_WEAKMAP(this)) {
101     throw MakeTypeError('incompatible_method_receiver',
102                         ['WeakMap.prototype.clear', this]);
103   }
104   // Replace the internal table with a new empty table.
105   %WeakCollectionInitialize(this);
106 }
107
108
109 // -------------------------------------------------------------------
110
111 function SetUpWeakMap() {
112   %CheckIsBootstrapping();
113
114   %SetCode($WeakMap, WeakMapConstructor);
115   %FunctionSetPrototype($WeakMap, new $Object());
116   %AddNamedProperty($WeakMap.prototype, "constructor", $WeakMap, DONT_ENUM);
117   %AddNamedProperty(
118       $WeakMap.prototype, symbolToStringTag, "WeakMap", DONT_ENUM | READ_ONLY);
119
120   // Set up the non-enumerable functions on the WeakMap prototype object.
121   InstallFunctions($WeakMap.prototype, DONT_ENUM, $Array(
122     "get", WeakMapGet,
123     "set", WeakMapSet,
124     "has", WeakMapHas,
125     "delete", WeakMapDelete,
126     "clear", WeakMapClear
127   ));
128 }
129
130 SetUpWeakMap();
131
132
133 // -------------------------------------------------------------------
134 // Harmony WeakSet
135
136 function WeakSetConstructor(iterable) {
137   if (!%_IsConstructCall()) {
138     throw MakeTypeError('constructor_not_function', ['WeakSet']);
139   }
140
141   var iter, adder;
142
143   if (!IS_NULL_OR_UNDEFINED(iterable)) {
144     iter = GetIterator(ToObject(iterable));
145     adder = this.add;
146     if (!IS_SPEC_FUNCTION(adder)) {
147       throw MakeTypeError('property_not_function', ['add', this]);
148     }
149   }
150
151   %WeakCollectionInitialize(this);
152
153   if (IS_UNDEFINED(iter)) return;
154
155   var next, done;
156   while (!(next = iter.next()).done) {
157     if (!IS_SPEC_OBJECT(next)) {
158       throw MakeTypeError('iterator_result_not_an_object', [next]);
159     }
160     %_CallFunction(this, next.value, adder);
161   }
162 }
163
164
165 function WeakSetAdd(value) {
166   if (!IS_WEAKSET(this)) {
167     throw MakeTypeError('incompatible_method_receiver',
168                         ['WeakSet.prototype.add', this]);
169   }
170   if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
171     throw %MakeTypeError('invalid_weakset_value', [this, value]);
172   }
173   return %WeakCollectionSet(this, value, true);
174 }
175
176
177 function WeakSetHas(value) {
178   if (!IS_WEAKSET(this)) {
179     throw MakeTypeError('incompatible_method_receiver',
180                         ['WeakSet.prototype.has', this]);
181   }
182   if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
183     throw %MakeTypeError('invalid_weakset_value', [this, value]);
184   }
185   return %WeakCollectionHas(this, value);
186 }
187
188
189 function WeakSetDelete(value) {
190   if (!IS_WEAKSET(this)) {
191     throw MakeTypeError('incompatible_method_receiver',
192                         ['WeakSet.prototype.delete', this]);
193   }
194   if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
195     throw %MakeTypeError('invalid_weakset_value', [this, value]);
196   }
197   return %WeakCollectionDelete(this, value);
198 }
199
200
201 function WeakSetClear() {
202   if (!IS_WEAKSET(this)) {
203     throw MakeTypeError('incompatible_method_receiver',
204                         ['WeakSet.prototype.clear', this]);
205   }
206   // Replace the internal table with a new empty table.
207   %WeakCollectionInitialize(this);
208 }
209
210
211 // -------------------------------------------------------------------
212
213 function SetUpWeakSet() {
214   %CheckIsBootstrapping();
215
216   %SetCode($WeakSet, WeakSetConstructor);
217   %FunctionSetPrototype($WeakSet, new $Object());
218   %AddNamedProperty($WeakSet.prototype, "constructor", $WeakSet, DONT_ENUM);
219   %AddNamedProperty(
220       $WeakSet.prototype, symbolToStringTag, "WeakSet", DONT_ENUM | READ_ONLY);
221
222   // Set up the non-enumerable functions on the WeakSet prototype object.
223   InstallFunctions($WeakSet.prototype, DONT_ENUM, $Array(
224     "add", WeakSetAdd,
225     "has", WeakSetHas,
226     "delete", WeakSetDelete,
227     "clear", WeakSetClear
228   ));
229 }
230
231 SetUpWeakSet();