Stream: show and tell

Topic: wasm platform


view this post on Zulip Oskar Hahn (Jun 25 2023 at 15:41):

With a lot of help from all of you, specially @Luke Boswell I was able to create a wasm platform.

https://github.com/ostcar/roc-wasm-platform

It can be used to create wasm files with roc, that accept an arbitrary value (for example a struct) as input and returns an arbitrary value as output. The only requirement is, that it can be serialized to json.

I added two examples.

One is a webserver written in go. It sends a (simple) request to roc and expects a (simple) response.

The other is the echo example from roc that can be called from javascript. It accepts a string and returns the echo of that string.

I would love to get some feedback and code reviews :innocent:

view this post on Zulip Anton (Jun 25 2023 at 17:30):

This would be a cool project to add to @Luke Boswell's roc awesome repo :)

view this post on Zulip Anton (Jun 26 2023 at 10:27):

The code looks good @Oskar Hahn, my only remark is that I'd also put some of the explanations in the "How to use it" section of the README as comments in the code.

view this post on Zulip Oskar Hahn (Jul 10 2023 at 21:26):

I was able to support Tasks in the wasm platform.

The effects are independent from the platform. The wasm-runtime and the roc-application have to agree on the effects but the roc-platform does not have to be adjusted.

With this example, I defines effects in javascript to get the current time and read and write to the local storage. The roc application uses this effects.

The code needs a lot of cleanup (and some memory deallocations). But I am excited, that it works :octopus:

view this post on Zulip Brendan Hansknecht (Jul 11 2023 at 01:10):

This is an outdated example, but I think the idea should still hold:

You can have multiple effect functions in js instead of sending a string over for the type of effect to do. Just make a function for each effect in the env of the wasm module: https://github.com/bhansconnect/functional-mos6502-web-performance/blob/c0033c4bf22f690320b7b56f7e7ede3a7dc44f32/implementations/roc-effectful/platform/host.js#L18-L20

view this post on Zulip Oskar Hahn (Jul 11 2023 at 06:24):

Brendan Hansknecht said:

You can have multiple effect functions in js instead of sending a string over for the type of effect to do. Just make a function for each effect in the env of the wasm module: https://github.com/bhansconnect/functional-mos6502-web-performance/blob/c0033c4bf22f690320b7b56f7e7ede3a7dc44f32/implementations/roc-effectful/platform/host.js#L18-L20

This looks nice, but I don't think, that this is possible in my case.

If the wasm-runtime (like Javascript) defines the roc_fx_{} functions, then they need to be added to the hosted Effect module in the roc-platform. But my goal is, that you can build a roc-wasm-application without the need to modify the wasm-platform.

view this post on Zulip Brendan Hansknecht (Jul 11 2023 at 14:19):

Interesting, I'm not sure I understand your goals. To me adding a new branch to your method in the host.js file in the platform and adding a new effect to the hosted module are really the same. Either way, you are wrapping another function in the platform and giving roc access to it. The string passing and matching version will just be much slower and potentially more error prone in practice.

view this post on Zulip Oskar Hahn (Jul 11 2023 at 16:02):

You are correct, that the string passing is much slower.

You don't have to add the effect to the platform, but only to the application. So the platform does not have to be modified. It can be imported via an url.

For you, it will probably be much better to write your own platform. But for less experienced users, it is easier to use a generic platform and just write some functions in javascript (or another wasm-host language like go) and the roc application.

view this post on Zulip Brendan Hansknecht (Jul 11 2023 at 16:47):

Ah, so the application would also define the JS function that interprets the string commands? I guess I had assumed host.js would be defined and downloaded with the platform.

view this post on Zulip Oskar Hahn (Jul 11 2023 at 18:57):

Yes. The naming was misleading. I now renamed host.js to app.js to make it clearer.


Last updated: Jul 06 2025 at 12:14 UTC