fixed: WASI compilation; environment variables
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 10 May 2023 17:55:03 +0000 (12:55 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 10 May 2023 17:55:03 +0000 (12:55 -0500)
compiler/src/types.c
core/os/env.onyx
core/runtime/platform/onyx/env.onyx
core/runtime/platform/wasi/env.onyx
core/runtime/platform/wasi/platform.onyx
core/runtime/platform/wasi/wasi_env.onyx [new file with mode: 0644]

index d4d85334b330bcf009cb39d06cbc9aa8e81bc4e1..6e8302347c307e8d4276a3b469602573737a96da 100644 (file)
@@ -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;
     }
 }
index da93d556b0feb5a19553646a8c8e48bc1a529fc9..92bfd488c891786f087a7c07708aaba44cebc9ad 100644 (file)
@@ -13,7 +13,7 @@ env_vars :: () -> Map(str, str) {
         |> iter.to_map();
 }
 
-env :: (key: str) -> str {
+env :: (key: str) -> str {
     return __get_env(key);
 }
 
index d2955613397e3c7f6e31a28bc706b52012894092..20424e8c0e4d19423b8e7259f1ec3af8306a1ab5 100644 (file)
@@ -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" {
index 1df44a2b3c24d5ddac3690aa3b01ecddca0b1e53..f3937476f4f96c0a4a873956ec83fc603984ceef 100644 (file)
@@ -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 {
index 85879726fbbd74dc2ca0db0c158e9802553f7010..6adeab5937ffb525ce5e34e271541ab5179c4873 100644 (file)
@@ -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 (file)
index 0000000..df1b33f
--- /dev/null
@@ -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);
+}
+
+
+