Files
scriptc/tests/corpus/global-symbol-cache/main.js
Chris Tate 08115a1d3d Support native startup caches and assignment expressions
- Share symbol-keyed global caches across statically compiled modules.
- Preserve assignment results and evaluation order for native fields, accessors, and nullish defaults.
- Read omitted options correctly in specialized JavaScript package calls.
2026-09-28 11:05:00 -05:00

30 lines
1.2 KiB
JavaScript

import { remoteRead, remoteClear } from "./remote.js";
const cacheKey = Symbol.for("scriptc.cache");
function singleton(key, factory) {
const bag = globalThis[cacheKey] ??= {};
if (!(key in bag)) bag[key] = factory();
return bag[key];
}
let made = 0;
function make() { made++; return { value: made }; }
const first = singleton("item", make);
const second = singleton("item", make);
console.log(first === second, first.value, made);
const sameKey = Symbol.for("scriptc.cache");
console.log(globalThis[cacheKey] === globalThis[sameKey], cacheKey in globalThis, remoteRead() === globalThis[cacheKey]);
const unique = Symbol("scriptc.cache");
console.log(globalThis[unique] === undefined, unique in globalThis);
globalThis[unique] = undefined;
console.log(globalThis[unique] === undefined, unique in globalThis);
delete globalThis[unique];
console.log(unique in globalThis);
remoteClear();
console.log(globalThis[sameKey] === undefined, sameKey in globalThis);
const replacement = singleton("item", make);
console.log(replacement === first, replacement.value, made);
let reads = 0;
function key() { reads++; return cacheKey; }
const again = globalThis[key()] ??= { unused: true };
console.log(reads, again === globalThis[cacheKey]);
delete globalThis[cacheKey];