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:
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:
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:
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?
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.
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.
@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
@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.