Stream: beginners

Topic: panic on platform requires


view this post on Zulip dank (Mar 15 2023 at 14:27):

in platforms, having more than one required function panics with "thread 'main' panicked at 'There were still outstanding Arc references to module_ids', crates/compiler/load_internal/src/file.rs:1842:37"

platform "hello world"
    requires {} { first : arg -> ret, second : arg -> ret}

removing second solves it
am i.. not supposed to have two decls there?

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 14:30):

Just not supported yet. The apps with multiple declarations use a record of declarations. Kinda hacky and not always done correctly. I wonder if we have a tracking bug for this, super old issue.

view this post on Zulip dank (Mar 15 2023 at 14:33):

oof i wanted it so i could have multiple methods as interop examples

is this a big deal to implement? perhaps i could take a look

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 14:42):

I mean you can have multiple method currently, just via a workaround.

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 14:43):

and no idea on difficulty

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 14:44):

https://github.com/roc-lang/roc/blob/2023770ce77756d01b63e67b13846f24fa95879c/examples/gui/breakout/platform/main.roc#L9

view this post on Zulip dank (Mar 15 2023 at 14:44):

Brendan Hansknecht said:

https://github.com/roc-lang/roc/blob/2023770ce77756d01b63e67b13846f24fa95879c/examples/gui/breakout/platform/main.roc#L9

ohhh now i see what u mean

view this post on Zulip dank (Mar 15 2023 at 14:44):

nice that's not even that bad

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 14:45):

There are gotcha on the platform side around implementation depending if those return functions are closures or pure functions with no extra data. If you can make sure they are pure with no extra data it will avoid issues.

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 14:48):

As in this may cause you problems depending on how you implement the host:

main = \_ ->
    someDat = [1,2,3]
    {
         getDataForHost: \_ -> someData
    }

vs the below shouldn't have any gotchas:

main = \_ ->
    {
        getDataForHost: \_ ->
            someDat = [1,2,3]
            someData
    }

view this post on Zulip dank (Mar 15 2023 at 14:58):

oh yea i think i ran into something like that in another code
ill be careful of that then
tnx

view this post on Zulip dank (Mar 15 2023 at 21:24):

uhhh the program trick makes it much harder to work with image.png

view this post on Zulip dank (Mar 15 2023 at 21:25):

now im actually not sure what extern function to expect in the host code

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:26):

Did you use as to name each function in the record?

view this post on Zulip dank (Mar 15 2023 at 21:28):

oh no i forgot
hold up ill see if it makes it better

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:28):

Basically do that. They each function will be exposed the same way mainForHost normally is.

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:28):

As long as they are implement without capture data. They can be called the same way as mainForHost

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:29):

Of course with their own arguments and return type.

view this post on Zulip dank (Mar 15 2023 at 21:29):

Brendan Hansknecht said:

As long as they are implement without capture data. They can be called the same way as mainForHost

capture data in what way?

view this post on Zulip Folkert de Vries (Mar 15 2023 at 21:29):

the functions you expose should be top-level functions

view this post on Zulip dank (Mar 15 2023 at 21:29):

Brendan Hansknecht said:

Basically do that. They each function will be exposed the same way mainForHost normally is.

um i see now the names i defined
got some _result_size suffix now

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:29):

Like i showed above with someData

view this post on Zulip dank (Mar 15 2023 at 21:30):

Folkert de Vries said:

the functions you expose should be top-level functions

ohh yea no closures here so we're good

view this post on Zulip dank (Mar 15 2023 at 21:30):

um why actually do they get a result_size suffix?

view this post on Zulip dank (Mar 15 2023 at 21:32):

maybe it's something else

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:32):

I think you should be able to ignore those and just use the same equivalent functions as with mainForHost.

view this post on Zulip dank (Mar 15 2023 at 21:32):

image.png

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:32):

It just returns the number of bytes the result is

view this post on Zulip dank (Mar 15 2023 at 21:32):

no they're not the same

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:32):

Which you probably already know

view this post on Zulip dank (Mar 15 2023 at 21:32):

at least not named the same

view this post on Zulip dank (Mar 15 2023 at 21:32):

_1_exposed_generic suffix is only on the program now, see?

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:32):

Can you share your current code

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:33):

Just the platform def

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:33):

Like programForHost type and definition

view this post on Zulip dank (Mar 15 2023 at 21:33):

sure

platform "jvm-interop"
    requires {} { program : _ }
    exposes []
    packages {}
    imports []
    provides [programForHost]

programForHost : {
    interpolateString : Str -> Str as InterpolateString,
    mulArrByScalar : List I32 -> List I32 as MulArrByScalar,
}
programForHost = program

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:33):

Put parens around the function types and try again.

view this post on Zulip Folkert de Vries (Mar 15 2023 at 21:36):

yes the as here binds only to the return type

view this post on Zulip dank (Mar 15 2023 at 21:36):

(put parens not including the as right?)
does look a bit different
image.png

view this post on Zulip Folkert de Vries (Mar 15 2023 at 21:36):

it needs to apply to the whole function type, so (Foo -> Bar) as Fx

view this post on Zulip dank (Mar 15 2023 at 21:36):

Folkert de Vries said:

it needs to apply to the whole function type, so (Foo -> Bar) as Fx

yea that's what i did

view this post on Zulip dank (Mar 15 2023 at 21:37):

is _caller what im after now?

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:37):

yep

view this post on Zulip dank (Mar 15 2023 at 21:37):

nicee thanks guys

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:38):

If this helps at all: https://github.com/roc-lang/roc/blob/main/examples/gui/breakout/platform/src/roc.rs

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:38):

Example of use in rust.

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:39):

You can see how the multiple functions all look the same more or less in how they get called. Which is very similar to mainForHost in other apps.

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:40):

Actual call example: https://github.com/roc-lang/roc/blob/main/examples/gui/breakout/platform/src/roc.rs#L358-L370

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:40):

Though if you have a static result_size, I don't think you need to call that function.

view this post on Zulip dank (Mar 15 2023 at 21:41):

so is _1_exposed_generic just for unnamed scenarios?

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:41):

essentially. Thouhgh it can never have closure capture data.

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:41):

These technically could.

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:42):

Then you would have to call them in a slightly more complex way.

view this post on Zulip dank (Mar 15 2023 at 21:42):

is that implemented?
wonder how you'd do that

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:43):

You call programForHost Then take the closure data that it returns and split it up into the closure data for each function and make sure to give that to each function.

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:44):

If the methods are called more than once, you may also need to deal with refcounts updating, not sure though.

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:45):

This is all of the stuff where fully working glue will be amazing

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 21:45):

Should all be generated correctly for you without needing to know all these details and byte offsets.

view this post on Zulip dank (Mar 15 2023 at 22:45):

seems that the _caller symbol offset is.. off
i keep crashing the jvm now

munmap_chunk(): invalid pointer
fish: Job 1, 'java javaSource.Greeter' terminated by signal SIGABRT (Abort)

though actually what happened before (and worked) is that i wrapped the required functions in new functions (because you cant re-export, at least that's what the compiler said) and exposed those
now that we turn over the same, it can't find the functions, ig

view this post on Zulip dank (Mar 15 2023 at 22:46):

dank said:

sure

platform "jvm-interop"
    requires {} { program : _ }
    exposes []
    packages {}
    imports []
    provides [programForHost]

programForHost : {
    interpolateString : (Str -> Str) as InterpolateString,
}
programForHost = program

to make stuff clear, this ^ doesn't work
but this V does

platform "jvm-interop"
    requires {} { interpolateString : arg -> ret }
    exposes []
    packages {}
    imports []
    provides [interpolateStringy] # (y suffix name change for convenience, updated accordingly in host while testing so it doesnt matter)

interpolateStringy = \arg -> interpolateString arg

view this post on Zulip dank (Mar 15 2023 at 22:47):

interesting bug

view this post on Zulip Brendan Hansknecht (Mar 15 2023 at 23:33):

It would surprise me if the symbol offsets are wrong. Probably a different root cause

view this post on Zulip dank (Mar 15 2023 at 23:41):

well idk. ill call it a night for now but look at it some more tomorrow

at least i found 6 (mostly parser) issues! also learned a shitton on platforms, so that's nice

view this post on Zulip dank (Mar 16 2023 at 18:50):

am i mad or does using a record in requires causes a different memory layout
image.png
image.png

view this post on Zulip Brendan Hansknecht (Mar 16 2023 at 19:08):

Best way to tell would be to use --debug and look at the llvm ir.


Last updated: Jul 06 2025 at 12:14 UTC