瀏覽代碼

samples/native-lib: Add a bit more complicated example (#1643)

Add test_hello sample and update the document
YAMAMOTO Takashi 3 年之前
父節點
當前提交
bc58778c34

+ 1 - 0
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)

+ 6 - 2
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!
 ```

+ 34 - 0
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 <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 - 0
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;
 }