• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16var m1 = new WeakMap();
17var k1 = {};
18assert (m1.set (k1, 3.14) instanceof WeakMap);
19assert (m1.has (k1) === true);
20assert (m1.get (k1) === 3.14);
21
22k1 = {};
23gc (); /* This will remove the entry, but there is no way to validate it from js. */
24
25assert (m1.has (k1) === false);
26assert (m1.delete(k1) === false);
27assert (m1.set (k1, "asd" + "fgh") instanceof WeakMap);
28assert (m1.has (k1) === true);
29assert (m1.get (k1) === "asdfgh");
30
31assert (m1.delete ("str") === false);
32assert (m1.has (42) === false);
33assert (m1.get (42) === undefined);
34try {
35  m1.set (42, 42);
36  assert (false);
37} catch (e) {
38  assert (e instanceof TypeError);
39}
40
41gc ();
42assert (m1.delete(k1) === true);
43gc ();
44assert (m1.has (k1) === false);
45k1 = {};
46gc ();
47
48Object.freeze(k1);
49assert (m1.set (k1, "frozen") instanceof WeakMap);
50assert (m1.has (k1) === true);
51assert (m1.get (k1) === "frozen");
52k1 = {};
53gc ();
54
55var m2 = new WeakMap();
56
57m1.set (k1, "str" + "str");
58m2.set (k1, 42.42);
59
60k1 = {};
61gc ();
62var k2 = {};
63
64m1.set (k1, "str" + "str");
65m1.set (k2, "str2" + "str2");
66m2.set (k1, 42.42);
67m2.set (k2, Infinity);
68
69m2.delete (k1);
70gc ();
71k1 = {};
72k2 = {};
73gc ();
74
75var k3 = {};
76m1 = new WeakMap();
77m1.set(k1, "str" + "str");
78m1.set(k2, "str2" + "str2");
79m1.set(k3, "str3" + "str3");
80k1 = undefined;
81k2 = undefined;
82k3 = undefined;
83m1 = undefined;
84gc();
85
86try {
87  WeakMap();
88  assert (false);
89} catch (e) {
90  assert (e instanceof TypeError);
91}
92
93try {
94  WeakMap.prototype.get.call({});
95  assert (false);
96} catch (e) {
97  assert (e instanceof TypeError);
98}
99
100assert (new WeakMap(null) instanceof WeakMap);
101
102k1 = {};
103k2 = {};
104m1 = new WeakMap([[k1, 1.2], [k2, 1.3]]);
105assert (m1.has (k1));
106assert (m1.has (k2));
107assert (m1.get (k1) === 1.2);
108assert (m1.get (k2) === 1.3);
109gc();
110m1 = undefined;
111gc();
112
113m1 = new WeakMap();
114m1.set(k1, "str");
115m1.set(k1, "4");
116m1.set(k1, null);
117m1.set(k1, 42);
118assert (m1.has (k1));
119k1 = {};
120gc();
121
122m1 = new WeakMap();
123m1.set(k1, "str");
124m1.set(k1, "4");
125m1.set(k1, 42);
126m1.set(k1, null);
127assert (m1.has (k1));
128m1 = new WeakMap();
129gc();
130
131try {
132  new WeakMap([[1,2],[3,4]]);
133  assert (false);
134} catch (e) {
135  assert (e instanceof TypeError);
136}
137
138function createIterable(arr, methods = {}) {
139  let iterable = function *() {
140    let idx = 0;
141    while (idx < arr.length) {
142      yield arr[idx];
143      idx++;
144    }
145  }();
146  iterable['return'] = methods['return'];
147  iterable['throw'] = methods['throw'];
148
149  return iterable;
150};
151
152var closed = false;
153var iter = createIterable([1, 2, 3], {
154  'return': function(){ closed = true; return {}; }
155});
156try {
157  new WeakMap(iter);
158} catch(e){}
159
160assert(closed === true);
161
162m1.set([], []);
163
164assert (WeakMap.prototype.toString() === "[object WeakMap]");
165
166WeakMap.prototype.set = function () { throw "abrupt set" };
167
168try {
169  new WeakMap([[{}, 1]]);
170  assert (false);
171} catch (e) {
172  assert (e === "abrupt set");
173}
174
175Object.defineProperty(WeakMap.prototype, 'set', {
176  get: () => { throw "abrupt set getter" }
177});
178
179try {
180  new WeakMap([]);
181  assert (false);
182} catch (e) {
183  assert (e === "abrupt set getter");
184}
185