diff --git a/quest/m2/README.md b/quest/m2/README.md index 59950ffff..0911a1ae6 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -15,8 +15,8 @@ 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 -- [Faster translator runtime](/quest/m2/ts-runtime-perf.md) - fewer allocations (`Result` included), direct byte reads, and number-only checked ops, kept where measured - [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 - [Shared ownership](/quest/m2/ts-shared.md) - `Arc`/`Rc`/`Weak` keep exact counts, and locks and atomics become plain values diff --git a/quest/m2/ts-byte-views.md b/quest/m2/ts-byte-views.md new file mode 100644 index 000000000..3bb8c7028 --- /dev/null +++ b/quest/m2/ts-byte-views.md @@ -0,0 +1,32 @@ +# [L] Cheaper byte slices + +## Goal + +Splitting a `&[u8]` in translated code stops costing a `Uint8Array` view per +split, so the codec's decode on the [TS benchmark](/ts/README.md#bench) moves +toward moq's hand-written cursor, with every differential test still passing. + +## Plan + +The report in PR #15 puts views at most of what decode still costs: +about 25 ns each in V8, and a varint takes one to four (`split_first`'s rest, +`split_first_chunk`'s chunk and rest, a `lo @ ..` pattern). moq's hand-written +`Cursor` reads through an offset and makes none. + +Options to measure, not a decision: + +- Lower `&[u8]` to an offset and length into a shared array, as `(buf, start, + end)` locals the translator scalar-replaces, materializing a `Uint8Array` + only at the API edge. +- Keep `Uint8Array` but skip views nothing reads: a split whose chunk is only + destructured into bytes, or whose rest is unused on the path taken. + +Engines differ: JavaScriptCore makes views slower than V8 but small copies +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 diff --git a/quest/m2/ts-runtime-perf.md b/quest/m2/ts-runtime-perf.md deleted file mode 100644 index 83380daca..000000000 --- a/quest/m2/ts-runtime-perf.md +++ /dev/null @@ -1,43 +0,0 @@ -# [M] Faster translator runtime - -## Goal - -The translated codec runs measurably faster on the -[TS benchmark](/ts/README.md#bench), with every differential test still -passing and no change to the semantics the subset documents. The report -attributes the remaining decode gap to moq's hand-written TS, workload by -workload. - -## Plan - -Decided with the maintainer on 2026-10-23. Candidates found in `rt.ts` and the -codec's output, each kept only if the benchmark shows it wins; no target is set -up front. - -The first benchmark (PR #7) puts decode at 20-140x slower than hand-written, -with costs at every width, so this quest runs before the -[wide integer spike](/quest/m2/ts-wide-ints.md) and gives it a cheaper baseline. -Start with the costs it found: - -- Every fallible call allocates a `Result`, and `?` allocates a fresh `Err` - instead of returning the incoming one. This quest may replace the - `{ ok, value } | { ok, error }` shape with a non-allocating one if it measures - faster; the types at the API edge stay as they are. -- `fromBeBytes`/`toBeBytes` and `leadingZeros` loop in `bigint` even for `u16` - and `u32`, and `from_be_bytes([a, b])` builds a `Uint8Array.of` first. Read and - write bytes directly or through `DataView`. -- Decode takes a `subarray` view and returns a tuple per varint. - -Then the rest: - -- Checked ops on integers of 32 bits or less compare a number against `bigint` - bounds, and `div`, `rem`, shifts, and the `checked`/`wrapping`/`saturating` - families convert to `bigint`. Use number-only checks (`>>> 0`, `| 0`, - `Math.imul`). -- Fieldless variants of data enums allocate `{ tag }` at each use; share one - frozen object per variant. -- `rt.copy` is a generic deep clone; emit a typed copy per `Copy` type. - -Leave the 64-bit representation to the wide integer spike. - -Public API: the translator's output and `rt.ts`. diff --git a/quest/m2/ts-wide-ints.md b/quest/m2/ts-wide-ints.md index 9ef90b7bb..8de723a6e 100644 --- a/quest/m2/ts-wide-ints.md +++ b/quest/m2/ts-wide-ints.md @@ -31,7 +31,3 @@ case, plus output size and readability. Record the result in `ts/README.md`'s Design section, replacing the `bigint` note. Public API: the translator's output for 64- and 128-bit integers. - -## Required - -- [Faster translator runtime](/quest/m2/ts-runtime-perf.md) - removes the costs at every width first, so the spike measures only the representation diff --git a/ts/README.md b/ts/README.md index 4b4fb73c4..c2bc536b2 100644 --- a/ts/README.md +++ b/ts/README.md @@ -93,7 +93,8 @@ Imports name the `.ts` file, which Node, Bun, and bundlers run as is. A `tsc` bu - 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. - `Result` is a plain `{ ok, value } | { ok, error }` object rather than an exception, so - `?` and `match` keep Rust's control flow exactly. + `?` and `match` keep Rust's control flow exactly. Making no `Ok` objects at all measured + no faster on `just ts bench`, in V8 or JavaScriptCore. - Bodies lower from THIR, which keeps loops, `match`, and `?` shaped as written. It does not say where values drop; [Drop](/quest/m2/ts-drop.md) decides how to get that from MIR. diff --git a/ts/fixtures/codec/expected/coding/decode.ts b/ts/fixtures/codec/expected/coding/decode.ts index 76f2d32c5..1bc86e3ef 100644 --- a/ts/fixtures/codec/expected/coding/decode.ts +++ b/ts/fixtures/codec/expected/coding/decode.ts @@ -231,7 +231,7 @@ export class Decoder { return rt.Err(slice.error); } const b = slice.value; - return rt.Ok(rt.u16.fromBeBytes(Uint8Array.of(rt.at(b, 0), rt.at(b, 1)))); + return rt.Ok((rt.at(b, 0) << 8) | rt.at(b, 1)); } /** Read a byte that must be 0 or 1. */ diff --git a/ts/fixtures/codec/expected/coding/varint.ts b/ts/fixtures/codec/expected/coding/varint.ts index 47bf2a01a..5aee013c7 100644 --- a/ts/fixtures/codec/expected/coding/varint.ts +++ b/ts/fixtures/codec/expected/coding/varint.ts @@ -96,8 +96,16 @@ export function writeQuic(value: bigint, out: rt.ByteVec): rt.Result>> 0)); } else if (hi < (1 << 30)) { - const [a, b, c, d] = rt.u32.toBeBytes((0xc000_0000 | hi) >>> 0); - const [e, f, g, h] = rt.u32.toBeBytes(lo); + const toBeBytes = rt.u32.toBeBytes((0xc000_0000 | hi) >>> 0); + const a = toBeBytes[0]; + const b = toBeBytes[1]; + const c = toBeBytes[2]; + const d = toBeBytes[3]; + const toBeBytes2 = rt.u32.toBeBytes(lo); + const e = toBeBytes2[0]; + const f = toBeBytes2[1]; + const g = toBeBytes2[2]; + const h = toBeBytes2[3]; out.extendFromSlice(Uint8Array.of(a, b, c, d, e, f, g, h)); } else { return rt.Err(new BoundsExceeded()); @@ -107,7 +115,11 @@ export function writeQuic(value: bigint, out: rt.ByteVec): rt.Result>> 8) & 0xff), hi & 0xff, a, b, c, d)); } else if (hi < (1 << 24)) { - const [, f, g, h] = rt.u32.toBeBytes(hi); + const toBeBytes2 = rt.u32.toBeBytes(hi); + const f = toBeBytes2[1]; + const g = toBeBytes2[2]; + const h = toBeBytes2[3]; out.extendFromSlice(Uint8Array.of(0xfe, f, g, h, a, b, c, d)); } else { - const [e, f2, g2, h2] = rt.u32.toBeBytes(hi); + const toBeBytes3 = rt.u32.toBeBytes(hi); + const e = toBeBytes3[0]; + const f2 = toBeBytes3[1]; + const g2 = toBeBytes3[2]; + const h2 = toBeBytes3[3]; out.extendFromSlice(Uint8Array.of(0xff, e, f2, g2, h2, a, b, c, d)); } } @@ -153,15 +172,21 @@ export function readQuic(buf: Uint8Array): rt.Result<[bigint, Uint8Array], Decod if (!okOr.ok) { return rt.Err(okOr.error); } - const [[a, b], rest2] = okOr.value; - return rt.Ok([BigInt(rt.u32.fromBeBytes(Uint8Array.of(0, 0, a & 0x3f, b))), rest2]); + const a = okOr.value[0][0]; + const b = okOr.value[0][1]; + const rest2 = okOr.value[1]; + return rt.Ok([BigInt(((a & 0x3f) << 8) | b), rest2]); } else if (first < 0xc0) { const okOr2 = rt.option.okOr(rt.slice.splitFirstChunk(buf, 4), { tag: "Short" } as DecodeError); if (!okOr2.ok) { return rt.Err(okOr2.error); } - const [[a2, b2, c, d], rest3] = okOr2.value; - return rt.Ok([BigInt(rt.u32.fromBeBytes(Uint8Array.of(a2 & 0x3f, b2, c, d))), rest3]); + const a2 = okOr2.value[0][0]; + const b2 = okOr2.value[0][1]; + const c = okOr2.value[0][2]; + const d = okOr2.value[0][3]; + const rest3 = okOr2.value[1]; + return rt.Ok([BigInt((((a2 & 0x3f) << 24) | (b2 << 16) | (c << 8) | d) >>> 0), rest3]); } else { const okOr3 = rt.option.okOr(rt.slice.splitFirstChunk(buf, 8), { tag: "Short" } as DecodeError); if (!okOr3.ok) { @@ -173,7 +198,7 @@ export function readQuic(buf: Uint8Array): rt.Result<[bigint, Uint8Array], Decod const d2 = okOr3.value[0][3]; const lo = okOr3.value[0].subarray(4, 8); const rest4 = okOr3.value[1]; - return rt.Ok([fromHalves(rt.u32.fromBeBytes(Uint8Array.of(a3 & 0x3f, b3, c2, d2)), rt.u32.fromBeBytes(rt.copy(lo))), rest4]); + return rt.Ok([fromHalves((((a3 & 0x3f) << 24) | (b3 << 16) | (c2 << 8) | d2) >>> 0, rt.u32.fromBeBytes(lo)), rest4]); } } @@ -193,22 +218,31 @@ export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[big if (!okOr.ok) { return rt.Err(okOr.error); } - const [[a, b], rest2] = okOr.value; - value = [BigInt(rt.u32.fromBeBytes(Uint8Array.of(0, 0, a & 0x3f, b))), rest2]; + const a = okOr.value[0][0]; + const b = okOr.value[0][1]; + const rest2 = okOr.value[1]; + value = [BigInt(((a & 0x3f) << 8) | b), rest2]; } else if (leadingOnes === 2) { const okOr2 = rt.option.okOr(rt.slice.splitFirstChunk(buf, 3), { tag: "Short" } as DecodeError); if (!okOr2.ok) { return rt.Err(okOr2.error); } - const [[a2, b2, c], rest3] = okOr2.value; - value = [BigInt(rt.u32.fromBeBytes(Uint8Array.of(0, a2 & 0x1f, b2, c))), rest3]; + const a2 = okOr2.value[0][0]; + const b2 = okOr2.value[0][1]; + const c = okOr2.value[0][2]; + const rest3 = okOr2.value[1]; + value = [BigInt(((a2 & 0x1f) << 16) | (b2 << 8) | c), rest3]; } else if (leadingOnes === 3) { const okOr3 = rt.option.okOr(rt.slice.splitFirstChunk(buf, 4), { tag: "Short" } as DecodeError); if (!okOr3.ok) { return rt.Err(okOr3.error); } - const [[a3, b3, c2, d], rest4] = okOr3.value; - value = [BigInt(rt.u32.fromBeBytes(Uint8Array.of(a3 & 0x0f, b3, c2, d))), rest4]; + const a3 = okOr3.value[0][0]; + const b3 = okOr3.value[0][1]; + const c2 = okOr3.value[0][2]; + const d = okOr3.value[0][3]; + const rest4 = okOr3.value[1]; + value = [BigInt((((a3 & 0x0f) << 24) | (b3 << 16) | (c2 << 8) | d) >>> 0), rest4]; } else if (leadingOnes === 4) { const okOr4 = rt.option.okOr(rt.slice.splitFirstChunk(buf, 5), { tag: "Short" } as DecodeError); if (!okOr4.ok) { @@ -217,7 +251,7 @@ export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[big const a4 = okOr4.value[0][0]; const lo = okOr4.value[0].subarray(1, 5); const rest5 = okOr4.value[1]; - value = [fromHalves(a4 & 0x07, rt.u32.fromBeBytes(rt.copy(lo))), rest5]; + value = [fromHalves(a4 & 0x07, rt.u32.fromBeBytes(lo)), rest5]; } else if (leadingOnes === 5) { const okOr5 = rt.option.okOr(rt.slice.splitFirstChunk(buf, 6), { tag: "Short" } as DecodeError); if (!okOr5.ok) { @@ -227,7 +261,7 @@ export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[big const b4 = okOr5.value[0][1]; const lo2 = okOr5.value[0].subarray(2, 6); const rest6 = okOr5.value[1]; - value = [fromHalves(rt.u32.fromBeBytes(Uint8Array.of(0, 0, a5 & 0x03, b4)), rt.u32.fromBeBytes(rt.copy(lo2))), rest6]; + value = [fromHalves(((a5 & 0x03) << 8) | b4, rt.u32.fromBeBytes(lo2)), rest6]; } else if (leadingOnes === 6 && !seven) { return rt.Err({ tag: "InvalidValue" }); } else if (leadingOnes === 6) { @@ -240,7 +274,7 @@ export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[big const c3 = okOr6.value[0][2]; const lo3 = okOr6.value[0].subarray(3, 7); const rest7 = okOr6.value[1]; - value = [fromHalves(rt.u32.fromBeBytes(Uint8Array.of(0, a6 & 0x01, b5, c3)), rt.u32.fromBeBytes(rt.copy(lo3))), rest7]; + value = [fromHalves(((a6 & 0x01) << 16) | (b5 << 8) | c3, rt.u32.fromBeBytes(lo3)), rest7]; } else if (leadingOnes === 7) { const okOr7 = rt.option.okOr(rt.slice.splitFirstChunk(buf, 8), { tag: "Short" } as DecodeError); if (!okOr7.ok) { @@ -251,7 +285,7 @@ export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[big const d2 = okOr7.value[0][3]; const lo4 = okOr7.value[0].subarray(4, 8); const rest8 = okOr7.value[1]; - value = [fromHalves(rt.u32.fromBeBytes(Uint8Array.of(0, b6, c4, d2)), rt.u32.fromBeBytes(rt.copy(lo4))), rest8]; + value = [fromHalves((b6 << 16) | (c4 << 8) | d2, rt.u32.fromBeBytes(lo4)), rest8]; } else { const okOr8 = rt.option.okOr(rt.slice.splitFirstChunk(buf, 9), { tag: "Short" } as DecodeError); if (!okOr8.ok) { @@ -263,7 +297,7 @@ export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[big const h = okOr8.value[0][8]; const hi = okOr8.value[0].subarray(1, 5); const rest9 = okOr8.value[1]; - value = [fromHalves(rt.u32.fromBeBytes(rt.copy(hi)), rt.u32.fromBeBytes(Uint8Array.of(e, f, g, h))), rest9]; + value = [fromHalves(rt.u32.fromBeBytes(hi), ((e << 24) | (f << 16) | (g << 8) | h) >>> 0), rest9]; } return rt.Ok(value); } diff --git a/ts/fixtures/codec/expected/harness.ts b/ts/fixtures/codec/expected/harness.ts index 44f8c3f3f..dd5bc9af3 100644 --- a/ts/fixtures/codec/expected/harness.ts +++ b/ts/fixtures/codec/expected/harness.ts @@ -85,7 +85,7 @@ export function takeU64(data: Uint8Array): [bigint, Uint8Array] { let buf: Uint8Array = new Uint8Array(8); const n = rt.min(data.length, 8); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data, 0, n)); - return [rt.u64.fromBeBytes(rt.copy(buf)), rt.slice.range(data, n)]; + return [rt.u64.fromBeBytes(buf), rt.slice.range(data, n)]; } /** A decode error's code, then its text. */ diff --git a/ts/fixtures/lang/expected/fmt.ts b/ts/fixtures/lang/expected/fmt.ts index 613bd35fb..f55dbccd6 100644 --- a/ts/fixtures/lang/expected/fmt.ts +++ b/ts/fixtures/lang/expected/fmt.ts @@ -16,7 +16,7 @@ export function ints(kind: number, data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(1); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const v = rt.u8.fromBeBytes(rt.copy(buf)); + const v = rt.u8.fromBeBytes(buf); put(out, rt.fmt.format([(f: rt.Formatter) => rt.u8.display(v, f), " ", (f: rt.Formatter) => rt.u8.display(v, f), " ", rt.fmt.spec({ width: 5 }, (f: rt.Formatter) => rt.u8.display(v, f)), "|", rt.fmt.spec({ align: "<", width: 5 }, (f: rt.Formatter) => rt.u8.display(v, f)), "|", rt.fmt.spec({ align: "^", width: 7 }, (f: rt.Formatter) => rt.u8.display(v, f)), "|", rt.fmt.spec({ align: ">", width: 4 }, (f: rt.Formatter) => rt.u8.display(v, f)), "|", rt.fmt.spec({ fill: "*", align: "^", width: 9 }, (f: rt.Formatter) => rt.u8.display(v, f)), "|", rt.fmt.spec({ fill: "é", align: ">", width: 6 }, (f: rt.Formatter) => rt.u8.display(v, f)), "|", rt.fmt.spec({ zero: true, width: 5 }, (f: rt.Formatter) => rt.u8.display(v, f)), "|", rt.fmt.spec({ align: "<", zero: true, width: 5 }, (f: rt.Formatter) => rt.u8.display(v, f)), "|", (f: rt.Formatter) => rt.u8.lowerHex(v, f), " ", (f: rt.Formatter) => rt.u8.upperHex(v, f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.u8.lowerHex(v, f)), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.u8.upperHex(v, f)), " ", rt.fmt.spec({ zero: true, width: 8 }, (f: rt.Formatter) => rt.u8.lowerHex(v, f)), " ", rt.fmt.spec({ alternate: true, zero: true, width: 10 }, (f: rt.Formatter) => rt.u8.lowerHex(v, f)), " ", rt.fmt.spec({ align: ">", width: 8 }, (f: rt.Formatter) => rt.u8.lowerHex(v, f)), "|", rt.fmt.spec({ align: "<", alternate: true, width: 6 }, (f: rt.Formatter) => rt.u8.upperHex(v, f)), "|", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.u8.display(v, f)), " ", rt.fmt.spec({ width: 3 }, (f: rt.Formatter) => rt.u8.display(v, f))])); put(out, rt.fmt.toString((f: rt.Formatter) => rt.u8.display(v, f))); } else if (value === 1) { @@ -24,7 +24,7 @@ export function ints(kind: number, data: Uint8Array, out: rt.ByteVec): void { let buf2: Uint8Array = new Uint8Array(2); const n2 = rt.min(data3.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data3, 0, n2)); - const v2 = rt.u16.fromBeBytes(rt.copy(buf2)); + const v2 = rt.u16.fromBeBytes(buf2); put(out, rt.fmt.format([(f: rt.Formatter) => rt.u16.display(v2, f), " ", (f: rt.Formatter) => rt.u16.display(v2, f), " ", rt.fmt.spec({ width: 5 }, (f: rt.Formatter) => rt.u16.display(v2, f)), "|", rt.fmt.spec({ align: "<", width: 5 }, (f: rt.Formatter) => rt.u16.display(v2, f)), "|", rt.fmt.spec({ align: "^", width: 7 }, (f: rt.Formatter) => rt.u16.display(v2, f)), "|", rt.fmt.spec({ align: ">", width: 4 }, (f: rt.Formatter) => rt.u16.display(v2, f)), "|", rt.fmt.spec({ fill: "*", align: "^", width: 9 }, (f: rt.Formatter) => rt.u16.display(v2, f)), "|", rt.fmt.spec({ fill: "é", align: ">", width: 6 }, (f: rt.Formatter) => rt.u16.display(v2, f)), "|", rt.fmt.spec({ zero: true, width: 5 }, (f: rt.Formatter) => rt.u16.display(v2, f)), "|", rt.fmt.spec({ align: "<", zero: true, width: 5 }, (f: rt.Formatter) => rt.u16.display(v2, f)), "|", (f: rt.Formatter) => rt.u16.lowerHex(v2, f), " ", (f: rt.Formatter) => rt.u16.upperHex(v2, f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.u16.lowerHex(v2, f)), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.u16.upperHex(v2, f)), " ", rt.fmt.spec({ zero: true, width: 8 }, (f: rt.Formatter) => rt.u16.lowerHex(v2, f)), " ", rt.fmt.spec({ alternate: true, zero: true, width: 10 }, (f: rt.Formatter) => rt.u16.lowerHex(v2, f)), " ", rt.fmt.spec({ align: ">", width: 8 }, (f: rt.Formatter) => rt.u16.lowerHex(v2, f)), "|", rt.fmt.spec({ align: "<", alternate: true, width: 6 }, (f: rt.Formatter) => rt.u16.upperHex(v2, f)), "|", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.u16.display(v2, f)), " ", rt.fmt.spec({ width: 3 }, (f: rt.Formatter) => rt.u16.display(v2, f))])); put(out, rt.fmt.toString((f: rt.Formatter) => rt.u16.display(v2, f))); } else if (value === 2) { @@ -32,7 +32,7 @@ export function ints(kind: number, data: Uint8Array, out: rt.ByteVec): void { let buf3: Uint8Array = new Uint8Array(4); const n3 = rt.min(data4.length, buf3.length); rt.slice.copyFromSlice(rt.slice.range(buf3, 0, n3), rt.slice.range(data4, 0, n3)); - const v3 = rt.u32.fromBeBytes(rt.copy(buf3)); + const v3 = rt.u32.fromBeBytes(buf3); put(out, rt.fmt.format([(f: rt.Formatter) => rt.u32.display(v3, f), " ", (f: rt.Formatter) => rt.u32.display(v3, f), " ", rt.fmt.spec({ width: 5 }, (f: rt.Formatter) => rt.u32.display(v3, f)), "|", rt.fmt.spec({ align: "<", width: 5 }, (f: rt.Formatter) => rt.u32.display(v3, f)), "|", rt.fmt.spec({ align: "^", width: 7 }, (f: rt.Formatter) => rt.u32.display(v3, f)), "|", rt.fmt.spec({ align: ">", width: 4 }, (f: rt.Formatter) => rt.u32.display(v3, f)), "|", rt.fmt.spec({ fill: "*", align: "^", width: 9 }, (f: rt.Formatter) => rt.u32.display(v3, f)), "|", rt.fmt.spec({ fill: "é", align: ">", width: 6 }, (f: rt.Formatter) => rt.u32.display(v3, f)), "|", rt.fmt.spec({ zero: true, width: 5 }, (f: rt.Formatter) => rt.u32.display(v3, f)), "|", rt.fmt.spec({ align: "<", zero: true, width: 5 }, (f: rt.Formatter) => rt.u32.display(v3, f)), "|", (f: rt.Formatter) => rt.u32.lowerHex(v3, f), " ", (f: rt.Formatter) => rt.u32.upperHex(v3, f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.u32.lowerHex(v3, f)), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.u32.upperHex(v3, f)), " ", rt.fmt.spec({ zero: true, width: 8 }, (f: rt.Formatter) => rt.u32.lowerHex(v3, f)), " ", rt.fmt.spec({ alternate: true, zero: true, width: 10 }, (f: rt.Formatter) => rt.u32.lowerHex(v3, f)), " ", rt.fmt.spec({ align: ">", width: 8 }, (f: rt.Formatter) => rt.u32.lowerHex(v3, f)), "|", rt.fmt.spec({ align: "<", alternate: true, width: 6 }, (f: rt.Formatter) => rt.u32.upperHex(v3, f)), "|", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.u32.display(v3, f)), " ", rt.fmt.spec({ width: 3 }, (f: rt.Formatter) => rt.u32.display(v3, f))])); put(out, rt.fmt.toString((f: rt.Formatter) => rt.u32.display(v3, f))); } else if (value === 3) { @@ -40,7 +40,7 @@ export function ints(kind: number, data: Uint8Array, out: rt.ByteVec): void { let buf4: Uint8Array = new Uint8Array(8); const n4 = rt.min(data5.length, buf4.length); rt.slice.copyFromSlice(rt.slice.range(buf4, 0, n4), rt.slice.range(data5, 0, n4)); - const v4 = rt.u64.fromBeBytes(rt.copy(buf4)); + const v4 = rt.u64.fromBeBytes(buf4); put(out, rt.fmt.format([(f: rt.Formatter) => rt.u64.display(v4, f), " ", (f: rt.Formatter) => rt.u64.display(v4, f), " ", rt.fmt.spec({ width: 5 }, (f: rt.Formatter) => rt.u64.display(v4, f)), "|", rt.fmt.spec({ align: "<", width: 5 }, (f: rt.Formatter) => rt.u64.display(v4, f)), "|", rt.fmt.spec({ align: "^", width: 7 }, (f: rt.Formatter) => rt.u64.display(v4, f)), "|", rt.fmt.spec({ align: ">", width: 4 }, (f: rt.Formatter) => rt.u64.display(v4, f)), "|", rt.fmt.spec({ fill: "*", align: "^", width: 9 }, (f: rt.Formatter) => rt.u64.display(v4, f)), "|", rt.fmt.spec({ fill: "é", align: ">", width: 6 }, (f: rt.Formatter) => rt.u64.display(v4, f)), "|", rt.fmt.spec({ zero: true, width: 5 }, (f: rt.Formatter) => rt.u64.display(v4, f)), "|", rt.fmt.spec({ align: "<", zero: true, width: 5 }, (f: rt.Formatter) => rt.u64.display(v4, f)), "|", (f: rt.Formatter) => rt.u64.lowerHex(v4, f), " ", (f: rt.Formatter) => rt.u64.upperHex(v4, f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.u64.lowerHex(v4, f)), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.u64.upperHex(v4, f)), " ", rt.fmt.spec({ zero: true, width: 8 }, (f: rt.Formatter) => rt.u64.lowerHex(v4, f)), " ", rt.fmt.spec({ alternate: true, zero: true, width: 10 }, (f: rt.Formatter) => rt.u64.lowerHex(v4, f)), " ", rt.fmt.spec({ align: ">", width: 8 }, (f: rt.Formatter) => rt.u64.lowerHex(v4, f)), "|", rt.fmt.spec({ align: "<", alternate: true, width: 6 }, (f: rt.Formatter) => rt.u64.upperHex(v4, f)), "|", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.u64.display(v4, f)), " ", rt.fmt.spec({ width: 3 }, (f: rt.Formatter) => rt.u64.display(v4, f))])); put(out, rt.fmt.toString((f: rt.Formatter) => rt.u64.display(v4, f))); } else if (value === 4) { @@ -48,7 +48,7 @@ export function ints(kind: number, data: Uint8Array, out: rt.ByteVec): void { let buf5: Uint8Array = new Uint8Array(16); const n5 = rt.min(data6.length, buf5.length); rt.slice.copyFromSlice(rt.slice.range(buf5, 0, n5), rt.slice.range(data6, 0, n5)); - const v5 = rt.u128.fromBeBytes(rt.copy(buf5)); + const v5 = rt.u128.fromBeBytes(buf5); put(out, rt.fmt.format([(f: rt.Formatter) => rt.u128.display(v5, f), " ", (f: rt.Formatter) => rt.u128.display(v5, f), " ", rt.fmt.spec({ width: 5 }, (f: rt.Formatter) => rt.u128.display(v5, f)), "|", rt.fmt.spec({ align: "<", width: 5 }, (f: rt.Formatter) => rt.u128.display(v5, f)), "|", rt.fmt.spec({ align: "^", width: 7 }, (f: rt.Formatter) => rt.u128.display(v5, f)), "|", rt.fmt.spec({ align: ">", width: 4 }, (f: rt.Formatter) => rt.u128.display(v5, f)), "|", rt.fmt.spec({ fill: "*", align: "^", width: 9 }, (f: rt.Formatter) => rt.u128.display(v5, f)), "|", rt.fmt.spec({ fill: "é", align: ">", width: 6 }, (f: rt.Formatter) => rt.u128.display(v5, f)), "|", rt.fmt.spec({ zero: true, width: 5 }, (f: rt.Formatter) => rt.u128.display(v5, f)), "|", rt.fmt.spec({ align: "<", zero: true, width: 5 }, (f: rt.Formatter) => rt.u128.display(v5, f)), "|", (f: rt.Formatter) => rt.u128.lowerHex(v5, f), " ", (f: rt.Formatter) => rt.u128.upperHex(v5, f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.u128.lowerHex(v5, f)), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.u128.upperHex(v5, f)), " ", rt.fmt.spec({ zero: true, width: 8 }, (f: rt.Formatter) => rt.u128.lowerHex(v5, f)), " ", rt.fmt.spec({ alternate: true, zero: true, width: 10 }, (f: rt.Formatter) => rt.u128.lowerHex(v5, f)), " ", rt.fmt.spec({ align: ">", width: 8 }, (f: rt.Formatter) => rt.u128.lowerHex(v5, f)), "|", rt.fmt.spec({ align: "<", alternate: true, width: 6 }, (f: rt.Formatter) => rt.u128.upperHex(v5, f)), "|", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.u128.display(v5, f)), " ", rt.fmt.spec({ width: 3 }, (f: rt.Formatter) => rt.u128.display(v5, f))])); put(out, rt.fmt.toString((f: rt.Formatter) => rt.u128.display(v5, f))); } else if (value === 5) { @@ -56,7 +56,7 @@ export function ints(kind: number, data: Uint8Array, out: rt.ByteVec): void { let buf6: Uint8Array = new Uint8Array(4); const n6 = rt.min(data7.length, buf6.length); rt.slice.copyFromSlice(rt.slice.range(buf6, 0, n6), rt.slice.range(data7, 0, n6)); - const v6 = rt.usize.fromBeBytes(rt.copy(buf6)); + const v6 = rt.usize.fromBeBytes(buf6); put(out, rt.fmt.format([(f: rt.Formatter) => rt.usize.display(v6, f), " ", (f: rt.Formatter) => rt.usize.display(v6, f), " ", rt.fmt.spec({ width: 5 }, (f: rt.Formatter) => rt.usize.display(v6, f)), "|", rt.fmt.spec({ align: "<", width: 5 }, (f: rt.Formatter) => rt.usize.display(v6, f)), "|", rt.fmt.spec({ align: "^", width: 7 }, (f: rt.Formatter) => rt.usize.display(v6, f)), "|", rt.fmt.spec({ align: ">", width: 4 }, (f: rt.Formatter) => rt.usize.display(v6, f)), "|", rt.fmt.spec({ fill: "*", align: "^", width: 9 }, (f: rt.Formatter) => rt.usize.display(v6, f)), "|", rt.fmt.spec({ fill: "é", align: ">", width: 6 }, (f: rt.Formatter) => rt.usize.display(v6, f)), "|", rt.fmt.spec({ zero: true, width: 5 }, (f: rt.Formatter) => rt.usize.display(v6, f)), "|", rt.fmt.spec({ align: "<", zero: true, width: 5 }, (f: rt.Formatter) => rt.usize.display(v6, f)), "|", (f: rt.Formatter) => rt.usize.lowerHex(v6, f), " ", (f: rt.Formatter) => rt.usize.upperHex(v6, f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.usize.lowerHex(v6, f)), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.usize.upperHex(v6, f)), " ", rt.fmt.spec({ zero: true, width: 8 }, (f: rt.Formatter) => rt.usize.lowerHex(v6, f)), " ", rt.fmt.spec({ alternate: true, zero: true, width: 10 }, (f: rt.Formatter) => rt.usize.lowerHex(v6, f)), " ", rt.fmt.spec({ align: ">", width: 8 }, (f: rt.Formatter) => rt.usize.lowerHex(v6, f)), "|", rt.fmt.spec({ align: "<", alternate: true, width: 6 }, (f: rt.Formatter) => rt.usize.upperHex(v6, f)), "|", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.usize.display(v6, f)), " ", rt.fmt.spec({ width: 3 }, (f: rt.Formatter) => rt.usize.display(v6, f))])); put(out, rt.fmt.toString((f: rt.Formatter) => rt.usize.display(v6, f))); } else if (value === 6) { @@ -64,7 +64,7 @@ export function ints(kind: number, data: Uint8Array, out: rt.ByteVec): void { let buf7: Uint8Array = new Uint8Array(1); const n7 = rt.min(data8.length, buf7.length); rt.slice.copyFromSlice(rt.slice.range(buf7, 0, n7), rt.slice.range(data8, 0, n7)); - const v7 = rt.i8.fromBeBytes(rt.copy(buf7)); + const v7 = rt.i8.fromBeBytes(buf7); put(out, rt.fmt.format([(f: rt.Formatter) => rt.i8.display(v7, f), " ", (f: rt.Formatter) => rt.i8.display(v7, f), " ", rt.fmt.spec({ width: 5 }, (f: rt.Formatter) => rt.i8.display(v7, f)), "|", rt.fmt.spec({ align: "<", width: 5 }, (f: rt.Formatter) => rt.i8.display(v7, f)), "|", rt.fmt.spec({ align: "^", width: 7 }, (f: rt.Formatter) => rt.i8.display(v7, f)), "|", rt.fmt.spec({ align: ">", width: 4 }, (f: rt.Formatter) => rt.i8.display(v7, f)), "|", rt.fmt.spec({ fill: "*", align: "^", width: 9 }, (f: rt.Formatter) => rt.i8.display(v7, f)), "|", rt.fmt.spec({ fill: "é", align: ">", width: 6 }, (f: rt.Formatter) => rt.i8.display(v7, f)), "|", rt.fmt.spec({ zero: true, width: 5 }, (f: rt.Formatter) => rt.i8.display(v7, f)), "|", rt.fmt.spec({ align: "<", zero: true, width: 5 }, (f: rt.Formatter) => rt.i8.display(v7, f)), "|", (f: rt.Formatter) => rt.i8.lowerHex(v7, f), " ", (f: rt.Formatter) => rt.i8.upperHex(v7, f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.i8.lowerHex(v7, f)), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.i8.upperHex(v7, f)), " ", rt.fmt.spec({ zero: true, width: 8 }, (f: rt.Formatter) => rt.i8.lowerHex(v7, f)), " ", rt.fmt.spec({ alternate: true, zero: true, width: 10 }, (f: rt.Formatter) => rt.i8.lowerHex(v7, f)), " ", rt.fmt.spec({ align: ">", width: 8 }, (f: rt.Formatter) => rt.i8.lowerHex(v7, f)), "|", rt.fmt.spec({ align: "<", alternate: true, width: 6 }, (f: rt.Formatter) => rt.i8.upperHex(v7, f)), "|", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.i8.display(v7, f)), " ", rt.fmt.spec({ width: 3 }, (f: rt.Formatter) => rt.i8.display(v7, f))])); put(out, rt.fmt.toString((f: rt.Formatter) => rt.i8.display(v7, f))); } else if (value === 7) { @@ -72,7 +72,7 @@ export function ints(kind: number, data: Uint8Array, out: rt.ByteVec): void { let buf8: Uint8Array = new Uint8Array(2); const n8 = rt.min(data9.length, buf8.length); rt.slice.copyFromSlice(rt.slice.range(buf8, 0, n8), rt.slice.range(data9, 0, n8)); - const v8 = rt.i16.fromBeBytes(rt.copy(buf8)); + const v8 = rt.i16.fromBeBytes(buf8); put(out, rt.fmt.format([(f: rt.Formatter) => rt.i16.display(v8, f), " ", (f: rt.Formatter) => rt.i16.display(v8, f), " ", rt.fmt.spec({ width: 5 }, (f: rt.Formatter) => rt.i16.display(v8, f)), "|", rt.fmt.spec({ align: "<", width: 5 }, (f: rt.Formatter) => rt.i16.display(v8, f)), "|", rt.fmt.spec({ align: "^", width: 7 }, (f: rt.Formatter) => rt.i16.display(v8, f)), "|", rt.fmt.spec({ align: ">", width: 4 }, (f: rt.Formatter) => rt.i16.display(v8, f)), "|", rt.fmt.spec({ fill: "*", align: "^", width: 9 }, (f: rt.Formatter) => rt.i16.display(v8, f)), "|", rt.fmt.spec({ fill: "é", align: ">", width: 6 }, (f: rt.Formatter) => rt.i16.display(v8, f)), "|", rt.fmt.spec({ zero: true, width: 5 }, (f: rt.Formatter) => rt.i16.display(v8, f)), "|", rt.fmt.spec({ align: "<", zero: true, width: 5 }, (f: rt.Formatter) => rt.i16.display(v8, f)), "|", (f: rt.Formatter) => rt.i16.lowerHex(v8, f), " ", (f: rt.Formatter) => rt.i16.upperHex(v8, f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.i16.lowerHex(v8, f)), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.i16.upperHex(v8, f)), " ", rt.fmt.spec({ zero: true, width: 8 }, (f: rt.Formatter) => rt.i16.lowerHex(v8, f)), " ", rt.fmt.spec({ alternate: true, zero: true, width: 10 }, (f: rt.Formatter) => rt.i16.lowerHex(v8, f)), " ", rt.fmt.spec({ align: ">", width: 8 }, (f: rt.Formatter) => rt.i16.lowerHex(v8, f)), "|", rt.fmt.spec({ align: "<", alternate: true, width: 6 }, (f: rt.Formatter) => rt.i16.upperHex(v8, f)), "|", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.i16.display(v8, f)), " ", rt.fmt.spec({ width: 3 }, (f: rt.Formatter) => rt.i16.display(v8, f))])); put(out, rt.fmt.toString((f: rt.Formatter) => rt.i16.display(v8, f))); } else if (value === 8) { @@ -80,7 +80,7 @@ export function ints(kind: number, data: Uint8Array, out: rt.ByteVec): void { let buf9: Uint8Array = new Uint8Array(4); const n9 = rt.min(data10.length, buf9.length); rt.slice.copyFromSlice(rt.slice.range(buf9, 0, n9), rt.slice.range(data10, 0, n9)); - const v9 = rt.i32.fromBeBytes(rt.copy(buf9)); + const v9 = rt.i32.fromBeBytes(buf9); put(out, rt.fmt.format([(f: rt.Formatter) => rt.i32.display(v9, f), " ", (f: rt.Formatter) => rt.i32.display(v9, f), " ", rt.fmt.spec({ width: 5 }, (f: rt.Formatter) => rt.i32.display(v9, f)), "|", rt.fmt.spec({ align: "<", width: 5 }, (f: rt.Formatter) => rt.i32.display(v9, f)), "|", rt.fmt.spec({ align: "^", width: 7 }, (f: rt.Formatter) => rt.i32.display(v9, f)), "|", rt.fmt.spec({ align: ">", width: 4 }, (f: rt.Formatter) => rt.i32.display(v9, f)), "|", rt.fmt.spec({ fill: "*", align: "^", width: 9 }, (f: rt.Formatter) => rt.i32.display(v9, f)), "|", rt.fmt.spec({ fill: "é", align: ">", width: 6 }, (f: rt.Formatter) => rt.i32.display(v9, f)), "|", rt.fmt.spec({ zero: true, width: 5 }, (f: rt.Formatter) => rt.i32.display(v9, f)), "|", rt.fmt.spec({ align: "<", zero: true, width: 5 }, (f: rt.Formatter) => rt.i32.display(v9, f)), "|", (f: rt.Formatter) => rt.i32.lowerHex(v9, f), " ", (f: rt.Formatter) => rt.i32.upperHex(v9, f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.i32.lowerHex(v9, f)), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.i32.upperHex(v9, f)), " ", rt.fmt.spec({ zero: true, width: 8 }, (f: rt.Formatter) => rt.i32.lowerHex(v9, f)), " ", rt.fmt.spec({ alternate: true, zero: true, width: 10 }, (f: rt.Formatter) => rt.i32.lowerHex(v9, f)), " ", rt.fmt.spec({ align: ">", width: 8 }, (f: rt.Formatter) => rt.i32.lowerHex(v9, f)), "|", rt.fmt.spec({ align: "<", alternate: true, width: 6 }, (f: rt.Formatter) => rt.i32.upperHex(v9, f)), "|", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.i32.display(v9, f)), " ", rt.fmt.spec({ width: 3 }, (f: rt.Formatter) => rt.i32.display(v9, f))])); put(out, rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v9, f))); } else if (value === 9) { @@ -88,7 +88,7 @@ export function ints(kind: number, data: Uint8Array, out: rt.ByteVec): void { let buf10: Uint8Array = new Uint8Array(8); const n10 = rt.min(data11.length, buf10.length); rt.slice.copyFromSlice(rt.slice.range(buf10, 0, n10), rt.slice.range(data11, 0, n10)); - const v10 = rt.i64.fromBeBytes(rt.copy(buf10)); + const v10 = rt.i64.fromBeBytes(buf10); put(out, rt.fmt.format([(f: rt.Formatter) => rt.i64.display(v10, f), " ", (f: rt.Formatter) => rt.i64.display(v10, f), " ", rt.fmt.spec({ width: 5 }, (f: rt.Formatter) => rt.i64.display(v10, f)), "|", rt.fmt.spec({ align: "<", width: 5 }, (f: rt.Formatter) => rt.i64.display(v10, f)), "|", rt.fmt.spec({ align: "^", width: 7 }, (f: rt.Formatter) => rt.i64.display(v10, f)), "|", rt.fmt.spec({ align: ">", width: 4 }, (f: rt.Formatter) => rt.i64.display(v10, f)), "|", rt.fmt.spec({ fill: "*", align: "^", width: 9 }, (f: rt.Formatter) => rt.i64.display(v10, f)), "|", rt.fmt.spec({ fill: "é", align: ">", width: 6 }, (f: rt.Formatter) => rt.i64.display(v10, f)), "|", rt.fmt.spec({ zero: true, width: 5 }, (f: rt.Formatter) => rt.i64.display(v10, f)), "|", rt.fmt.spec({ align: "<", zero: true, width: 5 }, (f: rt.Formatter) => rt.i64.display(v10, f)), "|", (f: rt.Formatter) => rt.i64.lowerHex(v10, f), " ", (f: rt.Formatter) => rt.i64.upperHex(v10, f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.i64.lowerHex(v10, f)), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.i64.upperHex(v10, f)), " ", rt.fmt.spec({ zero: true, width: 8 }, (f: rt.Formatter) => rt.i64.lowerHex(v10, f)), " ", rt.fmt.spec({ alternate: true, zero: true, width: 10 }, (f: rt.Formatter) => rt.i64.lowerHex(v10, f)), " ", rt.fmt.spec({ align: ">", width: 8 }, (f: rt.Formatter) => rt.i64.lowerHex(v10, f)), "|", rt.fmt.spec({ align: "<", alternate: true, width: 6 }, (f: rt.Formatter) => rt.i64.upperHex(v10, f)), "|", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.i64.display(v10, f)), " ", rt.fmt.spec({ width: 3 }, (f: rt.Formatter) => rt.i64.display(v10, f))])); put(out, rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v10, f))); } else if (value === 10) { @@ -96,7 +96,7 @@ export function ints(kind: number, data: Uint8Array, out: rt.ByteVec): void { let buf11: Uint8Array = new Uint8Array(16); const n11 = rt.min(data12.length, buf11.length); rt.slice.copyFromSlice(rt.slice.range(buf11, 0, n11), rt.slice.range(data12, 0, n11)); - const v11 = rt.i128.fromBeBytes(rt.copy(buf11)); + const v11 = rt.i128.fromBeBytes(buf11); put(out, rt.fmt.format([(f: rt.Formatter) => rt.i128.display(v11, f), " ", (f: rt.Formatter) => rt.i128.display(v11, f), " ", rt.fmt.spec({ width: 5 }, (f: rt.Formatter) => rt.i128.display(v11, f)), "|", rt.fmt.spec({ align: "<", width: 5 }, (f: rt.Formatter) => rt.i128.display(v11, f)), "|", rt.fmt.spec({ align: "^", width: 7 }, (f: rt.Formatter) => rt.i128.display(v11, f)), "|", rt.fmt.spec({ align: ">", width: 4 }, (f: rt.Formatter) => rt.i128.display(v11, f)), "|", rt.fmt.spec({ fill: "*", align: "^", width: 9 }, (f: rt.Formatter) => rt.i128.display(v11, f)), "|", rt.fmt.spec({ fill: "é", align: ">", width: 6 }, (f: rt.Formatter) => rt.i128.display(v11, f)), "|", rt.fmt.spec({ zero: true, width: 5 }, (f: rt.Formatter) => rt.i128.display(v11, f)), "|", rt.fmt.spec({ align: "<", zero: true, width: 5 }, (f: rt.Formatter) => rt.i128.display(v11, f)), "|", (f: rt.Formatter) => rt.i128.lowerHex(v11, f), " ", (f: rt.Formatter) => rt.i128.upperHex(v11, f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.i128.lowerHex(v11, f)), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.i128.upperHex(v11, f)), " ", rt.fmt.spec({ zero: true, width: 8 }, (f: rt.Formatter) => rt.i128.lowerHex(v11, f)), " ", rt.fmt.spec({ alternate: true, zero: true, width: 10 }, (f: rt.Formatter) => rt.i128.lowerHex(v11, f)), " ", rt.fmt.spec({ align: ">", width: 8 }, (f: rt.Formatter) => rt.i128.lowerHex(v11, f)), "|", rt.fmt.spec({ align: "<", alternate: true, width: 6 }, (f: rt.Formatter) => rt.i128.upperHex(v11, f)), "|", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.i128.display(v11, f)), " ", rt.fmt.spec({ width: 3 }, (f: rt.Formatter) => rt.i128.display(v11, f))])); put(out, rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v11, f))); } else { @@ -104,7 +104,7 @@ export function ints(kind: number, data: Uint8Array, out: rt.ByteVec): void { let buf12: Uint8Array = new Uint8Array(4); const n12 = rt.min(data13.length, buf12.length); rt.slice.copyFromSlice(rt.slice.range(buf12, 0, n12), rt.slice.range(data13, 0, n12)); - const v12 = rt.isize.fromBeBytes(rt.copy(buf12)); + const v12 = rt.isize.fromBeBytes(buf12); put(out, rt.fmt.format([(f: rt.Formatter) => rt.isize.display(v12, f), " ", (f: rt.Formatter) => rt.isize.display(v12, f), " ", rt.fmt.spec({ width: 5 }, (f: rt.Formatter) => rt.isize.display(v12, f)), "|", rt.fmt.spec({ align: "<", width: 5 }, (f: rt.Formatter) => rt.isize.display(v12, f)), "|", rt.fmt.spec({ align: "^", width: 7 }, (f: rt.Formatter) => rt.isize.display(v12, f)), "|", rt.fmt.spec({ align: ">", width: 4 }, (f: rt.Formatter) => rt.isize.display(v12, f)), "|", rt.fmt.spec({ fill: "*", align: "^", width: 9 }, (f: rt.Formatter) => rt.isize.display(v12, f)), "|", rt.fmt.spec({ fill: "é", align: ">", width: 6 }, (f: rt.Formatter) => rt.isize.display(v12, f)), "|", rt.fmt.spec({ zero: true, width: 5 }, (f: rt.Formatter) => rt.isize.display(v12, f)), "|", rt.fmt.spec({ align: "<", zero: true, width: 5 }, (f: rt.Formatter) => rt.isize.display(v12, f)), "|", (f: rt.Formatter) => rt.isize.lowerHex(v12, f), " ", (f: rt.Formatter) => rt.isize.upperHex(v12, f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.isize.lowerHex(v12, f)), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.isize.upperHex(v12, f)), " ", rt.fmt.spec({ zero: true, width: 8 }, (f: rt.Formatter) => rt.isize.lowerHex(v12, f)), " ", rt.fmt.spec({ alternate: true, zero: true, width: 10 }, (f: rt.Formatter) => rt.isize.lowerHex(v12, f)), " ", rt.fmt.spec({ align: ">", width: 8 }, (f: rt.Formatter) => rt.isize.lowerHex(v12, f)), "|", rt.fmt.spec({ align: "<", alternate: true, width: 6 }, (f: rt.Formatter) => rt.isize.upperHex(v12, f)), "|", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.isize.display(v12, f)), " ", rt.fmt.spec({ width: 3 }, (f: rt.Formatter) => rt.isize.display(v12, f))])); put(out, rt.fmt.toString((f: rt.Formatter) => rt.isize.display(v12, f))); } @@ -637,7 +637,7 @@ export const Fault = { if (value === 0) { return { tag: "Short" }; } else if (value === 1) { - return { tag: "Code", _0: rt.u16.fromBeBytes(Uint8Array.of(at(1), at(2))) }; + return { tag: "Code", _0: (at(1) << 8) | at(2) }; } else if (value === 2) { return { tag: "TooLarge", size: (((at(1) << 24) >>> 0) | at(2)) >>> 0, max: at(3) }; } else { diff --git a/ts/fixtures/lang/expected/ints.ts b/ts/fixtures/lang/expected/ints.ts index c520ef7ad..b3a9f3912 100644 --- a/ts/fixtures/lang/expected/ints.ts +++ b/ts/fixtures/lang/expected/ints.ts @@ -8,12 +8,12 @@ export function u8Op(op: number, data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(1); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const [a, data3] = [rt.u8.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, data3] = [rt.u8.fromBeBytes(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; const data4 = data3; let buf2: Uint8Array = new Uint8Array(1); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); - const [b, data5] = [rt.u8.fromBeBytes(rt.copy(buf2)), rt.slice.range(data4, n2)] as [number, Uint8Array]; + const [b, data5] = [rt.u8.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [number, Uint8Array]; const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0), rt.u32.mul(2, 8)); const put = (out2: rt.ByteVec, v: number): void => { out2.extendFromSlice(rt.u128.toBeBytes(BigInt(v))); @@ -98,12 +98,12 @@ export function u16Op(op: number, data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(2); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const [a, data3] = [rt.u16.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, data3] = [rt.u16.fromBeBytes(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; const data4 = data3; let buf2: Uint8Array = new Uint8Array(2); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); - const [b, data5] = [rt.u16.fromBeBytes(rt.copy(buf2)), rt.slice.range(data4, n2)] as [number, Uint8Array]; + const [b, data5] = [rt.u16.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [number, Uint8Array]; const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0), rt.u32.mul(2, 16)); const put = (out2: rt.ByteVec, v: number): void => { out2.extendFromSlice(rt.u128.toBeBytes(BigInt(v))); @@ -188,12 +188,12 @@ export function u32Op(op: number, data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(4); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const [a, data3] = [rt.u32.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, data3] = [rt.u32.fromBeBytes(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; const data4 = data3; let buf2: Uint8Array = new Uint8Array(4); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); - const [b, data5] = [rt.u32.fromBeBytes(rt.copy(buf2)), rt.slice.range(data4, n2)] as [number, Uint8Array]; + const [b, data5] = [rt.u32.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [number, Uint8Array]; const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0), rt.u32.mul(2, 32)); const put = (out2: rt.ByteVec, v: number): void => { out2.extendFromSlice(rt.u128.toBeBytes(BigInt(v))); @@ -278,12 +278,12 @@ export function u64Op(op: number, data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(8); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const [a, data3] = [rt.u64.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [bigint, Uint8Array]; + const [a, data3] = [rt.u64.fromBeBytes(buf), rt.slice.range(data2, n)] as [bigint, Uint8Array]; const data4 = data3; let buf2: Uint8Array = new Uint8Array(8); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); - const [b, data5] = [rt.u64.fromBeBytes(rt.copy(buf2)), rt.slice.range(data4, n2)] as [bigint, Uint8Array]; + const [b, data5] = [rt.u64.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [bigint, Uint8Array]; const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0), rt.u32.mul(2, 64)); const put = (out2: rt.ByteVec, v: bigint): void => { out2.extendFromSlice(rt.u128.toBeBytes(v)); @@ -368,12 +368,12 @@ export function u128Op(op: number, data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(16); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const [a, data3] = [rt.u128.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [bigint, Uint8Array]; + const [a, data3] = [rt.u128.fromBeBytes(buf), rt.slice.range(data2, n)] as [bigint, Uint8Array]; const data4 = data3; let buf2: Uint8Array = new Uint8Array(16); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); - const [b, data5] = [rt.u128.fromBeBytes(rt.copy(buf2)), rt.slice.range(data4, n2)] as [bigint, Uint8Array]; + const [b, data5] = [rt.u128.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [bigint, Uint8Array]; const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0), rt.u32.mul(2, 128)); const put = (out2: rt.ByteVec, v: bigint): void => { out2.extendFromSlice(rt.u128.toBeBytes(v)); @@ -458,12 +458,12 @@ export function usizeOp(op: number, data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(4); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const [a, data3] = [rt.usize.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, data3] = [rt.usize.fromBeBytes(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; const data4 = data3; let buf2: Uint8Array = new Uint8Array(4); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); - const [b, data5] = [rt.usize.fromBeBytes(rt.copy(buf2)), rt.slice.range(data4, n2)] as [number, Uint8Array]; + const [b, data5] = [rt.usize.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [number, Uint8Array]; const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0), rt.u32.mul(2, 32)); const put = (out2: rt.ByteVec, v: number): void => { out2.extendFromSlice(rt.u128.toBeBytes(BigInt(v))); @@ -548,12 +548,12 @@ export function i8Op(op: number, data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(1); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const [a, data3] = [rt.i8.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, data3] = [rt.i8.fromBeBytes(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; const data4 = data3; let buf2: Uint8Array = new Uint8Array(1); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); - const [b, data5] = [rt.i8.fromBeBytes(rt.copy(buf2)), rt.slice.range(data4, n2)] as [number, Uint8Array]; + const [b, data5] = [rt.i8.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [number, Uint8Array]; const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0), rt.u32.mul(2, 8)); const put = (out2: rt.ByteVec, v: number): void => { out2.extendFromSlice(rt.i128.toBeBytes(BigInt(v))); @@ -638,12 +638,12 @@ export function i16Op(op: number, data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(2); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const [a, data3] = [rt.i16.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, data3] = [rt.i16.fromBeBytes(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; const data4 = data3; let buf2: Uint8Array = new Uint8Array(2); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); - const [b, data5] = [rt.i16.fromBeBytes(rt.copy(buf2)), rt.slice.range(data4, n2)] as [number, Uint8Array]; + const [b, data5] = [rt.i16.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [number, Uint8Array]; const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0), rt.u32.mul(2, 16)); const put = (out2: rt.ByteVec, v: number): void => { out2.extendFromSlice(rt.i128.toBeBytes(BigInt(v))); @@ -728,12 +728,12 @@ export function i32Op(op: number, data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(4); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const [a, data3] = [rt.i32.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, data3] = [rt.i32.fromBeBytes(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; const data4 = data3; let buf2: Uint8Array = new Uint8Array(4); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); - const [b, data5] = [rt.i32.fromBeBytes(rt.copy(buf2)), rt.slice.range(data4, n2)] as [number, Uint8Array]; + const [b, data5] = [rt.i32.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [number, Uint8Array]; const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0), rt.u32.mul(2, 32)); const put = (out2: rt.ByteVec, v: number): void => { out2.extendFromSlice(rt.i128.toBeBytes(BigInt(v))); @@ -818,12 +818,12 @@ export function i64Op(op: number, data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(8); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const [a, data3] = [rt.i64.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [bigint, Uint8Array]; + const [a, data3] = [rt.i64.fromBeBytes(buf), rt.slice.range(data2, n)] as [bigint, Uint8Array]; const data4 = data3; let buf2: Uint8Array = new Uint8Array(8); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); - const [b, data5] = [rt.i64.fromBeBytes(rt.copy(buf2)), rt.slice.range(data4, n2)] as [bigint, Uint8Array]; + const [b, data5] = [rt.i64.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [bigint, Uint8Array]; const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0), rt.u32.mul(2, 64)); const put = (out2: rt.ByteVec, v: bigint): void => { out2.extendFromSlice(rt.i128.toBeBytes(v)); @@ -908,12 +908,12 @@ export function i128Op(op: number, data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(16); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const [a, data3] = [rt.i128.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [bigint, Uint8Array]; + const [a, data3] = [rt.i128.fromBeBytes(buf), rt.slice.range(data2, n)] as [bigint, Uint8Array]; const data4 = data3; let buf2: Uint8Array = new Uint8Array(16); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); - const [b, data5] = [rt.i128.fromBeBytes(rt.copy(buf2)), rt.slice.range(data4, n2)] as [bigint, Uint8Array]; + const [b, data5] = [rt.i128.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [bigint, Uint8Array]; const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0), rt.u32.mul(2, 128)); const put = (out2: rt.ByteVec, v: bigint): void => { out2.extendFromSlice(rt.i128.toBeBytes(v)); @@ -998,12 +998,12 @@ export function isizeOp(op: number, data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(4); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const [a, data3] = [rt.isize.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, data3] = [rt.isize.fromBeBytes(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; const data4 = data3; let buf2: Uint8Array = new Uint8Array(4); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); - const [b, data5] = [rt.isize.fromBeBytes(rt.copy(buf2)), rt.slice.range(data4, n2)] as [number, Uint8Array]; + const [b, data5] = [rt.isize.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [number, Uint8Array]; const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0), rt.u32.mul(2, 32)); const put = (out2: rt.ByteVec, v: number): void => { out2.extendFromSlice(rt.i128.toBeBytes(BigInt(v))); @@ -1088,7 +1088,7 @@ export function i8Neg(data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(1); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const [a, ] = [rt.i8.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, ] = [rt.i8.fromBeBytes(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; out.extendFromSlice(rt.i128.toBeBytes(BigInt(rt.i8.neg(a)))); } @@ -1098,7 +1098,7 @@ export function i16Neg(data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(2); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const [a, ] = [rt.i16.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, ] = [rt.i16.fromBeBytes(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; out.extendFromSlice(rt.i128.toBeBytes(BigInt(rt.i16.neg(a)))); } @@ -1108,7 +1108,7 @@ export function i32Neg(data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(4); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const [a, ] = [rt.i32.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, ] = [rt.i32.fromBeBytes(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; out.extendFromSlice(rt.i128.toBeBytes(BigInt(rt.i32.neg(a)))); } @@ -1118,7 +1118,7 @@ export function i64Neg(data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(8); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const [a, ] = [rt.i64.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [bigint, Uint8Array]; + const [a, ] = [rt.i64.fromBeBytes(buf), rt.slice.range(data2, n)] as [bigint, Uint8Array]; out.extendFromSlice(rt.i128.toBeBytes(rt.i64.neg(a))); } @@ -1128,7 +1128,7 @@ export function i128Neg(data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(16); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const [a, ] = [rt.i128.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [bigint, Uint8Array]; + const [a, ] = [rt.i128.fromBeBytes(buf), rt.slice.range(data2, n)] as [bigint, Uint8Array]; out.extendFromSlice(rt.i128.toBeBytes(rt.i128.neg(a))); } @@ -1138,7 +1138,7 @@ export function isizeNeg(data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(4); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const [a, ] = [rt.isize.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, ] = [rt.isize.fromBeBytes(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; out.extendFromSlice(rt.i128.toBeBytes(BigInt(rt.isize.neg(a)))); } @@ -1150,7 +1150,7 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec let buf: Uint8Array = new Uint8Array(1); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const v = ([rt.u8.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [number, Uint8Array])[0]; + const v = ([rt.u8.fromBeBytes(buf), rt.slice.range(data2, n)] as [number, Uint8Array])[0]; const out2 = out; const value2 = rt.u8.rem(to, 12); if (value2 === 0) { @@ -1183,7 +1183,7 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec let buf2: Uint8Array = new Uint8Array(2); const n2 = rt.min(data3.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data3, 0, n2)); - const v2 = ([rt.u16.fromBeBytes(rt.copy(buf2)), rt.slice.range(data3, n2)] as [number, Uint8Array])[0]; + const v2 = ([rt.u16.fromBeBytes(buf2), rt.slice.range(data3, n2)] as [number, Uint8Array])[0]; const out3 = out; const value3 = rt.u8.rem(to, 12); if (value3 === 0) { @@ -1216,7 +1216,7 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec let buf3: Uint8Array = new Uint8Array(4); const n3 = rt.min(data4.length, buf3.length); rt.slice.copyFromSlice(rt.slice.range(buf3, 0, n3), rt.slice.range(data4, 0, n3)); - const v3 = ([rt.u32.fromBeBytes(rt.copy(buf3)), rt.slice.range(data4, n3)] as [number, Uint8Array])[0]; + const v3 = ([rt.u32.fromBeBytes(buf3), rt.slice.range(data4, n3)] as [number, Uint8Array])[0]; const out4 = out; const value4 = rt.u8.rem(to, 12); if (value4 === 0) { @@ -1249,7 +1249,7 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec let buf4: Uint8Array = new Uint8Array(8); const n4 = rt.min(data5.length, buf4.length); rt.slice.copyFromSlice(rt.slice.range(buf4, 0, n4), rt.slice.range(data5, 0, n4)); - const v4 = ([rt.u64.fromBeBytes(rt.copy(buf4)), rt.slice.range(data5, n4)] as [bigint, Uint8Array])[0]; + const v4 = ([rt.u64.fromBeBytes(buf4), rt.slice.range(data5, n4)] as [bigint, Uint8Array])[0]; const out5 = out; const value5 = rt.u8.rem(to, 12); if (value5 === 0) { @@ -1282,7 +1282,7 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec let buf5: Uint8Array = new Uint8Array(16); const n5 = rt.min(data6.length, buf5.length); rt.slice.copyFromSlice(rt.slice.range(buf5, 0, n5), rt.slice.range(data6, 0, n5)); - const v5 = ([rt.u128.fromBeBytes(rt.copy(buf5)), rt.slice.range(data6, n5)] as [bigint, Uint8Array])[0]; + const v5 = ([rt.u128.fromBeBytes(buf5), rt.slice.range(data6, n5)] as [bigint, Uint8Array])[0]; const out6 = out; const value6 = rt.u8.rem(to, 12); if (value6 === 0) { @@ -1315,7 +1315,7 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec let buf6: Uint8Array = new Uint8Array(4); const n6 = rt.min(data7.length, buf6.length); rt.slice.copyFromSlice(rt.slice.range(buf6, 0, n6), rt.slice.range(data7, 0, n6)); - const v6 = ([rt.usize.fromBeBytes(rt.copy(buf6)), rt.slice.range(data7, n6)] as [number, Uint8Array])[0]; + const v6 = ([rt.usize.fromBeBytes(buf6), rt.slice.range(data7, n6)] as [number, Uint8Array])[0]; const out7 = out; const value7 = rt.u8.rem(to, 12); if (value7 === 0) { @@ -1348,7 +1348,7 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec let buf7: Uint8Array = new Uint8Array(1); const n7 = rt.min(data8.length, buf7.length); rt.slice.copyFromSlice(rt.slice.range(buf7, 0, n7), rt.slice.range(data8, 0, n7)); - const v7 = ([rt.i8.fromBeBytes(rt.copy(buf7)), rt.slice.range(data8, n7)] as [number, Uint8Array])[0]; + const v7 = ([rt.i8.fromBeBytes(buf7), rt.slice.range(data8, n7)] as [number, Uint8Array])[0]; const out8 = out; const value8 = rt.u8.rem(to, 12); if (value8 === 0) { @@ -1381,7 +1381,7 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec let buf8: Uint8Array = new Uint8Array(2); const n8 = rt.min(data9.length, buf8.length); rt.slice.copyFromSlice(rt.slice.range(buf8, 0, n8), rt.slice.range(data9, 0, n8)); - const v8 = ([rt.i16.fromBeBytes(rt.copy(buf8)), rt.slice.range(data9, n8)] as [number, Uint8Array])[0]; + const v8 = ([rt.i16.fromBeBytes(buf8), rt.slice.range(data9, n8)] as [number, Uint8Array])[0]; const out9 = out; const value9 = rt.u8.rem(to, 12); if (value9 === 0) { @@ -1414,7 +1414,7 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec let buf9: Uint8Array = new Uint8Array(4); const n9 = rt.min(data10.length, buf9.length); rt.slice.copyFromSlice(rt.slice.range(buf9, 0, n9), rt.slice.range(data10, 0, n9)); - const v9 = ([rt.i32.fromBeBytes(rt.copy(buf9)), rt.slice.range(data10, n9)] as [number, Uint8Array])[0]; + const v9 = ([rt.i32.fromBeBytes(buf9), rt.slice.range(data10, n9)] as [number, Uint8Array])[0]; const out10 = out; const value10 = rt.u8.rem(to, 12); if (value10 === 0) { @@ -1447,7 +1447,7 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec let buf10: Uint8Array = new Uint8Array(8); const n10 = rt.min(data11.length, buf10.length); rt.slice.copyFromSlice(rt.slice.range(buf10, 0, n10), rt.slice.range(data11, 0, n10)); - const v10 = ([rt.i64.fromBeBytes(rt.copy(buf10)), rt.slice.range(data11, n10)] as [bigint, Uint8Array])[0]; + const v10 = ([rt.i64.fromBeBytes(buf10), rt.slice.range(data11, n10)] as [bigint, Uint8Array])[0]; const out11 = out; const value11 = rt.u8.rem(to, 12); if (value11 === 0) { @@ -1480,7 +1480,7 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec let buf11: Uint8Array = new Uint8Array(16); const n11 = rt.min(data12.length, buf11.length); rt.slice.copyFromSlice(rt.slice.range(buf11, 0, n11), rt.slice.range(data12, 0, n11)); - const v11 = ([rt.i128.fromBeBytes(rt.copy(buf11)), rt.slice.range(data12, n11)] as [bigint, Uint8Array])[0]; + const v11 = ([rt.i128.fromBeBytes(buf11), rt.slice.range(data12, n11)] as [bigint, Uint8Array])[0]; const out12 = out; const value12 = rt.u8.rem(to, 12); if (value12 === 0) { @@ -1513,7 +1513,7 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec let buf12: Uint8Array = new Uint8Array(4); const n12 = rt.min(data13.length, buf12.length); rt.slice.copyFromSlice(rt.slice.range(buf12, 0, n12), rt.slice.range(data13, 0, n12)); - const v12 = ([rt.isize.fromBeBytes(rt.copy(buf12)), rt.slice.range(data13, n12)] as [number, Uint8Array])[0]; + const v12 = ([rt.isize.fromBeBytes(buf12), rt.slice.range(data13, n12)] as [number, Uint8Array])[0]; const out13 = out; const value13 = rt.u8.rem(to, 12); if (value13 === 0) { @@ -1550,12 +1550,12 @@ export function convert(data: Uint8Array, out: rt.ByteVec): void { let buf: Uint8Array = new Uint8Array(8); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); - const [wide, data3] = [rt.u64.fromBeBytes(rt.copy(buf)), rt.slice.range(data2, n)] as [bigint, Uint8Array]; + const [wide, data3] = [rt.u64.fromBeBytes(buf), rt.slice.range(data2, n)] as [bigint, Uint8Array]; const data4 = data3; let buf2: Uint8Array = new Uint8Array(4); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); - const [signed, ] = [rt.i32.fromBeBytes(rt.copy(buf2)), rt.slice.range(data4, n2)] as [number, Uint8Array]; + const [signed, ] = [rt.i32.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [number, Uint8Array]; out.push(rt.u8.tryFrom(wide).ok ? 1 : 0); const tryFrom = rt.u32.tryFrom(wide); if (tryFrom.ok) { @@ -1583,6 +1583,21 @@ export function convert(data: Uint8Array, out: rt.ByteVec): void { out.extendFromSlice(rt.u64.toBeBytes(widened)); out.extendFromSlice(rt.i64.toBeBytes(into)); out.push(rt.u8.add(true ? 1 : 0, false ? 1 : 0)); + const toBeBytes = rt.u32.toBeBytes(signed >>> 0); + const a = toBeBytes[0]; + const b = toBeBytes[1]; + const c = toBeBytes[2]; + const d = toBeBytes[3]; + out.extendFromSlice(rt.i8.toBeBytes((a << 24) >> 24)); + out.extendFromSlice(rt.u16.toBeBytes((a << 8) | b)); + out.extendFromSlice(rt.i16.toBeBytes((((a << 8) | b) << 16) >> 16)); + out.extendFromSlice(rt.u32.toBeBytes(((a << 24) | (b << 16) | (c << 8) | d) >>> 0)); + out.extendFromSlice(rt.u32.toBeBytes(((b & 0x3f) << 16) | (c << 8) | d)); + out.extendFromSlice(rt.i32.toBeBytes((a << 24) | (c << 8) | d)); + out.extendFromSlice(rt.u32.toBeBytes(0)); + out.extendFromSlice(rt.u64.toBeBytes(rt.u64.fromBeBytes(Uint8Array.of(d, c, b, a, a, b, c, d)))); + const bytes = Uint8Array.of(d, c, b, a); + out.extendFromSlice(rt.i32.toBeBytes(rt.i32.fromBeBytes(bytes))); } //# sourceMappingURL=ints.ts.map diff --git a/ts/fixtures/lang/src/ints.rs b/ts/fixtures/lang/src/ints.rs index fa99c9ce9..fe6b925ed 100644 --- a/ts/fixtures/lang/src/ints.rs +++ b/ts/fixtures/lang/src/ints.rs @@ -163,4 +163,16 @@ pub fn convert(data: &[u8], out: &mut Vec) { out.extend_from_slice(&widened.to_be_bytes()); out.extend_from_slice(&into.to_be_bytes()); out.push(true as u8 + false as u8); + // Arrays written in place, with constant and sign bytes, and one behind a reference. + let [a, b, c, d] = (signed as u32).to_be_bytes(); + out.extend_from_slice(&i8::from_be_bytes([a]).to_be_bytes()); + out.extend_from_slice(&u16::from_be_bytes([a, b]).to_be_bytes()); + out.extend_from_slice(&i16::from_be_bytes([a, b]).to_be_bytes()); + out.extend_from_slice(&u32::from_be_bytes([a, b, c, d]).to_be_bytes()); + out.extend_from_slice(&u32::from_be_bytes([0, b & 0x3f, c, d]).to_be_bytes()); + out.extend_from_slice(&i32::from_be_bytes([a, 0, c, d]).to_be_bytes()); + out.extend_from_slice(&u32::from_be_bytes([0, 0, 0, 0]).to_be_bytes()); + out.extend_from_slice(&u64::from_be_bytes([d, c, b, a, a, b, c, d]).to_be_bytes()); + let bytes = &[d, c, b, a]; + out.extend_from_slice(&i32::from_be_bytes(*bytes).to_be_bytes()); } diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index f65a8ab68..6847a9868 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -64,85 +64,122 @@ export interface Int { upperHex(a: T, f: Formatter): FmtResult; } -/** The exact arithmetic of a representation, before its result is checked. */ -interface Exact { - add(a: T, b: T): T; - sub(a: T, b: T): T; - mul(a: T, b: T): T; - neg(a: T): T; -} - function shift(b: number | bigint, bits: number, direction: string): number { const n = Number(b); if (n < 0 || n >= bits) panic(`attempt to shift ${direction} with overflow`); return n; } -function int( - bits: number, - signed: boolean, - of: (value: number | bigint) => T, - wrap: (value: bigint) => T, - exact: Exact, -): Int { - const min = signed ? -(1n << BigInt(bits - 1)) : 0n; - const max = signed ? (1n << BigInt(bits - 1)) - 1n : (1n << BigInt(bits)) - 1n; - const fits = (value: number | bigint) => value >= min && value <= max; - const checked = (value: T, op: string): T => { - if (!fits(value)) panic(`attempt to ${op} with overflow`); +/** + * An integer of 32 bits or less, in a number. Sums, differences, and quotients are + * exact; a product is inexact only past 2^53, far outside every type's range, so its + * check still fails. JS's 32-bit operators do the rest. + */ +function small(bits: number, signed: boolean): Int { + const min = signed ? -(2 ** (bits - 1)) : 0; + const max = signed ? 2 ** (bits - 1) - 1 : 2 ** bits - 1; + const mask = 2 ** bits - 1; + // What the low `bits` bits read as. + const wrap: (value: number) => number = + bits === 32 ? (signed ? (v) => v | 0 : (v) => v >>> 0) : signed ? (v) => (v << (32 - bits)) >> (32 - bits) : (v) => v & mask; + // The bits as unsigned, for leading zeros and hex. + const unsigned = bits === 32 ? (v: number) => v >>> 0 : (v: number) => v & mask; + const checked = (value: number, op: string): number => { + if (value < min || value > max) panic(`attempt to ${op} with overflow`); return value; }; - const big = (value: T) => BigInt(value); - const unsigned = (value: T) => BigInt.asUintN(bits, big(value)); + const clamp = (value: number) => (value < min ? min : value > max ? max : value); + const inRange = (value: number) => (value >= min && value <= max ? value : undefined); return { - MIN: of(min), - MAX: of(max), + MIN: min, + MAX: max, BITS: bits, - add: (a, b) => checked(exact.add(a, b), "add"), - sub: (a, b) => checked(exact.sub(a, b), "subtract"), - mul: (a, b) => checked(exact.mul(a, b), "multiply"), - neg: (a) => checked(exact.neg(a), "negate"), + add: (a, b) => checked(a + b, "add"), + sub: (a, b) => checked(a - b, "subtract"), + mul: (a, b) => checked(a * b, "multiply"), + neg: (a) => checked(-a, "negate"), div(a, b) { - if (big(b) === 0n) panic("attempt to divide by zero"); - return checked(of(big(a) / big(b)), "divide"); + if (b === 0) panic("attempt to divide by zero"); + return checked(Math.trunc(a / b), "divide"); }, rem(a, b) { - if (big(b) === 0n) panic("attempt to calculate the remainder with a divisor of zero"); - if (signed && big(b) === -1n && big(a) === min) panic("attempt to calculate the remainder with overflow"); - return of(big(a) % big(b)); - }, - shl(a, b) { - return wrap(big(a) << BigInt(shift(b, bits, "left"))); - }, - shr(a, b) { - return of(big(a) >> BigInt(shift(b, bits, "right"))); + if (b === 0) panic("attempt to calculate the remainder with a divisor of zero"); + if (signed && b === -1 && a === min) panic("attempt to calculate the remainder with overflow"); + return a % b; }, - checkedAdd(a, b) { - const sum = big(a) + big(b); - return fits(sum) ? of(sum) : undefined; - }, - checkedSub(a, b) { - const diff = big(a) - big(b); - return fits(diff) ? of(diff) : undefined; - }, - checkedMul(a, b) { - const product = big(a) * big(b); - return fits(product) ? of(product) : undefined; + shl: (a, b) => wrap(a << shift(b, bits, "left")), + shr: (a, b) => (signed ? a >> shift(b, bits, "right") : a >>> shift(b, bits, "right")), + checkedAdd: (a, b) => inRange(a + b), + checkedSub: (a, b) => inRange(a - b), + checkedMul: (a, b) => inRange(a * b), + saturatingAdd: (a, b) => clamp(a + b), + saturatingSub: (a, b) => clamp(a - b), + wrappingAdd: (a, b) => wrap(a + b), + wrappingSub: (a, b) => wrap(a - b), + leadingOnes: (a) => Math.clz32(~(a << (32 - bits))), + leadingZeros: (a) => Math.clz32(unsigned(a)) - (32 - bits), + fromBeBytes(bytes) { + let value = 0; + for (let i = 0; i < bytes.length; i++) value = (value << 8) | bytes[i]; + return wrap(value); }, - saturatingAdd(a, b) { - const sum = big(a) + big(b); - return of(sum < min ? min : sum > max ? max : sum); + toBeBytes(a) { + const out = new Uint8Array(bits / 8); + for (let i = out.length - 1; i >= 0; i--) { + out[i] = a; + a >>= 8; + } + return out; }, - saturatingSub(a, b) { - const diff = big(a) - big(b); - return of(diff < min ? min : diff > max ? max : diff); + tryFrom(value) { + if (value >= min && value <= max) return Ok(Number(value)); + return Err(new TryFromIntError(value < min ? "NegOverflow" : "PosOverflow")); }, - wrappingAdd(a, b) { - return wrap(big(a) + big(b)); + display: (a, f) => f.padIntegral(a >= 0, "", String(a < 0 ? -a : a)), + lowerHex: (a, f) => f.padIntegral(true, "0x", unsigned(a).toString(16)), + upperHex: (a, f) => f.padIntegral(true, "0x", unsigned(a).toString(16).toUpperCase()), + }; +} + +/** An integer wider than 32 bits, in a bigint. */ +function wide(bits: number, signed: boolean): Int { + const min = signed ? -(1n << BigInt(bits - 1)) : 0n; + const max = signed ? (1n << BigInt(bits - 1)) - 1n : (1n << BigInt(bits)) - 1n; + const wrap = (value: bigint) => (signed ? BigInt.asIntN(bits, value) : BigInt.asUintN(bits, value)); + const unsigned = (value: bigint) => BigInt.asUintN(bits, value); + const fits = (value: number | bigint) => value >= min && value <= max; + const checked = (value: bigint, op: string): bigint => { + if (!fits(value)) panic(`attempt to ${op} with overflow`); + return value; + }; + const clamp = (value: bigint) => (value < min ? min : value > max ? max : value); + const inRange = (value: bigint) => (fits(value) ? value : undefined); + return { + MIN: min, + MAX: max, + BITS: bits, + add: (a, b) => checked(a + b, "add"), + sub: (a, b) => checked(a - b, "subtract"), + mul: (a, b) => checked(a * b, "multiply"), + neg: (a) => checked(-a, "negate"), + div(a, b) { + if (b === 0n) panic("attempt to divide by zero"); + return checked(a / b, "divide"); }, - wrappingSub(a, b) { - return wrap(big(a) - big(b)); + rem(a, b) { + if (b === 0n) panic("attempt to calculate the remainder with a divisor of zero"); + if (signed && b === -1n && a === min) panic("attempt to calculate the remainder with overflow"); + return a % b; }, + shl: (a, b) => wrap(a << BigInt(shift(b, bits, "left"))), + shr: (a, b) => a >> BigInt(shift(b, bits, "right")), + checkedAdd: (a, b) => inRange(a + b), + checkedSub: (a, b) => inRange(a - b), + checkedMul: (a, b) => inRange(a * b), + saturatingAdd: (a, b) => clamp(a + b), + saturatingSub: (a, b) => clamp(a - b), + wrappingAdd: (a, b) => wrap(a + b), + wrappingSub: (a, b) => wrap(a - b), leadingOnes(a) { let n = 0; for (let bit = BigInt(bits - 1); bit >= 0n && (unsigned(a) >> bit) & 1n; bit--) n++; @@ -153,62 +190,38 @@ function int( for (let bit = BigInt(bits - 1); bit >= 0n && !((unsigned(a) >> bit) & 1n); bit--) n++; return n; }, + // A 32-bit word at a time: one bigint step per word, not per byte. fromBeBytes(bytes) { let value = 0n; - for (const byte of bytes) value = (value << 8n) | BigInt(byte); + for (let i = 0; i < bytes.length; i += 4) { + const word = ((bytes[i] << 24) | (bytes[i + 1] << 16) | (bytes[i + 2] << 8) | bytes[i + 3]) >>> 0; + value = (value << 32n) | BigInt(word); + } return wrap(value); }, toBeBytes(a) { const out = new Uint8Array(bits / 8); let value = unsigned(a); - for (let i = out.length - 1; i >= 0; i--) { - out[i] = Number(value & 0xffn); - value >>= 8n; + for (let i = out.length - 4; i >= 0; i -= 4) { + const word = Number(BigInt.asUintN(32, value)); + out[i] = word >>> 24; + out[i + 1] = word >>> 16; + out[i + 2] = word >>> 8; + out[i + 3] = word; + value >>= 32n; } return out; }, tryFrom(value) { - if (fits(value)) return Ok(of(value)); + if (fits(value)) return Ok(BigInt(value)); return Err(new TryFromIntError(value < min ? "NegOverflow" : "PosOverflow")); }, - display(a, f) { - const value = big(a); - return f.padIntegral(value >= 0n, "", (value < 0n ? -value : value).toString()); - }, - lowerHex(a, f) { - return f.padIntegral(true, "0x", unsigned(a).toString(16)); - }, - upperHex(a, f) { - return f.padIntegral(true, "0x", unsigned(a).toString(16).toUpperCase()); - }, + display: (a, f) => f.padIntegral(a >= 0n, "", (a < 0n ? -a : a).toString()), + lowerHex: (a, f) => f.padIntegral(true, "0x", unsigned(a).toString(16)), + upperHex: (a, f) => f.padIntegral(true, "0x", unsigned(a).toString(16).toUpperCase()), }; } -const numbers: Exact = { - add: (a, b) => a + b, - sub: (a, b) => a - b, - // Inexact only past 2^53, far outside every type's range, so the check still fails. - mul: (a, b) => a * b, - neg: (a) => -a, -}; - -const bigints: Exact = { - add: (a, b) => a + b, - sub: (a, b) => a - b, - mul: (a, b) => a * b, - neg: (a) => -a, -}; - -function small(bits: number, signed: boolean): Int { - const wrap = (value: bigint) => Number(signed ? BigInt.asIntN(bits, value) : BigInt.asUintN(bits, value)); - return int(bits, signed, Number, wrap, numbers); -} - -function wide(bits: number, signed: boolean): Int { - const wrap = (value: bigint) => (signed ? BigInt.asIntN(bits, value) : BigInt.asUintN(bits, value)); - return int(bits, signed, BigInt, wrap, bigints); -} - export const u8: Int = small(8, false); export const u16: Int = small(16, false); export const u32: Int = small(32, false); @@ -421,7 +434,9 @@ export class ByteVec { const needed = this.length + additional; if (needed <= this.data.length) return; const grown = new Uint8Array(Math.max(needed, this.data.length * 2, 8)); - grown.set(this.view()); + // All of `data`, not a view of it: a view of a small array makes V8 move it off its + // heap, which costs more than copying the few bytes past `length`. + grown.set(this.data); this.data = grown; } diff --git a/ts/src/lower/body.rs b/ts/src/lower/body.rs index 8524219a0..1caeede49 100644 --- a/ts/src/lower/body.rs +++ b/ts/src/lower/body.rs @@ -558,11 +558,17 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { .enumerate() .all(|(i, f)| f.field.index() == i && self.destructurable(&f.pattern, false)) } + // Bytes are a `Uint8Array`, which JS destructures through its iterator: indexing + // reads them faster. PatKind::Array { prefix, slice: None, suffix, - } => suffix.is_empty() && prefix.iter().all(|p| self.destructurable(p, false)), + } => { + !matches!(ty.kind(), ty::Array(elem, _) if matches!(elem.kind(), ty::Uint(ty::UintTy::U8))) + && suffix.is_empty() + && prefix.iter().all(|p| self.destructurable(p, false)) + } PatKind::Binding { mode, subpattern: None, .. } if !top => match mode.0 { diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 3ba20bbec..2b5cd8830 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -184,6 +184,9 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Args<'_>, "wrapping_sub" => "wrappingSub", _ => return None, }; + if let ("from_be_bytes", Args::Thir([bytes])) = (name.as_str(), &args) { + return Some(from_be_bytes(b, int, *bytes, out)); + } let values = b.args(args, out); return Some(int.method(method, values)); } @@ -659,6 +662,49 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Args<'_>, Some(e) } +/// `int::from_be_bytes(bytes)`. A number from an array written in place is its bytes +/// shifted and or-ed, with no array made. Otherwise the runtime reads the array the +/// argument names, uncopied, since the read neither keeps nor changes it. +fn from_be_bytes<'tcx>(b: &mut Body<'_, '_, 'tcx>, int: Int, bytes: ExprId, out: &mut Vec) -> Expr { + let thir = b.thir; + let fields = match &thir[b.strip(bytes)].kind { + ExprKind::Array { fields } if !int.big() => fields, + _ => { + let value = b.place(bytes, out); + return int.method("fromBeBytes", vec![value]); + } + }; + let values = b.operands(fields, out); + let last = values.len() - 1; + let mut word: Option = None; + let mut top = false; + for (i, value) in values.into_iter().enumerate() { + if matches!(&value, Expr::Lit(l) if l == "0") { + continue; + } + top |= i == 0; + let shift = 8 * (last - i); + let term = match shift { + 0 => value, + _ => value.binary("<<", Expr::lit(shift.to_string())), + }; + word = Some(match word { + Some(word) => word.binary("|", term), + None => term, + }); + } + let Some(word) = word else { + return Expr::lit("0"); + }; + // JS's `|` and `<<` make an `i32`, so only a `u32` with its top byte set and the + // narrower signed types need their bits read again. + match (int.signed, int.bits) { + (false, 32) if top => int.wrap(word), + (true, bits) if bits < 32 => int.wrap(word), + _ => word, + } +} + /// The `n`th type among a call's generic arguments, skipping lifetimes. fn targ<'tcx>(gargs: GenericArgsRef<'tcx>, n: usize) -> Ty<'tcx> { gargs.types().nth(n).expect("a type argument") diff --git a/ts/src/lower/num.rs b/ts/src/lower/num.rs index 3c4d227d3..49515845a 100644 --- a/ts/src/lower/num.rs +++ b/ts/src/lower/num.rs @@ -104,7 +104,7 @@ impl Int { } /// `value`, which is exact, wrapped into this type's range. - fn wrap(self, value: Expr) -> Expr { + pub fn wrap(self, value: Expr) -> Expr { match (self.big(), self.signed, self.bits) { (true, false, bits) => big_int("asUintN", bits, value), (true, true, bits) => big_int("asIntN", bits, value),