From cabb84e4399326d101316f881e5e66327d1b4374 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Sun, 12 Dec 2021 09:48:22 -0600 Subject: [PATCH] added command line arguments to onyx runtime --- core/runtime/onyx_run.onyx | 16 +++++++++++++-- include/onyx_library.h | 3 +++ modules/onyx_runtime/onyx_runtime.c | 32 +++++++++++++++++++++++++++++ src/wasm_runtime.c | 3 +++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/core/runtime/onyx_run.onyx b/core/runtime/onyx_run.onyx index 1b2c6d84..13c3f429 100644 --- a/core/runtime/onyx_run.onyx +++ b/core/runtime/onyx_run.onyx @@ -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); diff --git a/include/onyx_library.h b/include/onyx_library.h index 8c68ed76..dad767d9 100644 --- a/include/onyx_library.h +++ b/include/onyx_library.h @@ -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. diff --git a/modules/onyx_runtime/onyx_runtime.c b/modules/onyx_runtime/onyx_runtime.c index 1be097d6..66be4314 100644 --- a/modules/onyx_runtime/onyx_runtime.c +++ b/modules/onyx_runtime/onyx_runtime.c @@ -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; iargc; 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; iargc; 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 diff --git a/src/wasm_runtime.c b/src/wasm_runtime.c index 61e703c5..7416edfa 100644 --- a/src/wasm_runtime.c +++ b/src/wasm_runtime.c @@ -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; -- 2.25.1