Stream: wit-bindgen

Topic: How to work with resources on the host?


view this post on Zulip Karel Hrkal (kajacx) (May 02 2024 at 11:09):

Ok, I have been looking into resources in wit, and while the concept is very interesting and works well on the guest, it's a confusing mess on the host. Let's take this wit file:

Wit protocol

It's pretty straight forward - company is a host resource (method impl defined by host), while employee is a guest resource (method impl defined by guess). And the guess code is very nice and intuitive:

Guest file

We define what the "employee" resource will be with an associative type, then implement the resource's methods on in, and in the exported functions, we can call the resource methods on both resources. Very nice and idiomatic.

Now, on the host, it is anything but that. This is the host file by the way:

Host file

Now, there are many problems. First, I have to handle the companies myself in a custom ResHolder implementation. I would expect wasmtime to provide resource holding for host resources automatically, just like it provides resource holding for guest resources automatically.

Next, working with any resources on the host is beyond painful. You get a handle, and need to manually find the resource you want. Wasmtime should manage the resources automatically for you and you should implement methods that takes a proper resource object, just like it is on the quest.

But what is by far the most "what the fuck" thing is that there are actually two "employee" resources: one "host resource", and one "guest resource". How on earth does this happen??

Also having to store the guest resource as ResourceAny is just sad, no type safety for what kind of resource it is. Is there any plan to properly fix all of this in a future version?

view this post on Zulip Christof Petig (May 02 2024 at 12:53):

I perfectly understand why you are horrified seeing this manual process as described in the examples or docs, IIRC. I have been in a similar place when I wrote host code with resources.

I recall a ResourceTable type provided by wasmtime which made it bearable to me, see https://github.com/cpetig/resource-demo/blob/main/host-wasmtime/src/main.rs for the code. Do you consider this adequate?

But I feel this is or was hidden information, I mostly learned about it in wasmtime's source code.

A demo showing WASM component model resources in various environments - cpetig/resource-demo

view this post on Zulip Karel Hrkal (kajacx) (May 02 2024 at 13:12):

Well, the Resource table makes managing resources slightly less painful, but you still need to convert the handle into the resource type instead of getting the resource type directly.

Also, this doesn't address any of the other issues, but I 100% agree that there aren't really any good tutorials or examples that would explain how to work with resources.

view this post on Zulip Till Schneidereit (May 02 2024 at 13:14):

@Karel Hrkal (kajacx) without commenting on the substance of your feedback about the API: please stop using this kind of tone. People wrote this code, and they didn't write it to offend you—and it's not okay to be offensive towards them.

You used a question as the topic for this conversation, which is the right approach. Please stick to that approach in how you engage as well, instead of coming down extremely hard on other people's work

view this post on Zulip Karel Hrkal (kajacx) (May 02 2024 at 14:07):

@Till Schneidereit Hello and sorry if I was too negative. I honestly don't know if this is the intended way to use resources or not, if I'm missing something (like a function somewhere) I would appreciate some help.


Last updated: Nov 22 2024 at 17:03 UTC