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.
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.
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
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;
For
world aerugo-wm {
export wm
}
interface wm {
resource wm-res {
name: func() -> string
}
create-wm: func() -> result<wm-res, string>
}
(I'm guessing I might need to use the git main branch for now)
hm odd! wit-bindgen shouldn't successfully generate code with ERROR
, so you may be hitting a bug
do you know what version of wit-bindgen you're using?
I just swapped to the main git branch and still have the error. Was using whatever was published on crates.io earlier
kk I'll dig in today and see if I can fix
hm actually, how are you getting code generated? Are you invoking it via the wit-bindgen
crate? If so what options are you providing?
Proc macro in this case
wit_bindgen::generate!({
path: "wm.wit",
world: "aerugo-wm",
exports: {
"aerugo:wm/wm": Wm,
"aerugo:wm/wm/wm-res": WmRes
},
});
hm I'm not getting that error?
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 {}
//
}
}
that works for me
still happening copy pasting that verbatim
I updated rust with rustup and still nothing new
hm can you try uploading a git repo with intructions?
Uhh give me a min I can push my branch
https://github.com/i509VCB/aerugo/tree/wayland-0.30/wm2
aha it's the @0.0.1
in the package
header
I'll work on fixing that
if you remove that then it should work for now
Yeah it was the version
ok should be fixed by https://github.com/bytecodealliance/wit-bindgen/pull/677
i509VCB has marked this topic as resolved.
Last updated: Dec 23 2024 at 12:05 UTC