added command line arguments to onyx runtime
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 12 Dec 2021 15:48:22 +0000 (09:48 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 12 Dec 2021 15:48:22 +0000 (09:48 -0600)
core/runtime/onyx_run.onyx
include/onyx_library.h
modules/onyx_runtime/onyx_runtime.c
src/wasm_runtime.c

index 1b2c6d84e20132f220f9c75484f2374941f8f272..13c3f429a88f85575d6359718ff98bfaaaca9615 100644 (file)
@@ -14,7 +14,6 @@ __output_string :: (s: str) -> u32 {
     return wrote;
 }
 
-__exit :: (status: i32) -> void #foreign "onyx_runtime" "__exit" ---
 
 __read_from_input :: (buffer: [] u8) -> i32 {
     err, read := io.stream_read(^__stdin, buffer);
@@ -24,7 +23,14 @@ __read_from_input :: (buffer: [] u8) -> i32 {
 
 #library "onyx_runtime"
 
-#local __file_get_standard :: (fd: i32, out: ^fs.FileData.Handle) -> bool #foreign "onyx_runtime" "__file_get_standard" ---
+#foreign "onyx_runtime" {
+    __file_get_standard :: (fd: i32, out: ^fs.FileData.Handle) -> bool ---
+
+    __args_get       :: (argv: ^^u8, arg_buf: ^u8) -> void ---
+    __args_sizes_get :: (argc: ^i32, arg_buf_size: ^i32) -> void ---
+
+    __exit :: (status: i32) -> void ---
+}
 
 #export "_start" () {
     fd: fs.FileData.Handle;
@@ -44,6 +50,12 @@ __read_from_input :: (buffer: [] u8) -> i32 {
     context.thread_id = 0;
 
     args : [] cstr;
+    argv_buf_size : i32;
+    __args_sizes_get(^args.count, ^argv_buf_size);
+
+    args = memory.make_slice(cstr, args.count);
+    argv_buf := cast(cstr) calloc(argv_buf_size);
+    __args_get(args.data, argv_buf);
 
     (package main).main(args);
 
index 8c68ed7670d63c853e60ebd0b70445bda85b774f..dad767d9ec4c4d3350bbb7ca5dde651769b09011 100644 (file)
@@ -18,6 +18,9 @@ typedef struct OnyxRuntime {
     wasm_store_t*  wasm_store;
     wasm_extern_vec_t wasm_imports;
 
+    int argc;
+    char **argv;
+
     // HACK HACK HACK
     // There should need to be this much stuff in here, but because Wasmer doesn't ship a "wasmerdll.lib"
     // file for windows, it is impossible for it to link successfully against the function provided in onyx.exe.
index 1be097d656b76a332e10e7bfe6fb8808b633ce50..66be43143e3fcd8cfd7c1502b135bb64196f2b05 100644 (file)
@@ -754,6 +754,35 @@ ONYX_DEF(__process_destroy, (WASM_I64), ()) {
     return NULL;
 }
 
+ONYX_DEF(__args_get, (WASM_I32, WASM_I32), ()) {
+    if (runtime->argc == 0 || runtime->argv == NULL) {
+        return NULL;
+    }
+
+    i32 buffer_base = params->data[1].of.i32;
+
+    for (int i=0; i<runtime->argc; i++) {
+        // Should this be strncpy? What would the length be?
+        strcpy(ONYX_PTR(buffer_base), runtime->argv[i]);
+        *(i32 *) ONYX_PTR(params->data[0].of.i32 + i * 4) = buffer_base;
+        buffer_base += strlen(runtime->argv[i]) + 1;
+    }
+
+    return NULL;
+}
+
+ONYX_DEF(__args_sizes_get, (WASM_I32, WASM_I32), ()) {
+    *(i32 *) ONYX_PTR(params->data[0].of.i32) = runtime->argc;
+
+    i32 argv_size = 0;
+    for (int i=0; i<runtime->argc; i++) {
+        argv_size += strlen(runtime->argv[i]) + 1;
+    }
+
+    *(i32 *) ONYX_PTR(params->data[1].of.i32) = argv_size;
+    return NULL;
+}
+
 ONYX_DEF(__exit, (WASM_I32), ()) {
     exit(params->data[0].of.i32);
     return NULL;
@@ -785,6 +814,9 @@ ONYX_LIBRARY {
     ONYX_FUNC(__process_wait)
     ONYX_FUNC(__process_destroy)
 
+    ONYX_FUNC(__args_get)
+    ONYX_FUNC(__args_sizes_get)
+
     ONYX_FUNC(__exit)
     NULL
 };
\ No newline at end of file
index 61e703c51054441ed689e3e3dd9c808408859ff8..7416edfae7b02904503d9c67ead4543eb65fb33d 100644 (file)
@@ -274,6 +274,9 @@ b32 onyx_run_wasm(bh_buffer wasm_bytes, int argc, char *argv[]) {
     wasm_runtime.wasm_store = wasm_store;
     wasm_runtime.wasm_imports = wasm_imports;
     
+    wasm_runtime.argc = argc;
+    wasm_runtime.argv = argv;
+
     // See comment in onyx_library.h about us being the linker.
     wasm_runtime.wasm_memory_data = &wasm_memory_data;
     wasm_runtime.wasm_extern_lookup_by_name = &wasm_extern_lookup_by_name;