Hi! New to Roc, was inspired to learn it by some of the SYCL talks :)
I come from Zig land, so I'm interested in jumping right in to writing a platform to understand the low level details! Is there a reference anywhere for the functions I need to provide to roc, and how to call the entrypoint?
I've had a look at some example code but I'd like to know where the "source of truth" is for all the function names etc.
Not averse to reading the source, if that's the answer ^-^
@Silver https://github.com/roc-lang/roc/tree/main/examples/platform-switching/zig-platform :)
Also this conversation may be helpful https://roc.zulipchat.com/#narrow/stream/302903-Writing-a-platform/topic/How.20does.20.22roc__mainForHost_1_exposed_generic.22.20work.3F/near/435340464
I've been down the rabbit hole a bit lately on platform dev. When I get a spare moment, I'd like to try and write up something to help others with this, so any questions you have are really helpful.
@Zeljko Nesic Awesome! That's a great reference, thanks
I'm struggling a bit with understanding all the roc syntax involved - there's provides
which, in a platform
file, seems to export functions using C names? but I'm not entirely sure how those names are computed, since there's a number and sometimes a _generic
or _caller
or something involved
there's also hosted
files which seem to handle interfacing with functions exposed by the host implementation, but I'm not clear on how those interact with purity.
There doesn't seem to be much documentation on Roc's effect system so I'm not really sure how that works - there's Effect, but also Task, which wraps Effect? Though ofc both of those are defined by the platform so I guess it's just convention?
A lot of this is not documented because it is a WIP and there are some changes planned I think. Zulip is probably the best way to find this information out.
I'm currently doing some touristing in Milan before I get on a plane back to Aus, but happy to help where I can.
https://github.com/roc-lang/roc/issues/6382
For example this is how we want to change the interface between roc apps and the host.
Actually, there is a lot described in GH issues, but it's a bit scattered.
Thanks, that's good to know :)
I've had a look at the compiler source code and I think I understand the generated function names a little better now, but I'm still not 100% clear on the difference between roc__thing_0_exposed
and roc__thing_0_exposed_generic
- why does the platform-switching example use the _generic
variant, and when would you use the non-generic version?
Im not sure, I think it might have been testing two different APIs or a WIP thing.
Alright :)
How do I run roc glue
? I'm not sure what arg to give it for GLUE_SPEC and it's not giving terribly helpful errors when I try to guess haha
roc glue crates/glue/src/RustGlue.roc glue-stuff/ path/to/platform/main.roc
The first argument is the path to a glue spec, the second is the dir path to generate files into, the last is the platform you want to generate for. I suggest starting with a super basic platform.
Also this might help
https://roc.zulipchat.com/#narrow/stream/316715-contributing/topic/Glue.20generate.20Types.20in.20JSON/near/434573060
I find it useful to encode the types from the compiler into JSON and then I can pretty format in my editor and easily see what the data is.
hmm okay, I guess I need a clone of the compiler source code for that then? since the glue specs don't seem to be distributed with the nightlies
That's sort of annoying since I was hoping to integrate it into my Zig build process
Well no, you can use a package for the glue
I have an example somewhere
Looks like the Zig glue is for an older version of Zig too, that's unfortunate
I'll probably just handroll it for now :)
Yeah thats probably best. If you are interested in helping with Zig glue please let me know.
I'd love to at some point, but I'll prolly focus on just learning the language first! ^-^
You should be able to use this...
app [makeGlue] {
pf: platform "https://github.com/lukewilliamboswell/roc/releases/download/test/olBfrjtI-HycorWJMxdy7Dl2pcbbBoJy4mnSrDtRrlI.tar.br",
json: "https://github.com/lukewilliamboswell/roc-json/releases/download/0.7.0/xuaMzXRVG_SEhOFZucS3iBozlRdObWsfKaYZMHVE_q0.tar.br",
}
import pf.Types exposing [Types]
import pf.File exposing [File]
import json.Core exposing [json]
makeGlue : List Types -> Result (List File) Str
makeGlue = \typesByArch ->
typesByArch
|> List.map convertTypesToFile
|> List.concat staticFiles
|> Ok
staticFiles : List File
staticFiles = []
convertTypesToFile : Types -> File
convertTypesToFile = \types ->
content =
Encode.toBytes types json
|> Str.fromUtf8
|> Result.withDefault "INVALID UTF8 ENCODED FROM JSON"
{ name: "types.json", content }
Im just on my phone, but that's using a URL package of glue I have been using for testing things.
Ah interesting, thanks!
Because glue spec is an app that is just a plugin for the compiler there are only .roc
source files in the package and no prebuilt binaries.
Also if anyone is interested you can make your own bundle for glue package using something like roc build --bundle .tar.gz crates/glue/platform/main.roc
and upload that somewhere.
:)
image.png
Got effects working
Wasn't too bad once I figured out what everything does heheh
In the platform
header, what's the first {}
in requires {} { main : Whatever }
? I've only ever seen it empty so I'm intrigued as to what might go in it :)
You can pass Types to the platform
Types that are defined in the app
Oh neat! So the first one is types, and the second one is defs?
https://github.com/lukewilliamboswell/roc-ray/blob/main/examples/pong.roc
Here's an example
Cool!
Is there any way to introspect those types from the host implementation? :eyes:
Or reflection in the platform would work too I guess, if I could generate data to pass to the host
I'm not yet sure how much type-level hackery Roc allows haha
No reflection. I don't think its possible for the host to know beforehand as it's compiled separately from the app.
aww that's a shame, oh well :)
Silver said:
I'm not yet sure how much type-level hackery Roc allows haha
From my exploration, I don't think platform exposes much hacking for types. Though, I would really like to see it in future versions.
omg i only just realised I put this thread in #announcements hahahaha I'm so sorry, I meant to put this in #beginners
Is there a way to move it?
@Anton
moved!
Thanks! Sorry for the trouble haha
Last updated: Jul 06 2025 at 12:14 UTC