Stream: compiler development

Topic: main : Str -> Task examples?


view this post on Zulip Richard Feldman (Jul 14 2023 at 00:12):

do we have any examples of main : Str -> Task Str [] working?

view this post on Zulip Richard Feldman (Jul 14 2023 at 00:12):

I just tried main : Str -> Task Str [] and roc glue generated:

#[repr(C)]
#[derive(Debug, Clone)]
pub struct RocFunction_84 {
    closure_data: roc_std::RocList<u8>,
}

impl RocFunction_84 {
    pub fn force_thunk(mut self, arg0: ()) -> roc_std::RocStr {
        extern "C" {
            fn roc__mainForHost_3_caller(
                arg0: *const (),
                closure_data: *mut u8,
                output: *mut roc_std::RocStr,
            );
        }

        let mut output = std::mem::MaybeUninit::uninit();
        let ptr = &mut self.closure_data as *mut _ as *mut u8;

        unsafe { roc__mainForHost_3_caller(&arg0, ptr, output.as_mut_ptr()) };

        // ownership of the closure is transferred back to roc
        core::mem::forget(self.closure_data);

        unsafe { output.assume_init() }
    }
}

pub fn mainForHost(arg0: roc_std::RocStr) -> RocFunction_84 {
    extern "C" {
        fn roc__mainForHost_1_exposed_generic(_: *mut RocFunction_84, _: roc_std::RocStr);
    }

    let mut ret = std::mem::MaybeUninit::uninit();

    unsafe { roc__mainForHost_1_exposed_generic(ret.as_mut_ptr(), arg0) };

    unsafe { ret.assume_init() }
}

view this post on Zulip Richard Feldman (Jul 14 2023 at 00:14):

running it gave this output:

Undefined symbols for architecture arm64:
  "_roc__mainForHost_3_caller", referenced from:
      __ZN4host14RocFunction_8411force_thunk17hb922055437fe1593E in macos-arm64.o
ld: symbol(s) not found for architecture arm64

view this post on Zulip Richard Feldman (Jul 14 2023 at 00:14):

I tried changing mainForHost3 to mainForHost1 (like it usually is), and then the program segfaulted :sweat_smile:

view this post on Zulip Richard Feldman (Jul 14 2023 at 00:43):

hm, actually it might be more about main : Str -> Task :thinking:

view this post on Zulip Richard Feldman (Jul 14 2023 at 00:43):

if I change basic-cli to take a Str, same thing

view this post on Zulip Richard Feldman (Jul 14 2023 at 00:44):

and basic-cli already has main : Task {} I32 so it's evidently capable of sending data to the host by way of a Task

view this post on Zulip Richard Feldman (Jul 14 2023 at 01:57):

I have an idea for a truly heinous workaround for this, but we should definitely fix it :laughing:

view this post on Zulip Brendan Hansknecht (Jul 14 2023 at 02:52):

Shouldn't roc__mainForHost_1_exposed_generic take the roc string as a pointer?

view this post on Zulip Brendan Hansknecht (Jul 14 2023 at 02:53):

Also, probably best to look out the llvm ir of a small example and debug from what that generates

view this post on Zulip Oskar Hahn (Jul 14 2023 at 05:10):

Here is an example with main : List U8 -> Task using zig.

https://github.com/ostcar/roc-wasm-platform/blob/9c13cf01a1a6997a584d74b141aa62cb4bdce1c1/src/host.zig#L87

It takes the argument as a pointer

view this post on Zulip Folkert de Vries (Jul 14 2023 at 08:14):

you really need to look at the LLVM IR here


Last updated: Jul 06 2025 at 12:14 UTC