diff options
author | 2023-08-02 16:27:36 -0700 | |
---|---|---|
committer | 2023-08-02 16:27:36 -0700 | |
commit | c2a77cf7ec9de9eadf938046bdf78e58561c8a6d (patch) | |
tree | 0f90f1b323061455875333c9f40592b303585973 /src/js/wasi-runner.js | |
parent | 7656b4b17e91f15b58eeab8f45b78c416ec6a045 (diff) | |
download | bun-c2a77cf7ec9de9eadf938046bdf78e58561c8a6d.tar.gz bun-c2a77cf7ec9de9eadf938046bdf78e58561c8a6d.tar.zst bun-c2a77cf7ec9de9eadf938046bdf78e58561c8a6d.zip |
Rewrite built-in modules to use CommonJS over ESM (#3814)
* stfdsafsd
sadffdsa
stuff
finish commonjs stuff
asdf
not done but work
not done but work
not done yet but this is how far i am
remove files
lol
update built files
uncomment everything in events lol
export default
stuff
* afdsafsd
* its not perfect but almost done
* okay
* cool
* remove temp file
* finish rebase
* revert settings.json
* a
* ch-ch-ch-ch-changes
* okay
* remove this check in release for now
* sxdcfghnjm,
* lkjhgf
* fmt
* filename can be null
* Update NodeModuleModule.h
* weee
* fmt
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/js/wasi-runner.js')
-rw-r--r-- | src/js/wasi-runner.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/js/wasi-runner.js b/src/js/wasi-runner.js new file mode 100644 index 000000000..bd9e2d56c --- /dev/null +++ b/src/js/wasi-runner.js @@ -0,0 +1,51 @@ +/** This file is used when a .wasm file is ran. + * The transpiled contents of `./node/wasi.js` is pasted into the top of this file. + */ +import { WASI } from "node:wasi"; + +const filePath = process.argv.at(1); +if (!filePath) { + var err = new Error("To run a wasm file with Bun, the first argument must be a path to a .wasm file"); + err.name = "WasmFileNotFound"; + throw err; +} + +// The module specifier is the resolved path to the wasm file + +var { + WASM_CWD = process.cwd(), + WASM_ROOT_DIR = "/", + WASM_ENV_STR = undefined, + WASM_USE_ASYNC_INIT = "1", +} = process.env; + +var env = process.env; +if (WASM_ENV_STR?.length) { + env = JSON.parse(WASM_ENV_STR); +} + +const wasi = new WASI({ + args: process.argv.slice(1), + env, + preopens: { + ".": WASM_CWD || process.cwd(), + "/": WASM_ROOT_DIR || "/", + }, +}); + +let source = globalThis.wasmSourceBytes; +if (!source) { + const fs = Bun.fs(); + const file = import.meta.path; + source = fs.readFileSync(file); +} + +const wasm = await WebAssembly.compile(source); + +const instance = !Number(WASM_USE_ASYNC_INIT) + ? new WebAssembly.Instance(wasm, wasi.getImports(wasm)) + : await WebAssembly.instantiate(wasm, wasi.getImports(wasm)); + +wasi.start(instance); + +process.reallyExit(0); |