This example demonstrates the function dependencies between different wasm modules. That is, a wasm function defined in one wasm module can be called by another wasm module.
This example consists of three projects:
-
alice-wasm-libis a wasm library, in which a function namedaddis defined. -
bob-wasm-libis a wasm library, in which a function namedreal_addis defined. This function is called by the functionadddefined inalice-wasm-lib. -
host-appis a host application that loads the wasm binaries generated fromalice-wasm-libandbob-wasm-lib, and runs the wasm functionaddexported byaliceover WasmEdge Runtime.
Now let's build and run this example.
-
Install
rustupandRustGo to the official Rust webpage and follow the instructions to install
rustupandRust.It is recommended to use Rust 1.71 or above in the stable channel.
Then, add
wasm32-wasitarget to the Rustup toolchain:rustup target add wasm32-wasi
-
Install WasmEdge Runtime
Refer to the Quick Install section of WasmEdge Runtime Book to install WasmEdge Runtime. Or, use the following command directly
# NOTICE that the installation script needs `sudo` access # install wasmedge to the directory /usr/local/ curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash -s -- -v 0.14.0 source $HOME/.wasmedge/env
For users in China mainland (中国大陆地区), try the following command to install WasmEdge Runtime if failed to run the command above
# NOTICE that the installation script needs `sudo` access bash install_zh.sh -v 0.14.0 source $HOME/.wasmedge/env
-
Download example
git clone git@github.com:second-state/wasmedge-rustsdk-examples.git cd wasmedge-rustsdk-examples/load-module-in-module -
Build
alice-wasm-libcargo build -p alice-wasm-lib --target wasm32-wasi --release
If the command runs successfully,
alice-wasm-lib.wasmcan be found in the directory of../target/wasm32-wasi/release/. -
Build
bob-wasm-libcargo build -p bob-wasm-lib --target wasm32-wasi --release
If the command runs successfully,
bob-wasm-lib.wasmcan be found in the directory of../target/wasm32-wasi/release/. -
Build and run
multi-module-host-appcargo run -p multi-module-host-app -- 2 3
If the command runs successfully, then the following message is printed out on the screen:
args: ["target/debug/multi-module-host-app", "2", "3"] add(2, 3) = 5