From: Brendan Hansen Date: Wed, 10 May 2023 17:55:03 +0000 (-0500) Subject: fixed: WASI compilation; environment variables X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=f34ffbb79f110479348799e9a62935e820c75ad5;p=onyx.git fixed: WASI compilation; environment variables --- diff --git a/compiler/src/types.c b/compiler/src/types.c index d4d85334..6e830234 100644 --- a/compiler/src/types.c +++ b/compiler/src/types.c @@ -1594,6 +1594,7 @@ u32 type_structlike_mem_count(Type* type) { case Type_Kind_VarArgs: return 2; case Type_Kind_Function: return 3; case Type_Kind_DynArray: return 4; + case Type_Kind_Distinct: return 1; default: return 0; } } diff --git a/core/os/env.onyx b/core/os/env.onyx index da93d556..92bfd488 100644 --- a/core/os/env.onyx +++ b/core/os/env.onyx @@ -13,7 +13,7 @@ env_vars :: () -> Map(str, str) { |> iter.to_map(); } -env :: (key: str) -> str { +env :: (key: str) -> ? str { return __get_env(key); } diff --git a/core/runtime/platform/onyx/env.onyx b/core/runtime/platform/onyx/env.onyx index d2955613..20424e8c 100644 --- a/core/runtime/platform/onyx/env.onyx +++ b/core/runtime/platform/onyx/env.onyx @@ -7,10 +7,13 @@ __get_all_env :: () -> [] Pair(str, str) { return .[]; } -__get_env :: (key: str) -> str { +__get_env :: (key: str) -> ? str { buf: [512] u8; len := __lookup_env(key, buf); - return buf[0 .. len]; + + if len > 0 do return buf[0 .. len]; + + return .{}; } #local #foreign "onyx_runtime" { diff --git a/core/runtime/platform/wasi/env.onyx b/core/runtime/platform/wasi/env.onyx index 1df44a2b..f3937476 100644 --- a/core/runtime/platform/wasi/env.onyx +++ b/core/runtime/platform/wasi/env.onyx @@ -13,10 +13,6 @@ use wasi #error "'core.env' is only available with the 'wasi' and 'onyx' runtimes."; } -__get_all_env :: () -> [] Pair(str, str) --- -__get_env :: (key: str) -> str --- - - use wasi { environ_get, environ_sizes_get, Size } Environment :: struct { diff --git a/core/runtime/platform/wasi/platform.onyx b/core/runtime/platform/wasi/platform.onyx index 85879726..6adeab59 100644 --- a/core/runtime/platform/wasi/platform.onyx +++ b/core/runtime/platform/wasi/platform.onyx @@ -2,10 +2,12 @@ package runtime.platform #load "./wasi_defs" #load "./wasi_fs" +#load "./wasi_env" use core use wasi use runtime +use main { MAIN_PKG :: package } use wasi { IOVec, SubscriptionTagged, Subscription, Event, Size, @@ -84,8 +86,8 @@ __start :: () { __runtime_initialize(); context.thread_id = 0; - #if (typeof (package main).main) == #type () -> void { - (package main).main(); + #if (typeof MAIN_PKG.main) == #type () -> void { + MAIN_PKG.main(); } else { args : [] cstr; @@ -110,7 +112,7 @@ __start :: () { args[i] = cast(cstr) (cast([&] u32) args.data)[i]; } - (package main).main(args); + MAIN_PKG.main(args); } __flush_stdio(); diff --git a/core/runtime/platform/wasi/wasi_env.onyx b/core/runtime/platform/wasi/wasi_env.onyx new file mode 100644 index 00000000..df1b33fd --- /dev/null +++ b/core/runtime/platform/wasi/wasi_env.onyx @@ -0,0 +1,57 @@ +package runtime.platform + +use wasi { environ_get, environ_sizes_get, Size } +use core {Pair, string, slice} + +#local { + all_env: [] Pair(str, str); +} + +#local +__lookup_all_envs :: () { + if all_env do return; + + allocator := context.allocator; + + env_count, env_buf_size : Size; + environ_sizes_get(&env_count, &env_buf_size); + + env_var := make([] cstr, env_count, allocator); + env_buf := make([] u8, env_buf_size, allocator); + + environ_get(env_var.data, env_buf.data); + + // Fix pointers to be only 4 bytes wide + while i := cast(i32) (env_var.count - 1); i >= 0 { + defer i -= 1; + env_var[i] = cast(cstr) (cast([&]u32) env_var.data)[i]; + } + + result := make([..] Pair(str, str), env_count, allocator); + for env: env_var { + s := string.from_cstr(env); + var, val := string.bisect(s, '='); + result << .{var, val}; + } + + delete(&env_var, allocator); + + all_env = result; +} + +__get_all_env :: () -> [] Pair(str, str) { + __lookup_all_envs(); + + return all_env; +} + +__get_env :: (key: str) -> ? str { + __lookup_all_envs(); + + return slice.first(all_env, #(it.first == key)) + |> Optional.from_ptr() + |> Optional.transform(x => x.second); +} + + +