Repository navigation
Description
What I'm trying to do that found this is compile a script with qjs-wasi.wasm such that a WASM module is output and runs without a file reference.
What actually happens now.
echo "print(globalThis?.std)" > test.js
qjs --std -m --compile test.js -o test
chmod +x test
./test
undefinedLooking at the build workflow qjs-wasi.wasm is built with WASI-SDK. I'm not sure what the result of --compile is expected to be in WASM, nonetheless a .wasm file extension is output. It errors when running with wasmtime though
wasmtime run --dir=. ./qjs-wasi.wasm --std --compile ./test.js -o test-wasm
file test-wasm
test-wasm: WebAssembly (wasm) binary module version 0x1 (MVP)
wasmtime run test-wasm
Error: failed to parse WebAssembly module
Caused by:
malformed section id: 26 (at offset 0x177625)
What I'm doing now https://github.com/guest271314/native-messaging-webassembly/blob/main/install_hosts.js#L108-L112
"nm_qjs_wasi": {
"i": "nm_qjs_wasi.js",
"description": "QuickJS WASI Native Messaging host",
"compile":
`bun -e 'await fetch("https://github.com/quickjs-ng/quickjs/releases/latest/download/qjs-wasi.wasm").then(async(r)=> [r.headers.get("content-disposition").match(/(?<=filename=).+$/)[0], await r.bytes()]).then(async([filename,file])=>await Bun.write(filename,file))' && bun build --minify --outfile=nm_qjs_wasi_min.js nm_qjs_wasi.js`,
}, const shellscript = host.name === "nm_qjs_wasi"
? `#!/bin/bash
exec /home/user/bin/wasmtime run --dir=. /home/user/bin/qjs-wasi.wasm --std -m -e '${
readFileSync("./nm_qjs_wasi_min.js", "utf8")
}'`What I expect to happen
./test
[object Module]wasmtime run test-wasm # Run the compiled WASM module
[object Module]So I can include qjs-wasi.wasm here https://guest271314.github.io/native-messaging-webassembly/ using wasmtime run test.wasm.
If that is not possible I will probably have to dive in to compiling qjs_exe with WASI-SDK such that when it's called the script I select to run is executed, instead of dropping to a REPL prompt or help.
Either way, I think there should be a way to compile a native executable or WASM module (which doesn't work, now) with std exposed to the compiled binary.
Activity
[-]--std --compile doesn't make std, et al. make std available to compiled executable[/-][+]--std --compile doesn't make std, et al. available to compiled executable[/+]guest271314 commentedon Jun 9, 2026
This works for the native executable https://github.com/quickjs-ng/quickjs/blob/master/qjs.c#L640.
guest271314 commentedon Jun 9, 2026
Alright, reading
qjs.cI gotstdworking in the WASI build, too, along with compiling the arbitrary JavaScript code intoqjs.cthat does not drop to interactive mode or show help. I'll have to look ateval_fileor just use arguments or STDIN to pass code in, instead of this hardcoded first successful run.I commented the unused code per the warnings when building with WASI-SDK 33.0+m. One warning with Clang 22.1.0 on WASI-1.
The modified
qjs.csaved as_qjs.cis attached. The hardcoded part, to test compilingqjsto WASM with emdedded JavaScript that get's evaluated on executionclang: warning: argument '--target=wasm32-wasi' is deprecated, use --target=wasm32-wasip1 instead [-Wdeprecated]_qjs.c
guest271314 commentedon Jun 9, 2026
I guess it should be
CFLAGS="target=wasm32-wasip1"passed tomake.This gets the WASM module down to 960.3 KiB from 1.4 MiB
guest271314 commentedon Jun 9, 2026
Any interest in making this a thing here?
E.g.,
saghul commentedon Jun 9, 2026
Not sure what you are trying to do.
--stdand--compileare not designed to interact.--stdonly applies to eval really, scripts can import.Can you please clarify what is not working as expected, or what the feature request is?
guest271314 commentedon Jun 9, 2026
I think the bug is
qjs-wasi.wasm --compileemits something that is not executable bywasmtime.The feature request is a way to compile a script to embed a script into the engine before compilation to WASM, so the user can do this
wasmtime output.wasminstead ofwasmtime qjs-wasi.wasm script.js.One way to do that is somehow emit valid WASM from
https://github.com/guest271314/native-messaging-webassembly/blob/main/install_hosts.jsqjs-wasi.wasm.Or, provide a means to embed a script into the
qjstarget before compilation to WASM by WASI-SDK, and eval that script whenqjs-wasi.wasmis executed.What I'm really trying to do is integrate
qjs-wasi.wasminto this list of languages and projects that compile the same algorthm to WASM I am benchmarking. Theqjs-wasi.wasmversion does not follow the same pattern - because it'swasmtime <WASM binary> _script.js_, that is,whereas the rest on the list are just
wasmtime <WASM binary>. I think that's why the developer who PR'd this https://github.com/guest271314/native-messaging-webassembly/blob/main/.github/workflows/build-and-benchmark.yml into my repository didn't touch theqjs-wasi.wasmoriginal version of that benchmark build from here https://github.com/guest271314/native-messaging-webassembly/blob/main/install_hosts.js.What I've done above is abuse
qjs.cto remove all option passing capabilities, all variables that are not used because no options are passed, and skip straight to embedding JavaScript inqjs.cand eval'ing that emdedded JavaScript whenqjs-wasi.wasmis executed by a WebAssembly runtime. Not elegant, but achieves what I am trying to do with regard to just doing a JavaScript to WASM toolchain - directly with QuickJS (NG).Make sense?
saghul commentedon Jun 9, 2026
Certainly. The
--compilefeature essentially attaches the given script to the executable. In this case the executable is not a real executable since you use wasmtime to run it.This is simply not going to work.
That is the way really. Use QuickJS as a library to run the code you want. You can also skip the REPL for example which has some size, etc.
I'll ifdef the compile option out in case of WASM since it won't work.
guest271314 commentedon Jun 9, 2026
I guess that what will come out of this, officially, for this repository.
The branch that I'm probably gonna develop is what I've done above to
qjs.c. Emdedding to JavaScript intoqjs.cbefore compilation to WASM.