Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions moon.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@ description = "MoonBit SDK for making Firefly Zero games"

preferred_target = "wasm"

options(
source: "src",
)
source = "src"
3 changes: 3 additions & 0 deletions src/internal/ffi/misc.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions src/misc.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
48 changes: 48 additions & 0 deletions src/misc_duration.mbt
Original file line number Diff line number Diff line change
@@ -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 }
}
Loading