Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions quest/m2/ts-async.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ generators in Node, 1.8-5.5x in Bun, and 1.4-3.5x in Chrome, and past depth 1 fa
than moq's hand-written promises. `rt.spawn` stays.

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:
`Waker::from(Arc<W>)`, and `Waker::noop`, and so do `Box::pin` and `dyn Future`. What
moq-net still needs, by its census from MIR at the pinned revision:

- `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.
- Async closures, which only moq-net's tests write today.
- 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).
`Box<dyn FnMut>` tasks are [bindings](/ts/README.md#bindings).
- 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: 5 additions & 7 deletions quest/m3/ts-dyn.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

## Goal

[rust2-ts](/ts/README.md) translates trait objects (`&dyn Trait`, `Box<dyn Trait>`, and
`Box<dyn Future>`, `Pin`ned or not), with Rust's dispatch and drop order, proven by
differential tests against native Rust, so a consumer neither rewrites nor binds them.
[rust2-ts](/ts/README.md) translates trait objects (`&dyn Trait` and `Box<dyn Trait>`), with
Rust's dispatch and drop order, proven by differential tests against native Rust, so a
consumer neither rewrites nor binds them.

## Plan

Decided with the maintainer on 2026-10-27: a stock crate translates with no annotations or
config, so trait objects move from never to not yet. Until this lands, moq rewrites
`dyn Expiry` as an enum and binds its boxed futures ([bindings](/ts/README.md#bindings)).
`dyn Expiry` as an enum and binds its boxed closures ([bindings](/ts/README.md#bindings)).

- `&dyn Debug` and `&dyn Display` already translate; build on how they do.
- A boxed future must keep Rust's laziness and cancellation as
[async](/quest/m2/ts-async.md) defines them.
- `&dyn Debug`, `&dyn Display`, and `dyn Future` already translate; build on how they do.

Public API: the translator's output.
3 changes: 3 additions & 0 deletions ts/MIGRATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- A trait object in a function's suffix is named for its trait and the types it binds:
`show$dynstdfmtDebug` is `show$dyn_Debug`, and `poll$dyn_Future_u64` is a
`dyn Future<Output = u64>`'s.
- `rt.FromUtf8Error` holds its `rt.Utf8Error` as std's does: `e.validUpTo` and
`e.errorLen` are `e.error.validUpTo` and `e.error.errorLen`.
- The runtime no longer exports `ownedNth`, `ownedNthBack`, `ownedSkip`, `ownedTake`, or
Expand Down
5 changes: 3 additions & 2 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ in `rt/str.ts`, collections in `rt/collections.ts`, ownership and async in
| `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` |
| `Box::pin(x)`, `dyn Future<Output = T>` | `x`; `rt.Future<T>`: an `async` body or std's future as it is, and another future as an `rt.DynFuture` over its `poll`, which drops it with itself unless borrowed |

`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 @@ -444,8 +445,8 @@ they need.
## Not yet

`unsafe` and raw pointers ([unsafe](/quest/m3/ts-unsafe.md)), and trait objects other than
`&dyn Debug` and `&dyn Display` ([trait objects](/quest/m3/ts-dyn.md)), fail translation.
Until they translate, an item that needs one, such as a boxed future, binds to
`&dyn Debug`, `&dyn Display`, and `dyn Future` ([trait objects](/quest/m3/ts-dyn.md)), fail
translation. Until they translate, an item that needs one, such as a boxed closure, binds to
hand-written TS ([bindings](#bindings)).

These fail translation: formatting and parsing an `f64`, and `f64::clamp`, whose panic formats them ([float formatting](/quest/m2/ts-model/floats.md)), `f32` ([`f32`](/quest/m3/ts-f32.md)), statics, async closures, a closure that owns a value
Expand Down
23 changes: 12 additions & 11 deletions ts/fixtures/reject/census.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Census of `reject-fixture`

- Refusals: 76
- Function instances: 82
- Refusals: 77
- Function instances: 84
- Generic functions no instance reached, lowered over their type parameters: 6

## By construct
Expand All @@ -19,6 +19,7 @@
| 2 | `_` is not supported: its `None` and `Some` would both be `rt.None` | fixtures/reject/src/lib.rs:23:15 |
| 2 | `_` of `_`, whose `None` is `rt.None`, which is not supported yet | fixtures/reject/src/lib.rs:48:42 |
| 2 | `_` of a reference to an array of bytes it is generic over, which its mapping would take as the array, not as a slice of it | fixtures/reject/src/lib.rs:268:16 |
| 2 | a coercion to `_` | fixtures/reject/src/lib.rs:362:2 |
| 2 | an iterator of the crate implementing `_`, which is not supported yet | fixtures/reject/src/lib.rs:175:1 |
| 2 | constructing `_` from another crate | fixtures/reject/src/lib.rs:161:23 |
| 2 | overriding `_`, which std's adaptors call in place of `next` or beside it | fixtures/reject/src/lib.rs:155:2 |
Expand All @@ -36,7 +37,6 @@
| 1 | a `zip` of `_` and `_`, which std reads by index, of items that drop, which is not supported yet | fixtures/reject/src/lib.rs:242:30 |
| 1 | a `zip` of `_`, whose closures std calls one item further through `next` than through `fold` | fixtures/reject/src/lib.rs:188:51 |
| 1 | a cast from `_` to `_` | fixtures/reject/src/lib.rs:112:2 |
| 1 | a coercion to `_` | fixtures/reject/src/lib.rs:362:2 |
| 1 | a constant of type `_` | fixtures/reject/src/lib.rs:96:2 |
| 1 | a copy of a closure that changes what it captured | fixtures/reject/src/lib.rs:278:14 |
| 1 | a mutable borrow of a subslice of `_`, which JS would copy, having no views | fixtures/reject/src/lib.rs:247:13 |
Expand All @@ -62,10 +62,10 @@ The type or module each refused type or path belongs to.
| 6 | `Option` | fixtures/reject/src/lib.rs:23:15 |
| 6 | `f32` | fixtures/reject/src/lib.rs:7:14 |
| 4 | `Iterator` | fixtures/reject/src/lib.rs:48:42 |
| 4 | `std::sync::Arc` | fixtures/reject/src/lib.rs:117:2 |
| 3 | `PartialOrd` | fixtures/reject/src/lib.rs:381:4 |
| 3 | `std::iter::Map` | fixtures/reject/src/lib.rs:63:23 |
| 3 | `std::slice` | fixtures/reject/src/lib.rs:28:4 |
| 3 | `std::sync::Arc` | fixtures/reject/src/lib.rs:117:2 |
| 3 | `std::vec::Vec` | fixtures/reject/src/lib.rs:289:52 |
| 3 | `u8` | fixtures/reject/src/lib.rs:96:2 |
| 3 | `{closure}` | fixtures/reject/src/lib.rs:411:13 |
Expand Down Expand Up @@ -115,6 +115,7 @@ The type or module each refused type or path belongs to.
| 3 | unsupported type `_` | `f32` | fixtures/reject/src/lib.rs:7:14 |
| 2 | `_` is not supported: it would be pending two ways with one value | `Poll` | fixtures/reject/src/lib.rs:121:20 |
| 2 | `_` is not supported: its `None` and `Some` would both be `rt.None` | `Option` | fixtures/reject/src/lib.rs:23:15 |
| 2 | a coercion to `_` | `std::sync::Arc` | fixtures/reject/src/lib.rs:362:2 |
| 2 | an assignment through a reference to `_` | `std::pin::Pin` | fixtures/reject/src/lib.rs:132:2 |
| 2 | comparing `_` through its own `_`, which is not supported yet | `Lopsided`, `PartialOrd::lt` | fixtures/reject/src/lib.rs:381:4 |
| 2 | replacing the value behind a reference to `_` | `Meters` | fixtures/reject/src/lib.rs:101:2 |
Expand All @@ -137,7 +138,6 @@ The type or module each refused type or path belongs to.
| 1 | a `zip` of `_` and `_`, which std reads by index, of items that drop, which is not supported yet | `std::iter::Map`, `std::ops::Range` | fixtures/reject/src/lib.rs:242:30 |
| 1 | a `zip` of `_`, whose closures std calls one item further through `next` than through `fold` | `std::iter::Chain` | fixtures/reject/src/lib.rs:188:51 |
| 1 | a cast from `_` to `_` | `u64`, `f32` | fixtures/reject/src/lib.rs:112:2 |
| 1 | a coercion to `_` | `std::sync::Arc` | fixtures/reject/src/lib.rs:362:2 |
| 1 | a constant of type `_` | `&u8` | fixtures/reject/src/lib.rs:96:2 |
| 1 | a mutable borrow of a subslice of `_`, which JS would copy, having no views | `u32` | fixtures/reject/src/lib.rs:247:13 |
| 1 | a scalar constant of type `_` | `f32` | fixtures/reject/src/lib.rs:8:2 |
Expand Down Expand Up @@ -269,6 +269,13 @@ The type or module each refused type or path belongs to.

</details>

<details><summary>a coercion to `_` (2)</summary>

- fixtures/reject/src/lib.rs:362:2: a coercion to `std::sync::Arc<[u8]>`
- fixtures/reject/src/lib.rs:509:2: a coercion to `std::sync::Arc<dyn std::future::Future<Output = u8>>`

</details>

<details><summary>an iterator of the crate implementing `_`, which is not supported yet (2)</summary>

- fixtures/reject/src/lib.rs:175:1: an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet
Expand Down Expand Up @@ -375,12 +382,6 @@ The type or module each refused type or path belongs to.

</details>

<details><summary>a coercion to `_` (1)</summary>

- fixtures/reject/src/lib.rs:362:2: a coercion to `std::sync::Arc<[u8]>`

</details>

<details><summary>a constant of type `_` (1)</summary>

- fixtures/reject/src/lib.rs:96:2: a constant of type `&u8`
Expand Down
1 change: 1 addition & 0 deletions ts/fixtures/reject/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ replacing the value behind a reference to `Meters` at fixtures/reject/src/lib.rs
an assignment through a reference to `std::sync::Arc<u8>` at fixtures/reject/src/lib.rs:117:2
`Poll<std::option::Option<std::task::Poll<u8>>>` is not supported: it would be pending two ways with one value at fixtures/reject/src/lib.rs:121:20
`Poll<std::task::Poll<u8>>` is not supported: it would be pending two ways with one value at fixtures/reject/src/lib.rs:126:54
a coercion to `std::sync::Arc<dyn std::future::Future<Output = u8>>` at fixtures/reject/src/lib.rs:509:2
an assignment through a reference to `std::pin::Pin<std::sync::Arc<u8>>` at fixtures/reject/src/lib.rs:132:2
an assignment through a reference to `std::sync::MutexGuard<'_, u8>` at fixtures/reject/src/lib.rs:137:2
unsupported call to `<std::slice::Iter<'_, u8> as std::iter::Iterator>::cycle` at fixtures/reject/src/lib.rs:142:11
Expand Down
16 changes: 16 additions & 0 deletions ts/fixtures/reject/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,19 @@ pub fn fma(a: f64) -> f64 {
pub fn float_printed(a: f64) -> String {
format!("{a}")
}

/// An `Arc` around a future of the crate's: a `dyn Future` behind it would need the `Arc`'s
/// own count around an `rt.Future`.
pub fn shared_future() -> std::sync::Arc<dyn std::future::Future<Output = u8>> {
std::sync::Arc::new(Ready(1))
}

pub struct Ready(u8);

impl std::future::Future for Ready {
type Output = u8;

fn poll(self: std::pin::Pin<&mut Self>, _: &mut std::task::Context<'_>) -> std::task::Poll<u8> {
std::task::Poll::Ready(self.0)
}
}
1 change: 1 addition & 0 deletions ts/fixtures/reject/ts.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ roots = [
"replace_shared",
"nested_poll",
"pending_output",
"shared_future",
"replace_pinned",
"replace_guard",
"cycled",
Expand Down
14 changes: 14 additions & 0 deletions ts/fixtures/task/expected/core/task/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ export function map$Poll_Option_u64_Result_u64_u64_helpers_closure0_closure1_clo
return rt.Pending;
}

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

export function map$Poll_u64_u64_boxed_closure0_closure1_closure0(self: rt.Poll<rt.u64>, f: (a0: rt.u64) => rt.u64): rt.Poll<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;
Expand Down
25 changes: 25 additions & 0 deletions ts/fixtures/task/expected/futures.Forward.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Generated by rust2-ts. Do not edit.

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

/**
* A future of the crate's that forwards its polls to a boxed one, as moq's session
* driver does.
*/
export class Forward {
future: rt.Future<rt.u64>;

constructor(fields: { future: rt.Future<rt.u64> }) {
this.future = fields.future;
}

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

export function poll$Future(self: Forward, cx: rt.Context): rt.Poll<rt.u64> {
return self.future.poll(cx);
}

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