working to isolate the onyx_runtime functionality
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 10 Dec 2021 16:17:44 +0000 (10:17 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 10 Dec 2021 16:17:44 +0000 (10:17 -0600)
include/astnodes.h
include/wasm_emit.h
src/onyx.c
src/wasm_emit.c
src/wasm_output.h
src/wasm_runtime.c

index fdf729c89682f496546316682a68677de9d1547b..db33aef18d87f8bc91893da3b1839cff91604e8f 100644 (file)
@@ -1468,7 +1468,6 @@ struct CompileOptions {
     Runtime runtime;
 
     bh_arr(const char *) included_folders;
-    bh_arr(const char *) included_library_folders;
     bh_arr(const char *) files;
     const char* target_file;
     const char* documentation_file;
index 3892b3522fc4de5759288c7c1a714539bab48b3f..74569f47cfdd2d83fd463e37f0a02386097f73dd 100644 (file)
@@ -653,6 +653,7 @@ typedef struct OnyxWasmModule {
     bh_arr(WasmDatum)     data;
     bh_arr(i32)           elems;
     bh_arr(char *)        libraries;
+    bh_arr(char *)        library_paths;
 
     // NOTE: Set of things used when compiling; not part of the actual module
     u32 export_count;
@@ -679,7 +680,7 @@ void onyx_wasm_module_write_to_buffer(OnyxWasmModule* module, bh_buffer* buffer)
 void onyx_wasm_module_write_to_file(OnyxWasmModule* module, bh_file file);
 
 #ifdef ENABLE_RUN_WITH_WASMER
-b32 onyx_run_wasm(bh_buffer code_buffer);
+b32 onyx_run_wasm(bh_buffer code_buffer, int argc, char *argv[]);
 #endif
 
 #endif
index 30cb9b616219f286f66276c6f8a43c0eaf42dca3..3331c08c2bd7ad993cb97b58ead10fafd9437e4f 100644 (file)
@@ -86,7 +86,6 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg
 
     bh_arr_new(alloc, options.files, 2);
     bh_arr_new(alloc, options.included_folders, 2);
-    bh_arr_new(alloc, options.included_library_folders, 2);
 
     // NOTE: Add the current folder
     bh_arr_push(options.included_folders, CORE_INSTALLATION);
@@ -325,7 +324,7 @@ static void process_load_entity(Entity* ent) {
         bh_arr_push(context.options->included_folders, include->name);
 
     } else if (include->kind == Ast_Kind_Library_Path) {
-        bh_arr_push(context.options->included_library_folders, include->name);
+        bh_arr_push(context.wasm_module->library_paths, include->name);
     }
 }
 
@@ -596,7 +595,7 @@ static b32 onyx_run() {
     if (context.options->verbose_output > 0)
         bh_printf("Running program:\n");
 
-    return onyx_run_wasm(code_buffer);
+    return onyx_run_wasm(code_buffer, context.options->passthrough_argument_count, context.options->passthrough_argument_data);
 }
 #endif
 
index 7a4f2d11f4186c3b60eb927a1916f9d95e29e731..e34de4b418839dcda32eea509eac21f9bf193a95 100644 (file)
@@ -3751,6 +3751,7 @@ OnyxWasmModule onyx_wasm_module_create(bh_allocator alloc) {
         .null_proc_func_idx = -1,
 
         .libraries = NULL,
+        .library_paths = NULL,
     };
 
     bh_arena* eid = bh_alloc(global_heap_allocator, sizeof(bh_arena));
@@ -3765,6 +3766,7 @@ OnyxWasmModule onyx_wasm_module_create(bh_allocator alloc) {
     bh_arr_new(alloc, module.data, 4);
     bh_arr_new(alloc, module.elems, 4);
     bh_arr_new(alloc, module.libraries, 4);
+    bh_arr_new(alloc, module.library_paths, 4);
 
     bh_arr_new(global_heap_allocator, module.return_location_stack, 4);
     bh_arr_new(global_heap_allocator, module.structured_jump_target, 16);
index d24e8d00e3f04ddaeb693fb0fb3c772f03e02d60..ae1a77484be9cd4ed50832a4af0c812c161e6749 100644 (file)
@@ -680,6 +680,18 @@ static i32 output_onyx_libraries_section(OnyxWasmModule* module, bh_buffer* buff
     bh_buffer_append(&libs_buff, leb, leb_len);
     bh_buffer_append(&libs_buff, custom_name, strlen(custom_name));
 
+    leb = uint_to_uleb128((u64) bh_arr_length(module->library_paths), &leb_len);
+    bh_buffer_append(&libs_buff, leb, leb_len);
+
+    bh_arr_each(char *, lib, module->library_paths) {
+        assert(*lib != NULL);
+
+        u32 lib_len = strlen(*lib);
+        leb = uint_to_uleb128((u64) lib_len, &leb_len);
+        bh_buffer_append(&libs_buff, leb, leb_len);
+        bh_buffer_append(&libs_buff, *lib, lib_len);
+    }
+
     leb = uint_to_uleb128((u64) bh_arr_length(module->libraries), &leb_len);
     bh_buffer_append(&libs_buff, leb, leb_len);
 
index 74d98f77ce0f0cbfdd7ef0ea647246c5297159a2..bd52138c5d70b5957c39181278f0d44ebf17048d 100644 (file)
@@ -138,7 +138,7 @@ static i32 onyx_run_thread(void *data) {
 }
 
 WASM_INTEROP(onyx_spawn_thread_impl) {
-    if (threads == NULL) bh_arr_new(global_heap_allocator, threads, 128);
+    if (threads == NULL) bh_arr_new(bh_heap_allocator(), threads, 128);
     bh_arr_insert_end(threads, 1);
     OnyxThread *thread = &bh_arr_last(threads);
 
@@ -503,6 +503,7 @@ WASM_INTEROP(onyx_process_destroy_impl) {
 
 typedef void *(*LibraryLinker)(OnyxRuntime *runtime);
 static bh_arr(WasmFuncDefinition **) linkable_functions = NULL;
+static bh_arr(char *) library_paths = NULL;
 
 static void onyx_load_library(char *name) {
     #ifdef _BH_LINUX
@@ -517,11 +518,13 @@ static void onyx_load_library(char *name) {
         if (name[i] == DIR_SEPARATOR) library = &name[i + 1];
     }
 
-    char *library_load_name = bh_aprintf(global_scratch_allocator, "onyx_library_%s", library);
+    char *library_load_name_tmp = bh_bprintf("onyx_library_%s", library);
+    char *library_load_name = alloca(strlen(library_load_name_tmp));
+    strcpy(library_load_name, library_load_name_tmp);
     LibraryLinker library_load;
 
     #ifdef _BH_LINUX
-    char *library_name = lookup_included_file(name, ".", ".so", 1, context.options->included_library_folders, 1);
+    char *library_name = lookup_included_file(name, ".", ".so", 1, (const char **) library_paths, 1);
     void* handle = dlopen(library_name, RTLD_LAZY);
     if (handle == NULL) {
         printf("ERROR LOADING LIBRARY %s: %s\n", name, dlerror());
@@ -536,7 +539,7 @@ static void onyx_load_library(char *name) {
     #endif
 
     #ifdef _BH_WINDOWS
-    char *library_name = lookup_included_file(name, ".", ".dll", 1, context.options->included_library_folders, 1);
+    char *library_name = lookup_included_file(name, ".", ".dll", 1, (const char **) library_paths, 1);
     HMODULE handle = LoadLibraryA(library_name);
     if (handle == NULL) {
         printf("ERROR LOADING LIBRARY %s: %d\n", name, GetLastError());
@@ -567,6 +570,20 @@ static void onyx_lookup_and_load_custom_libraries(bh_buffer wasm_bytes) {
                 cursor += name_len;
                 u64 lib_count = uleb128_to_uint(wasm_bytes.data, &cursor);
 
+                fori (i, 0, (i64) lib_count) {
+                    u64 lib_path_length = uleb128_to_uint(wasm_bytes.data, &cursor);
+                    lib_path_length = bh_min(lib_path_length, 512);
+
+                    char *lib_path = malloc(lib_path_length);
+                    strncpy(lib_path, wasm_bytes.data + cursor, lib_path_length);
+                    lib_path[lib_path_length] = '\0';
+                    cursor += lib_path_length;
+
+                    bh_arr_push(library_paths, lib_path);
+                }
+
+                lib_count = uleb128_to_uint(wasm_bytes.data, &cursor);
+
                 fori (i, 0, (i64) lib_count) {
                     u64 lib_name_length = uleb128_to_uint(wasm_bytes.data, &cursor);
                     lib_name_length = bh_min(lib_name_length, 256);
@@ -587,10 +604,10 @@ static void onyx_lookup_and_load_custom_libraries(bh_buffer wasm_bytes) {
 }
 
 // Returns 1 if successful
-b32 onyx_run_wasm(bh_buffer wasm_bytes) {
+b32 onyx_run_wasm(bh_buffer wasm_bytes, int argc, char *argv[]) {
     runtime = &wasm_runtime;
 
-    bh_arr_new(global_heap_allocator, linkable_functions, 4);
+    bh_arr_new(bh_heap_allocator(), linkable_functions, 4);
     onyx_lookup_and_load_custom_libraries(wasm_bytes);
 
     wasmer_features_t* features = NULL;
@@ -611,9 +628,9 @@ b32 onyx_run_wasm(bh_buffer wasm_bytes) {
     wasm_config_set_features(wasm_config, features);
 
     wasi_config = wasi_config_new("onyx");
-    if (context.options->passthrough_argument_count > 0) {
-        fori (i, 0, context.options->passthrough_argument_count) {
-            wasi_config_arg(wasi_config, context.options->passthrough_argument_data[i]);
+    if (argc > 0) {
+        fori (i, 0, argc) {
+            wasi_config_arg(wasi_config, argv[i]);
         }
     }