use core::ffi::c_void;
use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Mutex, OnceLock, RwLock};
use std::time::Instant;
use crate::capi;
use crate::glue;
struct Clock {
origin: Instant,
epoch_origin_ms: f64,
}
fn clock() -> &'static Clock {
static CLOCK: OnceLock<Clock> = OnceLock::new();
CLOCK.get_or_init(|| Clock {
origin: Instant::now(),
epoch_origin_ms: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs_f64() * 1000.0)
.unwrap_or(0.0),
})
}
pub fn now_ms() -> f64 {
clock().origin.elapsed().as_secs_f64() * 1000.0
}
struct Timer {
cb: u64,
args: Vec<u64>,
next_fire: f64,
interval: f64,
repeating: bool,
}
type NativeTask = Box<dyn FnOnce(*mut c_void) + Send>;
#[derive(Default)]
struct Scheduler {
raf: Vec<(u32, u64)>, raf_next_id: u32,
timers: HashMap<u32, Timer>,
timer_next_id: u32,
idle: Vec<(u32, u64)>,
idle_next_id: u32,
native: Vec<(f64, NativeTask)>,
}
pub(crate) fn schedule_native(g: *mut c_void, delay_ms: f64, task: NativeTask) {
let deadline = now_ms() + delay_ms.max(0.0);
let mut reg = registry().lock().unwrap();
reg.entry(g as usize).or_default().native.push((deadline, task));
}
pub(crate) fn schedule_native_from(g: *mut c_void, id: Option<crate::context::ContextId>, delay_ms: f64, task: NativeTask) {
let deadline = now_ms() + delay_ms.max(0.0);
let mut reg = registry().lock().unwrap();
if id.is_some() && crate::context::id_of(g) != id {
drop(reg);
drop(task);
return;
}
reg.entry(g as usize).or_default().native.push((deadline, task));
drop(reg);
notify_embedder();
}
static WAKE_HOOK: OnceLock<fn()> = OnceLock::new();
pub fn set_wake_hook(hook: fn()) {
let _ = WAKE_HOOK.set(hook);
}
pub(crate) fn notify_embedder() {
if let Some(hook) = WAKE_HOOK.get() {
hook();
}
}
pub fn next_work_ms(g: *mut c_void) -> Option<f64> {
let gc = unsafe { glue::rjs_gc_timers_due_s(g) };
let gc = (gc >= 0.0).then(|| now_ms() + gc * 1000.0);
#[cfg(feature = "audio")]
let polled = crate::webapi::mediadevices::next_poll_ms(g);
#[cfg(not(feature = "audio"))]
let polled = None;
next_scheduled_ms(g).into_iter().chain(gc).chain(polled).reduce(f64::min)
}
fn next_scheduled_ms(g: *mut c_void) -> Option<f64> {
let reg = registry().lock().unwrap();
let s = reg.get(&(g as usize))?;
let timers = s.timers.values().map(|t| t.next_fire);
let native = s.native.iter().map(|(at, _)| *at);
let idle = (!s.idle.is_empty()).then(now_ms);
timers.chain(native).chain(idle).reduce(f64::min)
}
fn registry() -> &'static Mutex<HashMap<usize, Scheduler>> {
static REG: OnceLock<Mutex<HashMap<usize, Scheduler>>> = OnceLock::new();
REG.get_or_init(|| Mutex::new(HashMap::new()))
}
pub(crate) fn forget(gone: crate::context::Gone) {
let dropped = registry().lock().unwrap().remove(&gone.key);
drop(dropped); let w = wakers().lock().unwrap().remove(&gone.id.0);
drop(w);
}
pub(crate) fn has_scheduler(g: *mut c_void) -> bool {
registry().lock().unwrap().contains_key(&(g as usize))
}
#[inline]
unsafe fn cf_argc(cf: *mut c_void) -> u32 {
(*((cf as usize + 32) as *const u32)).saturating_sub(1)
}
#[inline]
unsafe fn cf_arg(cf: *mut c_void, i: u32) -> u64 {
if i < cf_argc(cf) {
*((cf as usize + 48 + (i as usize) * 8) as *const u64)
} else {
ENC_UNDEFINED
}
}
const ENC_UNDEFINED: u64 = 0xa;
const TAG_NUMBER: u64 = 0xfffe_0000_0000_0000;
#[inline]
fn enc_int32(i: i32) -> u64 {
TAG_NUMBER | (i as u32 as u64)
}
#[inline]
fn enc_double(d: f64) -> u64 {
let i = d as i32;
if i as f64 == d && !(i == 0 && d.is_sign_negative()) {
return enc_int32(i);
}
(d.to_bits() as i64).wrapping_add((1i64) << 49) as u64
}
#[inline]
fn dec_number(v: u64) -> Option<f64> {
if (v & TAG_NUMBER) == TAG_NUMBER {
return Some(v as u32 as i32 as f64);
}
if (v & TAG_NUMBER) != 0 {
return Some(f64::from_bits((v as i64).wrapping_sub((1i64) << 49) as u64));
}
None
}
#[inline]
fn is_cell(v: u64) -> bool {
v != 0 && (v & (TAG_NUMBER | 0x2)) == 0
}
#[crate::host_fn]
unsafe fn perf_now(_g: *mut c_void, _cf: *mut c_void) -> u64 {
enc_double(now_ms())
}
#[crate::host_fn]
unsafe fn raf_request(g: *mut c_void, cf: *mut c_void) -> u64 {
let cb = cf_arg(cf, 0);
if !is_cell(cb) {
return enc_int32(0);
}
capi::JSValueProtect(g, cb as usize as capi::JSValueRef);
let mut reg = registry().lock().unwrap();
let s = reg.entry(g as usize).or_default();
s.raf_next_id += 1;
let id = s.raf_next_id;
let first = s.raf.is_empty();
s.raf.push((id, cb));
drop(reg);
if first {
notify_embedder();
}
enc_int32(id as i32)
}
#[crate::host_fn]
unsafe fn raf_cancel(g: *mut c_void, cf: *mut c_void) -> u64 {
if let Some(id) = dec_number(cf_arg(cf, 0)) {
let removed = {
let mut reg = registry().lock().unwrap();
let s = reg.entry(g as usize).or_default();
match s.raf.iter().position(|(i, _)| *i == id as u32) {
Some(pos) => Some(s.raf.remove(pos).1),
None => None,
}
};
if let Some(cb) = removed {
capi::JSValueUnprotect(g, cb as usize as capi::JSValueRef);
}
}
ENC_UNDEFINED
}
unsafe fn timer_set(g: *mut c_void, cf: *mut c_void, repeating: bool) -> u64 {
timer_set_inner(g, cf, repeating, None, 2)
}
unsafe fn timer_set_inner(g: *mut c_void, cf: *mut c_void, repeating: bool, delay_override: Option<f64>, args_from: u32) -> u64 {
let cb = cf_arg(cf, 0);
if !is_cell(cb) {
return enc_int32(0);
}
let delay = delay_override.unwrap_or_else(|| dec_number(cf_arg(cf, 1)).unwrap_or(0.0).max(0.0));
let interval = if repeating { delay.max(1.0) } else { delay };
let args: Vec<u64> = (args_from..cf_argc(cf)).map(|i| cf_arg(cf, i)).collect();
capi::JSValueProtect(g, cb as usize as capi::JSValueRef);
for &a in &args {
if is_cell(a) {
capi::JSValueProtect(g, a as usize as capi::JSValueRef);
}
}
let now = now_ms();
let mut reg = registry().lock().unwrap();
let s = reg.entry(g as usize).or_default();
s.timer_next_id += 1;
let id = s.timer_next_id;
s.timers.insert(id, Timer { cb, args, next_fire: now + delay, interval, repeating });
enc_int32(id as i32)
}
#[crate::host_fn]
unsafe fn timer_set_timeout(g: *mut c_void, cf: *mut c_void) -> u64 {
timer_set(g, cf, false)
}
#[crate::host_fn]
unsafe fn timer_set_interval(g: *mut c_void, cf: *mut c_void) -> u64 {
timer_set(g, cf, true)
}
#[crate::host_fn]
unsafe fn immediate_set(g: *mut c_void, cf: *mut c_void) -> u64 {
timer_set_inner(g, cf, false, Some(0.0), 1)
}
#[crate::host_fn]
unsafe fn queue_microtask(g: *mut c_void, cf: *mut c_void) -> u64 {
let cb = cf_arg(cf, 0);
if glue::rjs_queue_microtask(g, cb) != 0 {
let m: Vec<u16> = "queueMicrotask: argument must be callable".encode_utf16().collect();
let mut err = ENC_UNDEFINED;
glue::rjs_error_create(g, 1, m.as_ptr(), m.len(), &mut err);
glue::rjs_throw(g, err);
}
ENC_UNDEFINED
}
#[crate::host_fn]
unsafe fn idle_request(g: *mut c_void, cf: *mut c_void) -> u64 {
let cb = cf_arg(cf, 0);
if !is_cell(cb) {
return enc_int32(0);
}
capi::JSValueProtect(g, cb as usize as capi::JSValueRef);
let mut reg = registry().lock().unwrap();
let s = reg.entry(g as usize).or_default();
s.idle_next_id += 1;
let id = s.idle_next_id;
s.idle.push((id, cb));
enc_int32(id as i32)
}
#[crate::host_fn]
unsafe fn idle_cancel(g: *mut c_void, cf: *mut c_void) -> u64 {
if let Some(id) = dec_number(cf_arg(cf, 0)) {
let removed = {
let mut reg = registry().lock().unwrap();
let s = reg.entry(g as usize).or_default();
match s.idle.iter().position(|(i, _)| *i == id as u32) {
Some(pos) => Some(s.idle.remove(pos).1),
None => None,
}
};
if let Some(cb) = removed {
capi::JSValueUnprotect(g, cb as usize as capi::JSValueRef);
}
}
ENC_UNDEFINED
}
#[crate::host_fn]
unsafe fn idle_time_remaining(g: *mut c_void, cf: *mut c_void) -> u64 {
let this = *((cf as usize + 40) as *const u64);
let a = glue::rjs_atom8(g, "__t0".as_ptr(), 4);
let mut t0 = ENC_UNDEFINED;
glue::rjs_get(g, this, a, &mut t0);
glue::rjs_atom_free(g, a);
let t0 = dec_number(t0).unwrap_or(0.0);
enc_double((4.0 - (now_ms() - t0)).max(0.0))
}
#[crate::host_fn]
unsafe fn timer_clear(g: *mut c_void, cf: *mut c_void) -> u64 {
if let Some(id) = dec_number(cf_arg(cf, 0)) {
let removed = {
let mut reg = registry().lock().unwrap();
let s = reg.entry(g as usize).or_default();
s.timers.remove(&(id as u32))
};
if let Some(t) = removed {
unprotect_timer(g, &t);
}
}
ENC_UNDEFINED
}
unsafe fn unprotect_timer(g: *mut c_void, t: &Timer) {
capi::JSValueUnprotect(g, t.cb as usize as capi::JSValueRef);
for &a in &t.args {
if is_cell(a) {
capi::JSValueUnprotect(g, a as usize as capi::JSValueRef);
}
}
}
unsafe fn define_native(g: *mut c_void, target: u64, name: &str, arity: u32, f: *const c_void) -> i32 {
let name16: Vec<u16> = name.encode_utf16().collect();
let mut func = 0u64;
let rc = glue::rjs_function_create(g, name16.as_ptr(), name16.len(), arity, f, &mut func);
if rc != 0 {
return rc;
}
let atom = glue::rjs_atom8(g, name.as_ptr(), name.len());
let rc = glue::rjs_define(g, target, atom, func, 0);
glue::rjs_atom_free(g, atom);
rc
}
pub unsafe fn install(g: *mut c_void) -> i32 {
crate::context::ensure(g);
let _ = clock(); registry().lock().unwrap().entry(g as usize).or_default();
let global = glue::rjs_global(g);
let mut perf = 0u64;
let rc = glue::rjs_object_create(g, ENC_UNDEFINED, &mut perf);
if rc != 0 {
return rc;
}
let rc = define_native(g, perf, "now", 0, perf_now as *const c_void);
if rc != 0 {
return rc;
}
let atom = glue::rjs_atom8(g, "timeOrigin".as_ptr(), "timeOrigin".len());
glue::rjs_define(g, perf, atom, enc_double(clock().epoch_origin_ms), 0);
glue::rjs_atom_free(g, atom);
let atom = glue::rjs_atom8(g, "performance".as_ptr(), "performance".len());
let rc = glue::rjs_define(g, global, atom, perf, 0);
glue::rjs_atom_free(g, atom);
if rc != 0 {
return rc;
}
let fns: [(&str, u32, *const c_void); 11] = [
("requestAnimationFrame", 1, raf_request as *const c_void),
("cancelAnimationFrame", 1, raf_cancel as *const c_void),
("setTimeout", 2, timer_set_timeout as *const c_void),
("setInterval", 2, timer_set_interval as *const c_void),
("clearTimeout", 1, timer_clear as *const c_void),
("clearInterval", 1, timer_clear as *const c_void),
("setImmediate", 1, immediate_set as *const c_void),
("clearImmediate", 1, timer_clear as *const c_void),
("queueMicrotask", 1, queue_microtask as *const c_void),
("requestIdleCallback", 1, idle_request as *const c_void),
("cancelIdleCallback", 1, idle_cancel as *const c_void),
];
for (name, arity, f) in fns {
let rc = define_native(g, global, name, arity, f);
if rc != 0 {
return rc;
}
}
0
}
pub const PUMP_TIMERS: u32 = 1;
pub const PUMP_RAF: u32 = 2;
pub const PUMP_MICROTASKS: u32 = 4;
pub const PUMP_CHECKPOINT_PER_TASK: u32 = 8;
pub use crate::ffi::RjsPumpStats;
unsafe fn fire(g: *mut c_void, cb: u64, args: &[u64], flags: u32, label: &'static str) {
let mut out = 0u64;
let per_task = flags & PUMP_CHECKPOINT_PER_TASK != 0;
let prev = COALESCED_CALLBACK.with(|c| c.replace(!per_task));
let rc = {
let _profile = crate::profiling::js_span(g, cb, label);
glue::rjs_call(g, cb, ENC_UNDEFINED, args.as_ptr(), args.len() as u32, &mut out)
};
COALESCED_CALLBACK.with(|c| c.set(prev));
if rc == 1 {
crate::errors::report(g, out, crate::errors::ErrKind::PumpCallback);
}
if per_task {
checkpoint(g);
}
}
thread_local! {
static COALESCED_CALLBACK: core::cell::Cell<bool> = const { core::cell::Cell::new(false) };
}
pub fn in_coalesced_callback() -> bool {
COALESCED_CALLBACK.with(|c| c.get())
}
pub struct Waker {
sys: wake_sys::Event,
}
impl Waker {
fn new() -> Waker {
Waker { sys: wake_sys::Event::new() }
}
pub fn wake(&self) {
self.sys.set();
}
pub fn wait(&self, timeout: std::time::Duration) -> bool {
self.sys.wait(timeout)
}
}
fn wakers() -> &'static Mutex<HashMap<u64, std::sync::Arc<Waker>>> {
static W: OnceLock<Mutex<HashMap<u64, std::sync::Arc<Waker>>>> = OnceLock::new();
W.get_or_init(|| Mutex::new(HashMap::new()))
}
pub fn waker(id: crate::context::ContextId) -> std::sync::Arc<Waker> {
wakers().lock().unwrap().entry(id.0).or_insert_with(|| std::sync::Arc::new(Waker::new())).clone()
}
pub fn wake(id: crate::context::ContextId) {
let w = wakers().lock().unwrap().get(&id.0).cloned();
if let Some(w) = w {
w.wake();
}
notify_embedder();
}
#[derive(Clone, Copy)]
pub struct PumpSource {
pub inflight: unsafe fn(*mut c_void) -> bool,
pub ready: unsafe fn(*mut c_void) -> bool,
pub drain: unsafe fn(*mut c_void) -> usize,
}
static ANY_SOURCE: AtomicBool = AtomicBool::new(false);
fn sources() -> &'static RwLock<Vec<PumpSource>> {
static S: OnceLock<RwLock<Vec<PumpSource>>> = OnceLock::new();
S.get_or_init(|| RwLock::new(Vec::new()))
}
pub fn register_pump_source(src: PumpSource) {
let mut s = sources().write().unwrap();
if !s.iter().any(|x| x.drain as usize == src.drain as usize) {
s.push(src);
}
ANY_SOURCE.store(true, Ordering::Release);
}
pub const DEFAULT_PUMP_BUDGET_US: u32 = 4000;
static PUMP_BUDGET_US: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(DEFAULT_PUMP_BUDGET_US);
pub fn set_pump_budget_us(us: u32) {
PUMP_BUDGET_US.store(us, Ordering::Relaxed);
}
pub fn pump_budget_us() -> u32 {
PUMP_BUDGET_US.load(Ordering::Relaxed)
}
const SPIN: std::time::Duration = std::time::Duration::from_micros(20);
unsafe fn drain_sources(g: *mut c_void) {
if !ANY_SOURCE.load(Ordering::Acquire) {
return;
}
let srcs: Vec<PumpSource> = sources().read().unwrap().clone();
let busy = |srcs: &[PumpSource]| srcs.iter().any(|s| (s.inflight)(g) || (s.ready)(g));
if !busy(&srcs) {
return;
}
let start = Instant::now();
let deadline = start + std::time::Duration::from_micros(pump_budget_us() as u64);
let waker = crate::context::id_of(g).map(waker);
loop {
let mut ran = 0usize;
for s in &srcs {
ran += (s.drain)(g);
}
let now = Instant::now();
if now >= deadline || !srcs.iter().any(|s| (s.inflight)(g)) {
break;
}
if ran > 0 || srcs.iter().any(|s| (s.ready)(g)) {
continue;
}
let spin_until = (now + SPIN).min(deadline);
let mut got = false;
while Instant::now() < spin_until {
if srcs.iter().any(|s| (s.ready)(g)) {
got = true;
break;
}
core::hint::spin_loop();
}
if got {
continue;
}
let Some(w) = &waker else { break };
let now = Instant::now();
if now >= deadline {
break;
}
w.wait(deadline - now);
}
}
#[cfg(windows)]
mod wake_sys {
use core::ffi::c_void;
use std::time::Duration;
type Handle = *mut c_void;
const CREATE_WAITABLE_TIMER_HIGH_RESOLUTION: u32 = 0x2;
const TIMER_ALL_ACCESS: u32 = 0x001F_0003;
const INFINITE: u32 = 0xFFFF_FFFF;
const WAIT_OBJECT_0: u32 = 0;
#[link(name = "kernel32")]
extern "system" {
fn CreateEventW(attrs: *mut c_void, manual_reset: i32, initial: i32, name: *const u16) -> Handle;
fn SetEvent(h: Handle) -> i32;
fn CreateWaitableTimerExW(attrs: *mut c_void, name: *const u16, flags: u32, access: u32) -> Handle;
fn SetWaitableTimer(h: Handle, due: *const i64, period: i32, cb: *mut c_void, arg: *mut c_void, resume: i32) -> i32;
fn CancelWaitableTimer(h: Handle) -> i32;
fn WaitForMultipleObjects(n: u32, handles: *const Handle, wait_all: i32, ms: u32) -> u32;
fn WaitForSingleObject(h: Handle, ms: u32) -> u32;
fn CloseHandle(h: Handle) -> i32;
}
pub struct Event {
ev: Handle,
timer: Handle,
}
unsafe impl Send for Event {}
unsafe impl Sync for Event {}
impl Event {
pub fn new() -> Event {
unsafe {
let ev = CreateEventW(core::ptr::null_mut(), 0, 0, core::ptr::null());
let mut timer = CreateWaitableTimerExW(
core::ptr::null_mut(),
core::ptr::null(),
CREATE_WAITABLE_TIMER_HIGH_RESOLUTION,
TIMER_ALL_ACCESS,
);
if timer.is_null() {
timer = CreateWaitableTimerExW(core::ptr::null_mut(), core::ptr::null(), 0, TIMER_ALL_ACCESS);
}
Event { ev, timer }
}
}
pub fn set(&self) {
unsafe {
SetEvent(self.ev);
}
}
pub fn wait(&self, timeout: Duration) -> bool {
unsafe {
if self.timer.is_null() {
let ms = timeout.as_millis().clamp(1, (INFINITE - 1) as u128) as u32;
return WaitForSingleObject(self.ev, ms) == WAIT_OBJECT_0;
}
let due: i64 = -((timeout.as_nanos() / 100).max(1) as i64);
SetWaitableTimer(self.timer, &due, 0, core::ptr::null_mut(), core::ptr::null_mut(), 0);
let hs = [self.ev, self.timer];
let r = WaitForMultipleObjects(2, hs.as_ptr(), 0, INFINITE);
CancelWaitableTimer(self.timer);
r == WAIT_OBJECT_0
}
}
}
impl Drop for Event {
fn drop(&mut self) {
unsafe {
if !self.ev.is_null() {
CloseHandle(self.ev);
}
if !self.timer.is_null() {
CloseHandle(self.timer);
}
}
}
}
}
#[cfg(not(windows))]
mod wake_sys {
use std::sync::{Condvar, Mutex};
use std::time::Duration;
pub struct Event {
set: Mutex<bool>,
cv: Condvar,
}
impl Event {
pub fn new() -> Event {
Event { set: Mutex::new(false), cv: Condvar::new() }
}
pub fn set(&self) {
*self.set.lock().unwrap() = true;
self.cv.notify_one();
}
pub fn wait(&self, timeout: Duration) -> bool {
let g = self.set.lock().unwrap();
let (mut g, _) = self.cv.wait_timeout_while(g, timeout, |s| !*s).unwrap();
let woken = *g;
*g = false;
woken
}
}
}
pub type CheckpointHook = unsafe fn(*mut c_void);
static ANY_CHECKPOINT_HOOK: AtomicBool = AtomicBool::new(false);
fn checkpoint_hooks() -> &'static RwLock<Vec<CheckpointHook>> {
static H: OnceLock<RwLock<Vec<CheckpointHook>>> = OnceLock::new();
H.get_or_init(|| RwLock::new(Vec::new()))
}
pub fn on_checkpoint(f: CheckpointHook) {
let mut hooks = checkpoint_hooks().write().unwrap();
if !hooks.iter().any(|h| *h as usize == f as usize) {
hooks.push(f);
}
ANY_CHECKPOINT_HOOK.store(true, Ordering::Release);
}
thread_local! {
static CHECKPOINTING: RefCell<Vec<usize>> = const { RefCell::new(Vec::new()) };
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Checkpoint {
Full,
DrainedInJs,
Nested,
}
pub unsafe fn checkpoint(g: *mut c_void) -> Checkpoint {
let key = g as usize;
let nested = CHECKPOINTING.with(|c| {
let mut c = c.borrow_mut();
if c.contains(&key) {
true
} else {
c.push(key);
false
}
});
if nested {
return Checkpoint::Nested;
}
{
let _profile_context = crate::profiling::ContextScope::enter(g as usize);
let _profile = crate::profiling::span("microtask", "Promise / microtask checkpoint");
glue::rjs_drain_microtasks(g);
}
let full = glue::rjs_in_js(g) == 0;
if full && ANY_CHECKPOINT_HOOK.load(Ordering::Acquire) {
let hooks: Vec<CheckpointHook> = checkpoint_hooks().read().unwrap().clone();
for h in hooks {
h(g);
}
}
CHECKPOINTING.with(|c| c.borrow_mut().retain(|&k| k != key));
if full { Checkpoint::Full } else { Checkpoint::DrainedInJs }
}
extern "C" {
fn mi_on_thread_idle();
}
const IDLE_SWEEP_MS: f64 = 250.0;
thread_local! {
static LAST_IDLE_SWEEP: std::cell::Cell<f64> = const { std::cell::Cell::new(f64::NEG_INFINITY) };
}
static IDLE_SWEEPS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
static IDLE_SWEEP_US: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
static IDLE_SWEEP_MAX_US: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
static RUN_LOOP_US: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
static RUN_LOOP_MAX_US: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
fn idle_sweep() {
let now = now_ms();
if LAST_IDLE_SWEEP.with(|l| now - l.get() < IDLE_SWEEP_MS) {
return;
}
let started = Instant::now();
unsafe { mi_on_thread_idle() };
LAST_IDLE_SWEEP.with(|l| l.set(now_ms()));
let us = started.elapsed().as_micros() as u64;
IDLE_SWEEPS.fetch_add(1, Ordering::Relaxed);
IDLE_SWEEP_US.fetch_add(us, Ordering::Relaxed);
IDLE_SWEEP_MAX_US.fetch_max(us, Ordering::Relaxed);
}
#[derive(Clone, Copy, Debug, Default)]
pub struct IdleWorkStats {
pub sweeps: u64,
pub sweep_us: u64,
pub sweep_max_us: u64,
pub run_loop_us: u64,
pub run_loop_max_us: u64,
}
pub fn idle_work_stats() -> IdleWorkStats {
IdleWorkStats {
sweeps: IDLE_SWEEPS.load(Ordering::Relaxed),
sweep_us: IDLE_SWEEP_US.load(Ordering::Relaxed),
sweep_max_us: IDLE_SWEEP_MAX_US.load(Ordering::Relaxed),
run_loop_us: RUN_LOOP_US.load(Ordering::Relaxed),
run_loop_max_us: RUN_LOOP_MAX_US.load(Ordering::Relaxed),
}
}
pub unsafe fn pump(g: *mut c_void, flags: u32, stats: *mut RjsPumpStats) {
let _profile_context = crate::profiling::ContextScope::enter(g as usize);
let _profile = crate::profiling::span("native", "JavaScript task pump");
let now = now_ms();
let key = g as usize;
let mut timers_fired = 0u32;
let mut raf_fired = 0u32;
let per_task = flags & PUMP_CHECKPOINT_PER_TASK != 0;
if flags & PUMP_TIMERS != 0 {
let due_native: Vec<NativeTask> = {
let mut reg = registry().lock().unwrap();
let s = reg.entry(key).or_default();
let mut due = Vec::new();
let mut i = 0;
while i < s.native.len() {
if now >= s.native[i].0 {
due.push(s.native.swap_remove(i).1);
} else {
i += 1;
}
}
due
};
for task in due_native {
{ let _profile = crate::profiling::span("native", "Async native task"); task(g); }
if per_task {
checkpoint(g);
}
}
let due: Vec<(u32, u64, Vec<u64>, f64, bool, f64)> = {
let mut reg = registry().lock().unwrap();
let s = reg.entry(key).or_default();
let mut due: Vec<_> = s
.timers
.iter()
.filter(|(_, t)| now >= t.next_fire)
.map(|(id, t)| (*id, t.cb, t.args.clone(), t.interval, t.repeating, t.next_fire))
.collect();
due.sort_by(|a, b| a.5.total_cmp(&b.5).then(a.0.cmp(&b.0)));
due
};
for (id, cb, args, interval, repeating, _deadline) in due {
let still_there = registry().lock().unwrap().entry(key).or_default().timers.contains_key(&id);
if !still_there {
continue;
}
fire(g, cb, &args, flags, if repeating { "setInterval callback" } else { "setTimeout callback" });
timers_fired += 1;
let removed = {
let mut reg = registry().lock().unwrap();
let s = reg.entry(key).or_default();
if repeating {
if let Some(t) = s.timers.get_mut(&id) {
t.next_fire = now + interval;
}
None
} else {
s.timers.remove(&id)
}
};
if let Some(t) = removed {
unprotect_timer(g, &t);
}
}
}
if flags & PUMP_RAF != 0 {
let batch: Vec<(u32, u64)> = {
let mut reg = registry().lock().unwrap();
core::mem::take(&mut reg.entry(key).or_default().raf)
};
let ts = [enc_double(now)];
for (_, cb) in &batch {
fire(g, *cb, &ts, flags, "requestAnimationFrame callback");
raf_fired += 1;
}
for (_, cb) in &batch {
capi::JSValueUnprotect(g, *cb as usize as capi::JSValueRef);
}
}
if flags & PUMP_RAF != 0 {
let idle_batch: Vec<(u32, u64)> = {
let mut reg = registry().lock().unwrap();
core::mem::take(&mut reg.entry(key).or_default().idle)
};
if !idle_batch.is_empty() {
let mut deadline = ENC_UNDEFINED;
glue::rjs_object_create(g, ENC_UNDEFINED, &mut deadline);
let a = glue::rjs_atom8(g, "didTimeout".as_ptr(), 10);
glue::rjs_define(g, deadline, a, 0x6 , 0);
glue::rjs_atom_free(g, a);
let a = glue::rjs_atom8(g, "__t0".as_ptr(), 4);
glue::rjs_define(g, deadline, a, enc_double(now_ms()), 2);
glue::rjs_atom_free(g, a);
let name: Vec<u16> = "timeRemaining".encode_utf16().collect();
let mut trf = ENC_UNDEFINED;
glue::rjs_function_create(g, name.as_ptr(), name.len(), 0, idle_time_remaining as *const c_void, &mut trf);
let a = glue::rjs_atom8(g, "timeRemaining".as_ptr(), 13);
glue::rjs_define(g, deadline, a, trf, 2);
glue::rjs_atom_free(g, a);
for (_, cb) in &idle_batch {
fire(g, *cb, &[deadline], flags, "requestIdleCallback callback");
}
for (_, cb) in &idle_batch {
capi::JSValueUnprotect(g, *cb as usize as capi::JSValueRef);
}
}
}
if flags & PUMP_TIMERS != 0 {
crate::webapi::permissions::pump(g);
if per_task {
checkpoint(g);
}
crate::webapi::websocket::pump(g);
if per_task {
checkpoint(g);
}
drain_sources(g);
}
#[cfg(feature = "audio")]
if flags & PUMP_TIMERS != 0 {
crate::webapi::audio::pump(g);
crate::webapi::mediadevices::pump(g);
crate::webapi::mediastream::pump(g);
if per_task {
checkpoint(g);
}
}
if flags & PUMP_MICROTASKS != 0 {
checkpoint(g);
}
if flags & (PUMP_TIMERS | PUMP_RAF) != 0 {
if let Some(ext) = crate::webapi::gpu_ext::get() {
ext.pump(g);
}
}
if glue::rjs_in_js(g) == 0 {
let started = Instant::now();
glue::rjs_run_loop_cycle();
let us = started.elapsed().as_micros() as u64;
RUN_LOOP_US.fetch_add(us, Ordering::Relaxed);
RUN_LOOP_MAX_US.fetch_max(us, Ordering::Relaxed);
idle_sweep();
}
if !stats.is_null() {
let (raf_pending, timers_active, next_deadline) = {
let mut reg = registry().lock().unwrap();
let s = reg.entry(key).or_default();
let next = s
.timers
.values()
.map(|t| t.next_fire)
.fold(f64::INFINITY, f64::min);
(s.raf.len() as u32, s.timers.len() as u32, if next.is_finite() { next } else { -1.0 })
};
*stats = RjsPumpStats {
now,
timers_fired,
raf_fired,
raf_pending,
timers_active,
next_timer_deadline: next_deadline,
};
}
}
pub fn set_uncaught(handler: *const c_void) {
crate::errors::set_handler(handler)
}
#[cfg(test)]
mod checkpoint_tests {
use super::*;
use crate::api::Context;
use crate::webapi::testing::{ok, s};
use crate::webapi::util::{define, make_fn};
fn hook_runs() -> &'static Mutex<HashMap<usize, u32>> {
static R: OnceLock<Mutex<HashMap<usize, u32>>> = OnceLock::new();
R.get_or_init(|| Mutex::new(HashMap::new()))
}
fn seen() -> &'static Mutex<HashMap<usize, Vec<Checkpoint>>> {
static S: OnceLock<Mutex<HashMap<usize, Vec<Checkpoint>>>> = OnceLock::new();
S.get_or_init(|| Mutex::new(HashMap::new()))
}
unsafe fn count_hook(g: *mut c_void) {
*hook_runs().lock().unwrap().entry(g as usize).or_default() += 1;
}
fn runs(c: &Context) -> u32 {
hook_runs().lock().unwrap().get(&(c.raw() as usize)).copied().unwrap_or(0)
}
fn outcomes(c: &Context) -> Vec<Checkpoint> {
seen().lock().unwrap().get(&(c.raw() as usize)).cloned().unwrap_or_default()
}
#[crate::host_fn]
unsafe fn cp_from_js(g: *mut c_void, _cf: *mut c_void) -> u64 {
let r = checkpoint(g);
seen().lock().unwrap().entry(g as usize).or_default().push(r);
ENC_UNDEFINED
}
fn ctx() -> Context {
on_checkpoint(count_hook);
let c = crate::webapi::testing::ctx();
unsafe {
install(c.raw());
let g = c.raw();
define(g, glue::rjs_global(g), "__cp", make_fn(g, "__cp", 0, cp_from_js), 2);
}
c
}
#[test]
fn a_checkpoint_from_rust_drains_and_runs_the_hooks() {
let c = ctx();
s(&c, "globalThis.x = 0; Promise.resolve().then(() => { x = 1; }); 0");
let before = runs(&c);
assert_eq!(unsafe { checkpoint(c.raw()) }, Checkpoint::Full);
ok(&c, "x === 1");
assert_eq!(runs(&c), before + 1, "hooks run once per full checkpoint");
c.drain_microtasks();
assert_eq!(runs(&c), before + 2);
}
#[test]
fn a_checkpoint_asked_for_mid_script_drains_but_never_runs_the_hooks() {
let c = ctx();
let before = runs(&c);
ok(&c, "globalThis.y = 0; Promise.resolve().then(() => { y = 1; }); __cp(); y === 1");
assert_eq!(outcomes(&c), vec![Checkpoint::DrainedInJs]);
assert_eq!(runs(&c), before, "no hook while JS is on the stack");
}
fn probes() -> &'static Mutex<Vec<usize>> {
static P: Mutex<Vec<usize>> = Mutex::new(Vec::new());
&P
}
unsafe fn reentering_hook(g: *mut c_void) {
if probes().lock().unwrap().contains(&(g as usize)) {
let r = checkpoint(g);
seen().lock().unwrap().entry(g as usize).or_default().push(r);
}
}
#[test]
fn a_checkpoint_inside_a_checkpoint_is_a_no_op() {
on_checkpoint(reentering_hook);
let c = ctx();
probes().lock().unwrap().push(c.raw() as usize);
let before = runs(&c);
assert_eq!(unsafe { checkpoint(c.raw()) }, Checkpoint::Full);
probes().lock().unwrap().retain(|&k| k != c.raw() as usize);
assert_eq!(outcomes(&c), vec![Checkpoint::Nested]);
assert_eq!(runs(&c), before + 1, "the nested call ran no hooks of its own");
assert_eq!(unsafe { checkpoint(c.raw()) }, Checkpoint::Full);
}
#[test]
fn per_task_checkpoints_run_microtasks_between_timers() {
for (flags, expect) in [
(PUMP_TIMERS | PUMP_MICROTASKS | PUMP_CHECKPOINT_PER_TASK, "a,micro,b"),
(PUMP_TIMERS | PUMP_MICROTASKS, "a,b,micro"),
] {
let c = ctx();
s(
&c,
"globalThis.log = [];
setTimeout(() => { log.push('a'); Promise.resolve().then(() => log.push('micro')); }, 0);
setTimeout(() => log.push('b'), 0); 0",
);
unsafe {
glue::rjs_lock(c.raw());
pump(c.raw(), flags, core::ptr::null_mut());
glue::rjs_unlock(c.raw());
}
assert_eq!(s(&c, "log.join()"), expect, "flags {flags}");
}
}
}
#[cfg(test)]
mod native_from_tests {
use super::*;
#[test]
fn a_late_native_task_reaches_only_the_context_that_asked() {
let c = crate::webapi::testing::ctx();
let g = c.raw();
unsafe { install(g) };
let old = crate::context::id_of(g);
assert!(old.is_some());
schedule_native_from(g, old, 0.0, Box::new(|_| {}));
assert!(has_scheduler(g), "the live context gets it");
let new = unsafe { crate::context::register(g) };
assert_ne!(Some(new), old);
assert!(!has_scheduler(g));
let owned = std::sync::Arc::new(());
let held = std::sync::Arc::clone(&owned);
schedule_native_from(g, old, 0.0, Box::new(move |_| drop(held)));
assert!(!has_scheduler(g), "nothing re-created for the old context");
assert_eq!(std::sync::Arc::strong_count(&owned), 1, "the task was dropped, not parked");
}
#[test]
fn an_idle_embedder_learns_of_due_and_queued_work() {
use std::sync::atomic::AtomicUsize;
static WOKEN: AtomicUsize = AtomicUsize::new(0);
set_wake_hook(|| {
WOKEN.fetch_add(1, Ordering::SeqCst);
});
let c = crate::webapi::testing::ctx();
let g = c.raw();
unsafe { install(g) };
assert_eq!(next_scheduled_ms(g), None);
crate::webapi::testing::s(&c, "setTimeout(() => {}, 5000); 0");
let due = next_scheduled_ms(g).expect("the timer");
assert!(due > now_ms() + 4000.0 && due <= now_ms() + 5000.0, "{due}");
assert!(next_work_ms(g).expect("the timer") <= due);
crate::webapi::testing::s(&c, "globalThis.junk = []; for (let i = 0; i < 200000; i++) junk.push({ i }); junk = null; 0");
let gc = unsafe { glue::rjs_gc_timers_due_s(g) };
assert!(gc >= 0.0 && gc < 60.0, "a GC timer is armed after allocating: {gc}");
let before = WOKEN.load(Ordering::SeqCst);
let id = crate::context::id_of(g);
let addr = g as usize;
std::thread::spawn(move || schedule_native_from(addr as *mut c_void, id, 0.0, Box::new(|_| {})))
.join()
.unwrap();
assert!(WOKEN.load(Ordering::SeqCst) > before, "woken");
assert!(next_work_ms(g).expect("the task") <= now_ms());
let before = WOKEN.load(Ordering::SeqCst);
crate::webapi::testing::s(&c, "requestAnimationFrame(() => {}); 0");
assert!(WOKEN.load(Ordering::SeqCst) > before, "a frame is wanted");
}
}