bytecodealliance / bytecodealliance/wasmtime

Get instance data in host defined functions with component model

Open
#6,848 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
18.6k
Forks
1.8k
Avg merge
1d 18h
Merged PRs (30d)
126

Description

#### Feature

Provide instance (`Instance` struct) or instance data to host defined functions in component model.

#### Benefit

In #2491, it is suggested to bind the instance data to the closure passed to `Func::wrap` to store per-instance data. However, this will not work for instances in component model because to bind a function (let host export a function to be called by guests), one need to configure [`LinkerInstance`](https://docs.wasmtime.dev/api/wasmtime/component/struct.LinkerInstance.html#) (e.g., use [`LinkerInstance::func_wrap`](https://docs.wasmtime.dev/api/wasmtime/component/struct.LinkerInstance.html#method.func_wrap) or similar methods). Yet, there is only a single linker set up for instantiating all the instances in a store, so there is no way to configure per-instance data.

Providing host function instance and instance data will let host function able to get data tied to the instance that calls the function, or let host perform a nested call to a guest function, etc.

#### Implementation

I think a possible implementation is to let the function passed to [`LinkerInstance::func_wrap`](https://docs.wasmtime.dev/api/wasmtime/component/struct.LinkerInstance.html#method.func_wrap) to be able to take `Caller` instead of `StoreContextMut`. The non-component model counterpart [`Func::wrap`](https://docs.wasmtime.dev/api/wasmtime/struct.Func.html#method.wrap) takes `AsContextMut` to achieve this, and I'm not sure why `func_wrap` takes `StoreContextMut` instead. Then, we can expose the instance stored in the `Caller` struct. Of course, this all rely on the assumption that `Caller` can be retrieve in calls to component host.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.