added: `os.env`
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 1 May 2023 03:35:25 +0000 (22:35 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 1 May 2023 03:35:25 +0000 (22:35 -0500)
13 files changed:
compiler/include/astnodes.h
compiler/src/entities.c
core/container/iter.onyx
core/io/stdio.onyx
core/os/env.onyx [new file with mode: 0644]
core/runtime/platform/js/platform.onyx
core/runtime/platform/onyx/env.onyx [new file with mode: 0644]
core/runtime/platform/onyx/platform.onyx
core/runtime/platform/wasi/env.onyx
core/runtime/platform/wasi/platform.onyx
core/std.onyx
runtime/onyx_runtime.c
runtime/src/ort_os.h

index 9a76ef8c3121b4cf5d9ae6221935a9ecb58b040e..65dde61d759fa24c923f4e7d8f64be4f6bb5c12f 100644 (file)
@@ -1614,6 +1614,7 @@ typedef struct Entity {
 typedef struct EntityHeap {
     bh_arena entity_arena;
     bh_arr(Entity *) entities;
+    bh_arr(Entity *) quick_unsorted_entities;
     i32 next_id;
 
     i32 state_count[Entity_State_Count];
index 1c3ab58ee8bd6c1f85edd813bda862dcfd040ccc..c02ab13f2b0bbc549d2877aee3ab02fd154c69e3 100644 (file)
@@ -92,10 +92,16 @@ void entity_heap_insert_existing(EntityHeap* entities, Entity* e) {
 
     if (entities->entities == NULL) {
         bh_arr_new(global_heap_allocator, entities->entities, 128);
+        bh_arr_new(global_heap_allocator, entities->quick_unsorted_entities, 128);
+    }
+
+    if (e->state <= Entity_State_Introduce_Symbols) {
+        bh_arr_push(entities->quick_unsorted_entities, e);
+    } else {
+        bh_arr_push(entities->entities, e);
+        eh_shift_up(entities, bh_arr_length(entities->entities) - 1);
     }
 
-    bh_arr_push(entities->entities, e);
-    eh_shift_up(entities, bh_arr_length(entities->entities) - 1);
     e->entered_in_queue = 1;
 
     entities->state_count[e->state]++;
@@ -110,6 +116,10 @@ Entity* entity_heap_insert(EntityHeap* entities, Entity e) {
 }
 
 Entity* entity_heap_top(EntityHeap* entities) {
+    if (bh_arr_length(entities->quick_unsorted_entities) > 0) {
+        return entities->quick_unsorted_entities[0];
+    }
+
     return entities->entities[0];
 }
 
@@ -128,14 +138,26 @@ void entity_heap_change_top(EntityHeap* entities, Entity* new_top) {
 }
 
 void entity_heap_remove_top(EntityHeap* entities) {
-    entities->state_count[entities->entities[0]->state]--;
-    entities->type_count[entities->entities[0]->type]--;
-    entities->all_count[entities->entities[0]->state][entities->entities[0]->type]--;
-    entities->entities[0]->entered_in_queue = 0;
+    Entity *e;
 
-    entities->entities[0] = entities->entities[bh_arr_length(entities->entities) - 1];
-    bh_arr_pop(entities->entities);
-    eh_shift_down(entities, 0);
+    if (bh_arr_length(entities->quick_unsorted_entities) > 0) {
+        e = entities->quick_unsorted_entities[0];
+    } else {
+        e = entities->entities[0];
+    }
+
+    entities->state_count[e->state]--;
+    entities->type_count[e->type]--;
+    entities->all_count[e->state][e->type]--;
+    e->entered_in_queue = 0;
+
+    if (bh_arr_length(entities->quick_unsorted_entities) > 0) {
+        bh_arr_fastdelete(entities->quick_unsorted_entities, 0);
+    } else {
+        entities->entities[0] = entities->entities[bh_arr_length(entities->entities) - 1];
+        bh_arr_pop(entities->entities);
+        eh_shift_down(entities, 0);
+    }
 }
 
 void entity_change_type(EntityHeap* entities, Entity *ent, EntityType new_type) {
index 4c8b0c8461ebcf3ea63efb93671bc0f820c319c0..80f04ed98865784789ddb4b8708de4fbf63f079f 100644 (file)
@@ -32,6 +32,7 @@ use core.intrinsics.types {type_is_struct}
     some :: some;
     every :: every;
     collect :: to_array;
+    collect_map :: to_map;
 }
 
 
@@ -735,6 +736,16 @@ to_array :: (it: Iterator($T), allocator := context.allocator) -> [..] T {
     return arr;
 }
 
+#doc """
+"""
+to_map :: (it: Iterator(Pair($K, $V)), allocator := context.allocator) -> Map(K, V) {
+    m := builtin.make(Map(K, V), allocator=allocator);
+    for p: it {
+        m->put(p.first, p.second);
+    }
+    return m;
+}
+
 
 #doc """
     Produces an iterator that first yields all values from the
index 052391486fe7489aa70b795f79733bf1618c4378..7e61518bdf6c66c0deec355515c9b0492763016d 100644 (file)
@@ -85,14 +85,14 @@ println :: (x) => {
 //
 // Standard formatted print to standard output.
 printf :: (format: str, va: ..any) {
-    flush :: (_, to_output) => {
-        io.write(&stdio.print_writer, to_output);
-        __flush_stdio();
-        return true;
-    }
-
     buffer: [1024] u8;
-    print(conv.format_va(buffer, format, va, .{null, flush}));
+    print(conv.format_va(buffer, format, va, .{ null,
+        (_: rawptr, to_output: str) -> bool {
+            io.write(&stdio.print_writer, to_output);
+            __flush_stdio();
+            return true;
+        }
+    }));
 }
 
 #if #defined(runtime.platform.__output_error) {
diff --git a/core/os/env.onyx b/core/os/env.onyx
new file mode 100644 (file)
index 0000000..da93d55
--- /dev/null
@@ -0,0 +1,19 @@
+package core.os
+
+use core.iter
+use runtime.platform {
+    __get_all_env,
+    __get_env
+}
+
+env_vars :: () -> Map(str, str) {
+    return __get_all_env()
+        |> iter.as_iter()
+        |> iter.map(x => *x)
+        |> iter.to_map();
+}
+
+env :: (key: str) -> str {
+    return __get_env(key);
+}
+
index 9c16771b4703d9134249e6f70c2d929c1a196436..9fb20ceb6646a1b4aa71c1937bb1d57ef9203b2b 100644 (file)
@@ -18,6 +18,7 @@ Supports_Time :: false
 Supports_Networking :: false
 Supports_Type_Info :: true
 Supports_Threads :: true
+Supports_Env_Vars :: false
 
 __output_string   :: (s: str)      -> u32  #foreign "host" "print_str" ---
 __output_error    :: (s: str)      -> u32  #foreign "host" "print_str" ---
diff --git a/core/runtime/platform/onyx/env.onyx b/core/runtime/platform/onyx/env.onyx
new file mode 100644 (file)
index 0000000..d295561
--- /dev/null
@@ -0,0 +1,19 @@
+package runtime.platform
+
+use core {Pair}
+
+__get_all_env :: () -> [] Pair(str, str) {
+    // TODO
+    return .[];
+}
+
+__get_env :: (key: str) -> str {
+    buf: [512] u8;
+    len := __lookup_env(key, buf);
+    return buf[0 .. len];
+}
+
+#local #foreign "onyx_runtime" {
+    __lookup_env :: (key: str, buf: str) -> i32 ---
+}
+
index c54352ff134fe6cf1a8b1906f4693dd3bc60d383..8c750b729171d0b46db01e358dc2cdb13b09d46d 100644 (file)
@@ -11,6 +11,7 @@ use runtime {
 }
 
 #load "./fs"
+#load "./env"
 
 
 // Platform supports
@@ -22,6 +23,7 @@ Supports_Time :: true
 Supports_Networking :: true
 Supports_Type_Info :: true
 Supports_Threads :: true
+Supports_Env_Vars :: true
 
 
 #library "onyx_runtime"
index 0a7a8d19c1168cef7d40db9dd88634de2591e9c2..1df44a2b3c24d5ddac3690aa3b01ecddca0b1e53 100644 (file)
@@ -1,6 +1,7 @@
 package core.env
 
 use runtime
+use core { Pair }
 use core.map
 use core.memory
 use core.string
@@ -12,6 +13,9 @@ 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 }
 
index ea96bac7fd4e7cbc30bbf7badef5b72eee4c6e16..85879726fbbd74dc2ca0db0c158e9802553f7010 100644 (file)
@@ -31,7 +31,7 @@ Supports_Time :: false
 Supports_Networking :: false
 Supports_Type_Info :: true
 Supports_Threads :: false
-
+Supports_Env_Vars :: true
 
 
 __output_string :: (s: str) -> u32 {
index e6761f88702fc48715c30b96f815cb6e718c2606..9764cdf4187077202ef2edf1b7bb8c5271bb8be1 100644 (file)
@@ -101,6 +101,10 @@ use runtime
     #load "./threads/thread"
 }
 
+#if runtime.platform.Supports_Env_Vars {
+    #load "./os/env"
+}
+
 #if runtime.Multi_Threading_Enabled {
     #load "./intrinsics/atomics"
 
index deb4d2ae5cf0fcff5071581244813706bafeef06..0472fa6cd643d943c8acc0ecf15642b5baab8841 100644 (file)
@@ -70,6 +70,7 @@ ONYX_LIBRARY {
     ONYX_FUNC(__exit)
     ONYX_FUNC(__sleep)
     ONYX_FUNC(__time)
+    ONYX_FUNC(__lookup_env)
     ONYX_FUNC(__register_cleanup)
 
     ONYX_FUNC(__time_localtime)
index 860784a070cc64dfe92e7a5e0115202795971406..bfc7c3ca4158d24c9aee72d7d774327de33f6c05 100644 (file)
@@ -54,6 +54,35 @@ ONYX_DEF(__time, (), (WASM_I64)) {
 }
 
 
+ONYX_DEF(__lookup_env, (WASM_I32, WASM_I32, WASM_I32, WASM_I32), (WASM_I32)) {
+
+    #ifdef _BH_LINUX
+    char *key_ptr = ONYX_PTR(params->data[0].of.i32);
+    int   key_len = params->data[1].of.i32;
+    char *out_ptr = ONYX_PTR(params->data[2].of.i32);
+    int   out_len = params->data[3].of.i32;
+
+    char key[512] = {0};
+    key_len = bh_min(key_len, 511);
+    strncpy(key, key_ptr, key_len);
+    key[key_len] = 0;
+
+    char * value = getenv(key);
+    if (!value) {
+        results->data[0] = WASM_I32_VAL(0);
+    } else {
+        out_len = bh_min(out_len, strlen(value));
+        memcpy(out_ptr, value, out_len);
+        results->data[0] = WASM_I32_VAL(out_len);
+    }
+    #endif
+
+    #ifdef _BH_WINDOWS
+    results->data[0] = WASM_I32_VAL(0);
+    #endif
+    return NULL;
+}
+