Stream: beginners

Topic: FFI Support?


view this post on Zulip Nikita Tchayka (Jan 26 2023 at 16:05):

Hi, I was thinking about tinkering with Roc, but the project I have in mind requires FFI. I understand that Roc compiles to WASM, so calling "exported" WASM functions is okay. Does Roc support this?

view this post on Zulip Brendan Hansknecht (Jan 26 2023 at 18:02):

Every interaction with a platform in Roc is wrapped in an Effect in Roc, but otherwise, yes, roc has FFI. It is just a more constrained FFI to keep roc pure.

view this post on Zulip Brendan Hansknecht (Jan 26 2023 at 18:02):

For wasm, it can call other js and wasm functions that are available in to the wasm environment.

view this post on Zulip Brendan Hansknecht (Jan 26 2023 at 18:03):

As a note, Roc also compiles to native arm and x86. For those, it uses c ffi for communication with the platform.

view this post on Zulip Nikita Tchayka (Jan 26 2023 at 22:20):

Nice, is there any docs related to FFI? I have experience in Haskell, Idris, Elm, etc.. so any pointers/similar code would be very appreciated <3

view this post on Zulip Brendan Hansknecht (Jan 26 2023 at 23:17):

@Brian Carroll any best example for helping someone start with a wasm platform?

view this post on Zulip Brian Carroll (Jan 27 2023 at 10:44):

We only have one example and it's either hello world or "platform switching", I can't remember which.

The majority of our examples and projects are targeting machine code rather than Wasm.

Is this project targeting the browser? Or running in another wasm interpreter? It makes a big difference to the platform design. The hello world example is for the browser.

I'm working on a virtual DOM platform but there's a long way to go.

view this post on Zulip Nikita Tchayka (Jan 27 2023 at 11:18):

The idea is to integrate with an existing Node framework. So I was thinking using Node's wasm capabilities to make Roc interact with it

view this post on Zulip Richard Feldman (Jan 27 2023 at 12:31):

Node has C interop (I don't know the details, but I know there's some way to do it!)

view this post on Zulip Richard Feldman (Jan 27 2023 at 12:32):

so that's an option too!

view this post on Zulip Nikita Tchayka (Jan 27 2023 at 14:10):

Is there any info on interoperability with Roc? Compiler source code works for me as well, just figuring out which thread to pull

view this post on Zulip Brian Carroll (Jan 27 2023 at 18:35):

No we don't have any docs on that yet, the only way is to read the source code of the example platforms and talk to people!

view this post on Zulip Brian Carroll (Jan 27 2023 at 18:36):

Ah ok Wasm on Node makes sense. Nobody has attempted it yet but it would be cool to see how it goes!

view this post on Zulip Brian Carroll (Jan 27 2023 at 18:43):

examples/virtual-dom-wip would be the closest thing we have. It doesn't build because of some features that aren't complete in the compiler.
But you can check out host.zig and host.js on the client side.
It's tricky to interface directly from JS to Roc because you need to make sure all the bytes are in the right locations. It's possible in js but it's nicer in lower level languages. So I used Zig for some glue code.

view this post on Zulip Nikita Tchayka (Jan 30 2023 at 12:24):

Thanks for the pointers, I really appreciate this!


Last updated: Jul 06 2025 at 12:14 UTC