mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-23 06:10:02 +08:00
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.
25 lines
951 B
TypeScript
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));
|