|
1 | 1 | --- |
2 | | -applyTo: '**' |
| 2 | +applyTo: 'samples/wamr/**' |
3 | 3 | --- |
4 | | - |
5 | 4 | # WAMR Instructions |
6 | 5 |
|
7 | | -- This code uses the [wasm-micro-runtime](https://github.com/bytecodealliance/wasm-micro-runtime) to host a wasm guest. |
| 6 | +## WebAssembly Micro Runtime Integration |
| 7 | + |
| 8 | +This code demonstrates how to use the [WebAssembly Micro Runtime (WAMR)](https://github.com/bytecodealliance/wasm-micro-runtime) to host WebAssembly components with Component Model bindings. |
| 9 | + |
| 10 | +### WAMR Overview |
| 11 | +WAMR is a lightweight, high-performance WebAssembly runtime designed for: |
| 12 | +- **Embedded Systems**: Optimized for resource-constrained environments |
| 13 | +- **Edge Computing**: Fast startup and low memory footprint |
| 14 | +- **Cross-platform**: Supports multiple architectures and operating systems |
| 15 | +- **Multiple Execution Modes**: Interpreter, AOT, and JIT compilation |
| 16 | + |
| 17 | +### Integration Architecture |
| 18 | + |
| 19 | +#### Host Function Registration |
| 20 | +```cpp |
| 21 | +// Register native functions that the WebAssembly guest can call |
| 22 | +bool success = wasm_runtime_register_natives_raw("module_name", symbols, count); |
| 23 | +``` |
| 24 | + |
| 25 | +#### Component Model Host Functions |
| 26 | +The `samples/wamr/main.cpp` demonstrates: |
| 27 | +- **Type Mapping**: C++ types to Component Model types using `cmcpp` namespace |
| 28 | +- **Function Wrappers**: Using `host_function()` to create WAMR-compatible function pointers |
| 29 | +- **Module Organization**: Grouping functions by interface/module names |
| 30 | + |
| 31 | +#### Memory Management |
| 32 | +- **Linear Memory**: Access WebAssembly linear memory through `wasm_memory_get_base_address()` |
| 33 | +- **Reallocation**: Use `cabi_realloc` for guest memory management |
| 34 | +- **Memory Safety**: Proper bounds checking and address validation |
| 35 | + |
| 36 | +### WAMR-Specific Implementation Details |
| 37 | + |
| 38 | +#### Execution Environment |
| 39 | +```cpp |
| 40 | +wasm_exec_env_t exec_env = wasm_runtime_create_exec_env(module_inst, stack_size); |
| 41 | +``` |
| 42 | +- **Stack Size**: Configure stack size for WebAssembly execution |
| 43 | +- **Heap Size**: Set heap size for guest memory allocation |
| 44 | +- **Module Instance**: Each module gets its own instance with isolated memory |
| 45 | + |
| 46 | +#### Function Calling Convention |
| 47 | +- **Native Symbols**: Use `NativeSymbol` arrays to define exported functions |
| 48 | +- **Argument Passing**: Arguments passed via `uint64_t *args` array |
| 49 | +- **Return Values**: Return values set through the same args array |
| 50 | +- **Type Conversion**: Automatic conversion between C++ and WebAssembly types |
| 51 | + |
| 52 | +#### Integration with Component Model |
| 53 | +The `wamr.hpp` header provides: |
| 54 | +- **Template Functions**: `export_func<F>()` for automatic type marshaling |
| 55 | +- **Host Function Creation**: `host_function()` helper for creating native symbols |
| 56 | +- **Memory Access**: Safe linear memory access with bounds checking |
| 57 | +- **Encoding Support**: UTF-8, UTF-16 string encoding handling |
| 58 | + |
| 59 | +### Sample Structure (`samples/wamr/`) |
| 60 | + |
| 61 | +#### Files |
| 62 | +- **main.cpp**: Complete example showing WAMR integration |
| 63 | +- **CMakeLists.txt**: Build configuration for WAMR sample |
| 64 | +- **main.c**: Alternative C implementation (if needed) |
| 65 | + |
| 66 | +#### Example Functions Demonstrated |
| 67 | +- **Void Functions**: Simple function calls without parameters |
| 68 | +- **Boolean Operations**: Logical operations with Component Model bool_t |
| 69 | +- **Numeric Operations**: Float and integer arithmetic |
| 70 | +- **String Manipulation**: UTF-8 string processing and transformation |
| 71 | +- **Logging**: Host-side logging from guest functions |
| 72 | + |
| 73 | +### Build Requirements |
| 74 | +- **WAMR Library**: Requires libiwasm and libvmlib |
| 75 | +- **vcpkg Integration**: Uses vcpkg for dependency management |
| 76 | +- **CMake Configuration**: Proper CMake setup for finding WAMR libraries |
| 77 | + |
| 78 | +### Error Handling |
| 79 | +- **Runtime Errors**: Check return values from WAMR API calls |
| 80 | +- **Memory Errors**: Validate memory access and allocation |
| 81 | +- **Type Errors**: Ensure proper type conversion between host and guest |
| 82 | +- **Module Loading**: Handle WebAssembly module loading failures |
| 83 | + |
| 84 | +### Performance Considerations |
| 85 | +- **Function Call Overhead**: Minimize marshaling overhead for hot paths |
| 86 | +- **Memory Allocation**: Efficient guest memory management |
| 87 | +- **Type Conversions**: Optimize type lifting/lowering operations |
| 88 | + |
| 89 | +### References |
| 90 | +- [WAMR GitHub Repository](https://github.com/bytecodealliance/wasm-micro-runtime) |
| 91 | +- [WAMR Documentation](https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/README.md) |
| 92 | +- [WAMR Host API](https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/core/iwasm/include/wasm_export.h) |
0 commit comments