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.
7 // This file relies on the fact that the following declaration has been made
9 // var $Array = global.Array;
11 var $WeakMap = global.WeakMap;
12 var $WeakSet = global.WeakSet;
15 // -------------------------------------------------------------------
18 function WeakMapConstructor() {
19 if (%_IsConstructCall()) {
20 %WeakCollectionInitialize(this);
22 throw MakeTypeError('constructor_not_function', ['WeakMap']);
27 function WeakMapGet(key) {
28 if (!IS_WEAKMAP(this)) {
29 throw MakeTypeError('incompatible_method_receiver',
30 ['WeakMap.prototype.get', this]);
32 if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
33 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
35 return %WeakCollectionGet(this, key);
39 function WeakMapSet(key, value) {
40 if (!IS_WEAKMAP(this)) {
41 throw MakeTypeError('incompatible_method_receiver',
42 ['WeakMap.prototype.set', this]);
44 if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
45 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
47 return %WeakCollectionSet(this, key, value);
51 function WeakMapHas(key) {
52 if (!IS_WEAKMAP(this)) {
53 throw MakeTypeError('incompatible_method_receiver',
54 ['WeakMap.prototype.has', this]);
56 if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
57 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
59 return %WeakCollectionHas(this, key);
63 function WeakMapDelete(key) {
64 if (!IS_WEAKMAP(this)) {
65 throw MakeTypeError('incompatible_method_receiver',
66 ['WeakMap.prototype.delete', this]);
68 if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
69 throw %MakeTypeError('invalid_weakmap_key', [this, key]);
71 return %WeakCollectionDelete(this, key);
75 function WeakMapClear() {
76 if (!IS_WEAKMAP(this)) {
77 throw MakeTypeError('incompatible_method_receiver',
78 ['WeakMap.prototype.clear', this]);
80 // Replace the internal table with a new empty table.
81 %WeakCollectionInitialize(this);
85 // -------------------------------------------------------------------
87 function SetUpWeakMap() {
88 %CheckIsBootstrapping();
90 %SetCode($WeakMap, WeakMapConstructor);
91 %FunctionSetPrototype($WeakMap, new $Object());
92 %SetProperty($WeakMap.prototype, "constructor", $WeakMap, DONT_ENUM);
94 // Set up the non-enumerable functions on the WeakMap prototype object.
95 InstallFunctions($WeakMap.prototype, DONT_ENUM, $Array(
99 "delete", WeakMapDelete,
100 "clear", WeakMapClear
107 // -------------------------------------------------------------------
110 function WeakSetConstructor() {
111 if (%_IsConstructCall()) {
112 %WeakCollectionInitialize(this);
114 throw MakeTypeError('constructor_not_function', ['WeakSet']);
119 function WeakSetAdd(value) {
120 if (!IS_WEAKSET(this)) {
121 throw MakeTypeError('incompatible_method_receiver',
122 ['WeakSet.prototype.add', this]);
124 if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
125 throw %MakeTypeError('invalid_weakset_value', [this, value]);
127 return %WeakCollectionSet(this, value, true);
131 function WeakSetHas(value) {
132 if (!IS_WEAKSET(this)) {
133 throw MakeTypeError('incompatible_method_receiver',
134 ['WeakSet.prototype.has', this]);
136 if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
137 throw %MakeTypeError('invalid_weakset_value', [this, value]);
139 return %WeakCollectionHas(this, value);
143 function WeakSetDelete(value) {
144 if (!IS_WEAKSET(this)) {
145 throw MakeTypeError('incompatible_method_receiver',
146 ['WeakSet.prototype.delete', this]);
148 if (!(IS_SPEC_OBJECT(value) || IS_SYMBOL(value))) {
149 throw %MakeTypeError('invalid_weakset_value', [this, value]);
151 return %WeakCollectionDelete(this, value);
155 function WeakSetClear() {
156 if (!IS_WEAKSET(this)) {
157 throw MakeTypeError('incompatible_method_receiver',
158 ['WeakSet.prototype.clear', this]);
160 // Replace the internal table with a new empty table.
161 %WeakCollectionInitialize(this);
165 // -------------------------------------------------------------------
167 function SetUpWeakSet() {
168 %CheckIsBootstrapping();
170 %SetCode($WeakSet, WeakSetConstructor);
171 %FunctionSetPrototype($WeakSet, new $Object());
172 %SetProperty($WeakSet.prototype, "constructor", $WeakSet, DONT_ENUM);
174 // Set up the non-enumerable functions on the WeakSet prototype object.
175 InstallFunctions($WeakSet.prototype, DONT_ENUM, $Array(
178 "delete", WeakSetDelete,
179 "clear", WeakSetClear