M4: executors — coroutine tasks, timers, deterministic test dispatcher, AsyncApp #5

Merged
nandi merged 1 commits from feat/executor into feat/app 2026-11-24 22:42:57 +00:00
Owner

Stacked on #4 (base feat/app); the diff shows only M4. Retarget to main once #1–#4 are merged.

What's here

  • executor, a port of gpui's executor.rs and the scheduler crate, reduced to one thread:
    • Coroutines use MoonBit's async compiler intrinsics (%async.suspend, %async.run, %async.capture_cancellation, %async.raise_cancellation_signal). They are scheduled through a Dispatcher trait, the platform hook, rather than moonbitlang/core/async's event loop. Core async owns the loop and doesn't support wasm-gc; this runs on all four backends, wasm-gc included.
    • Task[T]: wait, which re-raises the task's error, and cancel, which resumes the task with a cancellation signal at its next suspension point so its defer blocks run, while waiters get TaskCancelled. Also ready, try_result, is_ready, is_cancelled and detach.
    • Executor: spawn, timer, sleep, yield_now and now.
    • TestDispatcher: a deterministic FIFO queue on a virtual clock, with tick, run_until_parked and advance_clock. advance_clock behaves like gpui's: run everything, step to each due timer, repeat.
  • app integration: AsyncApp (update, update_entity, read_entity, spawn, executors), App::spawn and Context::spawn. The entity task gets a weak handle. App::new() owns a TestDispatcher (like TestAppContext); a platform will pass its own dispatcher. Tests drive it with app.run_until_parked() and app.advance_clock(ms).
  • The counter example gains a ticker task on the simulated clock, which is then cancelled.
  • PORTING.md: M4 status, M4 deviations (§6), a corrected async row in the ecosystem table (§3), and toolchain notes (§8).

Deviations

  • Experimental intrinsics. They sit behind experimental: true, and importers need allow-experimental-packages. They're isolated in src/executor/coroutine.mbt.
  • One thread. Foreground and background executors are the same; there are no priorities, dedicated threads or block_on.
  • Tasks aren't cancelled on drop (MoonBit has no Drop); call cancel().
  • Durations are milliseconds (Int), and now() returns Int64 milliseconds.
  • No randomized interleaving in the test dispatcher, and no parking detection.
  • AsyncApp methods return values rather than Results.

Testing

moon check --target all and moon test --target all pass: 124 tests on each of wasm, wasm-gc, js and native.

  • Executor tests:
    • Adapted from the scheduler crate: spawn, spawn order, and timer ordering on the virtual clock.
    • New: awaiting and error propagation, cancel before start, cancel while suspended (unwinding plus waiters notified), cancelled timers, a task cancelling itself, yield_now interleaving, tick.
    • Abort cases: wait outside a task, and a task waiting for itself.
  • App tests: tasks that update entities with effects flushed after each update, weak handles across a release, cancelling a spawned task, nested spawn and await, a caller-provided dispatcher, and an abort test.

🤖 Generated with Claude Code

Stacked on #4 (base `feat/app`); the diff shows only M4. Retarget to `main` once #1–#4 are merged. ## What's here - **`executor`**, a port of gpui's `executor.rs` and the `scheduler` crate, reduced to one thread: - **Coroutines** use MoonBit's async compiler intrinsics (`%async.suspend`, `%async.run`, `%async.capture_cancellation`, `%async.raise_cancellation_signal`). They are scheduled through a **`Dispatcher`** trait, the platform hook, rather than `moonbitlang/core/async`'s event loop. Core async owns the loop and doesn't support wasm-gc; this runs on **all four backends, wasm-gc included**. - **`Task[T]`**: `wait`, which re-raises the task's error, and `cancel`, which resumes the task with a cancellation signal at its next suspension point so its `defer` blocks run, while waiters get `TaskCancelled`. Also `ready`, `try_result`, `is_ready`, `is_cancelled` and `detach`. - **`Executor`**: `spawn`, `timer`, `sleep`, `yield_now` and `now`. - **`TestDispatcher`**: a deterministic FIFO queue on a virtual clock, with `tick`, `run_until_parked` and `advance_clock`. `advance_clock` behaves like gpui's: run everything, step to each due timer, repeat. - **`app` integration**: `AsyncApp` (`update`, `update_entity`, `read_entity`, `spawn`, executors), `App::spawn` and `Context::spawn`. The entity task gets a weak handle. `App::new()` owns a `TestDispatcher` (like `TestAppContext`); a platform will pass its own dispatcher. Tests drive it with `app.run_until_parked()` and `app.advance_clock(ms)`. - **The counter example** gains a ticker task on the simulated clock, which is then cancelled. - **PORTING.md**: M4 status, M4 deviations (§6), a corrected async row in the ecosystem table (§3), and toolchain notes (§8). ## Deviations - **Experimental intrinsics.** They sit behind `experimental: true`, and importers need `allow-experimental-packages`. They're isolated in `src/executor/coroutine.mbt`. - **One thread.** Foreground and background executors are the same; there are no priorities, dedicated threads or `block_on`. - **Tasks aren't cancelled on drop** (MoonBit has no `Drop`); call `cancel()`. - **Durations are milliseconds** (`Int`), and `now()` returns `Int64` milliseconds. - **No randomized interleaving** in the test dispatcher, and no parking detection. - **`AsyncApp` methods return values rather than `Result`s.** ## Testing `moon check --target all` and `moon test --target all` pass: 124 tests on each of wasm, wasm-gc, js and native. - **Executor tests:** - Adapted from the scheduler crate: spawn, spawn order, and timer ordering on the virtual clock. - New: awaiting and error propagation, cancel before start, cancel while suspended (unwinding plus waiters notified), cancelled timers, a task cancelling itself, `yield_now` interleaving, `tick`. - Abort cases: `wait` outside a task, and a task waiting for itself. - **App tests**: tasks that update entities with effects flushed after each update, weak handles across a release, cancelling a spawned task, nested spawn and await, a caller-provided dispatcher, and an abort test. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
nandi added 1 commit 2026-11-24 22:36:26 +00:00
- executor: coroutines on MoonBit's async intrinsics (%async.suspend /
  run / capture_cancellation / raise_cancellation_signal), scheduled by a
  Dispatcher trait (the platform hook) instead of core/async's event loop;
  works on wasm, wasm-gc, js and native.
- Task[T]: wait (re-raises task errors), cancel (unwinds at the next
  suspension point, running defers; waiters get TaskCancelled), ready,
  try_result, detach. Executor: spawn, timer, sleep, yield_now, now.
- TestDispatcher: deterministic FIFO queue and virtual clock with GPUI's
  advance_clock semantics (run, then step to each due timer).
- app: AsyncApp, App::spawn, Context::spawn (weak handle),
  foreground/background executors, run_until_parked / advance_clock;
  App::new() owns a TestDispatcher unless given a dispatcher.
- Tests ported/adapted from the scheduler crate; app async tests; the
  counter example gains a ticker task. Docs: M4 status and deviations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
nandi merged commit 1ae73d13c3 into feat/app 2026-11-24 22:42:57 +00:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: experiments/moon-gpui#5