Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
wit_import
attribute macro to import functions from Wasm modules (
#944) * Add missing lift and lowering round-trip tests Missing from some of the unit tests. * Refactor `repeat_macro!` to support lone elements Don't require a name and type pair, allowing macros to be applied on lists of either just names or just types as well. * Create a `FlatHostParameters` helper trait Make it easier to convert a host parameters type into the flat type parameters for an imported guest function. * Create a `FlatHostResults` helper trait Allow converting from the results received from an imported guest Wasm function into the host results type. * Create an `ImportedFunctionInterface` helper trait 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. * Extract namespace from attribute parameters 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. * Collect information for functions to import Parse the functions from a trait and extract the information to later be used to generate code to import functions. * Create a `TokensSetItem` helper type A type to allow `TokenStream`s to be used as an element in a `HashSet`. * Generate code to import functions from guests Given a trait interface, generate the code for a type with equivalent methods that load a function from the guest and calls it. * Export `InstanceWithFunction` trait Allow generated code to use it. * Export `Instance` trait Allow generated code to use it. * Refactor to use a trait alias for instances 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. * Rename `FakeInstance` to `MockInstance` And `FakeRuntime` to `MockRuntime`. Prepare to allow mocking exported functions in the mocked instance. * Allow adding mocked exported functions to instance Keep track of the mock exported functions and allow handlers to be called when the exported functions are called. * Use `String` as the export type Prepare to keep track of which handler should be called based on the exported function name. * Register canonical ABI functions an mocks Prepare to use the same mechanism for the canonical ABI memory functions. * Allow mock exported functions to be called Implement `InstanceWithFunction` for all valid parameters and results types. * Create a `MockExportedFunction` helper type Improve the ergonomics of mocking an exported function and checking that it's called correctly. * Test importing a simple function Check that a function without parameters or return types can be imported from a mock instance. * Test importing functions that return values Check that functions that have return types can be imported from a mock instance. * Test importing functions with single parameters Check that functions that have single parameters can be imported from a mock instance. * Test importing input/output functions Check that functions that have multiple parameters and return values can be imported from a mock instance.
- Loading branch information