My goal is for my webassembly component to be able to access resources which already exist in the host, but I'm having a little trouble. I'm looking at this example. How can the HostLogger constructor access data from elsewhere in the host to make available for the guest, since it only has access to the members of the MyState struct? Could someone help me with an example that was a reader rather than a logger, or otherwise point me in the right direction?
The theory is that you store the relevant state within MyState, or whatever the Self type is in the Host trait implementation. While not released just yet the documentation here should still give some pointers of how to do various patterns perhaps. Does that help?
I was able to follow along that far, storing data to be accessed by imported functions within a MyState struct. my question is more specifically about imported WIT resources. I was trying to have a WIT resource backed by data in a MyStruct field, or is that the wrong way to use it and the imported WIT resource should only be backed by what is in the ResourceTable as per the example?
it's up to you, WASI does a bit of a blend. For resources the HostFoo implementation is probably going to be on something like MyState where the resource you're given is translated to host-specific state through a ResourceTable, but you'll still have access to everything else still in MyState at that time to account for configuration/etc within a method. Either that or I may still be misunderstanding you, so please let me know if that's the case!
Right, I think I was there with you. This is where I got stuck - trying to load the ResourceTable from the Host side before the Guest runs. I tried using table.push(Foo{}) from the Host before the Guest runs and then storing the resulting a Resource<Foo> in a field of MyState separate from the ResourceTable, but that's where I got stuck because I can't return that resource in the method which only take &mut self as a parameter.
For that could you create a second Resource? You could call Resource::new_{own,borrow} on the rep value from before to pass that around? That's sort of like calling .clone() but not quite. I've meant to add Clone to the type but haven't gotten around to it yet
Thank you! I was able to get that to work with Resource::new_own(). Could you explain or point me in the right direction about what it means that this sets the state to AtomicResourceState::NOT_IN_TABLE? Also, I'd be happy to help with setting up an example for importing a reader resource like this if that would be welcome.
Last updated: Dec 06 2025 at 07:03 UTC