From: Brendan Hansen Date: Mon, 1 May 2023 03:35:25 +0000 (-0500) Subject: added: `os.env` X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=0bb9350a3f8ce636fe903c5b075d5fdf6b0c5c0f;p=onyx.git added: `os.env` --- diff --git a/compiler/include/astnodes.h b/compiler/include/astnodes.h index 9a76ef8c..65dde61d 100644 --- a/compiler/include/astnodes.h +++ b/compiler/include/astnodes.h @@ -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]; diff --git a/compiler/src/entities.c b/compiler/src/entities.c index 1c3ab58e..c02ab13f 100644 --- a/compiler/src/entities.c +++ b/compiler/src/entities.c @@ -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) { diff --git a/core/container/iter.onyx b/core/container/iter.onyx index 4c8b0c84..80f04ed9 100644 --- a/core/container/iter.onyx +++ b/core/container/iter.onyx @@ -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 diff --git a/core/io/stdio.onyx b/core/io/stdio.onyx index 05239148..7e61518b 100644 --- a/core/io/stdio.onyx +++ b/core/io/stdio.onyx @@ -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 index 00000000..da93d556 --- /dev/null +++ b/core/os/env.onyx @@ -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); +} + diff --git a/core/runtime/platform/js/platform.onyx b/core/runtime/platform/js/platform.onyx index 9c16771b..9fb20ceb 100644 --- a/core/runtime/platform/js/platform.onyx +++ b/core/runtime/platform/js/platform.onyx @@ -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 index 00000000..d2955613 --- /dev/null +++ b/core/runtime/platform/onyx/env.onyx @@ -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 --- +} + diff --git a/core/runtime/platform/onyx/platform.onyx b/core/runtime/platform/onyx/platform.onyx index c54352ff..8c750b72 100644 --- a/core/runtime/platform/onyx/platform.onyx +++ b/core/runtime/platform/onyx/platform.onyx @@ -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" diff --git a/core/runtime/platform/wasi/env.onyx b/core/runtime/platform/wasi/env.onyx index 0a7a8d19..1df44a2b 100644 --- a/core/runtime/platform/wasi/env.onyx +++ b/core/runtime/platform/wasi/env.onyx @@ -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 } diff --git a/core/runtime/platform/wasi/platform.onyx b/core/runtime/platform/wasi/platform.onyx index ea96bac7..85879726 100644 --- a/core/runtime/platform/wasi/platform.onyx +++ b/core/runtime/platform/wasi/platform.onyx @@ -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 { diff --git a/core/std.onyx b/core/std.onyx index e6761f88..9764cdf4 100644 --- a/core/std.onyx +++ b/core/std.onyx @@ -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" diff --git a/runtime/onyx_runtime.c b/runtime/onyx_runtime.c index deb4d2ae..0472fa6c 100644 --- a/runtime/onyx_runtime.c +++ b/runtime/onyx_runtime.c @@ -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) diff --git a/runtime/src/ort_os.h b/runtime/src/ort_os.h index 860784a0..bfc7c3ca 100644 --- a/runtime/src/ort_os.h +++ b/runtime/src/ort_os.h @@ -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; +} +