-
Notifications
You must be signed in to change notification settings - Fork 175
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
Add wit_import
attribute macro to import functions from Wasm modules
#944
Conversation
Missing from some of the unit tests.
Don't require a name and type pair, allowing macros to be applied on lists of either just names or just types as well.
Make it easier to convert a host parameters type into the flat type parameters for an imported guest function.
Allow converting from the results received from an imported guest Wasm function into the host results type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
<Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>; | ||
} | ||
|
||
impl<Parameters, Results> ImportedFunctionInterface for (Parameters, Results) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the only implementation of ImportedFunctionInterface
? Did you consider a helper struct struct ImportedFunctionInterface<P, R> { parameters: P, results: R }
, then defining a block impl<P, R> ImportedFunctionInterface<P, R> where .. { .. }
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it depends if you need the output associated types Guest*
as a short alias.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, unfortunately associated types in types isn't stable yet :/
Use the `FlatHostParameters` and `FlatHostResults` traits in a single trait that can be used to determine the interface to an imported function from a guest based on the host inteface types.
Prepare to add support for a new `wit_import` attribute procedural macro. The attribute requires receiving an extra parameter to determine the package of the imported functions, and the namespace can also be specified as a parameter. Implement extraction of the namespace by parsing the attribute parameters. It will expect the package to be specified, and will use the namespace if provided as a parameter or fallback to the trait name if it's not.
Parse the functions from a trait and extract the information to later be used to generate code to import functions.
A type to allow `TokenStream`s to be used as an element in a `HashSet`.
Given a trait interface, generate the code for a type with equivalent methods that load a function from the guest and calls it.
Allow generated code to use it.
Allow generated code to use it.
Generate a trait alias for the instance constraints for the implementation of the imported functions. Use this trait alias in the generated code as well instead of using the constraints directly.
And `FakeRuntime` to `MockRuntime`. Prepare to allow mocking exported functions in the mocked instance.
Keep track of the mock exported functions and allow handlers to be called when the exported functions are called.
Prepare to keep track of which handler should be called based on the exported function name.
Prepare to use the same mechanism for the canonical ABI memory functions.
Implement `InstanceWithFunction` for all valid parameters and results types.
Improve the ergonomics of mocking an exported function and checking that it's called correctly.
Check that a function without parameters or return types can be imported from a mock instance.
Check that functions that have return types can be imported from a mock instance.
Check that functions that have single parameters can be imported from a mock instance.
Check that functions that have multiple parameters and return values can be imported from a mock instance.
Motivation
The new Witty crate should allow calling functions exported from Wasm guest modules.
Proposal
Use a
wit_import
attribute macro that parses atrait
definition and generates the code to call functions from a guest Wasm instance defined by the trait.In order to make the behavior independent from the host runtime, some traits were defined to abstract away the exported function's signature and how parameters and results are converted to the necessary WIT primitives to call the function.
Test Plan
Some simple tests were written to ensure that the
wit_import
macro generates code that tries to load functions from a mock Wasm instance and allows calling them.The
test_case
crate together with theTestInstanceFactory
trait were used in order to make it simple for future runtime implementations to reuse the same tests.Links
Part of #906
Release Plan
Reviewer Checklist