Add example wasm component and runner which embeds the component

This commit is contained in:
michaelpivato
2024-05-11 14:22:49 +09:30
commit fca84b9240
10 changed files with 1560 additions and 0 deletions

33
README.md Normal file
View File

@@ -0,0 +1,33 @@
# Wasmtime test
Example project showcasing how to run a wasm component with wasmtime, and how to build the runnable component using cargo component.
## Structure
### component
This package includes an example wasm component, which simply calls a `hello()` function provided by the runner.
### runner
This package includes an example runtime, to run the built wasm component from the component package.
## Build
First run `cargo component build -p component --release` to build the wasm component.
Then run `cargo run -p runner --release` to run the runner with the build wasm component from the previous step.
## Troubleshooting
If you see errors about imports missing when trying to run the compiled component, it's likely there's a dependency on std that isn't supported by the runtime linker (e.g. by calling `println!()`).
To build without std components, run the following:
`cargo component build --target wasm32-unknown-unknown --release`
Note this is only required when using std components. Preferably don't do this, and instead
use #![no_std] to not accidentally use std. Otherwise, you'll need to either use a runtime that includes std (e.g. wasmtime cli), or add the appropriate imports to the runtime linker.
To check the imports and exports for a compiled .wasm component, run the following (where component.wasm is the compiled .wasm file to inspect):
`wasm-tools component wit component.wasm`