You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is allowed that one WASM module can *import**functions*, *globals*, *memories* and *tables* from other modules as its dependencies, and also one module can *export*those entities for other modules to *access* and may *write*.
3
+
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.
5
4
6
-
WAMR loads all dependencies recursively according to the *import section* of a module.
5
+
WAMR loads all dependencies recursively according to the _import section_ of a module.
7
6
8
-
> Currently WAMR only implements the load-time dynamic linking. Please refer to [dynamic linking](https://webassembly.org/docs/dynamic-linking/) for more details.
7
+
> WAMR only implements the load-time dynamic linking. Please refer to [dynamic linking](https://webassembly.org/docs/dynamic-linking/) for more details.
8
+
9
+
WAMR follows [WASI Command/Reactor Model](https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md#current-unstable-abi). The WASI model separates modules into commands and reactors. A Command is the main module that requires exports of reactors(submodules).
10
+
11
+
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_:
12
+
13
+
- a main module(a command) should include `_start()`
14
+
- a submodule(a reactor) should include `_initialize()`
15
+
- 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 root module, which is loaded by `wasm_runtime_load()` and doesn't have a chance to tell runtime its *module name*.
29
+
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_.
23
30
24
-
Fot all the sub modules, WAMR will get their names and load the .wasm files from the filesystem or stream, so no need to register the sub modules again.
31
+
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.
25
32
26
33
### Find a registered module
27
34
28
-
```c
35
+
```c
29
36
wasm_module_t
30
37
wasm_runtime_find_module_registered(
31
38
const char *module_name);
32
39
```
33
40
34
-
It is used to check if a module with a given *module_name* has been registered, if yes return the module.
41
+
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 filesystem or storage issues. `module_reader` and `module_destroyer` are two callbacks called when dynamic-loading/unloading the sub modules. Developers must implement the two callbacks by themselves.
58
+
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 to lookup the function of sub module and call it. There are two ways to indicate the function *name*:
69
+
Multi-module allows one to look up an exported function of a submodule. There are two ways to indicate the function _name_:
63
70
64
-
- parent function name only by default, used to lookup the function of parent module
65
-
- sub module name, function name of sub module and two $ symbols, e.g. `$sub_module_name$function_name`, used to lookup function of sub module
71
+
- parent function name only by default, used to look up the function of the parent module
72
+
- submodule name, function name and two $ symbols, e.g. `$submodule_name$function_name`, used to lookup function of submodule
73
+
-`signature` can be NULL
66
74
67
75
## Example
68
76
69
-
### WASM modules
70
-
Suppose we have three C files, *mA.c*, *mB.c* and *mC.c*. Each of them has some exported functions and import some from others except mA.
77
+
### Attributes in C/C++
71
78
72
-
Undefined symbols can be marked in the source code with the *import_name* clang attribute which means that they are expected to be undefined at static link time. Without the *import_module* clang attribute, undefined symbols will be marked from the *env* module.
79
+
Suppose there are three C files, _mA.c_, _mB.c_ and _mC.c_. Each of them exports functions and imports from others except mA.
73
80
74
-
```C
81
+
import/export with two kinds of `__attribute__`:
82
+
83
+
-`__attribute__((import_module("MODULE_NAME"))) __attribute__((import_name("FUNCTION_NAME")))`. to indicate dependencies of the current module.
84
+
85
+
-`__attribute__((export_name("FUNCTION_NAME")))`. to expose functions.
86
+
87
+
```C
75
88
// mA.c
76
-
intA() { return 10; }
89
+
__attribute__((export_name("A1"))) int
90
+
A1()
91
+
{
92
+
return 11;
93
+
}
77
94
```
78
95
79
-
```C
96
+
```C
80
97
// mB.c
81
-
__attribute__((import_module("mA"))) __attribute__((import_name("A"))) extern int A();
82
-
int B() { return 11; }
83
-
int call_A() { return A(); }
84
-
```
98
+
__attribute__((import_module("mA")))
99
+
__attribute__((import_name("A1"))) extern int
100
+
A1();
85
101
86
-
```C
87
-
// mC.c
88
-
__attribute__((import_module("mA"))) __attribute__((import_name("A"))) extern int A();
89
-
__attribute__((import_module("mB"))) __attribute__((import_name("B"))) extern int B();
90
-
int C() { return 12; }
91
-
int call_A() { return A(); }
92
-
int call_B() { return B(); }
102
+
__attribute__((export_name("B1"))) int
103
+
B1()
104
+
{
105
+
return 21;
106
+
}
93
107
```
94
108
95
-
By default no undefined symbols are allowed in the final binary. The flag *--allow-undefined* results in a WebAssembly import being defined for each undefined symbol. It is then up to the runtime to provide such symbols.
96
-
97
-
When building an executable, only the entry point (_start) and symbols with the *export_name* attribute exported by default. in addition, symbols can be exported via the linker command line using *--export*.
98
-
99
-
In the example, another linked command option *--export-all* is used.
100
-
101
-
> with more detail, please refer to [WebAssembly lld port](https://lld.llvm.org/WebAssembly.html)
109
+
### Compile Options
102
110
103
-
Here is an example how to compile a *.c* to a *.wasm* with clang. Since there is no *start* function, we use *--no-entry* option.
0 commit comments