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