Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
36 changes: 36 additions & 0 deletions src/internal/event_loop/event_loop.wasm-gc.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2025 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///|
let _ignore_unused_import : Unit = {
ignore(@c_buffer.unimplemented)
ignore(@fd_util.unimplemented)
ignore(@os_string.unimplemented)
ignore(@os_error.unimplemented)
ignore(@time.ms_since_epoch)
}

///|
pub fn reschedule() -> Unit {
guard !@coroutine.no_more_work() else { }
@coroutine.reschedule()
// Instead of looping blockingly until there is no ready task,
// we only perform one round of scheduling here
// (i.e., only those tasks already ready when `reschedule` is called are run).
// Remaining tasks are delayed until the next event loop iteration,
// so that those blocking jobs got a chance to execute instead of starving.
if @coroutine.has_immediately_ready_task() {
ignore(wasm_set_timeout(0, reschedule))
}
}
Comment thread
duobei marked this conversation as resolved.
Outdated
5 changes: 4 additions & 1 deletion src/internal/event_loop/moon.pkg
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ options(
"cancel_before_submit_test.mbt": [ "native" ],
"event_loop.js.mbt": [ "js" ],
"event_loop.mbt": [ "native" ],
"event_loop.wasm-gc.mbt": [ "wasm-gc" ],
"fs.mbt": [ "native" ],
"io.mbt": [ "native" ],
"io_unix.mbt": [ "native" ],
Expand All @@ -49,8 +50,10 @@ options(
"process_windows.mbt": [ "native" ],
"stdio.mbt": [ "native" ],
"thread_pool.mbt": [ "native" ],
"timer.js.mbt": [ "js" ],
"timer.mbt": [ "native" ],
"unimplemented.mbt": [ "wasm", "wasm-gc" ],
"timer.wasm-gc.mbt": [ "wasm-gc" ],
"unimplemented.mbt": [ "wasm" ],
"worker_wbtest.mbt": [ "native", "js" ],
},
)
36 changes: 36 additions & 0 deletions src/internal/event_loop/timer.wasm-gc.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2025 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///|
#external
type Timer

///|
fn wasm_set_timeout(duration : Int, f : () -> Unit) -> Timer = "moonbitlang_async_timer" "set_timeout"

///|
fn wasm_clear_timeout(timer : Timer) = "moonbitlang_async_timer" "clear_timeout"

///|
pub fn Timer::new(duration : Int, f : () -> Unit) -> Timer {
wasm_set_timeout(duration, () => {
f()
reschedule()
})
}

///|
pub fn Timer::cancel(self : Timer) -> Unit {
wasm_clear_timeout(self)
}
Comment thread
duobei marked this conversation as resolved.
Outdated
15 changes: 15 additions & 0 deletions src/internal/event_loop/wasm-gc-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Import objects for moonbitlang/async wasm-gc timer, event loop, and time modules.
// When loading the compiled wasm-gc module, merge these into the import object:
//
// import { moonbitlang_async_timer, moonbitlang_async_time } from "./wasm-gc-imports.js";
// const imports = { ...otherImports, moonbitlang_async_timer, moonbitlang_async_time };
// const { instance } = await WebAssembly.instantiateStreaming(fetch("module.wasm"), imports);

export const moonbitlang_async_timer = {
set_timeout: (duration, f) => setTimeout(f, duration),
clear_timeout: (timer) => clearTimeout(timer),
Comment thread
duobei marked this conversation as resolved.
Outdated
};

export const moonbitlang_async_time = {
date_now: () => Date.now(),
};
29 changes: 28 additions & 1 deletion src/internal/time/time.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,34 @@ pub fn ms_since_epoch() -> Int64 {
}

///|
#cfg(any(target="wasm", target="wasm-gc"))
#cfg(target="wasm-gc")
fn wasm_gc_date_now() -> Double = "moonbitlang_async_time" "date_now"

///|
/// Get the current wall-clock time in milliseconds.
///
/// This is the time source used by the async runtime. It is intended for
/// computing elapsed time by subtraction. The value can jump forwards or
/// backwards if the system clock is adjusted, so do not assume monotonicity.
///
/// Platform notes:
/// - Unix/macOS: `gettimeofday()` (Unix epoch)
/// - Windows: `GetSystemTimeAsFileTime()` (FILETIME epoch, 1601)
/// - JavaScript/wasm-gc: `Date.now()` (Unix epoch)
///
/// # Example
/// ```mbt check
/// test {
/// let _ : Int64 = ms_since_epoch()
/// }
/// ```
#cfg(target="wasm-gc")
pub fn ms_since_epoch() -> Int64 {
wasm_gc_date_now().to_int64()
}

///|
#cfg(target="wasm")
pub fn ms_since_epoch() -> Int64 {
abort("unimplemented")
}
Loading