mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-01 07:33:12 +08:00
Node islands stack, part 2 of 11 (scriptc-perf/results/islands-prs/PLAN.md). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@scriptc/node
Call into Node.js from a scriptc program. callSync and call run a function exported by a JavaScript module and copy JSON data both ways. In scriptc builds the function runs in Node.js, which the program loads into its own process on the first call (libnode); under Node.js it runs directly, so the program prints the same either way.
import { call, callSync } from "@scriptc/node";
const sum = callSync<number>("./math.cjs", "add", 1, 2);
const config = await call<{ regions: string[] }>("config-parser", "parse", source);
- The module loads with
require()from the calling module: relative specifiers resolve from its directory, bare ones from itsnode_modules. - Arguments and results cross as JSON copies; scriptc checks the result against the declared type.
- A thrown
Errorcrosses as its nearest intrinsic error class with itsmessageand stringcode. - The executable loads Node from the libnode pack recorded at build time (
SCRIPTC_LIBNODEorscriptc build --libnode=<pack directory>;--libnode=besidecopies the pack next to the executable).SCRIPTC_LIBNODEoverrides it at run time. A program that never calls into Node never loads it.