added: running `.wasm` binaries with `onyx run`; deprecated: `onyx-run`
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 28 Feb 2023 18:46:59 +0000 (12:46 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 28 Feb 2023 18:46:59 +0000 (12:46 -0600)
compiler/include/astnodes.h
compiler/src/onyx.c

index ac294ef5c7f24161bf74da97cba42f9ac57047a3..09d3dc2dd89bf223fbdf7cea5ba41c5413dce10d 100644 (file)
@@ -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,
 };
index caab41f69d406c72100749023f23d6e0a3074174..8ee38dc31fde8d7d93b193cc0c179b912269f7f6 100644 (file)
@@ -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;
     }