Path: blob/main/core/kernel/src/wasm/import-node.ts
1067 views
import { Options, WasmInstanceAbstractBaseClass, WorkerThread } from "./import";1import { Worker } from "worker_threads";2import { join } from "path";3import process from "node:process";4import debug from "debug";5import IOProviderUsingAtomics from "./io-using-atomics";6import type { WasmInstanceAsync } from "./types";78const log = debug("wasm:import-node");910export class WasmInstance extends WasmInstanceAbstractBaseClass {11protected initWorker(): WorkerThread {12const path = join(__dirname, "worker", "node.js");13return new Worker(path, {14trackUnmanagedFds: false, // this seems incompatible with our use of unionfs/memfs (lots of warnings).15});16}1718protected configureTerminal() {19const stdinListeners: any[] = process.stdin.listeners("data");20for (const f of stdinListeners) {21// save listeners on stdin so we can restore them22// when the terminal finishes23process.stdin.removeListener("data", f);24}25if (this.worker == null) throw Error("configureTerminal - bug");26this.worker.on("exit", () => {27// put back the original listeners on stdin28for (const f of stdinListeners) {29process.stdin.addListener("data", f);30}31});32process.stdin.on("data", (data) => {33if (log.enabled) {34log("stdin", data.toString());35}36this.writeToStdin(data);37});38}39}4041export default async function wasmImportNodeWorker(42wasmSource: string, // name of the wasm file43options: Options44): Promise<WasmInstanceAsync> {45return new WasmInstance(wasmSource, options, IOProviderUsingAtomics);46}474849