Foreign import webassembly module function

Hello, I’m looking for creating a wasm function purescript binding. I followed the emscripten tutorial and created a playground here. This is working fine with the default generated javascript interface in nodejs, see the Main.{js,purs} file working with spago run where purescript code can call the wasm function (e.g. addInt :: Int -> Int -> Int). However that doesn’t work in the browser, see the main.html and loader.js file. It seems like I would need to use WebAssembly.instantiateStreaming API with an XMLHttpRequest promise.

Are there existing wasm purescript binding example? Should I update the binding to include the Promise Effect? Or is it possible to somehow hide the loading part in the browser case, and keep the same binding? I’m not very familiar with nodejs vs webapi differences, any help would be very helpful :slight_smile: thanks!

3 Likes

I’m not sure if this covers your scenario but I know that @kritzcreek is working on the WASM binding in this new lib. It is still WIP I think.
Christoph is also streaming his exploration on twitch: https://www.twitch.tv/videos/794810765 and I really love this hacking series… and there is a new episode from yesterday - really cool! Thanks a lot for this series and the lib @kritzcreek!

5 Likes

Thank you @paluh and @kritzcreek for the pointer, I guess the runWasm function is what I’m looking for.

Though I’m not familiar with async ffi, to be able to call the inst.main function from Purescript, do you think this binding would be possible: loadWasm :: String -> Aff { main :: Unit -> Unit }?

It seems like I had issues with the loader and javascript module generated by emscripten. After patching the code to work synchronously I was able to use these purescript FFIs:

foreign import withWasm :: Fn2 String (WASM -> Effect Unit) (Effect Unit)
foreign import addInt :: Fn3 WASM Int Int Int

For the record I documented that process in the hellowasm playground.
Cheers :slight_smile:

2 Likes