From db60241aed46b7f8a0701b1fccde0d239faaf0b7 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Sat, 29 May 2021 16:39:39 -0500 Subject: [PATCH] added core.clock package for wasi runtime --- core/runtime/wasi.onyx | 2 +- core/std.onyx | 5 +++-- core/wasi/README.md | 1 + core/wasi/clock.onyx | 40 +++++++++++++++++++++++++++++++++++++++ core/{ => wasi}/env.onyx | 0 core/{ => wasi}/wasi.onyx | 2 +- tests/sets.onyx | 1 - 7 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 core/wasi/README.md create mode 100644 core/wasi/clock.onyx rename core/{ => wasi}/env.onyx (100%) rename core/{ => wasi}/wasi.onyx (99%) diff --git a/core/runtime/wasi.onyx b/core/runtime/wasi.onyx index a90ffabf..be8f7fad 100644 --- a/core/runtime/wasi.onyx +++ b/core/runtime/wasi.onyx @@ -1,6 +1,6 @@ package runtime -#load "core/wasi" +#load "core/wasi/wasi" #load "core/runtime/common" use package wasi diff --git a/core/std.onyx b/core/std.onyx index 15d5a7be..776d715d 100644 --- a/core/std.onyx +++ b/core/std.onyx @@ -35,9 +35,10 @@ package core #private_file runtime :: package runtime #if runtime.Runtime == runtime.Runtime_Wasi { #load "./runtime/wasi" - #load "./wasi" + #load "./wasi/wasi" + #load "./wasi/env" + #load "./wasi/clock" #load "./io/file" - #load "./env" } #if runtime.Runtime == runtime.Runtime_Js { diff --git a/core/wasi/README.md b/core/wasi/README.md new file mode 100644 index 00000000..3c3e6ee2 --- /dev/null +++ b/core/wasi/README.md @@ -0,0 +1 @@ +These files are only available on the 'wasi' runtime. In a C compilation, other files will be included, and the API between them will have to remain the same. \ No newline at end of file diff --git a/core/wasi/clock.onyx b/core/wasi/clock.onyx new file mode 100644 index 00000000..bd04ad03 --- /dev/null +++ b/core/wasi/clock.onyx @@ -0,0 +1,40 @@ +package core.clock + +#if (package runtime).Runtime != (package runtime).Runtime_Wasi { + #error "'core.clock' is only available with the 'wasi' runtime."; +} + +use package wasi + +time :: () -> u64 { + output_time: Timestamp; + clock_time_get(.Realtime, 500000, ^output_time); + return ~~(output_time / 1000000); +} + +time_ns :: () -> u64 { + output_time: Timestamp; + clock_time_get(.Realtime, 1, ^output_time); + return ~~output_time; +} + +sleep :: (milliseconds: u32) { + tagged: SubscriptionTagged; + tagged.tag = .Clock; + tagged.clock = .{ + id = .Realtime, + timeout = cast(u64) milliseconds * 1000000, + precision = 1000, + flags = .ClockAbsTime, + }; + + subscription := Subscription.{ + userdata = 0, + u = tagged, + }; + + event: Event; + number_of_events: u32; + + error_code := poll_oneoff(^subscription, ^event, 1, ^number_of_events); +} \ No newline at end of file diff --git a/core/env.onyx b/core/wasi/env.onyx similarity index 100% rename from core/env.onyx rename to core/wasi/env.onyx diff --git a/core/wasi.onyx b/core/wasi/wasi.onyx similarity index 99% rename from core/wasi.onyx rename to core/wasi/wasi.onyx index fa0e3a1a..4a31ab88 100644 --- a/core/wasi.onyx +++ b/core/wasi/wasi.onyx @@ -277,7 +277,7 @@ SubscriptionFDReadWrite :: struct { SubscriptionTagged :: struct { tag : EventType; - u : struct #union { + use u : struct #union { clock : SubscriptionClock; fd_read : SubscriptionFDReadWrite; fd_write : SubscriptionFDReadWrite; diff --git a/tests/sets.onyx b/tests/sets.onyx index a1b42452..aaf1875a 100644 --- a/tests/sets.onyx +++ b/tests/sets.onyx @@ -17,5 +17,4 @@ main :: (args: [] cstr) { set.remove(^S, 5); for entry: S.entries do println(entry.value); println(set.has(^S, 5)); - } \ No newline at end of file -- 2.25.1