From eaa4a451cdc1d7188bfdcbb65870ec25e93f8e6a Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Thu, 28 Oct 2021 12:24:11 -0500 Subject: [PATCH] more robustness to 'onyx run' --- core/io/file.onyx | 37 +++++++++++++++++++------------------ include/astnodes.h | 3 +++ src/onyx.c | 26 ++++++++++++++++++++++---- src/wasm_runtime.c | 9 ++++++++- 4 files changed, 52 insertions(+), 23 deletions(-) diff --git a/core/io/file.onyx b/core/io/file.onyx index 1bd7f76b..4c3a7f1e 100644 --- a/core/io/file.onyx +++ b/core/io/file.onyx @@ -32,9 +32,6 @@ File :: struct { } file_open :: (path: str, mode := OpenMode.Read, flags := FDFlags.Sync) -> (File, bool) { - // Currently the directory's file descriptor appears to always be 3 - DIR_FD :: 3; - // Requesting all of the rights because why not. rights := Rights.DataSync @@ -87,24 +84,28 @@ file_open :: (path: str, mode := OpenMode.Read, flags := FDFlags.Sync) -> (File, } file := File.{ fd = -1 }; - - if err := wasi.path_open( - DIR_FD, - .SymLinkFollow, - path, - open_flags, - rights, - rights, - fd_flags, - ^file.fd); - err != .Success { - return file, false; - } - file.mode = mode; file.rights = rights; file.flags = fd_flags; - return file, true; + + // Currently the directory's file descriptor appears to always be 3 + // However, this is not necessarily correct, so also try a preopened directory + for DIR_FD: .[ 3, 4 ] { + if err := wasi.path_open( + DIR_FD, + .SymLinkFollow, + path, + open_flags, + rights, + rights, + fd_flags, + ^file.fd); + err == .Success { + return file, true; + } + } + + return file, false; } file_close :: (file: File) -> bool { diff --git a/include/astnodes.h b/include/astnodes.h index 2f1514ee..bdcac1b7 100644 --- a/include/astnodes.h +++ b/include/astnodes.h @@ -1277,6 +1277,9 @@ struct CompileOptions { bh_arr(const char *) files; const char* target_file; const char* documentation_file; + + i32 passthrough_argument_count; + char** passthrough_argument_data; }; typedef struct Context Context; diff --git a/src/onyx.c b/src/onyx.c index d0ce5a64..fb02bef8 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -74,6 +74,9 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg .target_file = "out.wasm", .documentation_file = NULL, + + .passthrough_argument_count = 0, + .passthrough_argument_data = NULL, }; bh_arr_new(alloc, options.files, 2); @@ -149,6 +152,11 @@ static CompileOptions compile_opts_parse(bh_allocator alloc, int argc, char *arg else if (!strcmp(argv[i], "--doc")) { options.documentation_file = argv[++i]; } + else if (!strcmp(argv[i], "--")) { + options.passthrough_argument_count = argc - i - 1; + options.passthrough_argument_data = &argv[i + 1]; + break; + } #if defined(_BH_LINUX) // NOTE: Fun output is only enabled for Linux because Windows command line // is not ANSI compatible and for a silly feature, I don't want to learn @@ -517,7 +525,7 @@ static i32 onyx_compile() { return ONYX_COMPILER_PROGRESS_SUCCESS; } -CompilerProgress onyx_flush_module() { +static CompilerProgress onyx_flush_module() { // NOTE: Output to file bh_file output_file; @@ -564,6 +572,18 @@ CompilerProgress onyx_flush_module() { return ONYX_COMPILER_PROGRESS_SUCCESS; } +#ifdef ENABLE_RUN_WITH_WASMER +static void onyx_run() { + bh_buffer code_buffer; + onyx_wasm_module_write_to_buffer(context.wasm_module, &code_buffer); + + if (context.options->verbose_output > 0) + bh_printf("Running program:"); + + onyx_run_wasm(code_buffer); +} +#endif + int main(int argc, char *argv[]) { bh_scratch_init(&global_scratch, bh_heap_allocator(), 256 * 1024); // NOTE: 256 KiB @@ -593,9 +613,7 @@ int main(int argc, char *argv[]) { case ONYX_COMPILE_ACTION_RUN: compiler_progress = onyx_compile(); if (compiler_progress == ONYX_COMPILER_PROGRESS_SUCCESS) { - bh_buffer code_buffer; - onyx_wasm_module_write_to_buffer(context.wasm_module, &code_buffer); - onyx_run_wasm(code_buffer); + onyx_run(); } break; #endif diff --git a/src/wasm_runtime.c b/src/wasm_runtime.c index 3d79902c..792cba32 100644 --- a/src/wasm_runtime.c +++ b/src/wasm_runtime.c @@ -1,4 +1,5 @@ #include "bh.h" +#include "astnodes.h" #include "wasm.h" #include "wasmer.h" @@ -15,7 +16,13 @@ void onyx_run_wasm(bh_buffer wasm_bytes) { } wasi_config_t* wasi_config = wasi_config_new("onyx"); - wasi_config_preopen_dir(wasi_config, "."); + 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]); + } + } + + wasi_config_preopen_dir(wasi_config, "./"); wasi_env_t* wasi_env = wasi_env_new(wasi_config); -- 2.25.1