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
5 changes: 2 additions & 3 deletions driver/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub struct Check<'a> {
pub target: Option<&'a str>,
/// Passed on to `cargo check`, such as `--features`.
pub args: &'a [String],
/// The environment the wrapper reads its job from.
/// The environment cargo runs in, which the wrapper reads its job from.
pub env: &'a [(&'a str, &'a OsStr)],
}

Expand All @@ -110,7 +110,7 @@ impl Check<'_> {
let cargo = |subcommand: &str| {
let mut cmd = cargo();
// Cargo reads its config from where it runs, so run where the package is.
cmd.current_dir(package_dir);
cmd.current_dir(package_dir).envs(self.env.iter().copied());
cmd.arg(subcommand)
.arg("--manifest-path")
.arg(self.manifest)
Expand All @@ -137,7 +137,6 @@ impl Check<'_> {
.args(self.args)
.env("RUSTC_WRAPPER", &exe)
.env(crate::rustc::RAN, &ran)
.envs(self.env.iter().copied())
.status()
.map_err(|err| format!("running cargo: {err}"))?;
if !status.success() {
Expand Down
1 change: 0 additions & 1 deletion quest/m2/ts-model/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ Public API: the translator's output for moq-net's model.
- [Drop in the TypeScript translator](/quest/m2/ts-drop.md) - kio closes a channel when its last producer drops
- [Shared ownership](/quest/m2/ts-shared.md) - kio's waiters are `Arc`/`Weak` with observable counts, and its atomics and locks are plain values
- [Translate safe dependencies with the package](/quest/m2/ts-model/crates.md) - kio, moq-clock, moq-pattern, and rangemap translate as one program with moq-net
- [Std combinators from core's MIR](/quest/m2/ts-model/core-mir.md) - `Option`'s and `Result`'s combinators lower from core's own bodies
- [Std gaps the model reaches](/quest/m2/ts-model/std-more.md) - ranges, wrappers, and `Vec`'s editing methods
- [Options of values that can be undefined](/quest/m2/ts-model/nested-option.md) - `Option<Option<T>>` and `Option<()>` keep `None` apart from `Some(None)`
- [Mutable references to values JS copies](/quest/m2/ts-model/mut-place.md) - a `&mut` to a number or an `Option` passed on or kept
Expand Down
44 changes: 0 additions & 44 deletions quest/m2/ts-model/core-mir.md

This file was deleted.

6 changes: 6 additions & 0 deletions ts/MIGRATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

- Translation builds std from source for `wasm32-unknown-unknown`, so the toolchain needs
its `rust-src` component, as the flake's has, and the first run compiles std.
- `Option`'s and `Result`'s combinators lower from core's MIR into `core/option.ts` and
`core/result.ts`, so the runtime no longer exports `optionMap`, `optionMapOr`,
`optionUnwrapOr`, `optionOkOr`, `resultOk`, `resultErr`, `resultMap`, `resultMapErr`,
`resultAndThen`, `resultInspectErr`, or `resultUnwrapOr`.
- `--census` starts from the config's roots, as translation does. For the census of every
item, census with a config that names no roots: `roots = []`.
- A struct is a plain object typed by an interface, unless it may drop: build one as a
Expand Down
38 changes: 27 additions & 11 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ rust2-ts --config ts.toml (--out dir | --census file.md) [--manifest-path Cargo.
```

It runs `cargo check` on the package with itself as `RUSTC_WRAPPER`, for
`wasm32-unknown-unknown`. Every crate compiles as rustc would; the configured library is
also translated, after type and borrow checking, starting from its roots and following
`wasm32-unknown-unknown`, with std built from source for it, unwinding and with every
function's MIR, which needs the toolchain's `rust-src`. Every crate compiles as rustc
would; the configured library is also translated, after type and borrow checking, starting from its roots and following
what they reach. A construct outside the subset is a compile error with its span, and
then nothing is written. A type without a JS value is refused where a value of it is made
or received, such as a parameter, a field, or a call, rather than again at each variable
Expand Down Expand Up @@ -60,7 +61,9 @@ and each type a module of its own beside it, holding the type and its functions
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`.
`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
`core/option.ts` holds `map$Option_u64_u64_rescale_closure0`.

| Rust | TypeScript |
| --- | --- |
Expand Down Expand Up @@ -188,12 +191,14 @@ 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`). The runtime functions for std's collections,
`Arc`, and the std functions that may discard an operand (`unwrap_or`, a map's `insert`)
take what drops each value they may drop, and drop it where std's do. So do iterators: one
that owns its items, as a `Vec`'s `into_iter` or a map's `drain` does, drops those it still
holds by `[Symbol.dispose]`, an adaptor drops what it skips or rejects, and a consumer that
takes an iterator by value drops it, a panic included. A `Drop` impl that
function of its type's own (`drop$Held`). `Option`'s and `Result`'s combinators 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
where std's do. So do iterators: one that owns its items, as a `Vec`'s `into_iter` or a
map's `drain` does, drops those it still holds by `[Symbol.dispose]`, an adaptor drops
what it skips or rejects, and a consumer that takes an iterator by value drops it, a
panic included. A `Drop` impl that
panics leaves the rest to drop as Rust's does: a class's glue and the runtime's droppers
go on to the rest of the value and its container, but a hash table leaks its other
entries, as std's does.
Expand Down Expand Up @@ -379,6 +384,15 @@ they need.
in the middle and becomes a loop over a `switch` on the next block. Temporaries
assigned once and read once fold back into the expression that reads them unless that
would reorder an effect.
- `Option`'s and `Result`'s combinators lower from core's own MIR rather than runtime
functions, each of which restated core's drops and unwinding by hand. The wasm32
sysroot aborts on panic, so its MIR has no cleanups: std builds from source with
`-C panic=unwind`, which took a cold check from 33 to 42 seconds. Each closure gets
its own instance, keeping call sites monomorphic, and instances that print the same are
merged into one. An `Ok` rebuilt from a stable `Result`'s payload is that `Result`,
without which Node ran the codec 1.31x slower. Against the runtime functions, the codec
and async handoff bundles are within 25 bytes gzipped, and `just ts bench` measures
them within the A/A route's spread in Node, Bun, Chrome, and Firefox.

## Never

Expand All @@ -402,7 +416,8 @@ format specs (a precision, `+`, `{:b}`, `{:x?}`, and widths from arguments), wri
what it captured, `mem::swap`, `replace`, or `take` of a reference's whole pointee,
assigning an `Arc`, a waker, a `MutexGuard`, a pinned reference, or a future through a
reference, a switch on or a field of another crate's type without a mapping, and std
functions without a mapping in `src/lower/library.rs`.
functions without a mapping in `src/lower/library.rs`, but for `Option`'s and `Result`'s
methods that take no `&mut`, which lower from core's MIR.

## Tests

Expand Down Expand Up @@ -430,7 +445,8 @@ built natively for wasm32 and run beside its translation on the same inputs by
only in another module, behind a type parameter, or through a box, formatting, methods
on generic types, iterators, with
closures that log what each
adaptor reads, and std's collections, over keys whose own `Hash`, `Eq`, and `Ord`
adaptor reads, `Option`'s and `Result`'s combinators, with closures that log each call,
and std's collections, over keys whose own `Hash`, `Eq`, and `Ord`
disagree with JS's, and `vec![x; n]` of types whose `Clone` counts its clones. Its differential test runs through `caller.ts`, a
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,
Expand Down
7 changes: 4 additions & 3 deletions ts/fixtures/codec/expected/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type * as EncodeError from "./coding/encode.EncodeError.ts";
import * as Encoder from "./coding/encode.Encoder.ts";
import { zigzag, unzigzag } from "./coding/varint.ts";
import * as Form from "./coding/varint.Form.ts";
import { unwrapOr$Option_u32, map$Option_u64_u64_encode_msg_closure0 } from "./core/option.ts";
import { form, liteVersion, putU64, putUsize, takeU64, decodeError, encodeError } from "./harness.ts";
import * as Group from "./lite/group.Group.ts";
import type * as Version from "./lite/version.Version.ts";
Expand Down Expand Up @@ -222,7 +223,7 @@ export function decodeSplit(data: rt.ByteSlice, version: Version.Version, out: r
short = rt.usizeAdd(short, 1);
continue l2;
}
return rt.Err(_32.error);
return _32 as rt.Result<undefined, DecodeError.DecodeError>;
}
}
}
Expand Down Expand Up @@ -283,7 +284,7 @@ export function ordered(data: rt.ByteSlice, out: rt.ByteVec): void {
const iterEnd2: rt.u32 = count2;
while ((_28 = iterStart2++) < iterEnd2) {
const _38: rt.u32 = _10(rt.u32Sub(rt.u32Sub(count2, 1), _28));
sum = rt.u64Add(sum, rt.optionUnwrapOr(rt.btreeGet(map, _38), 0) as rt.u64);
sum = rt.u64Add(sum, unwrapOr$Option_u32(rt.btreeGet(map, _38), 0) as rt.u64);
}
const _49: rt.Range<rt.u32> = new rt.Range(1 << 14, 2 << 14);
const iter3: rt.Iter<[rt.u32, rt.u32]> = rt.btreeRange(map, _49);
Expand Down Expand Up @@ -380,7 +381,7 @@ export function rescale(selector: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): v
scaled = bounded(rt.u128Div(_36, rt.u128Cast(from)));
}
} else {
const _44: rt.u64 | undefined = rt.optionMap(rt.u64CheckedMul(value, to), (scaled3: rt.u64): rt.u64 => {
const _44: rt.u64 | undefined = map$Option_u64_u64_encode_msg_closure0(rt.u64CheckedMul(value, to), (scaled3: rt.u64): rt.u64 => {
return rt.u64Div(scaled3, from);
});
if (_44 !== undefined) {
Expand Down
7 changes: 4 additions & 3 deletions ts/fixtures/codec/expected/coding/decode.Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as rt from "../rt.ts";
import * as DecodeError from "../coding/decode.DecodeError.ts";
import { read } from "../coding/varint.ts";
import type * as Form from "../coding/varint.Form.ts";
import { mapErr$Result_usize_TryFromIntError_DecodeError_bytes_closure0, inspectErr$Result_u8s_DecodeError_bytes_closure1 } from "../core/result.ts";
import type * as Control from "../stats.Control.ts";

/**
Expand Down Expand Up @@ -134,7 +135,7 @@ export function varint(self: Decoder): rt.Result<rt.u64, DecodeError.DecodeError
} else if (_2.error.tag === "Short") {
_0 = rt.Err(short(self, rt.usizeAdd(self.buf.length, 1)));
} else {
_0 = rt.Err(_2.error);
_0 = _2 as rt.Result<rt.u64, DecodeError.DecodeError>;
}
return _0;
}
Expand All @@ -153,11 +154,11 @@ export function bytes(self: Decoder): rt.Result<rt.ByteSlice, DecodeError.Decode
const start: rt.ByteSlice = self.buf;
const _6: rt.Result<rt.u64, DecodeError.DecodeError> = varint(self);
if (_6.ok) {
const _3: rt.Result<rt.usize, DecodeError.DecodeError> = rt.resultMapErr(rt.usizeTryFrom(_6.value), (_2: rt.TryFromIntError): DecodeError.DecodeError => {
const _3: rt.Result<rt.usize, DecodeError.DecodeError> = mapErr$Result_usize_TryFromIntError_DecodeError_bytes_closure0(rt.usizeTryFrom(_6.value), (_2: rt.TryFromIntError): DecodeError.DecodeError => {
return { tag: "BoundsExceeded" } as DecodeError.DecodeError;
});
if (_3.ok) {
return rt.resultInspectErr(slice(self, _3.value), (_22: DecodeError.DecodeError): void => {
return inspectErr$Result_u8s_DecodeError_bytes_closure1(slice(self, _3.value), (_22: DecodeError.DecodeError): void => {
self.buf = start;
});
}
Expand Down
6 changes: 4 additions & 2 deletions ts/fixtures/codec/expected/coding/encode.Encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import type * as Prefix from "../coding/encode.Prefix.ts";
import type * as PrefixKind from "../coding/encode.PrefixKind.ts";
import { write } from "../coding/varint.ts";
import type * as Form from "../coding/varint.Form.ts";
import { okOr$Option_u64_EncodeError } from "../core/option.ts";
import { mapErr$Result_u16_TryFromIntError_EncodeError_fill_closure0 } from "../core/result.ts";
import type * as Control from "../stats.Control.ts";

/**
Expand Down Expand Up @@ -58,7 +60,7 @@ export function varintOpt(self: Encoder, v: rt.u64 | undefined): rt.Result<undef
if (v === undefined) {
v2 = 0;
} else {
const _6: rt.Result<rt.u64, EncodeError.EncodeError> = rt.optionOkOr(rt.u64CheckedAdd(v, 1), "TooLarge" as EncodeError.EncodeError);
const _6: rt.Result<rt.u64, EncodeError.EncodeError> = okOr$Option_u64_EncodeError(rt.u64CheckedAdd(v, 1), "TooLarge" as EncodeError.EncodeError);
if (_6.ok) {
v2 = _6.value;
} else {
Expand Down Expand Up @@ -144,7 +146,7 @@ export function fill(self: Encoder, prefix: Prefix.Prefix): rt.Result<undefined,
return rt.Err(EncodeError.from$From_BoundsExceeded({}));
}
} else {
const _7: rt.Result<rt.u16, EncodeError.EncodeError> = rt.resultMapErr(rt.u16TryFrom(rt.usizeSub(end, body)), (_2: rt.TryFromIntError): EncodeError.EncodeError => {
const _7: rt.Result<rt.u16, EncodeError.EncodeError> = mapErr$Result_u16_TryFromIntError_EncodeError_fill_closure0(rt.u16TryFrom(rt.usizeSub(end, body)), (_2: rt.TryFromIntError): EncodeError.EncodeError => {
return "TooLarge" as EncodeError.EncodeError;
});
if (_7.ok) {
Expand Down
Loading