Skip to content

Commit d3b9f88

Browse files
committed
added: os.args to get CLI arguments
1 parent 3bc1b53 commit d3b9f88

File tree

8 files changed

+67
-31
lines changed

8 files changed

+67
-31
lines changed

core/module.onyx

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ use runtime
8585

8686
#if runtime.platform.Supports_Os {
8787
#load "./os/os"
88+
#load "./os/args"
8889
}
8990

9091
#if runtime.platform.Supports_Processes {

core/os/args.onyx

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package core.os
2+
3+
use runtime
4+
use core.iter
5+
6+
args :: (allocator := context.allocator) -> (result: [] str) {
7+
args := runtime.platform.__args(allocator)
8+
9+
result = make([] str, args.count, allocator)
10+
for a, i in args {
11+
result[i] = args[i] |> str.as_str()
12+
}
13+
14+
return
15+
}
16+

core/runtime/platform/onyx/platform.onyx

+13-8
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,18 @@ ProcessData :: #distinct u64
107107
__random_get :: (buf: [] u8) -> void ---
108108
}
109109

110+
__args :: (allocator: Allocator) -> [] cstr {
111+
args : [] cstr;
112+
argv_buf_size : i32;
113+
__args_sizes_get(&args.count, &argv_buf_size);
114+
115+
args = make([] cstr, args.count, allocator);
116+
argv_buf := cast([&] u8) allocator->alloc(argv_buf_size);
117+
__args_get(args.data, argv_buf);
118+
119+
return args;
120+
}
121+
110122
#if !#defined(runtime.vars.no_entrypoint) {
111123
use main
112124
#local MAIN_FUNCTION :: main.main
@@ -147,14 +159,7 @@ __start :: () {
147159
MAIN_FUNCTION();
148160

149161
} else {
150-
args : [] cstr;
151-
argv_buf_size : i32;
152-
__args_sizes_get(&args.count, &argv_buf_size);
153-
154-
args = memory.make_slice(cstr, args.count);
155-
argv_buf := cast(cstr) calloc(argv_buf_size);
156-
__args_get(args.data, argv_buf);
157-
162+
args := __args(context.allocator);
158163
MAIN_FUNCTION(args);
159164
}
160165

core/runtime/platform/wasi/platform.onyx

+27-22
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,32 @@ __poll :: (fds: [] PollDescription, timeout: i32) -> void {
210210
}
211211
}
212212

213+
__args :: (allocator: Allocator) -> [] cstr {
214+
args : [] cstr;
215+
argv_buf_size : i32;
216+
args_sizes_get(&args.count, &argv_buf_size);
217+
218+
args = make(cstr, args.count, allocator);
219+
argv_buf := cast([&] u8) allocator->alloc(argv_buf_size);
220+
args_get(args.data, argv_buf);
221+
222+
// This post processing of the argv array needs to happen if the target is using
223+
// 32-bit pointers, instead of 64-bits. Right now, Onyx pointers take up 64-bits,
224+
// but in most circumstances, only the lower 32-bits are used. When webassembly
225+
// standardizes the 64-bit address space, it will be an easy conversion over.
226+
// But for right now, WASI will give the argv array 32-bit pointers, instead of
227+
// 64-bit pointers. This loops expands the 32-bit pointers into 64-bit pointers
228+
// while not clobbering any of them.
229+
// while i := cast(i32) (args.count - 1); i >= 0 {
230+
// defer i -= 1;
231+
232+
// args[i] = cast(cstr) (cast([&] u32) args.data)[i];
233+
// }
234+
235+
return args;
236+
}
237+
238+
213239

214240
// Sets up everything needed for execution.
215241
__start :: () {
@@ -220,28 +246,7 @@ __start :: () {
220246
MAIN_PKG.main();
221247

222248
} else {
223-
args : [] cstr;
224-
argv_buf_size : Size;
225-
args_sizes_get(&args.count, &argv_buf_size);
226-
227-
args = core.memory.make_slice(cstr, args.count);
228-
argv_buf := cast(cstr) calloc(argv_buf_size);
229-
args_get(args.data, argv_buf);
230-
231-
232-
// This post processing of the argv array needs to happen if the target is using
233-
// 32-bit pointers, instead of 64-bits. Right now, Onyx pointers take up 64-bits,
234-
// but in most circumstances, only the lower 32-bits are used. When webassembly
235-
// standardizes the 64-bit address space, it will be an easy conversion over.
236-
// But for right now, WASI will give the argv array 32-bit pointers, instead of
237-
// 64-bit pointers. This loops expands the 32-bit pointers into 64-bit pointers
238-
// while not clobbering any of them.
239-
while i := cast(i32) (args.count - 1); i >= 0 {
240-
defer i -= 1;
241-
242-
args[i] = cast(cstr) (cast([&] u32) args.data)[i];
243-
}
244-
249+
args := __args(context.allocator);
245250
MAIN_PKG.main(args);
246251
}
247252

docs/ideas/platform_layer.md

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ this document will serve as that "header file"
9090
### Procedures
9191
- `__exit(code: i32) -> void`
9292
- `__sleep(milliseconds: i32) -> void`
93+
- `__args(allocator: Allocator) -> [] cstr`
9394

9495
### Values
9596

scripts/run_tests.onyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ print_color :: (color: Color, format: str, args: ..any) {
1818
buffer: [2048] u8;
1919
output := conv.str_format_va(buffer, format, args);
2020

21-
if runtime.compiler_os == .Linux && !settings.no_color {
21+
if (runtime.compiler_os == .Linux || runtime.compiler_os == .MacOS) && !settings.no_color {
2222
color_code: str;
2323
switch color {
2424
case .Red do color_code = "\x1b[91m";

tests/stdlib/os_args

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
New Args: [ ]
2+
Old Args: [ ]

tests/stdlib/os_args.onyx

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use core {*}
2+
3+
main :: (args: [] cstr) {
4+
os.args() |> printf("New Args: {}\n", _)
5+
args |> printf("Old Args: {}\n", _)
6+
}

0 commit comments

Comments
 (0)