Skip to content

--std --compile doesn't make std, et al. available to compiled executable #1527

Closed

Description

@guest271314

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
undefined

Looking 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`,
  },

https://github.com/guest271314/native-messaging-webassembly/blob/main/install_hosts.js#L146-L150

      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

  1. changed the title [-]--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[/+] on Jun 9, 2026
  2. guest271314 commented on Jun 9, 2026

    @guest271314
    ContributorAuthor

    This works for the native executable https://github.com/quickjs-ng/quickjs/blob/master/qjs.c#L640.

    import * as std from 'qjs:std';
    print(std);
    const output = new Uint8Array([97]);
    std.out.write(output.buffer, 0, output.length);
    std.out.flush();
  3. guest271314 commented on Jun 9, 2026

    @guest271314
    ContributorAuthor

    Alright, reading qjs.c I got std working in the WASI build, too, along with compiling the arbitrary JavaScript code into qjs.c that does not drop to interactive mode or show help. I'll have to look at eval_file or 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.c saved as _qjs.c is attached. The hardcoded part, to test compiling qjs to WASM with emdedded JavaScript that get's evaluated on execution

        //if (!empty_run) {
            js_std_add_helpers(ctx, argc - optind, argv + optind);
    
            /* make 'std' and 'os' visible to non module code */
            //if (load_std) {
                const char *str =
                    "import * as bjson from 'qjs:bjson';\n"
                    "import * as std from 'qjs:std';\n"
                    "import * as os from 'qjs:os';\n"
                    "globalThis.bjson = bjson;\n"
                    "globalThis.std = std;\n"
                    "globalThis.os = os;\n"
                    "print('hello world');\n"
                    "const output = new Uint8Array([97]);\n"
                    "std.out.write(output.buffer, 0, output.length);\n"
                    "std.out.flush();";
                eval_buf(ctx, str, strlen(str), "<input>", JS_EVAL_TYPE_MODULE);
            //}
    /*
    clang: warning: argument '--target=wasm32-wasi' is deprecated, use --target=wasm32-wasip1 instead [-Wdeprecated]
    make CFLAGS="--target=wasm32-wasip1" -C build qjs_exe
    make: Entering directory '/media/user/x/quickjs/build'
    [ 45%] Built target qjs
    [ 63%] Built target qjs-libc
    [ 72%] Linking C executable qjs
    clang: warning: argument '--target=wasm32-wasi' is deprecated, use --target=wasm32-wasip1 instead [-Wdeprecated]
    [100%] Built target qjs_exe
    make: Leaving directory '/media/user/x/quickjs/build'
    mv build/qjs build/qjs-wasi.wasm
    wasmtime build/qjs-wasi.wasm
    hello world
    a

    _qjs.c

  4. guest271314 commented on Jun 9, 2026

    @guest271314
    ContributorAuthor

    I guess it should be CFLAGS="target=wasm32-wasip1" passed to make.

    This gets the WASM module down to 960.3 KiB from 1.4 MiB

    binaryen/bin/wasm-opt build/qjs-wasi.wasm -O4 --shrink-level 5 --strip-debug -o build/qjs-wasi-opt.wasm
  5. guest271314 commented on Jun 9, 2026

    @guest271314
    ContributorAuthor

    Any interest in making this a thing here?

    E.g.,

    tiny-qjs-wasi <input.js> -o <output.wasm>
    tiny-qjs-wasi "print('hello world')" # output.wasm
  6. saghul commented on Jun 9, 2026

    @saghul
    Contributor

    Not sure what you are trying to do. --std and --compile are not designed to interact. --std only applies to eval really, scripts can import.

    Can you please clarify what is not working as expected, or what the feature request is?

  7. guest271314 commented on Jun 9, 2026

    @guest271314
    ContributorAuthor

    I think the bug is qjs-wasi.wasm --compile emits something that is not executable by wasmtime.

    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.wasm instead of wasmtime 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 qjs target before compilation to WASM by WASI-SDK, and eval that script when qjs-wasi.wasm is executed.

    What I'm really trying to do is integrate qjs-wasi.wasm into this list of languages and projects that compile the same algorthm to WASM I am benchmarking. The qjs-wasi.wasm version does not follow the same pattern - because it's wasmtime <WASM binary> _script.js_, that is,

    #!/bin/bash
    exec /home/user/bin/wasmtime run --dir=. /home/user/bin/qjs-wasi.wasm --std -m -e 'function _(){let t=new Uint32Array(1);std.in.read(t.buffer,0,4);let s=new Uint8Array(t[0]),w=std.in.read(s.buffer,0,s.length);return s}function g(t){if(t.length<=1048576){let n=new Uint8Array(4);n[0]=t.length>>0&255,n[1]=t.length>>8&255,n[2]=t.length>>16&255,n[3]=t.length>>24&255;let f=new Uint8Array(4+t.length);f.set(n,0),f.set(t,4),std.out.write(f.buffer,0,f.length),std.out.flush();return}let i=0;while(i<t.length){let n,f=i+1048576-8;if(f>=t.length)n=t.length;else if(n=t.indexOf(44,f),n===-1)n=t.length;let l=t.subarray(i,n),u=l[0],a=l[l.length-1],c=null,o=null;if(u===91&&a!==93)o=93;else if(u===44){if(c=91,a!==93)o=93}let d=l.length,y=4,C=c!==null,E=o!==null,A=0;if(u===44)A=1,d-=1;let O=4+(C?1:0)+d+(E?1:0),e=new Uint8Array(O),h=O-4;e[0]=h>>0&255,e[1]=h>>8&255,e[2]=h>>16&255,e[3]=h>>24&255;let r=4;if(C)e[r]=c,r++;else if(u===44)e[r]=91,r++;if(e.set(l.subarray(A),r),r+=d,E)e[r]=o;std.out.write(e.buffer,0,e.length),std.out.flush(),std.gc(),i=n}}function M(){while(!0){let t=_();g(t)}}try{M()}catch(t){std.exit(0)}
    '

    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 the qjs-wasi.wasm original 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.c to remove all option passing capabilities, all variables that are not used because no options are passed, and skip straight to embedding JavaScript in qjs.c and eval'ing that emdedded JavaScript when qjs-wasi.wasm is 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?

  8. saghul commented on Jun 9, 2026

    @saghul
    Contributor

    I think the bug is qjs-wasi.wasm --compile emits something that is not executable by wasmtime.

    Certainly. The --compile feature 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.

    What I've done above is abuse qjs.c to remove all option passing capabilities, all variables that are not used because no options are passed, and skip straight to embedding JavaScript in qjs.c and eval'ing that emdedded JavaScript when qjs-wasi.wasm is 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).

    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.

  9. guest271314 commented on Jun 9, 2026

    @guest271314
    ContributorAuthor

    I'll ifdef the compile option out in case of WASM since it won't work.

    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 into qjs.c before compilation to WASM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions