Stream: wasi

Topic: Guest implemented exported resources


view this post on Zulip Ari Seyhun (Nov 02 2023 at 07:55):

I'm having difficulty exporting a resource within a wit file.

I have the following wit file:

package example:greeter;

world greeter {
    resource person {
        constructor(name: string);
        greet();
    }

    export person;
}

However this fails. The way I previously solved this when resources weren't working, is by passing in the state as list<u8> and deserializing it in the guest. But this also meant any changes I made to a struct needed to be serailized again and returned.

I was hoping resources could solve this, so I could have access to a &self or &mut self on a struct within the guest.
Though it seems like resources are only meant to be implemented on the host?

view this post on Zulip Ari Seyhun (Nov 02 2023 at 08:19):

I seemed to have some luck by doing this:

package example:greeter;

world greeter {
    export person: interface {
        resource person {
            constructor(name: string);
            greet();
        }
    };
}

Though I'm not sure if there's a better way

view this post on Zulip Ari Seyhun (Nov 02 2023 at 08:22):

Also, I can only use &self with resources? I can get around this limitation by wrapping my fields in RefCell, but it would be ideal if I could implement the trait with &mut self. Is there a reason it's currently just &self?

view this post on Zulip Ari Seyhun (Nov 02 2023 at 09:00):

(Where I put greet() should be green: func())

view this post on Zulip Alex Crichton (Nov 02 2023 at 13:50):

Currently resources must be in world interface exports, they can't be at the top level. For the &self bit it's something I'd like to fix but for now that does require refcell or cell yes

view this post on Zulip Ari Seyhun (Nov 04 2023 at 06:41):

From the host side, I'm using:

wasmtime::component::bindgen!({
    world: "aggregate",
    ownership: Borrowing { duplicate_if_necessary: true },
    async: true,
});

And it generates something like:

pub struct Aggregate {
    interface0: exports::aggregate::Aggregate,
}
pub mod exports {
    pub mod aggregate {
        pub struct Aggregate {
            init: wasmtime::component::Func,
            apply: wasmtime::component::Func,
            handle: wasmtime::component::Func,
        }
    }
}

The issue I'm having is that it doesn't add #[derive(Clone, Copy)] to the Aggregate structs.
I dug into the options for wasmtime::component::bindgen! and didn't see anything for adding derives to these, and sadly I can't implement them myself since the fields of aggregate are private.


Last updated: Nov 22 2024 at 16:03 UTC