getting more of the linking to work
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 5 Dec 2021 14:31:54 +0000 (08:31 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 5 Dec 2021 14:31:54 +0000 (08:31 -0600)
build.sh
docs/pluggable_modules.md
misc/onyx_library.h [new file with mode: 0644]
misc/onyx_module.h [deleted file]
modules/test_library/test_library.c
src/wasm_runtime.c

index 88388a8e533e6b06aa8febf5e1fbf0542bd60b30..6fb93aa97e7e5b22e14ca2da29c2eba3d2a66bb0 100755 (executable)
--- a/build.sh
+++ b/build.sh
@@ -44,7 +44,7 @@ else
 fi
 
 C_FILES="$C_FILES wasm_runtime"
-FLAGS="$FLAGS -DENABLE_RUN_WITH_WASMER"
+FLAGS="$FLAGS -DENABLE_RUN_WITH_WASMER -rdynamic"
 LIBS="-L$CORE_DIR/lib -lwasmer -Wl,-rpath=$CORE_DIR/lib -lpthread -ldl"
 INCLUDES="-I$WASMER_INCLUDE_DIR"
 
index 3151fdacd62fc38a799a0765f501c02fd79709ad..ee87eb7852e9e7a63795f9fa9c7d4c6912063d71 100644 (file)
@@ -68,4 +68,10 @@ The corresponding C file that will be compiled to a so/dll looks like:
 
 Compiling the C file with:
 
-    gcc -o some_useful_code.so -shared -fPIC some_useful_code.c -I ...
\ No newline at end of file
+    gcc -o some_useful_code.so -shared -fPIC some_useful_code.c -I ...
+
+
+A couple of questions that need to be answered:
+    - How is the WASM memory object going to be given to the shared object
+        code? Can it just be a public symbol that gets linked against? or
+        does it need to be passed to an initialization function?
\ No newline at end of file
diff --git a/misc/onyx_library.h b/misc/onyx_library.h
new file mode 100644 (file)
index 0000000..d9d9ed1
--- /dev/null
@@ -0,0 +1,54 @@
+
+#include "wasm.h"
+
+extern wasm_memory_t* wasm_memory;
+
+typedef struct WasmValkindBuffer {
+    unsigned int count;
+    wasm_valkind_t types[20];
+} WasmValkindBuffer;
+
+typedef struct WasmFuncDefinition {
+    char* module_name;
+    char* import_name;
+    wasm_func_callback_t func;
+
+    WasmValkindBuffer *params;
+    WasmValkindBuffer *results;
+} WasmFuncDefinition;
+
+#define STRINGIFY1(a) #a
+#define CONCAT2(a, b) a ## _ ## b
+#define CONCAT3(a, b, c) a ## _ ## b ## _ ## c
+#define ONYX_MODULE_NAME_GEN(m) CONCAT2(__onyx_library, m)
+#define ONYX_LINK_NAME_GEN(m) CONCAT2(onyx_library, m)
+#define ONYX_FUNC_NAME(m, n) CONCAT3(__onyx_internal, m, n)
+#define ONYX_DEF_NAME(m, n) CONCAT3(__onyx_internal_def, m, n)
+#define ONYX_PARAM_NAME(m, n) CONCAT3(__onyx_internal_param_buffer, m, n)
+#define ONYX_RESULT_NAME(m, n) CONCAT3(__onyx_internal_result_buffer, m, n)
+#define ONYX_IMPORT_NAME(m, n) STRINGIFY1(m) "_" #n
+
+#define NUM_VALS(...) (sizeof((wasm_valkind_t []){ 0, __VA_ARGS__ }) / sizeof(wasm_valkind_t))
+#define _VALS(...) { NUM_VALS(__VA_ARGS__) - 1, __VA_ARGS__ }
+
+#define ONYX_DEF(name, params_types, result_types) \
+    static wasm_trap_t* ONYX_FUNC_NAME(ONYX_LIBRARY_NAME, name)(const wasm_val_vec_t* params, wasm_val_vec_t* results); \
+    static struct WasmValkindBuffer  ONYX_PARAM_NAME(ONYX_LIBRARY_NAME, name) = _VALS params_types; \
+    static struct WasmValkindBuffer  ONYX_RESULT_NAME(ONYX_LIBRARY_NAME, name) = _VALS result_types; \
+    static struct WasmFuncDefinition ONYX_DEF_NAME(ONYX_LIBRARY_NAME, name) = { STRINGIFY1(ONYX_LIBRARY_NAME), #name, ONYX_FUNC_NAME(ONYX_LIBRARY_NAME, name), & ONYX_PARAM_NAME(ONYX_LIBRARY_NAME, name), & ONYX_RESULT_NAME(ONYX_LIBRARY_NAME, name) }; \
+    \
+    static wasm_trap_t* ONYX_FUNC_NAME(ONYX_LIBRARY_NAME, name)(const wasm_val_vec_t* params, wasm_val_vec_t* results)
+
+#define ONYX_FUNC(name) & ONYX_DEF_NAME(ONYX_LIBRARY_NAME, name),
+#define ONYX_LIBRARY \
+    extern struct WasmFuncDefinition *ONYX_MODULE_NAME_GEN(ONYX_LIBRARY_NAME)[]; \
+    WasmFuncDefinition** ONYX_LINK_NAME_GEN(ONYX_LIBRARY_NAME)() { \
+        return ONYX_MODULE_NAME_GEN(ONYX_LIBRARY_NAME); \
+    } \
+    struct WasmFuncDefinition *ONYX_MODULE_NAME_GEN(ONYX_LIBRARY_NAME)[] =
+
+// Shorter names
+#define I32 WASM_I32
+#define I64 WASM_I64
+#define F32 WASM_F32
+#define F64 WASM_F64
diff --git a/misc/onyx_module.h b/misc/onyx_module.h
deleted file mode 100644 (file)
index 3ad2e35..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-
-#include "wasm.h"
-
-#define NUM_VALS(...) (sizeof((wasm_valkind_t []){ 0, __VA_ARGS__ }) / sizeof(wasm_valkind_t))
-#define _VALS(...) { NUM_VALS(__VA_ARGS__) - 1, __VA_ARGS__ }
-
-typedef struct WasmValkindBuffer {
-    unsigned int count;
-    wasm_valkind_t types[20];
-} WasmValkindBuffer;
-
-typedef struct WasmFuncDefinition {
-    char* module_name;
-    char* import_name;
-    wasm_func_callback_t func;
-
-    WasmValkindBuffer *params;
-    WasmValkindBuffer *results;
-} WasmFuncDefinition;
-
-#define STRINGIFY1(a) #a
-#define CONCAT2(a, b) a ## _ ## b
-#define CONCAT3(a, b, c) a ## _ ## b ## _ ## c
-#define ONYX_MODULE_NAME_GEN(m) CONCAT2(__onyx_module, m)
-#define ONYX_FUNC_NAME(m, n) CONCAT3(__onyx_internal, m, n)
-#define ONYX_DEF_NAME(m, n) CONCAT3(__onyx_internal_def, m, n)
-#define ONYX_PARAM_NAME(m, n) CONCAT3(__onyx_internal_param_buffer, m, n)
-#define ONYX_RESULT_NAME(m, n) CONCAT3(__onyx_internal_result_buffer, m, n)
-#define ONYX_IMPORT_NAME(m, n) STRINGIFY1(m) "_" #n
-
-#define ONYX_DEF(name, params_types, result_types) \
-    static wasm_trap_t* ONYX_FUNC_NAME(ONYX_MODULE_NAME, name)(const wasm_val_vec_t* params, wasm_val_vec_t* results); \
-    static struct WasmValkindBuffer  ONYX_PARAM_NAME(ONYX_MODULE_NAME, name) = _VALS params_types; \
-    static struct WasmValkindBuffer  ONYX_RESULT_NAME(ONYX_MODULE_NAME, name) = _VALS result_types; \
-    static struct WasmFuncDefinition ONYX_DEF_NAME(ONYX_MODULE_NAME, name) = { STRINGIFY1(ONYX_MODULE_NAME), #name, ONYX_FUNC_NAME(ONYX_MODULE_NAME, name), & ONYX_PARAM_NAME(ONYX_MODULE_NAME, name), & ONYX_RESULT_NAME(ONYX_MODULE_NAME, name) }; \
-    \
-    static wasm_trap_t* ONYX_FUNC_NAME(ONYX_MODULE_NAME, name)(const wasm_val_vec_t* params, wasm_val_vec_t* results)
-
-#define ONYX_FUNC(name) & ONYX_DEF_NAME(ONYX_MODULE_NAME, name),
-#define ONYX_MODULE struct WasmFuncDefinition *ONYX_MODULE_NAME_GEN(ONYX_MODULE_NAME) [] =
-
-// Shorter names
-#define I32 WASM_I32
-#define I64 WASM_I64
-#define F32 WASM_F32
-#define F64 WASM_F64
-
index 0a8fd25a517d94b71187d782c29f9ee19e28d995..08df6ad9ac5c0c45f2516dcb629bd28b6c33a885 100644 (file)
@@ -1,13 +1,13 @@
-#include "onyx_module.h"
+#include "onyx_library.h"
 #include <stdio.h>
 
-#define ONYX_MODULE_NAME test_library
+#define ONYX_LIBRARY_NAME test_library
 
 ONYX_DEF(foo, (), ()) {
     printf("This worked!\n");
     return NULL;
 }
 
-ONYX_MODULE {
+ONYX_LIBRARY {
     ONYX_FUNC(foo)
 };
\ No newline at end of file
index ef74408c063492c074ec1b638c8ed1aac1084af5..3ca0ccf608eff4dce4f6f4e204828ad8fd38bf7e 100644 (file)
@@ -21,7 +21,7 @@ static wasm_engine_t*    wasm_engine;
 static wasm_store_t*     wasm_store;
 static wasm_extern_vec_t wasm_imports;
 static wasm_module_t*    wasm_module;
-static wasm_memory_t*    wasm_memory;
+wasm_memory_t*    wasm_memory;
 
 b32 wasm_name_equals(const wasm_name_t* name1, const wasm_name_t* name2) {
     if (name1->size != name2->size) return 0;
@@ -503,16 +503,6 @@ WASM_INTEROP(onyx_process_destroy_impl) {
 // Returns 1 if successful
 b32 onyx_run_wasm(bh_buffer wasm_bytes) {
 
-    // NOCHECKIN
-    void* handle = dlopen("./test_library.so", RTLD_LAZY);
-    printf("HANDLE: %p\n", handle);
-    if (handle == NULL) {
-        printf("ERROR: %s\n", dlerror());
-    }
-    void *wasm_library = dlsym(handle, "__onyx_module_test_library");
-    printf("LOADED: %p %s\n", wasm_library, wasm_library);
-    dlclose(handle);
-
 
     wasm_instance_t* instance = NULL;
     wasmer_features_t* features = NULL;
@@ -690,6 +680,17 @@ b32 onyx_run_wasm(bh_buffer wasm_bytes) {
 
     wasm_trap_t* traps = NULL;
 
+    // NOCHECKIN
+    void* handle = dlopen("./test_library.so", RTLD_LAZY);
+    printf("HANDLE: %p\n", handle);
+    if (handle == NULL) {
+        printf("ERROR: %s\n", dlerror());
+    }
+    void *wasm_library = dlsym(handle, "onyx_library_test_library");
+    printf("LOADED: %p\n", wasm_library);
+    printf("TABLE: %p\n", ((void* (*)()) wasm_library)());
+    dlclose(handle);
+
     instance = wasm_instance_new(wasm_store, wasm_module, &wasm_imports, &traps);
     if (!instance) goto error_handling;