diff --git a/moon.mod b/moon.mod index c0c0ee8..9bed861 100644 --- a/moon.mod +++ b/moon.mod @@ -18,6 +18,4 @@ description = "MoonBit SDK for making Firefly Zero games" preferred_target = "wasm" -options( - source: "src", -) +source = "src" diff --git a/src/internal/ffi/misc.mbt b/src/internal/ffi/misc.mbt index 2749bd4..476a9e5 100644 --- a/src/internal/ffi/misc.mbt +++ b/src/internal/ffi/misc.mbt @@ -14,6 +14,9 @@ pub fn set_seed(seed : UInt) = "misc" "set_seed" /// Get a random value. pub fn get_random() -> UInt = "misc" "get_random" +///| +pub fn get_time() -> Int64 = "misc" "get_time" + ///| /// Get name of the device. pub fn get_name(peer : Int, buf : UInt) -> Int = "misc" "get_name" diff --git a/src/misc.mbt b/src/misc.mbt index f1f17cf..5de01ef 100644 --- a/src/misc.mbt +++ b/src/misc.mbt @@ -76,6 +76,11 @@ pub fn get_random() -> UInt { @ffi.get_random() } +///| +pub fn get_time() -> Duration { + { us: @ffi.get_time() } +} + ///| /// Get name of the device. pub fn get_name(peer : Peer) -> Utf8 { diff --git a/src/misc_duration.mbt b/src/misc_duration.mbt new file mode 100644 index 0000000..8d6c407 --- /dev/null +++ b/src/misc_duration.mbt @@ -0,0 +1,48 @@ +///| +pub(all) struct Duration { + us : Int64 +} derive(Eq, Compare, Hash) + +///| +pub fn Duration::from_microseconds(us : Int64) -> Duration { + { us, } +} + +///| +pub fn Duration::from_milliseconds(ms : Int64) -> Duration { + { us: ms * 1000 } +} + +///| +pub fn Duration::from_seconds(s : Int64) -> Duration { + { us: s * 1_000_000 } +} + +///| +/// Add two durations. +pub fn Duration::add(self : Duration, rhs : Duration) -> Duration { + Duration::{ us: self.us + rhs.us } +} + +///| +/// The duration as an integer microsecond count. +pub fn Duration::to_microseconds(self : Duration) -> Int64 { + self.us +} + +///| +/// The duration as an integer millisecond count. +pub fn Duration::to_milliseconds(self : Duration) -> Int64 { + self.to_microseconds() / 1000 +} + +///| +/// The duration as an integer second count. +pub fn Duration::to_seconds(self : Duration) -> Int64 { + self.to_milliseconds() / 1000 +} + +///| +pub impl Neg for Duration with fn neg(self) -> Duration { + { us: -self.us } +}