v8/test/mjsunit/harmony/symbol-as-weakmap-key.js
Shu-yu Guo 94e8282325 [symbol-as-weakmap-key] Fix DCHECKs and add CanBeHeldWeakly
There are a few DCHECKs that weren't updated to allow for Symbols as
weak collection keys. This CL updates those DCHECKs and also does the
following refactors for clarity:

- Add Object::CanBeHeldWeakly
- Rename GotoIfCannotBeWeakKey -> GotoIfCannotBeHeldWeakly to align with
  spec AO name

Bug: chromium:1370400, chromium:1370402, v8:12947
Change-Id: I380840c8377497feae97e3fca37555dae0dcc255
Fixed: chromium:1370400, chromium:1370402
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3928150
Auto-Submit: Shu-yu Guo <syg@chromium.org>
Reviewed-by: Marja Hölttä <marja@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/main@{#83507}
2022-10-04 06:27:54 +00:00

109 lines
3.1 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 --allow-natives-syntax --noincremental-marking
(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);
(function () {
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));
assertEquals(1, %GetWeakCollectionSize(map));
})();
(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);
(function () {
const innerKey = Symbol('123');
set.add(innerKey);
assertTrue(set.has(innerKey));
})();
assertTrue(set.has(outerKey));
gc();
assertEquals(1, %GetWeakCollectionSize(set));
})();
(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));
})();
(function TestFinalizationRegistryUnregister() {
const fr = new FinalizationRegistry(function() {});
const key = {};
fr.register(Symbol('foo'), "holdings", key);
fr.unregister(key);
})();