Skip to content
14 changes: 5 additions & 9 deletions quest/m2/ts-async.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,17 @@ A handoff through 1, 4, and 16 async functions measured 1.5-2.7x faster than #16
generators in Node, 1.8-5.5x in Bun, and 1.4-3.5x in Chrome, and past depth 1 faster
than moq's hand-written promises. `rt.spawn` stays.

What moq-net still needs, by its census from MIR at the pinned revision:
std's future helpers translate: `Poll`'s methods, `pin!`, `poll_fn`, `future::pending`,
`Waker::from(Arc<W>)`, and `Waker::noop`. What moq-net still needs, by its census from
MIR at the pinned revision:

- `Poll`'s helpers (about 340 calls: `is_ready`, `is_pending`, `map`, `map_ok`,
`map_err`), `pin!` (50 with `PinMacroHelper`), `poll_fn` (31), `future::pending`
(5), and `Box::pin` (29, a boxed future that binds to hand-written TS at the
boundary, per the [model](/quest/m2/ts-model/README.md)).
- `Box::pin` (29, a boxed future that binds to hand-written TS at the boundary, per the
[model](/quest/m2/ts-model/README.md)), and polling a `Pin<Box<dyn Future>>`.
- Async closures.
- kio's own API, reached across crates (`Pending`, `Consumer::poll`,
`Waiter::poll_future`, `Tasks`), translates through
[safe dependencies](/quest/m2/ts-model/crates.md), and kio's `RawWaker` vtable and
`Box<dyn Future>` tasks are [bindings](/ts/README.md#bindings).

The model itself (its census of 2026-10-27) needs only `Poll`'s helpers and
`Waker::from(Arc<W>)`, 61 refusals; `pin!`, `poll_fn`, `Box::pin`, and async closures are
in moq-net's session, lite, and ietf layers.
- The edge: `rt.spawn`'s shape for callers, and cancelling a spawned task.

An async block that captures part of a variable, which THIR refused 47 times, is a
Expand Down
12 changes: 8 additions & 4 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ from. `rt.ts` beside them is the
runtime: what std does that JS does not, with iterators in `rt/iter.ts`, `str`'s searches
in `rt/str.ts`, collections in `rt/collections.ts`, ownership and async in
`rt/task.ts`, and `Duration` in `rt/time.ts`. What translates from std's own MIR,
`Option`'s and `Result`'s combinators, goes in a module under its crate's name, as
`Option`'s, `Result`'s, and `Poll`'s methods, goes in a module under its crate's name, as
`core/option.ts` holds `map$Option_u64_u64_rescale_closure0`.

| Rust | TypeScript |
Expand Down Expand Up @@ -92,6 +92,7 @@ in `rt/str.ts`, collections in `rt/collections.ts`, ownership and async in
| `Arc<T>`, `Weak<T>`, `Mutex<T>` | `rt.Arc`, counted; `rt.Weak`; `rt.Mutex`, its own guard |
| `async fn` | a function returning `rt.Future<T>`: a class of its own, its coroutine's state machine |
| `Poll<T>`, `Waker`, `Context` | `T \| typeof rt.Pending`, refused if `T` can already be pending; `rt.Waker`, which is also the context |
| `pin!(x)`, `poll_fn(f)`, `future::pending()` | `x`; `rt.PollFn`, which drops `f` with itself; `rt.NEVER` |

`Vec<u8>` storage and `[x; N]` byte arrays up to 1 KiB share 8 KiB `ArrayBuffer`s, as Node's
`Buffer` pool does, so the `buffer` behind a view holds other bytes too: read it through
Expand Down Expand Up @@ -204,7 +205,7 @@ elaboration put it. A panic unwinds: a function with cleanup to run catches `rt.
runs the cleanup blocks MIR has under `-C panic=unwind`, and throws it on, so what a
panic drops matches native Rust's. A value without `[Symbol.dispose]` of its own, such as
an enum or a tuple, drops through glue lowered from rustc's `drop_in_place` shim, a
function of its type's own (`drop$Held`). `Option`'s and `Result`'s combinators lower
function of its type's own (`drop$Held`). `Option`'s, `Result`'s, and `Poll`'s methods lower
from core's MIR, so they drop what core's do, where core's do. The runtime functions for
std's collections, `Arc`, and the std functions that may discard an operand
(`then_some`, a map's `insert`) take what drops each value they may drop, and drop it
Expand All @@ -221,7 +222,9 @@ own (`sumFuture`), the state machine MIR's coroutine transform makes. Its fields
arguments and the locals held across an `.await`, and its `poll` resumes at the state it
stopped in. A poll after the future returned panics, and dropping it cancels it,
dropping what its state holds. `rt.spawn(future)` runs one as a promise, polling woken tasks in a batch from a
microtask and yielding to a macrotask after 128 polls.
microtask and yielding to a macrotask after 128 polls. `Waker::from(arc)`, of an `Arc` of
the crate's `Wake` impl, is an `rt.ArcWaker`, which holds a count of the `Arc` and wakes
through the impl, and `Waker::noop()` is `rt.NOOP_WAKER`.

A file's items follow the Rust source, whatever
order translation reaches them in, so adding a root moves nothing it does not reach.
Expand Down Expand Up @@ -499,7 +502,8 @@ built natively for wasm32 and run beside its translation on the same inputs by
byte array overwritten whole through a type parameter, or, under its features, by std
called directly or through a pointer a generic function calls.
- `task` trims moq's kio to a channel of `Arc`ed state, waiters, and wakers, under async
functions, beside values that drop inside std's types. Its caller is hand-written TS (`tests/task.ts`): a FIFO executor that runs a
functions and std's future helpers, with wakers of the crate's `Wake` impls, beside
values that drop inside std's types. Its caller is hand-written TS (`tests/task.ts`): a FIFO executor that runs a
script of spawns, sends, polls, and drops, and logs every poll, wake, and drop, as the
fixture's native executor does. The logs must match byte for byte. `tests/unwind.ts`
also runs the scripts natively on the host (`examples/unwind.rs`), where a panic
Expand Down
7 changes: 7 additions & 0 deletions ts/fixtures/task/expected/core/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ export function okOr$Option_Guard_Guard(self: Guard.Guard | undefined, err: Guar
return _0;
}

export function okOr$Option_u64_u64(self: rt.u64 | undefined, err: rt.u64): rt.Result<rt.u64, rt.u64> {
if (self === undefined) {
return rt.Err(err);
}
return rt.Ok(self);
}

export function filter$Option_Guard_options_closure4(self: Guard.Guard | undefined, predicate: (a0: Guard.Guard) => boolean): Guard.Guard | undefined {
let x!: Guard.Guard;
let unwind: number = 0;
Expand Down
54 changes: 54 additions & 0 deletions ts/fixtures/task/expected/core/task/poll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "../../rt.ts";

export function map$Poll_Option_u64_Result_u64_u64_helpers_closure0_closure1_closure0(self: rt.Poll<rt.u64 | undefined>, f: (a0: rt.u64 | undefined) => rt.Result<rt.u64, rt.u64>): rt.Poll<rt.Result<rt.u64, rt.u64>> {
if (self !== rt.Pending) {
return f(self);
}
return rt.Pending;
}

export function isReady$Poll_Option_u64(self: rt.Poll<rt.u64 | undefined>): boolean {
if (self !== rt.Pending) {
return true;
}
return false;
}

export function isReady$Poll_u64(self: rt.Poll<rt.u64>): boolean {
if (self !== rt.Pending) {
return true;
}
return false;
}

export function isPending$Poll_Option_u64(self: rt.Poll<rt.u64 | undefined>): boolean {
return !isReady$Poll_Option_u64(self);
}

export function isPending$Poll_u64(self: rt.Poll<rt.u64>): boolean {
return !isReady$Poll_u64(self);
}

export function mapOk$Poll_Result_u64_u64_u64_helpers_closure0_closure1_closure1(self: rt.Poll<rt.Result<rt.u64, rt.u64>>, f: (a0: rt.u64) => rt.u64): rt.Poll<rt.Result<rt.u64, rt.u64>> {
if (self !== rt.Pending) {
if (self.ok) {
return rt.Ok(f(self.value));
}
return self;
}
return rt.Pending;
}

export function mapErr$Poll_Result_u64_u64_u64_helpers_closure0_closure1_closure2(self: rt.Poll<rt.Result<rt.u64, rt.u64>>, f: (a0: rt.u64) => rt.u64): rt.Poll<rt.Result<rt.u64, rt.u64>> {
if (self !== rt.Pending) {
if (self.ok) {
return self;
}
return rt.Err(f(self.error));
}
return rt.Pending;
}

//# sourceMappingURL=poll.ts.map
2 changes: 1 addition & 1 deletion ts/fixtures/task/expected/drops/closures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export function scoped(log: Log.Log, id: rt.u32, at: rt.u8): void {
const held: Guard.Guard[] = [];
unwind = 77;
_179 = true;
const captured13 = held;
let captured13 = held;
push = rt.closure((g2: Guard.Guard): rt.usize => {
captured13.push(g2);
return captured13.length;
Expand Down
39 changes: 39 additions & 0 deletions ts/fixtures/task/expected/futures.Blast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";
import * as Guard from "./model.Guard.ts";

/** Panics as its last waker drops, after which its guard drops. */
export class Blast {
_0: Guard.Guard;

constructor(_0: Guard.Guard) {
this._0 = _0;
}

[Symbol.dispose](): void {
let dropped: number = 0;
try {
drop$Drop(this);
dropped = 1;
rt.drop(this._0);
} catch (e) {
if (dropped < 1) {
rt.drop(this._0);
}
throw e;
}
}
}

export function wake$Wake(self: rt.Arc<Blast>): void {
rt.drop(self);
}

export function drop$Drop(self: Blast): void {
const part = Guard.id(self._0);
const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.u32Display(part, f)];
rt.panicFmt(["g", args2[0], " blew"]);
}

//# sourceMappingURL=futures.Blast.ts.map
34 changes: 34 additions & 0 deletions ts/fixtures/task/expected/futures.Fused.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";
import type * as Guard from "./model.Guard.ts";

/**
* A waker, then a guard: glue that drops the waker first must still drop the guard when
* the waker's drop panics.
*/
export class Fused {
waker: rt.Waker;
after: Guard.Guard;

constructor(fields: { waker: rt.Waker; after: Guard.Guard }) {
this.waker = fields.waker;
this.after = fields.after;
}

[Symbol.dispose](): void {
let dropped: number = 0;
try {
rt.drop(this.waker);
dropped = 1;
rt.drop(this.after);
} catch (e) {
if (dropped < 1) {
rt.drop(this.after);
}
throw e;
}
}
}

//# sourceMappingURL=futures.Fused.ts.map
37 changes: 37 additions & 0 deletions ts/fixtures/task/expected/futures.Plain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";
import type * as Relay from "./futures.Relay.ts";
import * as Guard from "./model.Guard.ts";

/** A relay without its own `wake_by_ref`. */
export class Plain {
_0: Relay.Relay;

constructor(_0: Relay.Relay) {
this._0 = _0;
}

[Symbol.dispose](): void {
rt.drop(this._0);
}
}

export function wake$Wake(self: rt.Arc<Plain>): void {
let unwind: number = 0;
try {
unwind = 7;
Guard.note(self.value._0.guard, "plain wake");
self.value._0.task.wakeByRef();
unwind = 0;
rt.drop(self);
} catch (e) {
if (unwind === 0) {
throw e;
}
rt.drop(self);
throw e;
}
}

//# sourceMappingURL=futures.Plain.ts.map
53 changes: 53 additions & 0 deletions ts/fixtures/task/expected/futures.Relay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";
import * as Guard from "./model.Guard.ts";

/** Wakes `task`, logging through `guard`, which logs the relay's drop. */
export class Relay {
task: rt.Waker;
guard: Guard.Guard;

constructor(fields: { task: rt.Waker; guard: Guard.Guard }) {
this.task = fields.task;
this.guard = fields.guard;
}

[Symbol.dispose](): void {
let dropped: number = 0;
try {
rt.drop(this.task);
dropped = 1;
rt.drop(this.guard);
} catch (e) {
if (dropped < 1) {
rt.drop(this.guard);
}
throw e;
}
}
}

export function wake$Wake(self: rt.Arc<Relay>): void {
let unwind: number = 0;
try {
unwind = 7;
Guard.note(self.value.guard, "relay wake");
self.value.task.wakeByRef();
unwind = 0;
rt.drop(self);
} catch (e) {
if (unwind === 0) {
throw e;
}
rt.drop(self);
throw e;
}
}

export function wakeByRef$Wake(self: rt.Arc<Relay>): void {
Guard.note(self.value.guard, "relay wake_by_ref");
self.value.task.wakeByRef();
}

//# sourceMappingURL=futures.Relay.ts.map
Loading