mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-21 05:50:55 +08:00
- Replace SDK AST materialization with checked native node, list, and text decoding. - Preserve checker handle identity and snapshot reuse across the frontend. - Compile generic visitors and guarded node-list iteration without dynamic execution.
31 lines
1007 B
TypeScript
31 lines
1007 B
TypeScript
class Item {
|
|
readonly value: number;
|
|
constructor(value: number) { this.value = value; }
|
|
}
|
|
|
|
const entries: (Item | Item[] | undefined)[] = [new Item(1), [new Item(2), new Item(3)], undefined];
|
|
function lookup(index: number): Item | Item[] | undefined {
|
|
return entries[index];
|
|
}
|
|
|
|
for (let index = 0; index < 4; index++) {
|
|
const value = lookup(index);
|
|
if (Array.isArray(value)) {
|
|
for (const item of value) console.log("list", item.value);
|
|
} else if (value !== undefined) console.log("node", value.value);
|
|
else console.log("missing");
|
|
}
|
|
|
|
// A captured assignment can invalidate a checker narrow. The loop must
|
|
// validate the actual payload instead of reading an absent array pointer.
|
|
let captured: Item | Item[] | undefined = lookup(1);
|
|
function clear(): void { captured = undefined; }
|
|
if (Array.isArray(captured)) {
|
|
clear();
|
|
try {
|
|
for (const item of captured) console.log("unreachable", item.value);
|
|
} catch (error) {
|
|
if (error instanceof Error) console.log(error.name);
|
|
}
|
|
}
|