we (web engine): Experimental web browser project to understand the limits of Claude

Implement timer APIs: setTimeout, setInterval, requestAnimationFrame #283

Open pierrelf.com
targeting main
Open opened by and assigned to pierrelf.com
targeting main

Implements issue 3mhyfo3nvw22s — Timer APIs: setTimeout, setInterval, requestAnimationFrame

Changes #

New module: crates/js/src/timers.rs #

  • Thread-local timer registry with sorted pending timers
  • Monotonically increasing timer IDs
  • Timer lifecycle: schedule, cancel, take due timers, cleanup
  • GC root collection for pending timer callbacks

Global functions registered in builtins #

  • setTimeout(callback, delay) / clearTimeout(id)
  • setInterval(callback, delay) / clearInterval(id)
  • requestAnimationFrame(callback) / cancelAnimationFrame(id)

VM event loop integration #

  • drain_due_timers() executes due callbacks with microtask drain after each
  • pump_event_loop() for single-pass timer drain
  • run_event_loop(max_iterations) for running until all timers fire (tests)

Tests (15 tests) #

  • Timer ID generation and uniqueness
  • Deferred execution (setTimeout(fn, 0) is not synchronous)
  • clearTimeout/clearInterval cancellation
  • setInterval fires repeatedly and can be stopped
  • requestAnimationFrame passes timestamp argument
  • Timer callbacks survive GC
  • Promise microtasks drain between timer callbacks
  • Edge cases (non-function args, invalid IDs)

This pull request has no versions submitted yet.