Stream: compiler development

Topic: ZigGlue


view this post on Zulip Oskar Hahn (Dec 06 2023 at 19:10):

I would like to help with the zig glue. But I don't know, how to start. If you could point me to the right direction, I could give it a try. (But I am not sure,if I can succeed.)

view this post on Zulip Brendan Hansknecht (Dec 06 2023 at 19:17):

Probably to start would be to look at RustGlue.roc in the repo. The new file would be very similar but generating zig code. It will probably a 1 type at a kind thing. Where you start with a super simple platform that just returns and int and then work outwards with most branches crashing to being with

view this post on Zulip Luke Boswell (Dec 06 2023 at 19:34):

Your roc-wasi platform is actually perfect for this.

I was thinking of starting by getting ZigGlue.roc to just vendor or copy the builtins that we need from crates/compiler/builtins/bitcode/src. I think we can just import the files as bytes directly and write them out.

Or maybe starting with like a single Error tag union in the platform (maybe to represent a couple of IO errors) without payload and then generate the zig for that tag. We can use this for the openFile effect instead of just passing a U8 back to Roc.

view this post on Zulip Folkert de Vries (Dec 06 2023 at 20:27):

in preparation a cleanup of the existing RocGlue.roc might also be useful. The code quality on that is not great

view this post on Zulip Folkert de Vries (Dec 06 2023 at 20:27):

especially consistency between the tag union flavors is hard to judge

view this post on Zulip Folkert de Vries (Dec 06 2023 at 20:27):

also it has known bugs, like deriving Hash or Eq when that is invalid

view this post on Zulip Oskar Hahn (Dec 07 2023 at 00:03):

I don't know any rust. I tried it once and thought, that this is above my skills. This is the reason, I use zig for my platforms.

Currently, there is the directory glue/src/ with the file RustGlue.roc and some .rs files. Should the ZigGlue.roc file and all the .zig files be in the same directory, or should they be separated by language?

view this post on Zulip Luke Boswell (Dec 07 2023 at 00:28):

PR #6211 makes a start on this. I think it would be acceptable to merge this and then add additional functionality, or perhaps we want to get further along on a branch before adding to main? @Oskar Hahn what do you think?

view this post on Zulip Brendan Hansknecht (Dec 07 2023 at 00:33):

One other question: should we put this not in the roc repo? I know that long term we theoretically want these to be community maintained and to live elsewhere. Rust is special because it is an example for other to look at and we use it for roc itself.

view this post on Zulip Luke Boswell (Dec 07 2023 at 00:47):

I think that makes sense in future, but for now I don't have any concerns with it being in the repo. If anything, I think it supports us maturing glue itself by having two independent implementations. We can always move it at a later time. It's buried rather deep and all.

view this post on Zulip Richard Feldman (Dec 07 2023 at 01:17):

yeah that works for me too, as long as the expectation is clear that we'll move it in the future :thumbs_up:

view this post on Zulip Oskar Hahn (Dec 07 2023 at 05:59):

Luke Boswell said:

PR #6211 makes a start on this. I think it would be acceptable to merge this and then add additional functionality, or perhaps we want to get further along on a branch before adding to main? Oskar Hahn what do you think?

Perfect. I will have a look

view this post on Zulip Notification Bot (Dec 09 2023 at 09:20):

12 messages were moved here from #compiler development > Casual Conversation by Oskar Hahn.

view this post on Zulip Oskar Hahn (Dec 09 2023 at 09:35):

I played around a bit with the ZigGlue and was able to generate the glue file for my AoC platform: https://github.com/ostcar/aoc2023/blob/with-glue/platform/glue.zig

https://github.com/roc-lang/roc/compare/main...ostcar:zig-glue

There are some things, that I don't understand or that would be helpful:

view this post on Zulip Oskar Hahn (Dec 09 2023 at 09:39):

Oskar Hahn said:

Oh, this was my mistake. I did not give the tagunion any name in roc. I only used [Part1, Part2]. I get it, that it would be very hard for roc glue to give it a name :big_smile: After adding (and using) Part : [Part1, Part2], it works.

view this post on Zulip Luke Boswell (Dec 09 2023 at 09:54):

I had a look at dbg, it's easy enough to add to the platform by adding the following to crates/glue/src/lib.rs. But I suspect roc is stripping out dbg or ignoring these so this isn't being called for some reason. Requires further investigation, but it's getting late for me.

#[no_mangle]
pub unsafe extern "C" fn roc_dbg(loc: *mut roc_std::RocStr, msg: *mut roc_std::RocStr, src: *mut roc_std::RocStr) {
    eprintln!("[{}] {} = {}", &*loc, &*src, &*msg);
}

view this post on Zulip Oskar Hahn (Dec 09 2023 at 09:57):

You have to call roc glue --dev ...

view this post on Zulip Luke Boswell (Dec 09 2023 at 09:58):

Woo, teamwork FTW :octopus:

view this post on Zulip Luke Boswell (Dec 09 2023 at 10:00):

It doesn't quite work for me because the dev backend isn't complete on arm64, so I hit an assertion, but it definitely looks like its trying. So I think we should be able to change something somewhere so the dbg's are called when using roc glue

view this post on Zulip Richard Feldman (Dec 09 2023 at 12:20):

oh yeah those should definitely Just Work with no flags needed or anything...I think it's just that nobody's ever tried to use dbg in glue before :big_smile:


Last updated: Jul 26 2025 at 12:14 UTC