WebAssembly / wabt

The WebAssembly Binary Toolkit
Apache License 2.0
6.82k stars 695 forks source link

wasm-interp for the web #2157

Closed iBicha closed 1 year ago

iBicha commented 1 year ago

Can wasm-interp be built for wasm? Solving for https://github.com/WebAssembly/binaryen/issues/5530

sbc100 commented 1 year ago

I don't see why not, in fact I think we already do build it as part of CI: https://github.com/WebAssembly/wabt/blob/e426908d8f0022b13a7ba39725e96fcbe18cb220/.github/workflows/build.yml#L68-L80

iBicha commented 1 year ago

I don't see why not, in fact I think we already do build it as part of CI:

https://github.com/WebAssembly/wabt/blob/e426908d8f0022b13a7ba39725e96fcbe18cb220/.github/workflows/build.yml#L68-L80

Perhaps I need to look closer, but after inspecting the artifacts, I don't see something like wasm-interp.wasm which is what I'm actually after (similar to https://github.com/wasm3/wasm3/releases/download/v0.5.0/wasm3-wasi.wasm)

I appreciate the guidance! I'm pretty new to wabt

sbc100 commented 1 year ago

I don't think we public the emscripten build as part of the release. It should be fairly straight forward to build it yourself if you need to.

sbc100 commented 1 year ago

If you want to create a PR that will stash the CI artifacts somewhere that would be great too (then I think you could download them from any CI run).

iBicha commented 1 year ago

What I'm saying is that the github action might not be producing the actual interpreter in wasm. I ran the build locally, and the produced artifacts look like test artifacts, not wasm-interp.wasm

> find *.wasm
callback.wasm
finalize.wasm
global.wasm
hello.wasm
hostref.wasm
memory.wasm
multi.wasm
reflect.wasm
serialize.wasm
start.wasm
table.wasm
threads.wasm
trap.wasm
sbc100 commented 1 year ago

Yes those are not the build artifacts. How did you do that build? The build artifacts end up the bin directory inside the build directory.

I would do something like this:

$ mkdir build
$ cd build
$ emcmake cmake -G Ninja ..
$ ninja wasm-interp
$ node wasm-interp.js 
wasm-interp: expected filename argument.

It looks like wabt build to pure JS by default, if you want the wasm version you would need to edit CMakeLists.txt and remove -sWASM=0. (We should probably remove that).

iBicha commented 1 year ago

I followed the CI (docker was nice, since I didn't have to worry about the right deps)

docker run -di --name emscripten -v $(pwd):/src emscripten/emsdk:latest bash
docker exec emscripten emcc -v
docker exec emscripten emcmake cmake .
docker exec emscripten make -j 2 VERBOSE=1

I removed -sWASM=0, but that didn't change the output.

sbc100 commented 1 year ago

And do you get a wasm-interp.js file being produced?

sbc100 commented 1 year ago

BTW, you don't need to use docker, you can also just install emsdk in your machine if you want.

iBicha commented 1 year ago

And do you get a wasm-interp.js file being produced?

Yes, and I'm trying make it produce wasm-interp.wasm I dug a bit in the settings, and then changed the extra link flags to

    set(EXTRA_LINK_FLAGS
      "${EXTRA_LINK_FLAGS} -sSINGLE_FILE -Oz -sSTANDALONE_WASM -sALLOW_MEMORY_GROWTH"
    )

But ninja.build still contains target for wasm-interp.js, and still produces that file, not the wasm

sbc100 commented 1 year ago

Oh you have to remove -sSINGLE_FILE too since otherwise it embeds the wasm inside the JS.

BTW do you really want to the wasm file or do you just want to run it on the web (in which case you probably want to the JS file too).

iBicha commented 1 year ago

Oh you have to remove -sSINGLE_FILE too since otherwise it embeds the wasm inside the JS.

BTW do you really want to the wasm file or do you just want to run it on the web (in which case you probably want to the JS file too).

I will try removing the SINGLE_FILE flag and see!

Yeah sorry for leaving an important detail (related to the ticket I mentioned) I do not have a js runtime. What I have is a tool that is capable of transforming wasm into brightscript. But the transformed code has limitations. So I was hoping I can built wasm-interp into wasm, transform that into brightscript, and use it to load other wasm modules, bringing functionality to Roku OS apps.

iBicha commented 1 year ago

Yup, removing SINGLE_FILE produces the wasm module! I will close this issue, since this is a very niche thing, so I doubt someone would need to build into wasm specifically. Thank you for the guidance @sbc100