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

This commit was merged in pull request #5.
This commit is contained in:
nandi committed 2026-11-24 22:42:56 +00:00
1 parent 64bf747047
commit 1ae73d13c3
16 files changed
+1108 -13

No files matched your search

+45 -6
View File
@@ -136,7 +136,7 @@ stays isolated.
| `keymap` | `keymap.rs`, `keymap/*`, `platform/keystroke.rs` | pure | planned |
| `action` | `action.rs` | pure | planned |
| `app` | `app.rs`, `app/entity_map.rs`, `app/context.rs`, `subscription.rs`, `global.rs` | pure | **done** (no windows, executors or actions yet) |
| `executor` | `executor.rs`, `platform_scheduler.rs`, `scheduler` crate | pure core + platform hooks | planned |
| `executor` | `executor.rs`, `scheduler` crate (`executor.rs`, `test_scheduler.rs`) | pure core + `Dispatcher` platform hook | **done** (single thread) |
| `element` | `element.rs`, `view.rs`, `elements/div.rs`, `interactive.rs` | pure | planned |
| `window` | `window.rs`, `key_dispatch.rs`, `tab_stop.rs` | pure (talks to platform traits) | planned |
| `text` | `text_system.rs`, `text_system/*` | mixed: wrapping/caching pure, shaping platform | planned |
@@ -218,7 +218,7 @@ records what I verified with toolchain `moon 0.1.20261119`.
| Browser APIs | `mizchi/js_browser@0.13.0` (MIT), `dijdzv/websys`, `peter-jerry-ye/canvas` | **js only** | DOM, Canvas2D, events, and rAF. |
| WebGPU | `Milky2018/wgpu_mbt@0.16.2` | **native only** | wgpu-native C API bindings; macOS is supported, Linux and Windows are experimental. `tonyfettes/webgpu` and `emadurandal/rhodonite_webgpu` cover browser WebGPU. |
| Windowing | `Milky2018/window@0.6.2` (winit port, **native, macOS only**), `mizchi/glfw`, `klaseca/sdl`, `laplaceliu/tycho_window` (Xlib) | native only | |
| Async | `moonbitlang/async@0.22.10` | **native** (some wasm) | Not usable on js. Core has a `moonbitlang/core/async` package; its js story needs checking before the executor milestone. |
| Async | `moonbitlang/async@0.22.10`, `moonbitlang/core/async` | native, wasm; js partly; **not wasm-gc** | Not used. Both own the event loop (`async fn main`, task groups), which conflicts with a platform-driven executor, and neither supports wasm-gc. `executor` uses the same compiler intrinsics (`%async.suspend`/`%async.run`) directly; see M4. |
| Taffy-free layout ref | `Frank-III/onebit-tui` | native | Uses Yoga through FFI. Not useful here. |
Recommendation: depend on `moon_cosmic` for text when that milestone
@@ -361,10 +361,16 @@ gantt
- Tests: GPUI's subscription tests are ported, plus app-model tests,
including the aborts expected on misuse.
- `src/examples/counter` shows the model end to end.
- **M4: executor.**
- Foreground executor, timers, and `Task[T]` (cancel and detach).
- `TestScheduler` with a virtual clock and `run_until_parked`.
- `spawn` from `Context`.
- **M4: executor (done).**
- `Executor` (spawn, timer, sleep, yield_now, now), and `Task[T]` (wait,
cancel with unwinding, try_result, ready, detach).
- A `Dispatcher` trait as the platform hook, plus `TestDispatcher`, a
deterministic dispatcher with a virtual clock (`tick`,
`run_until_parked`, `advance_clock`, with GPUI's timer semantics).
- `AsyncApp`, `App::spawn`, `Context::spawn` (weak entity), and the
app's `foreground_executor`/`background_executor`.
- `App::new()` uses its own `TestDispatcher` (like GPUI's
`TestAppContext`); a platform passes its own dispatcher.
- **M5: input model.**
- `Keystroke::parse`, `Modifiers`.
- `KeyContext` and the predicate parser and evaluator.
@@ -499,6 +505,33 @@ gantt
- Actions (M5).
- Leak detection.
### Deviations taken in M4 (executor)
- **Coroutines come from MoonBit's experimental async intrinsics.**
`%async.suspend`, `%async.run`, `%async.capture_cancellation`, and
`%async.raise_cancellation_signal` are scheduled by our own `Dispatcher`.
This is what `moonbitlang/core/async` is built on, minus its event loop.
It works on all four backends, including wasm-gc.
- The package must set `experimental: true`, and packages importing it
need `"allow-experimental-packages": true`.
- Risk: the intrinsics may change with the toolchain. They are wrapped in
`src/executor/coroutine.mbt`.
- **One thread.** Background and foreground executors are the same executor
on the main thread, as in GPUI's single-threaded web backend. There are no
priorities, dedicated threads, `block_on`, or `scoped`.
- **Tasks are not cancelled on drop.** A GPUI `Task` cancels its future when
dropped. Here a task runs to completion unless `cancel()` is called.
- Cancellation resumes the task with a cancellation signal at its next
suspension point, running its `defer` blocks.
- Waiters get `TaskCancelled`; errors raised by a task are re-raised by
`wait`.
- **Durations are integer milliseconds.** `now()` returns `Int64`
milliseconds.
- **The test dispatcher is FIFO and deterministic.** GPUI's `TestScheduler`
can randomize the interleaving of sessions with a seed. That is not ported.
There is also no parking detection.
- **`AsyncApp` methods return values, not `Result`s.** An app is never
dropped while a task references it.
## 7. Design decisions
Decided on 2026-11-24, following the recommendations below.
@@ -541,5 +574,11 @@ Decided on 2026-11-24, following the recommendations below.
inside a callback in a test. Record values and assert afterwards. A test
whose name starts with `panic` passes only if it aborts; use this to test
the fail-loudly paths.
- `test` is a keyword, so a binding cannot be named `test`. Trait-object
coercion does not reach inside tuples: build `(&Trait, ...)` values
separately.
- Async code: `async fn` bodies that never suspend get an "unused async"
warning; a plain `fn` closure is accepted where an `async` one is
expected. Closures that raise must say so (`fn() raise { ... }`).
- `Ref(x)` builds a `Ref`; `{ val: x }` needs a type annotation.
- Test-only imports go in `import { ... } for "test"` in `moon.pkg`.