Files
scriptc/tests/corpus/4593-weak-keys-empty-objects.ts
Chris Tate 34cb02d6d1 Keep the native compiler independent of weak keys
Compiler caches keyed by programs and lowering passes move onto the
objects that own their lifetime, and the field-creation pass recognizes
its own writes by their receiver temporaries. A source test keeps weak
collections out of the compiler. Records and arrays that reach an
untyped slot through a union now keep their identity, and empty object
literals are keyed by their native object, so both work as WeakMap and
WeakSet keys instead of failing at runtime.
2026-10-09 20:26:57 -05:00

25 lines
951 B
TypeScript

// Empty object literals are distinct identities and serve as weak keys,
// for example as private tokens or per-request markers.
const ready = {};
const pending = {};
const seen = new WeakSet<object>();
const owners = new WeakMap<object, string>();
seen.add(ready);
owners.set(ready, "scheduler");
owners.set(pending, "queue");
console.log(seen.has(ready), seen.has(pending), seen.has({}));
console.log(owners.get(ready), owners.get(pending), owners.get({}));
function mark(token: object, owner: string): void {
owners.set(token, owner);
}
mark(pending, "worker");
console.log(owners.get(pending), owners.has(pending));
const markers: object[] = [{}, {}, ready];
markers.forEach((marker, index) => seen.add(marker) && owners.set(marker, `marker-${index}`));
console.log(markers.map((marker) => owners.get(marker)).join(" "), seen.has(markers[1]!));
console.log(seen.delete(ready), seen.has(ready), owners.delete(pending), owners.get(pending));