Multiple Modules As Dependencies
Last updated
Last updated
A WASM module can import functions, globals, memories and tables from other modules as dependencies. A module can also export those entities for other modules like a library.
WAMR loads all dependencies recursively according to the import section of a module.
WAMR only implements the load-time dynamic linking. Please refer to for more details.
WAMR follows . The WASI model separates modules into commands and reactors. A Command is the main module that requires exports of reactors(submodules).
if WASM_ENABLE_LIBC_WASI
is enabled, any module imports a WASI APIs, like (import "wasi_snapshot_preview1" "XXX")
, should follow restrictions of the WASI application ABI:
a main module(a command) should include _start()
a submodule(a reactor) should include _initialize()
both a command and a reactor should include an exported memory
It is used to register a module with a module_name to WASM runtime, especially for the main module, which is loaded by wasm_runtime_load()
and doesn't have a chance to tell runtime its module name.
WAMR will get submodules' names(according to the import section of the main module) and load .wasm files from the filesystem or stream and then register them internally.
It is used to check whether a module with a given module_name has been registered before or not. Return the module if yes.
WAMR hopes that the native host or embedding environment loads/unloads the module WASM files by themselves and only passes runtime the binary content without worrying about filesystem or storage issues. module_reader
and module_destroyer
are two callbacks called when dynamic-loading/unloading submodules. Developers must implement the two callbacks by themselves.
Multi-module allows one to look up an exported function of a submodule. There are two ways to indicate the function name:
parent function name only by default, used to look up the function of the parent module
submodule name, function name and two $ symbols, e.g. $submodule_name$function_name
, used to lookup function of submodule
signature
can be NULL
Suppose there are three C files, mA.c, mB.c and mC.c. Each of them exports functions and imports from others except mA.
import/export with two kinds of __attribute__
:
__attribute__((import_module("MODULE_NAME"))) __attribute__((import_name("FUNCTION_NAME")))
. to indicate dependencies of the current module.
__attribute__((export_name("FUNCTION_NAME")))
. to expose functions.
to generate a wasm module as a command
to generate a wasm module as a reactor
In the above case, mA and mB are reactors(submodules), mC is the command(main module). Their import relationships will be like:
After all the preparation, we can call some functions from native code with APIs
First, create two callbacks to load WASM module files into memory and unload them later
Second, create a large buffer and tell WAMR malloc any resource only from this buffer later.
More details
We need to enable WAMR_BUILD_MULTI_MODULE option when building WAMR vmlib. Please ref to for a thoughtful guide.
Third, put all together. Please refer to