summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/native-lib/README.md
blob: 2bf65814d36ef17e5e73e0c87ffd0e718937a250 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# "native-lib" sample introduction

This sample demonstrates how to write required interfaces in native library, build it into a shared library and register the shared library to iwasm.

The native library should provide `get_native_lib` API for iwasm to return the native library info, including the module name, the native symbol list and the native symbol count, so that iwasm can use them to regiter the native library, for example:

```C
static int
foo_wrapper(wasm_exec_env_t exec_env, int x, int y)
{
    return x + y;
}

#define REG_NATIVE_FUNC(func_name, signature) \
    { #func_name, func_name##_wrapper, signature, NULL }

static NativeSymbol native_symbols[] = {
    REG_NATIVE_FUNC(foo, "(ii)i")
};

uint32_t
get_native_lib(char **p_module_name, NativeSymbol **p_native_symbols)
{
    *p_module_name = "env";
    *p_native_symbols = native_symbols;
    return sizeof(native_symbols) / sizeof(NativeSymbol);
}
```

## Preparation

Please install WASI SDK, download the [wasi-sdk release](https://github.com/CraneStation/wasi-sdk/releases) and extract the archive to default path `/opt/wasi-sdk`.

## Build the sample

```bash
mkdir build
cd build
cmake ..
make
```

`iwasm`, one wasm module `test.wasm` and two shared libraries `libtest_add.so`, `libtest_sqrt.so`
will be generated.

## Run workload

### Linux

```bash
cd build
./iwasm --native-lib=./libtest_add.so --native-lib=./libtest_sqrt.so --native-lib=./libtest_hello.so --native-lib=./libtest_hello2.so wasm-app/test.wasm
```

### macOS

```bash
cd build
./iwasm --native-lib=libtest_add.dylib --native-lib=libtest_sqrt.dylib --native-lib=libtest_hello.dylib --native-lib=libtest_hello2.dylib wasm-app/test.wasm
```

The output is:

```bash
Hello World!
10 + 20 = 30
sqrt(10, 20) = 500
test_hello("main", 0x0, 0) = 41
malloc(42) = 0x24e8
test_hello("main", 0x24e8, 42) = 41
Message from test_hello: Hello, main. This is test_hello_wrapper!
test_hello2("main", 0x0, 0) = 85
malloc(86) = 0x24e8
test_hello2("main", 0x24e8, 86) = 85
Message from test_hello2: Hello, main. This is test_hello2_wrapper! Your wasm_module_inst_t is 0x7fd443704990.
```