Files
scriptc/tests/corpus/6823-node-island-nested-stack/main.ts
T
Chris Tate 1a2eb52016 Start nested Node entries below the frames a fiber switch left live
A host callback marks where it runs on Node's stack, and a call into
Node.js from a fiber starts below that mark. Runtime code the callback
runs may go deeper on Node's stack before it starts or resumes a fiber,
and those frames stay live while the fiber runs: a call into Node.js
from the fiber overwrote them. scr_switch now reports where it leaves
Node's stack, and the mark moves down to it for the rest of the
callback.

6823 runs a timer callback 200 frames deep that starts an async
function calling into Node.js before its first await.
2026-10-25 19:54:03 -05:00

27 lines
953 B
TypeScript

// @requires-libnode
// A timer on Node's loop runs runtime code deep below the callback's own
// frame, and an async function it starts calls into Node.js before its
// first await: that call's frames go below the deepest live runtime frame
// on Node's stack, so the frames above it return intact.
import { callSync } from "@scriptc/node";
async function viaNode(n: number): Promise<number> {
const echoed = callSync<number>("./echo.cjs", "echo", n);
await null;
return echoed;
}
const started: Promise<number>[] = [];
function deep(depth: number, n: number): number {
const local = [depth, n, depth * n];
if (depth > 0) return deep(depth - 1, n) + local[2]!;
started.push(viaNode(n));
return local[0]! + local[1]!;
}
console.log("loaded", callSync<number>("./echo.cjs", "echo", 1));
setTimeout(() => {
console.log("deep", deep(200, 3));
void Promise.all(started).then((values) => console.log("echoed", values.join(",")));
}, 0);