Compiling to WASM for JS

Following a few posts I was able to get Crystal to compile to WASM with wasi. From what I understand, WASI is just the basic WASM without any Javascript stuff added. It sounds like in order to get WASM actually in the web, it needs to target emscripten?

Has anyone had success doing this?

> crystal build sum.cr -o sum.wasm --target wasm32-emscripten

In /usr/share/crystal/src/string.cr:1:1

 1 | require "c/stdlib"
     ^
Error: can't find file 'c/stdlib'

I tried to changing the target, but get some missing libs. I know for the WASI stuff, I needed to build GitHub - lbguilherme/wasm-libs and copy the wasi libs over. I assume I’m missing something for doing emscripten, but I’m not sure what.

I also have wasmer installed, but didn’t see any docs on converting the format over (if that’s even a thing)… My goal is to use WebAssembly.instantiateStreaming() - WebAssembly | MDN and access WASM functions from javascript.

1 Like

These shards should hell with what you’re looking for:

7 Likes

@noaheverett had a tutorial about webassembly in Crystal at WebAssembly Host Functions in Crystal by @noah | Glue

3 Likes

Yeah, I read those. They are really great posts! Though, they are meant for server-side WASM, and not client-side. Still totally recommended though. Thanks

1 Like