mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-25 06:24:27 +08:00
- Add native worker execution with isolated module state, structured messages, and lifecycle controls. - Support shared buffers and integer atomics with coordinated runtime ownership and cancellation. - Extend compiler, runtime-pack, differential, and compatibility coverage for native concurrency.
23 lines
811 B
TypeScript
23 lines
811 B
TypeScript
import { Worker, isMainThread, parentPort, workerData } from "node:worker_threads";
|
|
|
|
if (isMainThread) {
|
|
for (const kind of ["for", "do", "for-of"]) {
|
|
await new Promise<void>((resolve) => {
|
|
const worker = new Worker(new URL(import.meta.url), {workerData: kind});
|
|
worker.once("message", () => { worker.terminate(); });
|
|
worker.once("exit", (code: number) => { console.log(kind, code); resolve(); });
|
|
});
|
|
}
|
|
} else {
|
|
parentPort!.postMessage("ready");
|
|
try {
|
|
if (workerData === "for") { for (;;) { continue; } }
|
|
else if (workerData === "do") { do { continue; } while (true); }
|
|
else {
|
|
const growing = [1];
|
|
for (const value of growing) growing.push(value);
|
|
}
|
|
} catch { console.log("unexpected catch"); }
|
|
finally { console.log("unexpected finally"); }
|
|
}
|