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
1 change: 0 additions & 1 deletion quest/m2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ deletes the quests that follow it.
## Required

- [Async and drop spike](/quest/m2/ts-async-spike.md) - generators from THIR against MIR state machines, deciding async, drop points, and the lowering
- [Wide integer spike](/quest/m2/ts-wide-ints.md) - `bigint` against boxed and unboxed hi/lo picks the 64- and 128-bit representation
- [Cheaper byte slices](/quest/m2/ts-byte-views.md) - splitting a `&[u8]` stops making a `Uint8Array` view per split
- [Drop in the TypeScript translator](/quest/m2/ts-drop.md) - a `Drop` type's `drop()` runs where Rust drops it
- [Async in the TypeScript translator](/quest/m2/ts-async.md) - async code translates with Rust's lazy futures
Expand Down
4 changes: 0 additions & 4 deletions quest/m2/ts-byte-views.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,3 @@ faster, and V8 gives a lone small array its own buffer, about a microsecond,
the first time it is viewed. Measure Node, Bun, and Chrome.

Public API: the translator's output for byte slices.

## Related

- [Wide integer spike](/quest/m2/ts-wide-ints.md) - the other half of decode's remaining gap
9 changes: 3 additions & 6 deletions quest/m2/ts-collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ by the translated `Ord`, and `VecDeque` a ring buffer.

- `HashMap` iteration order is unspecified in Rust, so insertion order is a
valid refinement; differential tests must not compare it.
- Which keys are primitive depends on the
[wide-integer representation](/quest/m2/ts-wide-ints.md): a hi/lo `u64` is
not one.
- A 64- or 128-bit integer is a number while it is a safe integer, and an object
of 32-bit limbs past that ([representation](/ts/README.md#design)), so it is a
primitive key only while it is a number.
- Each collection's iterators (`iter`, `values`, `range`, `drain`) are `rt.Iter`s,
as `rt/iter.ts` makes std's, so the adaptors over them already translate. moq-net's
`BTreeMap::range` takes `RangeFrom` and `RangeTo`, which are not values yet.

Public API: the translator's output for collections.

## Required

- [Wide integer spike](/quest/m2/ts-wide-ints.md) - which keys lower to primitives
2 changes: 1 addition & 1 deletion quest/m2/ts-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Decided with the maintainer on 2026-10-23, for the subset:
- Out for good: `unsafe` and raw pointers (all in `model/frame.rs`'s `Segment`),
and trait objects except at boundaries (`Box<dyn Future>`, transport erasure).
Those bind to hand-written TS through config instead of translating.
- `u128` stays, on the [wide-integer](/quest/m2/ts-wide-ints.md) representation.
- `u128` stays, on the [wide-integer](/ts/README.md#design) representation.
- What the census (`just ts census`, nightly) still ranks after the required quests
(`Bytes`, `Duration`/`Instant`, cross-crate generics) lands here or splits into
its own quest.
Expand Down
33 changes: 0 additions & 33 deletions quest/m2/ts-wide-ints.md

This file was deleted.

37 changes: 32 additions & 5 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ runtime: what std does that JS does not, with iterators in `rt/iter.ts`.
| Rust | TypeScript |
| --- | --- |
| `bool`, `str`, `String` | `boolean`, `string` |
| `u8`-`u32`, `i8`-`i32`, `usize`, `isize` | `number`, checked: overflow panics |
| `u64`, `i64`, `u128`, `i128` | `bigint`, checked the same way |
| `u8`-`u32`, `i8`-`i32`, `usize`, `isize` | `rt.u8` and so on: a `number` branded with its type, checked: overflow panics |
| `u64`, `i64`, `u128`, `i128` | `rt.u64` and so on: a branded `number` while a safe integer, 32-bit limbs past 2^53, checked the same way |
| `struct` | a class; methods take `this`, trait methods included |
| `enum` without data | a union of strings: `"Lite01" \| "Lite02"`, or `never` without variants |
| `enum` | a union of objects tagged by variant, with a companion object of functions |
Expand All @@ -69,6 +69,18 @@ runtime: what std does that JS does not, with iterators in `rt/iter.ts`.
a debug build checks it, so translated code panics where native code would: it never
wraps silently. A panic throws `rt.Panic`, with the message native Rust panics with.

Each integer type is its own type to `tsc`, named as Rust's: `rt.u8` is a `number` with a
type-only brand, so a `u16` where a `u8` goes fails the type check, while a plain
number, such as a literal, passes for any. A cast that keeps the value is `x as rt.u16`,
which costs nothing at run time.

A wider integer is a `number` up to 2^53 - 1 in magnitude, and past that an immutable
object of 32-bit limbs, `{ hi, lo }`, with `w2` and `w1` between them at 128 bits and
`hi` signed in a signed type. Each value has one form, so `x === 0` is exact; a caller
makes one past 2^53 with `rt.u64.of(18446744073709551615n)`. Its other operators and
comparisons are runtime calls, `rt.u64.add(a, b)` and `rt.u64.lt(a, b)`, and `x as u32`
is `rt.u64.low(x)`.

`format!`, `write!`, `panic!`, `assert!`, and the other macros of `format_args!`
translate, as do `Display` and `Debug` impls, derived and thiserror's included. The
runtime ports `core::fmt`, so the text matches native Rust's byte for byte. A placeholder
Expand Down Expand Up @@ -109,8 +121,20 @@ Imports name the `.ts` file, which Node, Bun, and bundlers run as is. A `tsc` bu

## Design

- 64-bit integers are native `bigint`: lossless and readable. A pair of `u32` halves is
faster but waits until `just ts bench` measures `bigint` as the cost.
- 64- and 128-bit integers are numbers while they are safe integers, which most values
are: lengths, IDs, sequence numbers, and timestamps. `just ts bench` measured it
against `bigint`, against limbs for every value, and against unboxed halves, two
numbers in locals, fields, and arguments. It ties unboxed halves on the codec and
beats `bigint` there in Node, Bun, and Chrome: varint encode 13-65% faster, frame
decode 8-32%, and message decode 22-31%, though Chrome's 8-byte varint decode is
25-45% slower. A loop of sums and comparisons past 2^32 measured 2-6x faster. Limbs
for every value allocate an object each, which costs what a `bigint` does in V8, so
they lost decode; unboxed halves would lower every value to two. Past 2^53 an
operation allocates limbs: a product past 2^53 divided back down is 1.5-1.9x slower
than `bigint`, and 128-bit arithmetic there goes through `bigint` inside the runtime.
The runtime adds 6 KB to the codec's bundle, 2 KB gzipped. Brands tell `u64` from
`i64` and `u32` from `usize` at no cost, since TypeScript erases them; a required
brand would cast every literal, so a plain number passes for any type.
- A generic type is one class or union, the type TS callers name, and a method on it is
one instance's, typed by its `this`. A class per instance (`Wrap$Pair`) would rename
the type for callers, and TS generics with trait dictionaries would undo
Expand Down Expand Up @@ -164,7 +188,10 @@ built natively for wasm32 and run beside its translation on the same inputs by
- `lang` covers every integer width's arithmetic and casts, control flow, patterns,
`Copy` aggregates, formatting, methods on generic types, and iterators, with closures
that log what each adaptor reads. Its differential test runs through `caller.ts`, a
hand-written caller that also prints a `printable` error nothing translated prints.
hand-written caller that also prints a `printable` error nothing translated prints,
and adds every 64- and 128-bit case over each pair of values next to a power of two,
where the runtime switches between numbers and limbs, and its limbs carry and borrow.
`brands.ts`, another caller, mixes integer types where `tsc` must refuse them.
- `reject` holds constructs outside the subset; its expected errors are `expected.txt`,
and its expected census is `census.md`.

Expand Down
48 changes: 24 additions & 24 deletions ts/fixtures/codec/expected/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function encodeVarints(data: Uint8Array, form_: Form, out: rt.ByteVec): r
const [count, data2] = takeU64(data);
const [value, ] = takeU64(data2);
let w: Encoder = Encoder.new(out, form_);
const end = Number(BigInt.asUintN(32, count));
const end = rt.u64.low(count);
for (let _ = 0; _ < end; _++) {
const varint = w.varint(value);
if (!varint.ok) {
Expand All @@ -73,8 +73,8 @@ export function encodeVarints(data: Uint8Array, form_: Form, out: rt.ByteVec): r
/** Every varint on the wire: how many, and the last. */
export function decodeVarints(wire: Uint8Array, form_: Form, out: rt.ByteVec): rt.Result<undefined, DecodeError> {
let r: Decoder = Decoder.new(wire, form_);
let count: number = 0;
let last: bigint = 0n;
let count: rt.usize = 0;
let last: rt.u64 = 0;
while (!r.isEmpty()) {
const varint = r.varint();
if (!varint.ok) {
Expand All @@ -100,14 +100,14 @@ export function encodeFrames(data: Uint8Array, version: Version, out: rt.ByteVec
const [size, data5] = takeU64(data4);
const [keyEvery, data6] = takeU64(data5);
const [keySize, ] = takeU64(data6);
const [delta2, keyEvery2] = [BigInt.asIntN(64, delta), Number(BigInt.asUintN(32, keyEvery))] as [bigint, number];
const [delta2, keyEvery2] = [rt.i64.cast(delta), rt.u64.low(keyEvery)] as [rt.i64, rt.usize];
let w: Encoder = Encoder.new(out, Form.from(version));
const end = Number(BigInt.asUintN(32, count));
const end = rt.u64.low(count);
for (let n = 0; n < end; n++) {
let value: [bigint, bigint];
let value: [rt.i64, rt.u64];
arms: {
if (n === 0) {
value = [0n, first];
value = [0, first];
break arms;
}
const n2 = n;
Expand All @@ -133,9 +133,9 @@ export function encodeFrames(data: Uint8Array, version: Version, out: rt.ByteVec
/** Every FRAME header on the wire: how many, their total size, and the last delta. */
export function decodeFrames(wire: Uint8Array, version: Version, out: rt.ByteVec): rt.Result<undefined, DecodeError> {
let r: Decoder = Decoder.new(wire, Form.from(version));
let count: number = 0;
let total: bigint = 0n;
let last: bigint = 0n;
let count: rt.usize = 0;
let total: rt.u64 = 0;
let last: rt.i64 = 0;
while (!r.isEmpty()) {
const varint = r.varint();
if (!varint.ok) {
Expand All @@ -151,54 +151,54 @@ export function decodeFrames(wire: Uint8Array, version: Version, out: rt.ByteVec
}
putUsize(out, count);
putU64(out, total);
putU64(out, BigInt.asUintN(64, last));
putU64(out, rt.u64.cast(last));
return rt.Ok(undefined);
}

/** One size-prefixed GROUP header, starting at the first frame. */
export function encodeGroup(data: Uint8Array, version: Version, out: rt.ByteVec): rt.Result<undefined, EncodeError> {
const [subscribe, data2] = takeU64(data);
const [sequence, ] = takeU64(data2);
const group = new Group({ subscribe, sequence, frameStart: 0n });
const group = new Group({ subscribe, sequence, frameStart: 0 });
return group.encode(Encoder.new(out, Form.from(version)), version);
}

/**
* One iterator chain over every byte, picked by `chain`, with closures that cost little
* next to the iterator protocol they run through.
*/
export function iterate(chain: number, data: Uint8Array, out: rt.ByteVec): void {
export function iterate(chain: rt.u8, data: Uint8Array, out: rt.ByteVec): void {
if (chain === 0) {
putUsize(out, rt.iter.slice(data).map((arg: number): number => {
putUsize(out, rt.iter.slice(data).map((arg: rt.u8): rt.u8 => {
const b = arg;
return b >> 1;
}).filter((arg2: number): boolean => {
}).filter((arg2: rt.u8): boolean => {
const b2 = arg2;
return (b2 & 1) === 0;
}).count());
} else if (chain === 1) {
out.push(rt.iter.slice(data).any((arg3: number): boolean => {
out.push(rt.iter.slice(data).any((arg3: rt.u8): boolean => {
const b3 = arg3;
return b3 === 0xff;
}) ? 1 : 0);
} else if (chain === 2) {
const odd = rt.iter.slice(data).filter((arg4: number): boolean => {
const odd = rt.iter.slice(data).filter((arg4: rt.u8): boolean => {
const b4 = arg4;
return (b4 & 1) === 1;
}).toByteVec();
putUsize(out, odd.length);
} else if (chain === 3) {
putU64(out, BigInt(rt.iter.slice(data).map((arg5: number): number => {
putU64(out, rt.iter.slice(data).map((arg5: rt.u8): rt.u32 => {
const b5 = arg5;
return b5;
}).sum(rt.u32.add, 0)));
return b5 as rt.u32;
}).sum(rt.u32.add, 0) as rt.u64);
} else if (chain === 4) {
putU64(out, BigInt(rt.iter.slice(data).skip(16).take(512).fold(0, (acc: number, arg6: number): number => {
putU64(out, rt.iter.slice(data).skip(16).take(512).fold(0, (acc: rt.u32, arg6: rt.u8): rt.u32 => {
const b6 = arg6;
return (acc ^ b6) >>> 0;
})));
return (acc ^ b6 as rt.u32) >>> 0;
}) as rt.u64);
} else {
let last: number = 0;
let last: rt.usize = 0;
const iter = rt.iter.slice(data).enumerate();
while (true) {
const item = iter.next();
Expand Down
Loading