Stream: wit-bindgen

Topic: ✔ Storing state in world/interface


view this post on Zulip i509VCB (Sep 25 2023 at 18:14):

Hello!, I've been playing around with an idea to implement window manager api to use within wasm with wit-bindgen for my WIP Wayland compositor.

I've noticed with the generated Rust code that there is no &mut self/&self on the interfaces marked as export. I'm guessing the expectation is that the Rust guest will store any state in a &'static?

Using one of the examples (result)

The generated code for a world looks like this, showing there is no &mut self/&self:

impl test_exports::Guest for Exports {
    fn string_error(a: f32) -> Result<f32, String> {
        test_imports::string_error(a)
    }

    ...
}

Right now I have something like this (syntax may be wrong, still working through this):

world aerugo-wm {
    export wm
}

interface wm {
    use self.types.toplevel

    // where toplevel is a resource
    new_toplevel: func(toplevel: own<toplevel>)
}

In particular new_toplevel could be called at any time and the previous state needs to be preserved.

view this post on Zulip Joel Dice (Sep 25 2023 at 18:18):

I think you're going to want to represent this as a resource rather than a freestanding function. That will ensure that you get a self parameter. Note that resource support is quite new in wasm-tools etc., so you'll need to make sure you have the latest versions of everything.
Sounds like a cool project, by the way.

view this post on Zulip Alex Crichton (Sep 25 2023 at 18:19):

Yes a WIT exported function is a top-level function that uses the entire memory as its state, so in rust you'd store that in a static or similar. Otherwise though I was gonna mention what Joel just did, so I'd recommend using that if you can

view this post on Zulip i509VCB (Sep 25 2023 at 19:17):

resource seems to line up better with what I was intending. Although now the problem I have is a proc macro error?

Apparently generating

pub use super::super::super::super::ERROR as WmRes;

view this post on Zulip i509VCB (Sep 25 2023 at 19:17):

For

world aerugo-wm {
    export wm
}

interface wm {
    resource wm-res {
        name: func() -> string
    }

    create-wm: func() -> result<wm-res, string>
}

view this post on Zulip i509VCB (Sep 25 2023 at 19:20):

(I'm guessing I might need to use the git main branch for now)

view this post on Zulip Alex Crichton (Sep 25 2023 at 19:23):

hm odd! wit-bindgen shouldn't successfully generate code with ERROR, so you may be hitting a bug

view this post on Zulip Alex Crichton (Sep 25 2023 at 19:23):

do you know what version of wit-bindgen you're using?

view this post on Zulip i509VCB (Sep 25 2023 at 19:24):

I just swapped to the main git branch and still have the error. Was using whatever was published on crates.io earlier

view this post on Zulip Alex Crichton (Sep 25 2023 at 19:26):

kk I'll dig in today and see if I can fix

view this post on Zulip Alex Crichton (Sep 25 2023 at 19:26):

hm actually, how are you getting code generated? Are you invoking it via the wit-bindgen crate? If so what options are you providing?

view this post on Zulip i509VCB (Sep 25 2023 at 19:27):

Proc macro in this case

view this post on Zulip i509VCB (Sep 25 2023 at 19:27):

wit_bindgen::generate!({
    path: "wm.wit",

    world: "aerugo-wm",

    exports: {
        "aerugo:wm/wm": Wm,
        "aerugo:wm/wm/wm-res": WmRes
    },
});

view this post on Zulip Alex Crichton (Sep 25 2023 at 19:30):

hm I'm not getting that error?

view this post on Zulip Alex Crichton (Sep 25 2023 at 19:31):

wit_bindgen::generate!({
    path: "wm.wit",
    world: "aerugo-wm",
    exports: {
        "aerugo:wm/wm": Wm,
        "aerugo:wm/wm/wm-res": WmRes
    },
});
use crate::exports::aerugo::wm::wm::Guest;
use crate::exports::aerugo::wm::wm::GuestWmRes;
use wit_bindgen::Resource;

pub struct WmRes;

impl GuestWmRes for WmRes {
    fn name(&self) -> String {
        loop {}
    }
}

pub struct Wm;

impl Guest for Wm {
    fn create_wm() -> Result<Resource<WmRes>, String> {
        loop {}
        //
    }
}

view this post on Zulip Alex Crichton (Sep 25 2023 at 19:31):

that works for me

view this post on Zulip i509VCB (Sep 25 2023 at 19:38):

still happening copy pasting that verbatim

view this post on Zulip i509VCB (Sep 25 2023 at 19:38):

I updated rust with rustup and still nothing new

view this post on Zulip Alex Crichton (Sep 25 2023 at 19:39):

hm can you try uploading a git repo with intructions?

view this post on Zulip i509VCB (Sep 25 2023 at 19:40):

Uhh give me a min I can push my branch

view this post on Zulip i509VCB (Sep 25 2023 at 19:41):

https://github.com/i509VCB/aerugo/tree/wayland-0.30/wm2

view this post on Zulip Alex Crichton (Sep 25 2023 at 19:44):

aha it's the @0.0.1 in the package header

view this post on Zulip Alex Crichton (Sep 25 2023 at 19:44):

I'll work on fixing that

view this post on Zulip Alex Crichton (Sep 25 2023 at 19:44):

if you remove that then it should work for now

view this post on Zulip i509VCB (Sep 25 2023 at 19:56):

Yeah it was the version

view this post on Zulip Alex Crichton (Sep 25 2023 at 21:34):

ok should be fixed by https://github.com/bytecodealliance/wit-bindgen/pull/677

Previously one location looking up exports would account for the version and another wouldn't which caused the ERROR case to leak through by accident. These are now kept in sync to ensure that the ...

view this post on Zulip Notification Bot (Sep 25 2023 at 22:06):

i509VCB has marked this topic as resolved.


Last updated: Nov 22 2024 at 16:03 UTC