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 @@ -27,7 +27,6 @@ The fork's feature quests folded into the [exporter](/quest/m1/ffi-export/README
## Required

- [Byte-slice locals that crash the translator](/quest/m2/ts-scalar-ice.md) - `take_apart` never reaches the arm `find_scalars` promised it wouldn't
- [Fixture cases by name](/quest/m2/ts-harness-names.md) - adding a harness case never renumbers another
- [Byte slices without objects](/quest/m2/ts-byte-views/README.md) - reading through a translated cursor allocates no object per read
- [Async in the TypeScript translator](/quest/m2/ts-async.md) - moq-net's and kio's async code translates with Rust's lazy futures
- [Shared ownership](/quest/m2/ts-shared.md) - `Arc`/`Rc`/`Weak` keep exact counts, and locks and atomics become plain values
Expand Down
24 changes: 0 additions & 24 deletions quest/m2/ts-harness-names.md

This file was deleted.

31 changes: 11 additions & 20 deletions ts/fixtures/codec/expected/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,21 @@ import * as Group from "./lite/group.Group.ts";
import * as Probe from "./lite/probe.Probe.ts";
import type * as Version from "./lite/version.Version.ts";

/**
* Each case, which the first byte picks modulo their count, so a case added at the end
* moves no other.
*/
export function cases(): Array<(a0: rt.ByteSlice, a1: rt.ByteVec) => void> {
return [decodeScript, encodeScript, zigzag, sizes, roundtrip$Versions, roundtripLite$Group, roundtripLite$Probe, encodeMessages, errors];
}

/** Run one case. */
export function run(input: rt.ByteSlice): rt.ByteVec {
const out: rt.ByteVec = new rt.ByteVec();
if (input.length !== 0) {
const _8: rt.u8 = rt.u8Rem(input.data[input.start], 9);
if (_8 === 0) {
decodeScript(new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out);
} else if (_8 === 1) {
encodeScript(new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out);
} else if (_8 === 2) {
zigzag(new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out);
} else if (_8 === 3) {
sizes(new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out);
} else if (_8 === 4) {
roundtrip$Versions(new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out);
} else if (_8 === 5) {
roundtripLite$Group(new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out);
} else if (_8 === 6) {
roundtripLite$Probe(new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out);
} else if (_8 === 7) {
encodeMessages(new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out);
} else {
errors(new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out);
}
const op: rt.u8 = input.data[input.start];
const cases_: Array<(a0: rt.ByteSlice, a1: rt.ByteVec) => void> = cases();
cases_[rt.idx(cases_, rt.usizeRem(op as rt.usize, cases_.length))](new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out);
return out;
}
return out;
Expand Down
33 changes: 21 additions & 12 deletions ts/fixtures/codec/src/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,33 @@ use crate::coding::{
};
use crate::{SessionError, lite};

/// What a case runs on the rest of the input.
type Case = fn(&[u8], &mut Vec<u8>);

/// Each case, which the first byte picks modulo their count, so a case added at the end
/// moves no other.
fn cases() -> Vec<Case> {
vec![
decode_script,
encode_script,
zigzag,
sizes,
roundtrip::<Versions>,
roundtrip_lite::<lite::Group>,
roundtrip_lite::<lite::Probe>,
encode_messages,
errors,
]
}

/// Run one case.
pub fn run(input: &[u8]) -> Vec<u8> {
let mut out = Vec::new();
let Some((&op, data)) = input.split_first() else {
return out;
};

match op % 9 {
0 => decode_script(data, &mut out),
1 => encode_script(data, &mut out),
2 => zigzag(data, &mut out),
3 => sizes(data, &mut out),
4 => roundtrip::<Versions>(data, &mut out),
5 => roundtrip_lite::<lite::Group>(data, &mut out),
6 => roundtrip_lite::<lite::Probe>(data, &mut out),
7 => encode_messages(data, &mut out),
_ => errors(data, &mut out),
}
let cases = cases();
cases[usize::from(op) % cases.len()](data, &mut out);
out
}

Expand Down
106 changes: 95 additions & 11 deletions ts/fixtures/lang/caller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// `harness::entry`, from the output directory.

import * as Fault from "./fmt.Fault.ts";
import { run as harness } from "./harness.ts";
import { cases as named, run as harness, op } from "./harness.ts";
import * as rt from "./rt.ts";

export function run(input: rt.ByteSlice): rt.ByteVec {
Expand All @@ -25,7 +25,7 @@ export function run(input: rt.ByteSlice): rt.ByteVec {
}

/** `shapes::loops` with each first byte that one of its cases of `!` takes, with one more byte and without. */
const loops = [0xf1, 0xf3, 0xf4, 0xf5, 0xf6, 0xf9, 0xfa].flatMap((b) => [Uint8Array.of(16, 0, b), Uint8Array.of(16, 0, b, 1)]);
const loops = [0xf1, 0xf3, 0xf4, 0xf5, 0xf6, 0xf9, 0xfa].flatMap((b) => [Uint8Array.of(op("shapes::loops"), 0, b), Uint8Array.of(op("shapes::loops"), 0, b, 1)]);

/** A `Duration`'s bytes as `durations` takes them: its seconds, then its nanoseconds. */
function duration(secs: bigint, nanos: number): number[] {
Expand All @@ -43,11 +43,12 @@ function duration(secs: bigint, nanos: number): number[] {
*/
const edges = [0n, 1n, (1n << 53n) - 1n, 1n << 53n, (1n << 64n) - 1n].flatMap((secs) => [0, 1, 999_999_999].map((nanos) => duration(secs, nanos)));
const factors = [0, 1, 3, 1000, 9_007_199, 1_000_000_000, 0xffffffff].map((n) => [n >>> 24, (n >>> 16) & 0xff, (n >>> 8) & 0xff, n & 0xff]);
const durationsOp = op("durations::durations");
const durations = [
...[0n, 1n, 1n << 32n, (1n << 53n) - 1n, 1n << 53n, (1n << 64n) - 2n, (1n << 64n) - 1n].flatMap((secs) =>
[0, 1, 999_999_999, 1_000_000_000, 0xffffffff].flatMap((nanos) => [0, 1].map((kind) => Uint8Array.of(47, kind, ...duration(secs, nanos)))),
[0, 1, 999_999_999, 1_000_000_000, 0xffffffff].flatMap((nanos) => [0, 1].map((kind) => Uint8Array.of(durationsOp, kind, ...duration(secs, nanos)))),
),
...edges.flatMap((a) => edges.flatMap((b) => factors.flatMap((n) => [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13].map((kind) => Uint8Array.of(47, kind, ...a, ...b, ...n))))),
...edges.flatMap((a) => edges.flatMap((b) => factors.flatMap((n) => [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13].map((kind) => Uint8Array.of(durationsOp, kind, ...a, ...b, ...n))))),
// Each side of where minutes, hours, and a `u128` of nanoseconds pass `u64::MAX` seconds.
...[2n ** 64n / 60n, 2n ** 64n / 3600n].flatMap((units) => [units, units + 1n].map((n) => wide(n << 64n))),
...[(7n << 64n) + 999_999_999n, 2n ** 64n * 1_000_000_000n - 1n, 2n ** 64n * 1_000_000_000n, 2n ** 128n - 1n].map((n) => wide(n)),
Expand All @@ -56,7 +57,7 @@ const durations = [
/** `durations`' larger units, of a `u128` whose high half is also its `u64`. */
function wide(n: bigint): Uint8Array {
const bytes = new Uint8Array(18);
bytes.set([47, 12]);
bytes.set([durationsOp, 12]);
const view = new DataView(bytes.buffer);
view.setBigUint64(2, n >> 64n);
view.setBigUint64(10, n & 0xffff_ffff_ffff_ffffn);
Expand Down Expand Up @@ -86,23 +87,26 @@ const doubles: (number | bigint)[] = [
];
/** Integers whose `f64` rounds, as `floats`' kind 2 takes them: 16 bytes of a `u128`. */
const wides = [0n, 1n, (1n << 53n) + 1n, (1n << 53n) + 3n, (1n << 54n) + 2n, (1n << 63n) + 1025n, (1n << 64n) - 1n, (1n << 64n) - 1025n, (1n << 64n) + (1n << 11n) + 1n, (1n << 127n) + 1n, (1n << 128n) - 1n, (1n << 128n) - (1n << 74n), -(1n << 100n) - 1n];
const floatsOp = op("floats::floats");
/** How many cases the first byte picks among, modulo. */
const count = named().length;
const floats = [
...Array.from({ length: 10 }, (_, kind) => doubles.flatMap((a) => doubles.map((b) => Uint8Array.of(58, kind, ...double(a), ...double(b), 1)))).flat(),
...Array.from({ length: 10 }, (_, kind) => doubles.flatMap((a) => doubles.map((b) => Uint8Array.of(floatsOp, kind, ...double(a), ...double(b), 1)))).flat(),
// `from_secs_f64`, `mul_f64`, and `div_f64` panicking, and `Duration::MAX.mul_f64`.
...[18, 28, 38].flatMap((kind) => doubles.map((a) => Uint8Array.of(58, kind, ...double(a), ...double(a), 3))),
...[18, 28, 38].flatMap((kind) => doubles.map((a) => Uint8Array.of(floatsOp, kind, ...double(a), ...double(a), 3))),
...wides.map((v) => {
const bytes = new Uint8Array(16);
new DataView(bytes.buffer).setBigUint64(0, BigInt.asUintN(64, v >> 64n));
new DataView(bytes.buffer).setBigUint64(8, BigInt.asUintN(64, v));
return Uint8Array.of(58, 2, ...bytes);
return Uint8Array.of(floatsOp, 2, ...bytes);
}),
];

/** How many ULPs from native Rust's each `f64` of `floats`' kind 7 may be, which Rust leaves to libm. */
const ULPS = 2n;

export function within(input: Uint8Array, native: Uint8Array, translated: Uint8Array): boolean {
if (input[0] % 59 !== 58 || input[1] % 10 !== 7 || native.length !== translated.length) return false;
if (input[0] % count !== floatsOp || input[1] % 10 !== 7 || native.length !== translated.length) return false;
const [a, b] = [native, translated].map((v) => new DataView(v.buffer, v.byteOffset, v.length));
// Bits as a line through zero, so the distance between two is how many doubles apart they are.
const line = (v: bigint) => (v < 0n ? -(v & 0x7fffffffffffffffn) : v);
Expand All @@ -113,6 +117,86 @@ export function within(input: Uint8Array, native: Uint8Array, translated: Uint8A
return true;
}

/**
* The 64- and 128-bit cases over operands that random bytes rarely make: every pair of
* values next to a power of two.
*/
function ints(): Uint8Array[] {
const [u64, u128, i64, i128] = ["ints::u64_op", "ints::u128_op", "ints::i64_op", "ints::i128_op"].map(op);
const [neg, cast, convert, fmtInts] = ["ints::neg", "ints::cast", "ints::convert", "fmt::ints"].map(op);
const cases: Uint8Array[] = [];
// 2^k - 1, 2^k, and 2^k + 1 for each k where a limb, a safe number, or a sign ends,
// and values without that structure, whose quotients and remainders are not round.
const edges = new Set<bigint>([
1_000_000n,
0x9e3779b97f4a7c15n,
0x0123456789abcdefn,
0xfedcba9876543210n,
// Safe integers whose halves carry when multiplied, and a negative one.
0x1d3c5b7a9f8e6dn,
-0x1b2a3c4d5e6f71n & 0xffffffffffffffffn,
]);
for (const k of [0n, 1n, 16n, 21n, 31n, 32n, 33n, 52n, 53n, 54n, 63n, 64n]) {
for (const delta of [-1n, 0n, 1n]) edges.add(((1n << k) + delta) & 0xffffffffffffffffn);
}
// Small negative numbers, -2^32, and where negative safe numbers end, as signed values.
for (const k of [1n, 32n, 53n]) {
for (const delta of [-1n, 0n, 1n]) edges.add((-(1n << k) + delta) & 0xffffffffffffffffn);
}
const values = [...edges];
const be = (v: bigint) => {
const bytes = new Uint8Array(8);
new DataView(bytes.buffer).setBigUint64(0, v);
return [...bytes];
};
// Shift amounts in and past each limb, and past the width, which panics.
const shifts = [0, 1, 7, 31, 32, 33, 63, 64, 65, 95, 96, 127, 128, 200];
let i = 0;
for (const x of values) {
for (const y of values) {
// A 64-bit case's operands are `x` and `y`. A 128-bit case's are `x:y` and `u:v`,
// whose high halves are equal a third of the time, so the low half decides.
const u = i % 3 === 0 ? x : values[(i * 7) % values.length];
const v = values[(i * 13 + 5) % values.length];
const shift = shifts[i++ % shifts.length];
for (let kind = 0; kind < 30; kind++) {
for (const o of [u64, i64]) cases.push(Uint8Array.of(o, kind, ...be(x), ...be(y), shift));
for (const o of [u128, i128]) cases.push(Uint8Array.of(o, kind, ...be(x), ...be(y), ...be(u), ...be(v), shift));
}
// `clamp` between two values of the sweep, whose third operand follows the shift's place.
for (const o of [u64, i64]) cases.push(Uint8Array.of(o, 26, ...be(x), ...be(y), ...be(u)));
for (const o of [u128, i128]) cases.push(Uint8Array.of(o, 26, ...be(x), ...be(y), ...be(u), ...be(v), ...be(y), ...be(x)));
// Negating an `i64` and an `i128`.
cases.push(Uint8Array.of(neg, 3, ...be(x)), Uint8Array.of(neg, 4, ...be(x), ...be(y)));
// `x * y`, `checked_mul`, `saturating_mul`, and `wrapping_mul` at 128 bits, extended in
// each type's sign: a product of two numbers, past 2^53, multiplies their halves.
const signs = (v: bigint) => (v >> 63n === 1n ? 0xffffffffffffffffn : 0n);
for (const kind of [2, 13, 24, 25]) {
cases.push(Uint8Array.of(u128, kind, ...be(0n), ...be(x), ...be(0n), ...be(y)));
cases.push(Uint8Array.of(i128, kind, ...be(signs(x)), ...be(x), ...be(signs(y)), ...be(y)));
}
// `x:y / d`, `x:y % d`, `div_ceil`, and `is_multiple_of` at 128 bits by a divisor
// below 2^32 in magnitude, which divides limbs by a number, extended in each type's
// sign: -1 overflows `i128::MIN`.
for (const d of [3n, 1_000_000n, 0xffffffffn, -1n, -0xffffffffn]) {
for (const kind of [3, 4, 27, 28]) {
for (const o of [u128, i128]) cases.push(Uint8Array.of(o, kind, ...be(x), ...be(y), ...be(signs(d & 0xffffffffffffffffn)), ...be(d & 0xffffffffffffffffn)));
}
}
}
// Conversions, and halves joined back.
cases.push(Uint8Array.of(convert, 0, ...be(x), ...be(x)));
for (const y of [0n, 1n, 1n << 32n, 1n << 63n, 0xffffffffffffffffn]) {
for (const from of [3, 4, 9, 10]) {
// A cast to every type, and every format spec.
for (let to = 0; to < 12; to++) cases.push(Uint8Array.of(cast, from, to, ...be(x), ...be(y)));
cases.push(Uint8Array.of(fmtInts, from, ...be(x), ...be(y)));
}
}
}
return cases;
}

/**
* `utf8::decodes` of raw bytes: a byte order mark, which std keeps, and the sequences at
* each edge of what it takes: surrogates, overlongs, past U+10FFFF, and cut short at the
Expand All @@ -129,6 +213,6 @@ const utf8 = [
[0xe2, 0x82],
[0xc2],
[0x80, 0xf8, 0xff],
].map((bytes) => Uint8Array.of(57, 0, ...bytes));
].map((bytes) => Uint8Array.of(op("utf8::decodes"), 0, ...bytes));

export const cases: Uint8Array[] = [...loops, ...durations, ...utf8, ...floats];
export const cases: Uint8Array[] = [...loops, ...durations, ...ints(), ...utf8, ...floats];
Loading