Stream: wasi

Topic: ✔ Supporting resources in Host implementations


view this post on Zulip Richard Backhouse (Mar 24 2024 at 11:06):

I'm trying to do an host implementation of part of the wasi-messaging API and I'm struggling with how I support the resources that are defined.

In particular I'm trying to implement the following WIT setup

world pubsub {
    import wasi:messaging/producer@0.2.0-draft;
}

The producer interface has a function that returns an error resource

    resource error {
        trace: static func() -> string;
    }

interface producer {
    use messaging-types.{client, channel, message, error};

    send: func(c: client, ch: channel, m: list<message>) -> result<_, error>;
}

For the host implementation I have this

#[async_trait::async_trait]
impl wasi::messaging::producer::Host for PublishData {
    async fn send(
        &mut self,
        component:wasmtime::component::Resource<Client>,
        channel:Channel,
        messages:Vec<Message>) -> wasmtime::Result<Result<(),wasmtime::component::Resource<Error>>> {
        for msg in messages {
            self.publisher.publishmsg(channel.to_string(), msg.data);
        }
        let err = MyError;
        let resource: Resource<Error> = self.table.push(err).unwrap();
        Ok(Ok(resource));
    }
}

I understand that I have to use the ResourceTable to create the resource but cannot see how I create the Error type. The bindgen generates error as an empty enum

            pub enum Error{}

and I see there is also a HostError trait generated too

            pub trait HostError {
                async fn trace(&mut self,) -> wasmtime::Result<String>;

                fn drop(&mut self,rep:wasmtime::component::Resource<Error>) -> wasmtime::Result<()>;

                }#[doc = " There are two types of channels:"]

but its not clear to me if this trait is what I implement or do it some other way. I haven't really found any examples that provide a guide on this.

view this post on Zulip Alex Crichton (Mar 24 2024 at 19:51):

In your call to bindgen! you'll want to specify with for the error type pointing to your own error type to avoid using the default enum Error {}.

WIth a change like this we'd probably require you to specify with to avoid having a hard-to-use default.

This issue was originally encountered using wasmtime::bindgen, but I'm posting this here since I think it likely impacts all bindings generators. Currently, bindings are generated for the package t...

view this post on Zulip Richard Backhouse (Mar 25 2024 at 09:10):

Thanks Alex, I had seen the "with" statements before but had not connected the dots

view this post on Zulip Notification Bot (Mar 25 2024 at 09:10):

Richard Backhouse has marked this topic as resolved.


Last updated: Nov 22 2024 at 17:03 UTC