Upstream version 7.36.149.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() {
19   if (%_IsConstructCall()) {
20     %WeakCollectionInitialize(this);
21   } else {
22     throw MakeTypeError('constructor_not_function', ['WeakMap']);
23   }
24 }
25
26
27 function WeakMapGet(key) {
28   if (!IS_WEAKMAP(this)) {
29     throw MakeTypeError('incompatible_method_receiver',
30                         ['WeakMap.prototype.get', this]);
31   }
32   if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
33     throw %MakeTypeError('invalid_weakmap_key', [this, key]);
34   }
35   return %WeakCollectionGet(this, key);
36 }
37
38
39 function WeakMapSet(key, value) {
40   if (!IS_WEAKMAP(this)) {
41     throw MakeTypeError('incompatible_method_receiver',
42                         ['WeakMap.prototype.set', this]);
43   }
44   if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
45     throw %MakeTypeError('invalid_weakmap_key', [this, key]);
46   }
47   return %WeakCollectionSet(this, key, value);
48 }
49
50
51 function WeakMapHas(key) {
52   if (!IS_WEAKMAP(this)) {
53     throw MakeTypeError('incompatible_method_receiver',
54                         ['WeakMap.prototype.has', this]);
55   }
56   if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
57     throw %MakeTypeError('invalid_weakmap_key', [this, key]);
58   }
59   return %WeakCollectionHas(this, key);
60 }
61
62
63 function WeakMapDelete(key) {
64   if (!IS_WEAKMAP(this)) {
65     throw MakeTypeError('incompatible_method_receiver',
66                         ['WeakMap.prototype.delete', this]);
67   }
68   if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
69     throw %MakeTypeError('invalid_weakmap_key', [this, key]);
70   }
71   return %WeakCollectionDelete(this, key);
72 }
73
74
75 function WeakMapClear() {
76   if (!IS_WEAKMAP(this)) {
77     throw MakeTypeError('incompatible_method_receiver',
78                         ['WeakMap.prototype.clear', this]);
79   }
80   // Replace the internal table with a new empty table.
81   %WeakCollectionInitialize(this);
82 }
83
84
85 // -------------------------------------------------------------------
86
87 function SetUpWeakMap() {
88   %CheckIsBootstrapping();
89
90   %SetCode($WeakMap, WeakMapConstructor);
91   %FunctionSetPrototype($WeakMap, new $Object());
92   %SetProperty($WeakMap.prototype, "constructor", $WeakMap, DONT_ENUM);
93
94   // Set up the non-enumerable functions on the WeakMap prototype object.
95   InstallFunctions($WeakMap.prototype, DONT_ENUM, $Array(
96     "get", WeakMapGet,
97     "set", WeakMapSet,
98     "has", WeakMapHas,
99     "delete", WeakMapDelete,
100     "clear", WeakMapClear
101   ));
102 }
103
104 SetUpWeakMap();
105
106
107 // -------------------------------------------------------------------
108 // Harmony WeakSet
109
110 function WeakSetConstructor() {
111   if (%_IsConstructCall()) {
112     %WeakCollectionInitialize(this);
113   } else {
114     throw MakeTypeError('constructor_not_function', ['WeakSet']);
115   }
116 }
117
118
119 function WeakSetAdd(value) {
120   if (!IS_WEAKSET(this)) {
121     throw MakeTypeError('incompatible_method_receiver',
122                         ['WeakSet.prototype.add', this]);
123   }
124   if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
125     throw %MakeTypeError('invalid_weakset_value', [this, value]);
126   }
127   return %WeakCollectionSet(this, value, true);
128 }
129
130
131 function WeakSetHas(value) {
132   if (!IS_WEAKSET(this)) {
133     throw MakeTypeError('incompatible_method_receiver',
134                         ['WeakSet.prototype.has', this]);
135   }
136   if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
137     throw %MakeTypeError('invalid_weakset_value', [this, value]);
138   }
139   return %WeakCollectionHas(this, value);
140 }
141
142
143 function WeakSetDelete(value) {
144   if (!IS_WEAKSET(this)) {
145     throw MakeTypeError('incompatible_method_receiver',
146                         ['WeakSet.prototype.delete', this]);
147   }
148   if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
149     throw %MakeTypeError('invalid_weakset_value', [this, value]);
150   }
151   return %WeakCollectionDelete(this, value);
152 }
153
154
155 function WeakSetClear() {
156   if (!IS_WEAKSET(this)) {
157     throw MakeTypeError('incompatible_method_receiver',
158                         ['WeakSet.prototype.clear', this]);
159   }
160   // Replace the internal table with a new empty table.
161   %WeakCollectionInitialize(this);
162 }
163
164
165 // -------------------------------------------------------------------
166
167 function SetUpWeakSet() {
168   %CheckIsBootstrapping();
169
170   %SetCode($WeakSet, WeakSetConstructor);
171   %FunctionSetPrototype($WeakSet, new $Object());
172   %SetProperty($WeakSet.prototype, "constructor", $WeakSet, DONT_ENUM);
173
174   // Set up the non-enumerable functions on the WeakSet prototype object.
175   InstallFunctions($WeakSet.prototype, DONT_ENUM, $Array(
176     "add", WeakSetAdd,
177     "has", WeakSetHas,
178     "delete", WeakSetDelete,
179     "clear", WeakSetClear
180   ));
181 }
182
183 SetUpWeakSet();