Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Component: Unclear how to access exported resource returned by guest #9946

Open
tliron opened this issue Jan 8, 2025 · 2 comments
Open

Component: Unclear how to access exported resource returned by guest #9946

tliron opened this issue Jan 8, 2025 · 2 comments

Comments

@tliron
Copy link

tliron commented Jan 8, 2025

The exported resources example shows how to create a guest resource. Unfortunately it doesn't show how to actually send that resource to an exported client function.

But, to this issue, it doesn't show how to access a resource returned by a call to the guest.

The guest returns ResourceAny, and the documentation does make it clear that this is expected. However, now what?

It cannot be converted to a Resource, because try_into_resource only works on host resources. (That is also not clear in the documentation, I had to delve into the source code to figure that out.)

Also, it's also unclear to me if I must call resource_drop on the value returned by the guest. Or is that necessary just for host resources?

The documentation could be more specific, and the example is not especially useful. In any case, I do not know how to proceed.

@bjorn3
Copy link
Contributor

bjorn3 commented Jan 8, 2025

A resource is an opaque id. I think all the host can do with a guest resource is to pass it as argument when calling a guest function or to call the drop function of the resource.

@tliron
Copy link
Author

tliron commented Jan 8, 2025

@bjorn3 , actually I figured it out and it is possible. The example code is the hint. Here's how it's done:

  1. First, create the dispatcher. From the example: let logger = guest.logger();
  2. Then, do not call call_constructor. We already have the ResourceAny! In fact, call_constructor returns a ResourceAny, too.
  3. So now just call the resource functions via the dispatcher on the ResourceAny that you got from the guest, e.g. logger.call_log(&mut store, my_returned_value, Level::Debug, "hello!")?;
  4. I'm pretty sure you need to drop_resource, too, when you're done. Well, at least it doesn't return an error when I call it.

I wish the example actually showed this. Instead, the example doesn't have any exported functions in the interface, which doesn't seem to me to be a very common scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants