From: Brendan Hansen Date: Tue, 28 Feb 2023 18:46:59 +0000 (-0600) Subject: added: running `.wasm` binaries with `onyx run`; deprecated: `onyx-run` X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=4de9fa1eb4dccac6177bdd9cbfd329b07b6825f2;p=onyx.git added: running `.wasm` binaries with `onyx run`; deprecated: `onyx-run` --- diff --git a/compiler/include/astnodes.h b/compiler/include/astnodes.h index ac294ef5..09d3dc2d 100644 --- a/compiler/include/astnodes.h +++ b/compiler/include/astnodes.h @@ -1562,6 +1562,7 @@ enum CompileAction { ONYX_COMPILE_ACTION_COMPILE, ONYX_COMPILE_ACTION_CHECK, ONYX_COMPILE_ACTION_RUN, + ONYX_COMPILE_ACTION_RUN_WASM, ONYX_COMPILE_ACTION_DOCUMENT, ONYX_COMPILE_ACTION_PRINT_HELP, }; diff --git a/compiler/src/onyx.c b/compiler/src/onyx.c index caab41f6..8ee38dc3 100644 --- a/compiler/src/onyx.c +++ b/compiler/src/onyx.c @@ -206,7 +206,22 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg } #endif else { - bh_arr_push(options.files, argv[i]); + if (bh_str_ends_with(argv[i], ".wasm") && options.action == ONYX_COMPILE_ACTION_RUN) { + if (bh_arr_length(options.files) > 0) { + bh_printf("Expected only one '.wasm', or multiple '.onyx' files to be given, not a mixture.\n"); + exit(1); + } + + options.action = ONYX_COMPILE_ACTION_RUN_WASM; + options.target_file = argv[i]; + + options.passthrough_argument_count = argc - i - 1; + options.passthrough_argument_data = &argv[i + 1]; + break; + + } else { + bh_arr_push(options.files, argv[i]); + } } } } @@ -799,12 +814,7 @@ static CompilerProgress onyx_flush_module() { } #ifdef ENABLE_RUN_WITH_WASMER -static b32 onyx_run() { - link_wasm_module(); - - bh_buffer code_buffer; - onyx_wasm_module_write_to_buffer(context.wasm_module, &code_buffer); - +static b32 onyx_run_module(bh_buffer code_buffer) { onyx_run_initialize(context.options->debug_enabled); if (context.options->verbose_output > 0) @@ -812,6 +822,26 @@ static b32 onyx_run() { return onyx_run_wasm(code_buffer, context.options->passthrough_argument_count, context.options->passthrough_argument_data); } + +static b32 onyx_run_wasm_file(const char *filename) { + bh_file_contents contents = bh_file_read_contents(global_heap_allocator, filename); + + bh_buffer code_buffer; + code_buffer.data = contents.data; + code_buffer.length = contents.length; + + return onyx_run_module(code_buffer); +} + +static b32 onyx_run() { + link_wasm_module(); + + bh_buffer code_buffer; + onyx_wasm_module_write_to_buffer(context.wasm_module, &code_buffer); + + return onyx_run_module(code_buffer); + +} #endif int main(int argc, char *argv[]) { @@ -854,6 +884,14 @@ int main(int argc, char *argv[]) { break; #endif + #ifdef ENABLE_RUN_WITH_WASMER + case ONYX_COMPILE_ACTION_RUN_WASM: + compiler_progress = ONYX_COMPILER_PROGRESS_SUCCESS; + if (!onyx_run_wasm_file(context.options->target_file)) { + compiler_progress = ONYX_COMPILER_PROGRESS_ERROR; + } + #endif + default: break; }