2024-05-08 01:30:29 +00:00
# Wasm Function
2023-03-23 02:37:38 +00:00
## Internal data structure

2023-03-25 01:39:20 +00:00
## Module level data (function)
2024-05-08 01:30:29 +00:00
**WASMModule**: Data structure created for loading a module.
2023-03-25 01:39:20 +00:00
- `WASMImport *import_functions`: initialized from the Wasm file function section
2024-05-08 01:30:29 +00:00
- `WASMImport *import_functions`: initialized from the Wasm file import section. The runtime will try to solve the imports from the native API registration, refer to [Export native API to WASM application](../../../doc/export_native_api.md).
2023-03-25 01:39:20 +00:00
2024-05-08 01:30:29 +00:00
**WASMFunction**: represent a Wasm function located in Wasm file code section. Track the links to the compiled function body.
2023-03-25 01:39:20 +00:00
**WASMImport**: represent a imported Wasm function which can be a solved as a native function or another Wasm module exported function.
## Instance level data (function)
**WASMModuleInstance**: Data structure created for instantiating a module
- `WASMModuleInstanceExtra::functions`: combined the imported and internal functions into single array of structure `WASMFunctionInstance`
2024-05-08 01:30:29 +00:00
- `WASMModuleInstance::import_func_ptrs`: pointer array for solved function imports. This array is referred during calling imported native function. Note it is initialized with the module level solved imports, but may points to different native function later due to c-api calls.
2023-03-25 01:39:20 +00:00
## Execution paths
**Interpreter**:
- Execute internal bytecode function:
```
2024-05-08 01:30:29 +00:00
WASMModuleInstance::e
-> WASMModuleInstanceExtra::functions[..]
-> WASMFunctionInstance::func
2023-03-25 01:39:20 +00:00
-> WASMFunction::code
```
- Execute imported function from other module:
```
2024-05-08 01:30:29 +00:00
WASMModuleInstance::e
-> WASMModuleInstanceExtra::functions[..]
2023-03-25 01:39:20 +00:00
(WASMFunctionInstance flag indicates an import)
-> WASMFunctionInstance::import_func_inst
-> WASMModuleInstance(second)::func
-> WASMFunction (second module)::code
```
- Execute imported native function:
```
2024-05-08 01:30:29 +00:00
WASMModuleInstance::e
-> WASMModuleInstanceExtra::functions[..]
2023-03-25 01:39:20 +00:00
(flag indicates imported native)
WASMModuleInstance::import_func_ptrs[..]
-> native function
2024-05-08 01:30:29 +00:00
```