more robustness to 'onyx run'
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 28 Oct 2021 17:24:11 +0000 (12:24 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 28 Oct 2021 17:24:11 +0000 (12:24 -0500)
core/io/file.onyx
include/astnodes.h
src/onyx.c
src/wasm_runtime.c

index 1bd7f76bacf647737d4e7f2d127d0193d852aae9..4c3a7f1e5db13dc009c4d53564c369481e81a172 100644 (file)
@@ -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 {
index 2f1514ee47ab1bf16964ea252cd65f3bf9299f28..bdcac1b77695f91871e53526695c1a7b6ac7c338 100644 (file)
@@ -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;
index d0ce5a64f103865344a963e550d4ee31b566a470..fb02bef8467b11f97ba7d217643e30f403bf3470 100644 (file)
@@ -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
index 3d79902ce7345eb0ae090af8f307d6f568422796..792cba328b6dcae2a48b2db4d7e3a98fbe9b94aa 100644 (file)
@@ -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);