/** * Micro-kernel Tool Runner * * Each tool invocation spawns an isolated WASM process with its own linear * memory, libc, heap, and Emscripten filesystem. The kernel (this module) * manages: * * - Process lifecycle: create → configure → run → teardown * - VFS ↔ process FS bridging: pre-populate input files, harvest outputs * - TTY: route stdout/stderr from the process to the caller * - ENV: inject environment variables into each process * * There is NO shared MAIN_MODULE, NO dlopen/dlsym, NO shared linear memory. * Each tool is compiled as a standalone Emscripten module (MODULARIZE + EXPORT_ES6). */ import type { VFSManager } from './vfs/index.js'; export interface ToolResult { exitCode: number; stdout: string; stderr: string; } export interface RunOptions { env?: Record; cwd?: string; onStdout?: (text: string) => void; onStderr?: (text: string) => void; stdin?: () => number | null | Promise; /** * When true, the tool invocation is an info/version query (e.g. --version). * setupProcessFS will skip mounting VFSFS since the tool doesn't need * filesystem access. */ isInfoQuery?: boolean; /** * Performance hints for bundle preloading. * - `bundlesNeeded`: names of CDN bundles (e.g. 'sdl3', 'raylib', 'allegro') * that the tool invocation requires. When provided, only those graphics * bundles are preloaded — others are skipped to avoid wasted network/IDB * traffic on unrelated builds (e.g. a terminal C++ build doesn't need sdl3). */ hints?: { bundlesNeeded?: string[]; }; } export interface ToolVersionConfig { pythonMajorMinor: string; pythonMajorMinorCompact: string; } export declare class ToolRunner { private vfs; private versions; constructor(vfs: VFSManager, versions?: ToolVersionConfig); /** Resolve an explicit output artifact path from argv (-o ). */ private getOutputPathFromArgv; private escapeRegExp; private tryReadProcessFile; /** * Run a tool by name with the given argv. Each call spawns an isolated * WASM process and tears it down after completion. */ run(tool: string, argv: string[], options?: RunOptions): Promise; /** * Execute a ninja build by parsing build.ninja in JS and running each * build command directly through the tool runner. This avoids the * Asyncify crash that happens when ninja.wasm tries to dispatch * subprocesses via system("__dispatch_subprocess"). */ private ninjaBuildBypass; /** * Transform a clang/clang++ link command into a direct wasm-ld invocation. * clang++ can't fork wasm-ld via posix_spawn in the WASM environment. */ private transformLinkToWasmLd; /** * Pre-create output directories for a build command. * * In a real ninja build, ninja creates output directories before running * each build command. Our JS-side bypass doesn't do this, so clang fails * with "No such file or directory" when trying to write object/dependency * files to CMakeFiles/.dir/ which doesn't exist yet. * * This method scans the command's argv for -o (output) and -MF (depfile) * flags, extracts their parent directories, and creates them in the VFS * overlay (which VFSFS will see via write-through). */ private ensureOutputDirectories; /** * Resolve include/subninja directives in a ninja build file by inlining * the referenced files. CMake generates rules in a separate rules.ninja. */ private resolveNinjaIncludes; /** * Parse a build.ninja file and extract the ordered list of build commands. * Handles CMake-generated ninja files: rules with command templates, * build edges with variable substitution ($in, $out, edge variables). */ private parseBuildNinja; /** * Run a shell command string (for system() / popen() emulation). */ system(command: string, cwd?: string): Promise; /** * Read a file from the kernel VFS. */ getFile(path: string): Promise; /** * List directory contents from the kernel VFS overlay. */ listDir(path: string): Promise; /** * Spawn an isolated WASM process for a tool invocation: * * 1. Load the module factory (cached Emscripten JS glue) * 2. Configure process: ENV, argv, stdout/stderr hooks, FS callbacks * 3. Instantiate the WASM module (creates fresh linear memory) * 4. Pre-populate the process FS with files from the kernel VFS * 5. Call main(argc, argv) * 6. Harvest output files back to the kernel VFS * 7. Discard the process (GC reclaims the linear memory) */ private spawnProcess; /** * Set up the process FS for a tool invocation using VFSFS mounts. * * Instead of copying files or patching lookupPath, this method: * 1. Mounts VFSFS at /usr, /etc (backed by kernel VFS + Asyncify on-demand fetch) * 2. Registers path aliases for sysroot cache mapping * 3. Creates essential synthetic files (shim, config, stubs) * 4. Sets CWD * * File loading is entirely on-demand via Asyncify: the FS syscall * JS imports (listed in ASYNCIFY_IMPORTS) call Module["onPreOpen"]/ * ["onPreStat"] hooks before each syscall, which fetch from CDN → * IDB. Emscripten's Asyncify runtime automatically /** * Create sentinel stub files for LLVM/Binaryen/system tools in /usr/bin/. * cmake and Emscripten Python tools call os.path.exists() on these paths * for compiler detection. Actual execution routes through subprocess dispatch. */ private installToolStubs; /** * Pre-seed the cmake build directory with generated compiler/system info * files. cmake's C++ code (cmGlobalGenerator::EnableLanguage) DELETES * these files before running Determine scripts, then expects * configure_file() to recreate them. The early-return in our patched * Determine scripts fires but skips configure_file(), so the files * vanish permanently. * * Fix: write files to VFSFS fileData + VFS overlay (IndexedDB), AND * register each path in protectedPaths. The VFSFS unlink handler * skips fileData.delete() for protected paths, so cmake's delete is * effectively a no-op on the RAM copy. When cmake re-reads the file, * VFSFS lookup() finds it in fileData and serves it. */ private preSeedCmakeBuildDir; /** * Extract the build directory from cmake's argv (the -B flag). */ private extractCmakeBuildDir; /** * Set up the per-process filesystem for a WASM tool invocation. * * unwinds/rewinds the WASM stack while the fetch Promise resolves. * * When `options.isInfoQuery` is true, only basic dir creation + CWD is done. */ private setupProcessFS; /** * Run a standalone WASM binary compiled by emcc with a minimal WASI runtime. * * When emcc compiles to a .wasm output, it produces a standalone binary that * uses WASI (WebAssembly System Interface) imports for I/O. This method * provides fd_write (stdout/stderr), proc_exit, and stubs for the remaining * WASI snapshot_preview1 imports so that simple programs (printf, return 0) * can execute directly in the browser without Node.js. */ private runWasi; /** * In-memory cache of /usr/lib/emscripten tool sources. The node tool * loader's synchronous fs.readFileSync needs the sources available * without await, but LazyFS only serves CDN files asynchronously — * so preload them once. Lean by design: only the JS toolchain files * (tools/*.mjs, src/*.mjs, src/*.js, node_modules/acorn), NOT the * multi-thousand-file system/ headers that also live in the * emscripten-core bundle. */ private nodeToolFileCache; private isNodeToolFile; private preloadNodeToolFiles; /** * Run an Emscripten-compiled JavaScript file in the browser. * * After emcc produces main.js + main.wasm, this evaluator: * 1. Reads main.js from the kernel VFS. * 2. Reads the companion .wasm file. * 3. Sets up a Module object with wasmBinary + print/printErr hooks. * 4. Evaluates the JS via `new Function()`. */ private runJavaScript; private resolveToolDescriptor; private loadModuleFactory; private parseCommand; } //# sourceMappingURL=tool-runner.d.ts.map