Skip to content

Commit

Permalink
samples/native-lib: Add a bit more complicated example (bytecodeallia…
Browse files Browse the repository at this point in the history
…nce#1643)

Add test_hello sample and update the document
  • Loading branch information
yamt authored Oct 27, 2022
1 parent 264fdfa commit bc58778
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions samples/native-lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ target_link_libraries(iwasm vmlib -lpthread -lm -ldl)
################ native libraries ###############
add_library (test_add SHARED test_add.c)
add_library (test_sqrt SHARED test_sqrt.c)
add_library (test_hello SHARED test_hello.c)

################ wasm application ###############
add_subdirectory(wasm-app)
8 changes: 6 additions & 2 deletions samples/native-lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ will be generated.

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

### macOS

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

The output is:
Expand All @@ -65,4 +65,8 @@ The output is:
Hello World!
10 + 20 = 30
sqrt(10, 20) = 500
test_hello("main", 0x0, 0) = 41
malloc(42) = 0x24b8
test_hello("main", 0x24b8, 42) = 41
Message from test_hello: Hello, main. This is test_hello_wrapper!
```
34 changes: 34 additions & 0 deletions samples/native-lib/test_hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/

#include <stdio.h>
#include <stdlib.h>

#include "wasm_export.h"

static int
test_hello_wrapper(wasm_exec_env_t exec_env, const char *name, char *result,
size_t resultlen)
{
return snprintf(result, resultlen, "Hello, %s. This is %s!\n", name,
__func__);
}

/* clang-format off */
#define REG_NATIVE_FUNC(func_name, signature) \
{ #func_name, func_name##_wrapper, signature, NULL }

static NativeSymbol native_symbols[] = {
REG_NATIVE_FUNC(test_hello, "($*~)i")
};
/* clang-format on */

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);
}
15 changes: 15 additions & 0 deletions samples/native-lib/wasm-app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ test_add(int x, int y);
int
test_sqrt(int x, int y);

int
test_hello(const char *name, char *buf, size_t buflen);

int
main(int argc, char **argv)
{
const char *name = __func__;
char *buf;
size_t buflen;
int x = 10, y = 20, res;

printf("Hello World!\n");
Expand All @@ -25,5 +31,14 @@ main(int argc, char **argv)
res = test_sqrt(x, y);
printf("sqrt(%d, %d) = %d\n", x, y, res);

res = test_hello(name, NULL, 0);
printf("test_hello(\"%s\", %p, %zu) = %d\n", name, NULL, (size_t)0, res);
buflen = res + 1;
buf = malloc(buflen);
printf("malloc(%zu) = %p\n", buflen, buf);
res = test_hello(__func__, buf, buflen);
printf("test_hello(\"%s\", %p, %zu) = %d\n", name, buf, buflen, res);
printf("Message from test_hello: %s", buf);

return 0;
}

0 comments on commit bc58778

Please sign in to comment.