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
4 changes: 2 additions & 2 deletions quest/m2/ts-model/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Decided with the maintainer on 2026-10-27, from a census of the model's 22 publi

- This is an epic; each group of refusals is a child below. moq must work, but nothing in
the translator is specific to moq: dependencies follow general rules, and moq's own
changes (safe `cfg` twins of its `unsafe`, `dyn Expiry` as an enum, dropping or binding
`hashlink`, `slab`, and `smallvec`) are one research-tier quest in moq's tree.
changes (safe `cfg` twins of its `unsafe`, dropping or binding `hashlink`, `slab`, and
`smallvec`) are one research-tier quest in moq's tree.
- Until `unsafe` and trait objects translate, internal `unsafe` in moq-net and kio gets
safe `cfg` twins in moq, so differential tests cover it, and a dependency containing
`unsafe` is bound by the consumer. These are stopgaps: decided with the maintainer on
Expand Down
16 changes: 7 additions & 9 deletions quest/m3/ts-dyn.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@
## Goal

[rust2-ts](/ts/README.md) translates the trait objects moq uses that it still refuses
(`Arc<dyn Trait>`, `Box<dyn FnMut>` and `&dyn Fn`, and other crates' traits), with Rust's
(`Box<dyn FnMut>` and `&dyn Fn`, `Arc<dyn Any>`, and other crates' traits), 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 closures ([bindings](/ts/README.md#bindings)).
config, so trait objects move from never to not yet. Until this lands, moq binds its boxed
closures ([bindings](/ts/README.md#bindings)).

- `&dyn Debug` and `&dyn Display` translate as functions writing their value, `dyn Future`
as an `rt.Future`, and `&dyn Trait`, `&mut dyn Trait`, and `Box<dyn Trait>` of the
crate's own traits as an object with the trait's methods, which a class per type wraps
the value in. Build the rest on those.
- `Arc<dyn Trait>`: an `Arc` unsized from an `Arc<T>` shares its count, so the wrapper
can't replace the `Arc`'s value; its clone, drop, and deref must still see one count.
`Arc<dyn Future>` has the same problem.
as an `rt.Future`, and `&dyn Trait`, `&mut dyn Trait`, `Box<dyn Trait>`, and
`Arc<dyn Trait>` of the crate's own traits as an object with the trait's methods, which
a class per type wraps the value in; an `Arc`'s is an `rt.ArcDyn` sharing the count of
the `Arc` it was made from. Build the rest on those.
- Another crate's traits: `Fn*` objects can be the closure itself, as `&dyn Debug` is the
function writing it; `Iterator`, `Error`, `Any`, and `bytes::Buf` need their std
methods mapped onto the object.
Expand Down
17 changes: 10 additions & 7 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ in `rt/str.ts`, collections in `rt/collections.ts`, ownership, atomics, and asyn
| `Ordering`, `DefaultHasher` | `rt.Ordering`, which is `-1`, `0`, or `1`; `rt.Hasher` |
| `Duration` | `rt.Duration`: `{ secs, nanos }`, std's whole seconds as an `rt.u64` and the nanoseconds past them, never changed once made |
| closure | an arrow function, with `[Symbol.dispose]` if it owns a value that drops |
| `&dyn Trait`, `&mut dyn Trait`, `Box<dyn Trait>` | an object with the trait's methods, its supertraits' included: `shape.area$Shape()` |
| `&dyn Trait`, `&mut dyn Trait`, `Box<dyn Trait>`, `Arc<dyn Trait>` | an object with the trait's methods, its supertraits' included: `shape.area$Shape()`; behind an `Arc`, an `rt.ArcDyn` sharing the count of the `Arc` it was made from |
| constant | its value, inlined as Rust inlines it |
| a type that drops | a class with `[Symbol.dispose]`: its `Drop` impl, then its fields |
| a type's function | a function of the type's module, taking `self` first: `Decoder.slice(dec, 4)` after `import * as Decoder` |
Expand Down Expand Up @@ -234,18 +234,21 @@ panics leaves the rest to drop as Rust's does: a class's glue and the runtime's
go on to the rest of the value and its container, but a hash table leaks its other
entries, as std's does.

A trait object of the crate's own trait, behind `&`, `&mut`, or `Box`, is an object with
A trait object of the crate's own trait, behind `&`, `&mut`, `Box`, or `Arc`, is an object with
a method for each function a call can dispatch to, its supertraits' included, named for
its trait as an impl's function is and taking what follows `self`: `shape.area$Shape()`.
The trait's module exports its type, `Shape.Shape` from `dyns.Shape.ts`, which a TS
caller can implement. A coercion wraps the value in a class of its type's, `new
Shape.Square(sq)`, whose methods call that type's impl; one from a `&mut` to a value JS
copies holds its `rt.Place`, so `&mut self` writes where the value is. An upcast is the
same object. A `Box<dyn Trait>` drops what it holds through its `[Symbol.dispose]`, which
a TS caller's object may leave out. A trait object of another crate's trait, but for
`&dyn Debug`, `&dyn Display`, and `dyn Future`, of a generic trait or one with an
associated type, of a byte array, or behind any other pointer, such as an `Arc` or a
struct's last field, is refused.
a TS caller's object may leave out. An `Arc<T>` unsized to an `Arc<dyn Trait>`, or to an
`Arc<dyn Future>`, is an `rt.ArcDyn`: the object a box of its value would be, and the
`Arc<T>` it was made from, whose one count both sides' clones and drops change, and the
last of which drops the value. `Arc::ptr_eq` compares the `Arc<T>`s. A trait object of
another crate's trait, but for `&dyn Debug`, `&dyn Display`, and `dyn Future`, of a
generic trait or one with an associated type, of a byte array, or behind any other
pointer, such as a `Weak` or a struct's last field, is refused.

An `async fn` returns a future without running anything: an instance of a class of its
own (`sumFuture`), the state machine MIR's coroutine transform makes. Its fields are the
Expand Down Expand Up @@ -462,7 +465,7 @@ they need.

`unsafe` and raw pointers ([unsafe](/quest/m3/ts-unsafe.md)), and trait objects other than
`&dyn Debug`, `&dyn Display`, `dyn Future`, and the crate's own traits' behind `&`, `&mut`,
or `Box` ([trait objects](/quest/m3/ts-dyn.md)), fail translation. Until they translate, an item that needs one, such as a boxed closure, binds to
`Box`, or `Arc` ([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
10 changes: 9 additions & 1 deletion ts/fixtures/lang/caller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,12 @@ const cellScripts = [
];
const cells = Array.from({ length: 256 }, (_, kind) => cellScripts.map((script) => Uint8Array.of(cellsOp, kind, ...script))).flat();

export const cases: Uint8Array[] = [...loops, ...durations, ...ints(), ...utf8, ...floats, ...dyns, ...cells];
/**
* `dyns::shared` too short to run, and with `Arc`s of each type, the last count of each at
* zero and at the top of its range.
*/
const shared = [[], [1], [0, 0], [255, 255, 0, 1, 2, 3, 4, 5], [7, 0xff, 255, 254, 253]].map((bytes) =>
Uint8Array.of(op("dyns::shared"), 0, ...bytes),
);

export const cases: Uint8Array[] = [...loops, ...durations, ...ints(), ...utf8, ...floats, ...dyns, ...cells, ...shared];
14 changes: 11 additions & 3 deletions ts/fixtures/lang/expected/core/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { copy$u8s_u8s } from "../lib.ts";
import type * as Nibble from "../collections.Nibble.ts";
import type * as Rank from "../collections.Rank.ts";
import type * as Tally from "../collections.Tally.ts";
import type * as Shape from "../dyns.Shape.ts";
import { side, order } from "../keys.ts";
import type * as Hop from "../keys.Hop.ts";
import type * as Side from "../keys.Side.ts";
Expand All @@ -13,7 +14,7 @@ import { from$From_Dir_u8 } from "../shapes.ts";
import type * as Dir from "../shapes.Dir.ts";
import type * as Meters from "../shapes.Meters.ts";
import type * as Point from "../shapes.Point.ts";
import type * as Shape from "../shapes.Shape.ts";
import type * as shapes_Shape from "../shapes.Shape.ts";
import type * as Short from "../splits.Short.ts";

export function isSomeAnd$Option_u8_bools_closure0(self: rt.u8 | undefined, f: (a0: rt.u8) => boolean): boolean {
Expand Down Expand Up @@ -135,7 +136,14 @@ export function unwrapOrDefault$Option_u8(self: rt.u8 | undefined): rt.u8 {
return self;
}

export function map$Option_Dir_Shape_rematched_closure1(self: Dir.Dir | undefined, f: (a0: Dir.Dir) => Shape.Shape): Shape.Shape | undefined {
export function map$Option_Arc_dyn_Shape_u32_shared_closure0(self: rt.Arc<Shape.Shape> | undefined, f: (a0: rt.Arc<Shape.Shape>) => rt.u32): rt.u32 | undefined {
if (self === undefined) {
return undefined;
}
return f(self);
}

export function map$Option_Dir_Shape_rematched_closure1(self: Dir.Dir | undefined, f: (a0: Dir.Dir) => shapes_Shape.Shape): shapes_Shape.Shape | undefined {
if (self === undefined) {
return undefined;
}
Expand Down Expand Up @@ -317,7 +325,7 @@ export function mapOr$Option_Rank_Tally_u8_hash_maps_closure11(self: [Rank.Rank,
return f(self);
}

export function mapOr$Option_Shape_u8_rematched_closure0(self: Shape.Shape | undefined, default_: rt.u8, f: (a0: Shape.Shape) => rt.u8): rt.u8 {
export function mapOr$Option_Shape_u8_rematched_closure0(self: shapes_Shape.Shape | undefined, default_: rt.u8, f: (a0: shapes_Shape.Shape) => rt.u8): rt.u8 {
if (self === undefined) {
return default_;
}
Expand Down
38 changes: 38 additions & 0 deletions ts/fixtures/lang/expected/dyns.Hits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Generated by rust2-ts. Do not edit.

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

/** Provided, and overridden by one impl. */
export function sides$Shape(_self: Hits): rt.u8 {
return 4;
}

export function intoArea$Shape(self: Hits): rt.u32 {
return rt.u32WrappingAdd(area$Shape(self), 1);
}

/** Over early-bound lifetimes, which its signature takes as arguments too. */
export function larger$Shape(self: Hits, other: rt.u32): rt.u32 {
return rt.max(area$Shape(self), other);
}

/** Writes through a `&mut` to a number, which is an `rt.Place`. */
export function tally$Shape(self: Hits, sum: rt.Place<rt.u32>): void {
sum.v = rt.u32WrappingAdd(sum.v, area$Shape(self));
}

/** Counts its calls, which every `Arc` of it sees. */
export interface Hits {
_0: rt.Atomic<rt.u32>;
}

export function area$Shape(self: Hits): rt.u32 {
return rt.atomicFetch(self._0, rt.u32WrappingAdd, 1, 0);
}

export function grow$Shape(self: Hits, by: rt.u8): void {
const _3: rt.u32 = rt.u32WrappingAdd(self._0.v, by as rt.u32);
self._0.v = _3;
}

//# sourceMappingURL=dyns.Hits.ts.map
33 changes: 33 additions & 0 deletions ts/fixtures/lang/expected/dyns.Shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,42 @@

import * as rt from "./rt.ts";
import { sides$Shape_u32, intoArea$Shape_u32, larger$Shape_u32, tally$Shape_u32, area$Shape_u32, grow$Shape_u32 } from "./dyns.ts";
import * as dyns_Hits from "./dyns.Hits.ts";
import * as dyns_Square from "./dyns.Square.ts";
import * as dyns_Tri from "./dyns.Tri.ts";

export class Hits {
v: dyns_Hits.Hits;

constructor(v: dyns_Hits.Hits) {
this.v = v;
}

area$Shape(): rt.u32 {
return dyns_Hits.area$Shape(this.v);
}

grow$Shape(a: rt.u8): void {
return dyns_Hits.grow$Shape(this.v, a);
}

intoArea$Shape(): rt.u32 {
return dyns_Hits.intoArea$Shape(this.v);
}

larger$Shape(a: rt.u32): rt.u32 {
return dyns_Hits.larger$Shape(this.v, a);
}

sides$Shape(): rt.u8 {
return dyns_Hits.sides$Shape(this.v);
}

tally$Shape(a: rt.Place<rt.u32>): void {
return dyns_Hits.tally$Shape(this.v, a);
}
}

export type Shape =
| { area$Shape: () => rt.u32; grow$Shape: (a0: rt.u8) => void; sides$Shape: () => rt.u8; intoArea$Shape: () => rt.u32; larger$Shape: (a0: rt.u32) => rt.u32; tally$Shape: (a0: rt.Place<rt.u32>) => void };

Expand Down
Loading