v8/test/mjsunit/harmony/symbol-as-weakmap-key.js
jameslahm c400af48b5 [symbol-as-weakmap-key] Implement Symbol as WeakMap Keys
Allow non-registered symbols as keys in weakmap and weakset.
Allow non-registered symbols as target and unregisterToken in
WeakRef and FinalizationRegistry.

Bug: v8:12947
Change-Id: Ieb63bda66e3cc378879ac651e23300b71caed627
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3865056
Reviewed-by: Dominik Inführ <dinfuehr@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Reviewed-by: Jakob Linke <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/main@{#83313}
2022-09-20 07:24:42 +00:00

100 lines
2.7 KiB
JavaScript

// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-symbol-as-weakmap-key --expose-gc
(function TestWeakMapWithNonRegisteredSymbolKey() {
const key = Symbol('123');
const value = 1;
const map = new WeakMap();
assertFalse(map.has(key));
assertSame(undefined, map.get(key));
assertFalse(map.delete(key));
assertSame(map, map.set(key, value));
assertSame(value, map.get(key));
assertTrue(map.has(key));
assertTrue(map.delete(key));
assertFalse(map.has(key));
assertSame(undefined, map.get(key));
assertFalse(map.delete(key));
assertFalse(map.has(key));
assertSame(undefined, map.get(key));
})();
(function TestWeakMapWithNonRegisteredSymbolKeyGC() {
const map = new WeakMap();
const outerKey = Symbol('234');
const outerValue = 1;
map.set(outerKey, outerValue);
{
const innerKey = Symbol('123');
const innerValue = 1;
map.set(innerKey, innerValue);
assertTrue(map.has(innerKey));
assertSame(innerValue, map.get(innerKey));
}
gc();
assertTrue(map.has(outerKey));
assertSame(outerValue, map.get(outerKey));
})();
(function TestWeakMapWithRegisteredSymbolKey() {
const key = Symbol.for('123');
const value = 1;
const map = new WeakMap();
assertFalse(map.has(key));
assertSame(undefined, map.get(key));
assertFalse(map.delete(key));
assertThrows(() => {
map.set(key, value);
}, TypeError, 'Invalid value used as weak map key');
assertFalse(map.has(key));
assertSame(undefined, map.get(key));
assertFalse(map.delete(key));
assertFalse(map.has(key));
assertSame(undefined, map.get(key));
})();
(function TestWeakSetWithNonRegisteredSymbolKey() {
const key = Symbol('123');
const set = new WeakSet();
assertFalse(set.has(key));
assertFalse(set.delete(key));
assertSame(set, set.add(key));
assertTrue(set.has(key));
assertTrue(set.delete(key));
assertFalse(set.has(key));
assertFalse(set.delete(key));
assertFalse(set.has(key));
})();
(function TestWeakSetWithNonRegisteredSymbolKeyGC() {
const set = new WeakSet();
const outerKey = Symbol('234');
set.add(outerKey);
{
const innerKey = Symbol('123');
set.add(innerKey);
assertTrue(set.has(innerKey));
}
gc();
assertTrue(set.has(outerKey));
})();
(function TestWeakSetWithRegisteredSymbolKey() {
const key = Symbol.for('123');
const set = new WeakSet();
assertFalse(set.has(key));
assertFalse(set.delete(key));
assertThrows(() => {
assertSame(set, set.add(key));
}, TypeError, 'Invalid value used in weak set');
assertFalse(set.has(key));
assertFalse(set.delete(key));
assertFalse(set.has(key));
})();