From e3fb34fa4dffc22e5f88412ff3a5079c93b2f9db Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Fri, 10 Dec 2021 10:17:44 -0600 Subject: [PATCH] working to isolate the onyx_runtime functionality --- include/astnodes.h | 1 - include/wasm_emit.h | 3 ++- src/onyx.c | 5 ++--- src/wasm_emit.c | 2 ++ src/wasm_output.h | 12 ++++++++++++ src/wasm_runtime.c | 35 ++++++++++++++++++++++++++--------- 6 files changed, 44 insertions(+), 14 deletions(-) diff --git a/include/astnodes.h b/include/astnodes.h index fdf729c8..db33aef1 100644 --- a/include/astnodes.h +++ b/include/astnodes.h @@ -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; diff --git a/include/wasm_emit.h b/include/wasm_emit.h index 3892b352..74569f47 100644 --- a/include/wasm_emit.h +++ b/include/wasm_emit.h @@ -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 diff --git a/src/onyx.c b/src/onyx.c index 30cb9b61..3331c08c 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -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 diff --git a/src/wasm_emit.c b/src/wasm_emit.c index 7a4f2d11..e34de4b4 100644 --- a/src/wasm_emit.c +++ b/src/wasm_emit.c @@ -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); diff --git a/src/wasm_output.h b/src/wasm_output.h index d24e8d00..ae1a7748 100644 --- a/src/wasm_output.h +++ b/src/wasm_output.h @@ -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); diff --git a/src/wasm_runtime.c b/src/wasm_runtime.c index 74d98f77..bd52138c 100644 --- a/src/wasm_runtime.c +++ b/src/wasm_runtime.c @@ -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]); } } -- 2.25.1