From bc58778c34d9e1cbd409b6be837b869afde6c714 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 27 Oct 2022 16:06:14 +0900 Subject: [PATCH] samples/native-lib: Add a bit more complicated example (#1643) Add test_hello sample and update the document --- samples/native-lib/CMakeLists.txt | 1 + samples/native-lib/README.md | 8 +++++-- samples/native-lib/test_hello.c | 34 ++++++++++++++++++++++++++++++ samples/native-lib/wasm-app/main.c | 15 +++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 samples/native-lib/test_hello.c diff --git a/samples/native-lib/CMakeLists.txt b/samples/native-lib/CMakeLists.txt index 3da067777..b6665c2cf 100644 --- a/samples/native-lib/CMakeLists.txt +++ b/samples/native-lib/CMakeLists.txt @@ -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) diff --git a/samples/native-lib/README.md b/samples/native-lib/README.md index 797469bfe..d5b50a716 100644 --- a/samples/native-lib/README.md +++ b/samples/native-lib/README.md @@ -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: @@ -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! ``` diff --git a/samples/native-lib/test_hello.c b/samples/native-lib/test_hello.c new file mode 100644 index 000000000..c322ec24e --- /dev/null +++ b/samples/native-lib/test_hello.c @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include + +#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); +} diff --git a/samples/native-lib/wasm-app/main.c b/samples/native-lib/wasm-app/main.c index f00ec0602..09b283789 100644 --- a/samples/native-lib/wasm-app/main.c +++ b/samples/native-lib/wasm-app/main.c @@ -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"); @@ -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; }