From 7a1dcc0caa3605f1880c06f25552267fd82e357f Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 05:18:16 -0700 Subject: [PATCH 1/8] chore(quest): claim quest/m2/ts-wide-ints.md Quest-Claim: c50f424a11193534 From 90a88c2508837ffc787e05899b86b1cc2f21d914 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 05:52:03 -0700 Subject: [PATCH 2/8] feat(ts): 64- and 128-bit integers as objects of 32-bit limbs (checkpoint) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/fixtures/codec/expected/bench.ts | 20 +- ts/fixtures/codec/expected/coding/decode.ts | 10 +- ts/fixtures/codec/expected/coding/encode.ts | 14 +- ts/fixtures/codec/expected/coding/varint.ts | 46 +-- ts/fixtures/codec/expected/coding/version.ts | 11 +- ts/fixtures/codec/expected/harness.ts | 32 +- ts/fixtures/codec/expected/lite/group.ts | 14 +- ts/fixtures/codec/expected/lite/probe.ts | 26 +- ts/fixtures/lang/expected/fmt.ts | 28 +- ts/fixtures/lang/expected/ints.ts | 396 ++++++++++--------- ts/fixtures/lang/expected/shapes.ts | 52 +-- ts/runtime/rt.ts | 387 +++++++++++++++--- ts/src/lower/body.rs | 24 +- ts/src/lower/consts.rs | 9 +- ts/src/lower/library.rs | 12 +- ts/src/lower/mod.rs | 7 +- ts/src/lower/num.rs | 162 +++++--- ts/src/lower/pat.rs | 14 +- ts/tests/differential.ts | 48 ++- ts/tests/fixtures.rs | 10 +- 20 files changed, 843 insertions(+), 479 deletions(-) diff --git a/ts/fixtures/codec/expected/bench.ts b/ts/fixtures/codec/expected/bench.ts index 38e047b2f..7b1a1f487 100644 --- a/ts/fixtures/codec/expected/bench.ts +++ b/ts/fixtures/codec/expected/bench.ts @@ -58,7 +58,7 @@ export function encodeVarints(data: Uint8Array, form_: Form, out: rt.ByteVec): r const [count, data2] = takeU64(data); const [value, ] = takeU64(data2); let w: Encoder = Encoder.new(out, form_); - const end = Number(BigInt.asUintN(32, count)); + const end = count.lo; for (let _ = 0; _ < end; _++) { const varint = w.varint(value); if (!varint.ok) { @@ -72,7 +72,7 @@ export function encodeVarints(data: Uint8Array, form_: Form, out: rt.ByteVec): r export function decodeVarints(wire: Uint8Array, form_: Form, out: rt.ByteVec): rt.Result { let r: Decoder = Decoder.new(wire, form_); let count: number = 0; - let last: bigint = 0n; + let last: rt.U64 = rt.u64.of(0); while (!r.isEmpty()) { const varint = r.varint(); if (!varint.ok) { @@ -98,14 +98,14 @@ export function encodeFrames(data: Uint8Array, version: Version, out: rt.ByteVec const [size, data5] = takeU64(data4); const [keyEvery, data6] = takeU64(data5); const [keySize, ] = takeU64(data6); - const [delta2, keyEvery2] = [BigInt.asIntN(64, delta), Number(BigInt.asUintN(32, keyEvery))] as [bigint, number]; + const [delta2, keyEvery2] = [rt.i64.cast(delta), keyEvery.lo] as [rt.I64, number]; let w: Encoder = Encoder.new(out, Form.from(version)); - const end = Number(BigInt.asUintN(32, count)); + const end = count.lo; for (let n = 0; n < end; n++) { - let value: [bigint, bigint]; + let value: [rt.I64, rt.U64]; arms: { if (n === 0) { - value = [0n, first]; + value = [rt.i64.of(0), first]; break arms; } const n2 = n; @@ -132,8 +132,8 @@ export function encodeFrames(data: Uint8Array, version: Version, out: rt.ByteVec export function decodeFrames(wire: Uint8Array, version: Version, out: rt.ByteVec): rt.Result { let r: Decoder = Decoder.new(wire, Form.from(version)); let count: number = 0; - let total: bigint = 0n; - let last: bigint = 0n; + let total: rt.U64 = rt.u64.of(0); + let last: rt.I64 = rt.i64.of(0); while (!r.isEmpty()) { const varint = r.varint(); if (!varint.ok) { @@ -149,7 +149,7 @@ export function decodeFrames(wire: Uint8Array, version: Version, out: rt.ByteVec } putUsize(out, count); putU64(out, total); - putU64(out, BigInt.asUintN(64, last)); + putU64(out, rt.u64.cast(last)); return rt.Ok(undefined); } @@ -157,7 +157,7 @@ export function decodeFrames(wire: Uint8Array, version: Version, out: rt.ByteVec export function encodeGroup(data: Uint8Array, version: Version, out: rt.ByteVec): rt.Result { const [subscribe, data2] = takeU64(data); const [sequence, ] = takeU64(data2); - const group = new Group({ subscribe, sequence, frameStart: 0n }); + const group = new Group({ subscribe, sequence, frameStart: rt.u64.of(0) }); return group.encode(Encoder.new(out, Form.from(version)), version); } diff --git a/ts/fixtures/codec/expected/coding/decode.ts b/ts/fixtures/codec/expected/coding/decode.ts index 1bc86e3ef..4902c56be 100644 --- a/ts/fixtures/codec/expected/coding/decode.ts +++ b/ts/fixtures/codec/expected/coding/decode.ts @@ -10,7 +10,7 @@ export type DecodeError = | { tag: "Short" } | { tag: "Long" } | { tag: "InvalidString"; _0: rt.FromUtf8Error } - | { tag: "InvalidMessage"; _0: bigint } + | { tag: "InvalidMessage"; _0: rt.U64 } | { tag: "InvalidSubscribeLocation" } | { tag: "InvalidValue" } | { tag: "TooMany" } @@ -41,7 +41,7 @@ export const DecodeError = { return __formatter.writeStr("invalid string"); } else if (self.tag === "InvalidMessage") { const _0 = self._0; - const value = [_0] as [bigint]; + const value = [_0] as [rt.U64]; const __field0 = value[0]; return __formatter.writeFmt(["invalid message: ", (f: rt.Formatter) => rt.u64.display(__field0, f)]); } else if (self.tag === "InvalidSubscribeLocation") { @@ -200,7 +200,7 @@ export class Decoder { } /** Read a varint. */ - varint(): rt.Result { + varint(): rt.Result { const read_ = read(this.buf, this.form); if (read_.ok) { const value = read_.value[0]; @@ -250,12 +250,12 @@ export class Decoder { } /** Read an optional varint: 0 is `None`, and `n + 1` is `Some(n)`. */ - varintOpt(): rt.Result { + varintOpt(): rt.Result { const varint = this.varint(); if (!varint.ok) { return rt.Err(varint.error); } - return rt.Ok(rt.u64.checkedSub(varint.value, 1n)); + return rt.Ok(rt.u64.checkedSub(varint.value, rt.u64.of(1))); } /** Read a varint length, then that many raw bytes. */ diff --git a/ts/fixtures/codec/expected/coding/encode.ts b/ts/fixtures/codec/expected/coding/encode.ts index 4ede16b1e..ada80089e 100644 --- a/ts/fixtures/codec/expected/coding/encode.ts +++ b/ts/fixtures/codec/expected/coding/encode.ts @@ -79,7 +79,7 @@ export class Encoder { } /** Write a varint, or fail with [`EncodeError::BoundsExceeded`] if the form cannot carry it. */ - varint(v: bigint): rt.Result { + varint(v: rt.U64): rt.Result { const write_ = write(v, this.form, this.buf); if (!write_.ok) { return rt.Err(EncodeError.from(write_.error)); @@ -88,24 +88,24 @@ export class Encoder { } /** Write an optional varint: `None` as 0, and `Some(n)` as `n + 1`. */ - varintOpt(v: bigint | undefined): rt.Result { - let v2: bigint; + varintOpt(v: rt.U64 | undefined): rt.Result { + let v2: rt.U64; if (v !== undefined) { const v3 = v; - const okOr = rt.option.okOr(rt.u64.checkedAdd(v3, 1n), "TooLarge" as EncodeError); + const okOr = rt.option.okOr(rt.u64.checkedAdd(v3, rt.u64.of(1)), "TooLarge" as EncodeError); if (!okOr.ok) { return rt.Err(okOr.error); } v2 = okOr.value; } else { - v2 = 0n; + v2 = rt.u64.of(0); } return this.varint(v2); } /** Write a varint length, then the raw bytes. */ bytes(v: Uint8Array): rt.Result { - const varint = this.varint(BigInt(v.length)); + const varint = this.varint(rt.u64.cast(v.length)); if (!varint.ok) { return rt.Err(varint.error); } @@ -153,7 +153,7 @@ export class Encoder { const size = mapErr.value; rt.slice.copyFromSlice(rt.slice.range(this.buf.view(), rt.usize.sub(body, 2), body), rt.u16.toBeBytes(size)); } else { - const write_ = write(BigInt(rt.usize.sub(end, body)), this.form, this.buf); + const write_ = write(rt.u64.cast(rt.usize.sub(end, body)), this.form, this.buf); if (!write_.ok) { return rt.Err(EncodeError.from(write_.error)); } diff --git a/ts/fixtures/codec/expected/coding/varint.ts b/ts/fixtures/codec/expected/coding/varint.ts index 5aee013c7..baf70306b 100644 --- a/ts/fixtures/codec/expected/coding/varint.ts +++ b/ts/fixtures/codec/expected/coding/varint.ts @@ -30,17 +30,17 @@ export const Form = { }; /** The high and low 32 bits: the only place the codec splits a `u64`. */ -export function toHalves(value: bigint): [number, number] { - return [Number(BigInt.asUintN(32, value >> 32n)), Number(BigInt.asUintN(32, value))]; +export function toHalves(value: rt.U64): [number, number] { + return [rt.u64.shr(value, 32).lo, value.lo]; } /** The inverse of [`to_halves`]. */ -export function fromHalves(hi: number, lo: number): bigint { - return BigInt.asUintN(64, BigInt(hi) << 32n) | BigInt(lo); +export function fromHalves(hi: number, lo: number): rt.U64 { + return rt.u64.or(rt.u64.shl(rt.u64.cast(hi), 32), rt.u64.cast(lo)); } /** The bytes `value` takes on the wire in `form`, or [`BoundsExceeded`] if it does not fit. */ -export function size(value: bigint, form: Form): rt.Result { +export function size(value: rt.U64, form: Form): rt.Result { const [hi, lo] = toHalves(value); let value2: number; if (form.tag === "Quic" && hi === 0 && lo < (1 << 6)) { @@ -78,7 +78,7 @@ export function size(value: bigint, form: Form): rt.Result { +export function write(value: rt.U64, form: Form, out: rt.ByteVec): rt.Result { if (form.tag === "Quic") { return writeQuic(value, out); } else { @@ -87,7 +87,7 @@ export function write(value: bigint, form: Form, out: rt.ByteVec): rt.Result { +export function writeQuic(value: rt.U64, out: rt.ByteVec): rt.Result { const [hi, lo] = toHalves(value); if (hi === 0 && lo < (1 << 6)) { out.push(lo & 0xff); @@ -113,7 +113,7 @@ export function writeQuic(value: bigint, out: rt.ByteVec): rt.Result { +export function read(buf: Uint8Array, form: Form): rt.Result<[rt.U64, Uint8Array], DecodeError> { if (form.tag === "Quic") { return readQuic(buf); } else { @@ -158,7 +158,7 @@ export function read(buf: Uint8Array, form: Form): rt.Result<[bigint, Uint8Array } } -export function readQuic(buf: Uint8Array): rt.Result<[bigint, Uint8Array], DecodeError> { +export function readQuic(buf: Uint8Array): rt.Result<[rt.U64, Uint8Array], DecodeError> { const splitFirst = rt.slice.splitFirst(buf); if (splitFirst === undefined) { return rt.Err({ tag: "Short" }); @@ -166,7 +166,7 @@ export function readQuic(buf: Uint8Array): rt.Result<[bigint, Uint8Array], Decod const first = splitFirst[0]; const rest = splitFirst[1]; if (first < 0x40) { - return rt.Ok([BigInt(first), rest]); + return rt.Ok([rt.u64.cast(first), rest]); } else if (first < 0x80) { const okOr = rt.option.okOr(rt.slice.splitFirstChunk(buf, 2), { tag: "Short" } as DecodeError); if (!okOr.ok) { @@ -175,7 +175,7 @@ export function readQuic(buf: Uint8Array): rt.Result<[bigint, Uint8Array], Decod 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]); + return rt.Ok([rt.u64.cast(((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) { @@ -186,7 +186,7 @@ export function readQuic(buf: Uint8Array): rt.Result<[bigint, Uint8Array], Decod 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]); + return rt.Ok([rt.u64.cast((((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) { @@ -202,17 +202,17 @@ export function readQuic(buf: Uint8Array): rt.Result<[bigint, Uint8Array], Decod } } -export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[bigint, Uint8Array], DecodeError> { +export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[rt.U64, Uint8Array], DecodeError> { const splitFirst = rt.slice.splitFirst(buf); if (splitFirst === undefined) { return rt.Err({ tag: "Short" }); } const first = splitFirst[0]; const rest = splitFirst[1]; - let value: [bigint, Uint8Array]; + let value: [rt.U64, Uint8Array]; const leadingOnes = rt.u8.leadingOnes(first); if (leadingOnes === 0) { - value = [BigInt(first), rest]; + value = [rt.u64.cast(first), rest]; } else if (leadingOnes === 1) { const okOr = rt.option.okOr(rt.slice.splitFirstChunk(buf, 2), { tag: "Short" } as DecodeError); if (!okOr.ok) { @@ -221,7 +221,7 @@ export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[big const a = okOr.value[0][0]; const b = okOr.value[0][1]; const rest2 = okOr.value[1]; - value = [BigInt(((a & 0x3f) << 8) | b), rest2]; + value = [rt.u64.cast(((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) { @@ -231,7 +231,7 @@ export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[big 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]; + value = [rt.u64.cast(((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) { @@ -242,7 +242,7 @@ export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[big 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]; + value = [rt.u64.cast((((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) { @@ -307,13 +307,13 @@ export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[big * * Small magnitudes stay small (-1 -> 1, 1 -> 2, -2 -> 3, ...), and all of `i64` fits. */ -export function zigzag(signed: bigint): bigint { - return BigInt.asUintN(64, BigInt.asIntN(64, signed << 1n) ^ (signed >> 63n)); +export function zigzag(signed: rt.I64): rt.U64 { + return rt.u64.cast(rt.i64.xor(rt.i64.shl(signed, 1), rt.i64.shr(signed, 63))); } /** The inverse of [`zigzag`]. */ -export function unzigzag(value: bigint): bigint { - return BigInt.asIntN(64, value >> 1n) ^ rt.i64.neg(BigInt.asIntN(64, value & 1n)); +export function unzigzag(value: rt.U64): rt.I64 { + return rt.i64.xor(rt.i64.cast(rt.u64.shr(value, 1)), rt.i64.neg(rt.i64.cast(rt.u64.and(value, rt.u64.of(1))))); } //# sourceMappingURL=varint.ts.map diff --git a/ts/fixtures/codec/expected/coding/version.ts b/ts/fixtures/codec/expected/coding/version.ts index f6d97a525..542b72c43 100644 --- a/ts/fixtures/codec/expected/coding/version.ts +++ b/ts/fixtures/codec/expected/coding/version.ts @@ -8,9 +8,9 @@ import { type Version as version_Version } from "../lite/version.ts"; /** A version number negotiated during the setup. */ export class Version { - _0: bigint; + _0: rt.U64; - constructor(_0: bigint) { + constructor(_0: rt.U64) { this._0 = _0; } @@ -63,7 +63,7 @@ export class Versions { /** Encode the version list. */ encode$Form(w: Encoder, version: Form): rt.Result { - const varint = w.varint(BigInt(this._0.length)); + const varint = w.varint(rt.u64.cast(this._0.length)); if (!varint.ok) { return rt.Err(varint.error); } @@ -82,7 +82,7 @@ export class Versions { /** Encode the version list. */ encode$Version(w: Encoder, version: version_Version): rt.Result { - const varint = w.varint(BigInt(this._0.length)); + const varint = w.varint(rt.u64.cast(this._0.length)); if (!varint.ok) { return rt.Err(varint.error); } @@ -103,8 +103,9 @@ export class Versions { } const count = varint.value; let vs: Version[] = []; + const start = rt.u64.of(0); const end = count; - for (let _ = 0n; _ < end; _++) { + for (const _ of rt.u64.range(start, end, false)) { const decode = Version.decode(r, version); if (!decode.ok) { return rt.Err(decode.error); diff --git a/ts/fixtures/codec/expected/harness.ts b/ts/fixtures/codec/expected/harness.ts index dd5bc9af3..03a7a9114 100644 --- a/ts/fixtures/codec/expected/harness.ts +++ b/ts/fixtures/codec/expected/harness.ts @@ -72,16 +72,16 @@ export function liteVersion(selector: number): version_Version { } } -export function putU64(out: rt.ByteVec, value: bigint): void { +export function putU64(out: rt.ByteVec, value: rt.U64): void { out.extendFromSlice(rt.u64.toBeBytes(value)); } export function putUsize(out: rt.ByteVec, value: number): void { - putU64(out, BigInt(value)); + putU64(out, rt.u64.cast(value)); } /** Take up to 8 big-endian bytes as a `u64`, zero-padded on the right. */ -export function takeU64(data: Uint8Array): [bigint, Uint8Array] { +export function takeU64(data: Uint8Array): [rt.U64, 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)); @@ -305,20 +305,20 @@ export function encodeScript(data: Uint8Array, out: rt.ByteVec): void { let result: rt.Result; const value2 = rt.u8.rem(step, 10); if (value2 === 0) { - w.u8(Number(BigInt.asUintN(8, value))); + w.u8(value.lo & 0xff); result = rt.Ok(undefined); } else if (value2 === 1) { - w.u16(Number(BigInt.asUintN(16, value))); + w.u16(value.lo & 0xffff); result = rt.Ok(undefined); } else if (value2 === 2) { - w.bool((value & 1n) === 1n); + w.bool(rt.u64.eq(rt.u64.and(value, rt.u64.of(1)), rt.u64.of(1))); result = rt.Ok(undefined); } else if (value2 === 3) { result = w.varint(value); } else if (value2 === 4) { const arg = w; - const value3 = rt.u64.rem(value, 3n); - result = arg.varintOpt(value3 === 0n ? undefined : value3 === 1n ? value : 18446744073709551615n); + const value3 = rt.u64.rem(value, rt.u64.of(3)); + result = arg.varintOpt(rt.u64.eq(value3, rt.u64.of(0)) ? undefined : rt.u64.eq(value3, rt.u64.of(1)) ? value : rt.u64.of(18446744073709551615n)); } else if (value2 === 5) { result = w.bytes(rt.slice.range(rt.u64.toBeBytes(value), 0, rt.u8.rem(rt.u8.div(step, 10), 9))); } else if (value2 === 6) { @@ -330,7 +330,7 @@ export function encodeScript(data: Uint8Array, out: rt.ByteVec): void { result = w.string("not utf-8"); } } else if (value2 === 7) { - prefixes.push(rt.u64.rem(value, 2n) === 0n ? w.prefixVarint() : w.prefixU16()); + prefixes.push(rt.u64.eq(rt.u64.rem(value, rt.u64.of(2)), rt.u64.of(0)) ? w.prefixVarint() : w.prefixU16()); result = rt.Ok(undefined); } else if (value2 === 8) { const pop = prefixes.pop(); @@ -373,10 +373,10 @@ export function encodeScript(data: Uint8Array, out: rt.ByteVec): void { export function zigzag(data: Uint8Array, out: rt.ByteVec): void { const [value, ] = takeU64(data); - const signed = BigInt.asIntN(64, value); + const signed = rt.i64.cast(value); putU64(out, varint_zigzag(signed)); - putU64(out, BigInt.asUintN(64, unzigzag(value))); - putU64(out, BigInt.asUintN(64, unzigzag(varint_zigzag(signed)))); + putU64(out, rt.u64.cast(unzigzag(value))); + putU64(out, rt.u64.cast(unzigzag(varint_zigzag(signed)))); } export function sizes(data: Uint8Array, out: rt.ByteVec): void { @@ -490,9 +490,9 @@ export function encodeMessages(data: Uint8Array, out: rt.ByteVec): void { const [frameStart, data5] = takeU64(data4); const [bitrate, data6] = takeU64(data5); const [rtt, ] = takeU64(data6); - const group = new Group({ subscribe, sequence, frameStart: rt.u64.rem(frameStart, 4n) }); + const group = new Group({ subscribe, sequence, frameStart: rt.u64.rem(frameStart, rt.u64.of(4)) }); encodeMessage$Group(group, version, out); - const probe = new Probe({ bitrate: rt.u64.checkedSub(bitrate, 1n), rtt: rt.thenSome(rt.u64.rem(rtt, 3n) !== 0n, rtt) }); + const probe = new Probe({ bitrate: rt.u64.checkedSub(bitrate, rt.u64.of(1)), rtt: rt.thenSome(rt.u64.ne(rt.u64.rem(rtt, rt.u64.of(3)), rt.u64.of(0)), rtt) }); encodeMessage$Probe(probe, version, out); const versions = Versions.from([new Version(subscribe), new Version(sequence)]); let buf: rt.ByteVec = new rt.ByteVec(); @@ -547,9 +547,9 @@ export function errors(data: Uint8Array, out: rt.ByteVec): void { if (value2 === 0) { err = { tag: "InvalidMessage", _0: value }; } else if (value2 === 1) { - err = { tag: "MessageTooLarge", size: Number(BigInt.asUintN(32, value)), max: Number(BigInt.asUintN(32, value >> 32n)) }; + err = { tag: "MessageTooLarge", size: value.lo, max: rt.u64.shr(value, 32).lo }; } else if (value2 === 2) { - err = { tag: "Session", _0: { tag: "App", _0: Number(BigInt.asUintN(16, value)) } as SessionError }; + err = { tag: "Session", _0: { tag: "App", _0: value.lo & 0xffff } as SessionError }; } else if (value2 === 3) { err = { tag: "Session", _0: { tag: "ProtocolViolation" } as SessionError }; } else if (value2 === 4) { diff --git a/ts/fixtures/codec/expected/lite/group.ts b/ts/fixtures/codec/expected/lite/group.ts index d2ff70230..b28cbc7fa 100644 --- a/ts/fixtures/codec/expected/lite/group.ts +++ b/ts/fixtures/codec/expected/lite/group.ts @@ -8,11 +8,11 @@ import { decodeWith$Group } from "../lite/message.ts"; import { Version } from "../lite/version.ts"; export class Group { - subscribe: bigint; - sequence: bigint; - frameStart: bigint; + subscribe: rt.U64; + sequence: rt.U64; + frameStart: rt.U64; - constructor(fields: { subscribe: bigint; sequence: bigint; frameStart: bigint }) { + constructor(fields: { subscribe: rt.U64; sequence: rt.U64; frameStart: rt.U64 }) { this.subscribe = fields.subscribe; this.sequence = fields.sequence; this.frameStart = fields.frameStart; @@ -70,7 +70,7 @@ export class Group { if (!varint3.ok) { return rt.Err(varint3.error); } - } else if (this.frameStart !== 0n) { + } else if (rt.u64.ne(this.frameStart, rt.u64.of(0))) { return rt.Err("Version"); } return rt.Ok(undefined); @@ -88,7 +88,7 @@ export class Group { return rt.Err(varint2.error); } const sequence = varint2.value; - let frameStart: bigint; + let frameStart: rt.U64; if (Version.hasFrameBounds(version)) { const varint3 = r.varint(); if (!varint3.ok) { @@ -96,7 +96,7 @@ export class Group { } frameStart = varint3.value; } else { - frameStart = 0n; + frameStart = rt.u64.of(0); } return rt.Ok(new Group({ subscribe, sequence, frameStart })); } diff --git a/ts/fixtures/codec/expected/lite/probe.ts b/ts/fixtures/codec/expected/lite/probe.ts index ab520683e..8729adb78 100644 --- a/ts/fixtures/codec/expected/lite/probe.ts +++ b/ts/fixtures/codec/expected/lite/probe.ts @@ -15,10 +15,10 @@ import { type Control } from "../stats.ts"; * On the wire, 0 means unknown (None). Some(0) is rounded up to Some(1). */ export class Probe { - bitrate: bigint | undefined; - rtt: bigint | undefined; + bitrate: rt.U64 | undefined; + rtt: rt.U64 | undefined; - constructor(fields: { bitrate: bigint | undefined; rtt: bigint | undefined }) { + constructor(fields: { bitrate: rt.U64 | undefined; rtt: rt.U64 | undefined }) { this.bitrate = fields.bitrate; this.rtt = fields.rtt; } @@ -65,17 +65,17 @@ export class Probe { if (version === "Lite01" || version === "Lite02") { return rt.Err("Version"); } - const wire = rt.option.unwrapOr(rt.option.map(this.bitrate, (v: bigint): bigint => { - return rt.max(v, 1n); - }), 0n); + const wire = rt.option.unwrapOr(rt.option.map(this.bitrate, (v: rt.U64): rt.U64 => { + return rt.u64.max(v, rt.u64.of(1)); + }), rt.u64.of(0)); const varint = w.varint(wire); if (!varint.ok) { return rt.Err(varint.error); } if (Version.hasProbeRtt(version)) { - const wire2 = rt.option.unwrapOr(rt.option.map(this.rtt, (v2: bigint): bigint => { - return rt.max(v2, 1n); - }), 0n); + const wire2 = rt.option.unwrapOr(rt.option.map(this.rtt, (v2: rt.U64): rt.U64 => { + return rt.u64.max(v2, rt.u64.of(1)); + }), rt.u64.of(0)); const varint2 = w.varint(wire2); if (!varint2.ok) { return rt.Err(varint2.error); @@ -89,18 +89,18 @@ export class Probe { if (version === "Lite01" || version === "Lite02") { return rt.Err({ tag: "Version" }); } - let bitrate: bigint | undefined; + let bitrate: rt.U64 | undefined; const varint = r.varint(); if (!varint.ok) { return rt.Err(varint.error); } - if (varint.value === 0n) { + if (rt.u64.eq(varint.value, rt.u64.of(0))) { bitrate = undefined; } else { const v = varint.value; bitrate = v; } - let rtt: bigint | undefined; + let rtt: rt.U64 | undefined; if (!Version.hasProbeRtt(version)) { rtt = undefined; } else { @@ -108,7 +108,7 @@ export class Probe { if (!varint2.ok) { return rt.Err(varint2.error); } - if (varint2.value === 0n) { + if (rt.u64.eq(varint2.value, rt.u64.of(0))) { rtt = undefined; } else { const v2 = varint2.value; diff --git a/ts/fixtures/lang/expected/fmt.ts b/ts/fixtures/lang/expected/fmt.ts index f55dbccd6..b7882ea35 100644 --- a/ts/fixtures/lang/expected/fmt.ts +++ b/ts/fixtures/lang/expected/fmt.ts @@ -181,9 +181,9 @@ export class Bare { export class Pair { _0: number; - _1: bigint; + _1: rt.I64; - constructor(_0: number, _1: bigint) { + constructor(_0: number, _1: rt.I64) { this._0 = _0; this._1 = _1; } @@ -193,7 +193,7 @@ export class Pair { } eq(other: Pair): boolean { - return this._0 === other._0 && this._1 === other._1; + return this._0 === other._0 && rt.i64.eq(this._1, other._1); } } @@ -202,11 +202,11 @@ export class Wide { a: number; b: number; c: number; - d: bigint; + d: rt.U64; e: number; f: boolean; - constructor(fields: { a: number; b: number; c: number; d: bigint; e: number; f: boolean }) { + constructor(fields: { a: number; b: number; c: number; d: rt.U64; e: number; f: boolean }) { this.a = fields.a; this.b = fields.b; this.c = fields.c; @@ -323,8 +323,8 @@ export function nested(data: Uint8Array): Nested { const arg4 = arg2; const arg5 = value2 === 0 ? { tag: "Dot" } as Shape : value2 === 1 ? { tag: "Line", _0: new Point({ x: 1, y: 2 }), _1: new Point({ x: -3, y: 4 }) } as Shape : { tag: "Rect", corner: new Point({ x: 0, y: 0 }), width: at(3), height: 9 } as Shape; const arg6 = rt.slice.toByteVec(rt.slice.range(data, 0, rt.min(data.length, 4))); - const arg7 = rt.slice.toVec([new Pair(at(4), -1n), new Pair(2, -9223372036854775808n)]); - const arg8 = rt.u8.rem(at(5), 2) === 0 ? new Pair(at(5), 5n) : undefined; + const arg7 = rt.slice.toVec([new Pair(at(4), rt.i64.of(-1)), new Pair(2, rt.i64.of(-9223372036854775808n))]); + const arg8 = rt.u8.rem(at(5), 2) === 0 ? new Pair(at(5), rt.i64.of(5)) : undefined; const arg9 = rt.u8.rem(at(6), 2) === 0 ? rt.Ok(at(6)) : rt.Err("no"); const arg10 = [rt.i16.sub(at(7), 100), 32767]; const arg11 = [at(8), true, "t"] as [number, boolean, string]; @@ -342,7 +342,7 @@ export function derived(data: Uint8Array, out: rt.ByteVec): void { const value = nested(data); put(out, rt.fmt.format([(f: rt.Formatter) => value.fmt(f)])); put(out, rt.fmt.format([rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => value.fmt(f))])); - const wide = new Wide({ a: at(0), b: 300, c: 70_000, d: 18446744073709551615n, e: -1, f: at(1) > 10 }); + const wide = new Wide({ a: at(0), b: 300, c: 70_000, d: rt.u64.of(18446744073709551615n), e: -1, f: at(1) > 10 }); const six = new Six(at(0), 1, 2, 3, 4, at(1)); const arg3 = out; const arg = new Unit(); @@ -351,7 +351,7 @@ export function derived(data: Uint8Array, out: rt.ByteVec): void { put(out, rt.fmt.format([rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => wide.fmt(f)), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => six.fmt(f))])); const point = new Point({ x: at(2), y: -1 }); const arg5 = out; - const arg4 = new Pair(at(3), -9n); + const arg4 = new Pair(at(3), rt.i64.of(-9)); put(arg5, rt.fmt.format([rt.fmt.spec({ width: 5 }, (f: rt.Formatter) => point.fmt(f)), "|", rt.fmt.spec({ align: "<", width: 4 }, (f: rt.Formatter) => point.fmt(f)), "|", rt.fmt.spec({ alternate: true, width: 4 }, (f: rt.Formatter) => point.fmt(f)), "|", rt.fmt.spec({ align: ">", width: 3 }, (f: rt.Formatter) => arg4.fmt(f))])); const kinds = [{ tag: "Empty" } as Kind, { tag: "One", _0: at(4) } as Kind, { tag: "Braced" } as Kind, { tag: "Bare" } as Kind, { tag: "Many", count: 0, items: new rt.ByteVec() } as Kind]; const arg7 = out; @@ -494,7 +494,7 @@ export function written(data: Uint8Array, out: rt.ByteVec): void { const manual = new Manual({ id: at(1), list: rt.slice.toVec([1, at(2)]) }); const empty = new Manual({ id: 0, list: [] }); put(out, rt.fmt.format([(f: rt.Formatter) => manual.fmt(f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => manual.fmt(f)), " ", (f: rt.Formatter) => empty.fmt(f)])); - const seq = new Seq(rt.slice.toVec([new Pair(at(3), 0n), new Pair(1, -1n)])); + const seq = new Seq(rt.slice.toVec([new Pair(at(3), rt.i64.of(0)), new Pair(1, rt.i64.of(-1))])); const none = new Seq([]); put(out, rt.fmt.format([(f: rt.Formatter) => seq.fmt(f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => seq.fmt(f)), " ", (f: rt.Formatter) => none.fmt(f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => none.fmt(f))])); const tup = new Tup(at(4), rt.u8.rem(at(5), 2) === 0 ? at(5) : undefined); @@ -518,7 +518,7 @@ export function panics(kind: number, rest: Uint8Array, out: rt.ByteVec): void { rt.panic(["internal error: entered unreachable code: never ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.u8.lowerHex(n, f)), " ", (f: rt.Formatter) => arg.fmt$Display(f)]); } else if (value === 2) { if (!(n > 200)) { - const arg2 = new Pair(n, 0n); + const arg2 = new Pair(n, rt.i64.of(0)); rt.panic(["small ", (f: rt.Formatter) => rt.u8.display(n, f), " ", (f: rt.Formatter) => arg2.fmt(f)]); } } else if (value === 3) { @@ -534,7 +534,7 @@ export function panics(kind: number, rest: Uint8Array, out: rt.ByteVec): void { rt.assertFailed(kind2, (f: rt.Formatter) => rt.u8.display(leftVal, f), (f: rt.Formatter) => rt.u8.display(rightVal, f)); } } else if (value === 5) { - const value3 = [new Pair(n, 1n), new Pair(n, 1n)] as [Pair, Pair]; + const value3 = [new Pair(n, rt.i64.of(1)), new Pair(n, rt.i64.of(1))] as [Pair, Pair]; const leftVal2 = value3[0]; const rightVal2 = value3[1]; if (leftVal2.eq(rightVal2)) { @@ -555,7 +555,7 @@ export function panics(kind: number, rest: Uint8Array, out: rt.ByteVec): void { const arg3 = rt.result.unwrap(r, (value: Kind, f: rt.Formatter) => Kind.fmt(value, f)); put(arg4, rt.fmt.toString((f: rt.Formatter) => rt.u8.display(arg3, f))); } else if (value === 8) { - const r2 = n > 128 ? rt.Ok(n) : rt.Err(new Pair(n, -2n)); + const r2 = n > 128 ? rt.Ok(n) : rt.Err(new Pair(n, rt.i64.of(-2))); const arg6 = out; const arg5 = rt.result.expect(rt.copy(r2), "expected a byte", (value: Pair, f: rt.Formatter) => value.fmt(f)); put(arg6, rt.fmt.toString((f: rt.Formatter) => rt.u8.display(arg5, f))); @@ -641,7 +641,7 @@ export const Fault = { } else if (value === 2) { return { tag: "TooLarge", size: (((at(1) << 24) >>> 0) | at(2)) >>> 0, max: at(3) }; } else { - return { tag: "At", _0: new Pair(at(1), BigInt.asIntN(64, BigInt((at(2) << 24) >> 24) << 40n)) }; + return { tag: "At", _0: new Pair(at(1), rt.i64.shl(rt.i64.cast((at(2) << 24) >> 24), 40)) }; } }, diff --git a/ts/fixtures/lang/expected/ints.ts b/ts/fixtures/lang/expected/ints.ts index b3a9f3912..45e14905f 100644 --- a/ts/fixtures/lang/expected/ints.ts +++ b/ts/fixtures/lang/expected/ints.ts @@ -16,13 +16,13 @@ export function u8Op(op: number, data: Uint8Array, out: rt.ByteVec): void { 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))); + out2.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v))); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.u128.toBeBytes(BigInt(v3))); + out3.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v3))); } else { out3.push(0); } @@ -106,13 +106,13 @@ export function u16Op(op: number, data: Uint8Array, out: rt.ByteVec): void { 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))); + out2.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v))); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.u128.toBeBytes(BigInt(v3))); + out3.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v3))); } else { out3.push(0); } @@ -196,13 +196,13 @@ export function u32Op(op: number, data: Uint8Array, out: rt.ByteVec): void { 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))); + out2.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v))); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.u128.toBeBytes(BigInt(v3))); + out3.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v3))); } else { out3.push(0); } @@ -278,21 +278,21 @@ 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(buf), rt.slice.range(data2, n)] as [bigint, Uint8Array]; + const [a, data3] = [rt.u64.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.U64, 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(buf2), rt.slice.range(data4, n2)] as [bigint, Uint8Array]; + const [b, data5] = [rt.u64.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.U64, 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)); + const put = (out2: rt.ByteVec, v: rt.U64): void => { + out2.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v))); }; - const putOpt = (out3: rt.ByteVec, v2: bigint | undefined): void => { + const putOpt = (out3: rt.ByteVec, v2: rt.U64 | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.u128.toBeBytes(v3)); + out3.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v3))); } else { out3.push(0); } @@ -309,17 +309,17 @@ export function u64Op(op: number, data: Uint8Array, out: rt.ByteVec): void { } else if (value === 4) { put(out, rt.u64.rem(a, b)); } else if (value === 5) { - put(out, a & b); + put(out, rt.u64.and(a, b)); } else if (value === 6) { - put(out, a | b); + put(out, rt.u64.or(a, b)); } else if (value === 7) { - put(out, a ^ b); + put(out, rt.u64.xor(a, b)); } else if (value === 8) { put(out, rt.u64.shl(a, shift)); } else if (value === 9) { put(out, rt.u64.shr(a, shift)); } else if (value === 10) { - put(out, BigInt.asUintN(64, ~a)); + put(out, rt.u64.not(a)); } else if (value === 11) { putOpt(out, rt.u64.checkedAdd(a, b)); } else if (value === 12) { @@ -339,25 +339,27 @@ export function u64Op(op: number, data: Uint8Array, out: rt.ByteVec): void { } else if (value === 19) { out.extendFromSlice(rt.u32.toBeBytes(rt.u64.leadingZeros(a))); } else if (value === 20) { - put(out, rt.max(a, b)); + put(out, rt.u64.max(a, b)); } else if (value === 21) { - put(out, rt.min(a, b)); + put(out, rt.u64.min(a, b)); } else if (value === 22) { - out.push(a === b ? 1 : 0); - out.push(a < b ? 1 : 0); - out.push(a >= b ? 1 : 0); + out.push(rt.u64.eq(a, b) ? 1 : 0); + out.push(rt.u64.lt(a, b) ? 1 : 0); + out.push(rt.u64.ge(a, b) ? 1 : 0); } else { - let x: bigint = a; + let x: rt.U64 = a; x = rt.u64.add(x, b); - x = rt.u64.sub(x, 1n); - x = rt.u64.mul(x, 2n); + const value2 = rt.u64.of(1); + x = rt.u64.sub(x, value2); + const value3 = rt.u64.of(2); + x = rt.u64.mul(x, value3); x = rt.u64.shr(x, 1); x = rt.u64.shl(x, 1); - const value2 = 1n << 3n; - x = x | value2; - x = x ^ b; - const value3 = BigInt.asUintN(64, ~(1n << 2n)); - x = x & value3; + const value4 = rt.u64.of(8); + x = rt.u64.or(x, value4); + x = rt.u64.xor(x, b); + const value5 = rt.u64.not(rt.u64.of(4)); + x = rt.u64.and(x, value5); put(out, x); } } @@ -368,17 +370,17 @@ 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(buf), rt.slice.range(data2, n)] as [bigint, Uint8Array]; + const [a, data3] = [rt.u128.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.U128, 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(buf2), rt.slice.range(data4, n2)] as [bigint, Uint8Array]; + const [b, data5] = [rt.u128.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.U128, 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 => { + const put = (out2: rt.ByteVec, v: rt.U128): void => { out2.extendFromSlice(rt.u128.toBeBytes(v)); }; - const putOpt = (out3: rt.ByteVec, v2: bigint | undefined): void => { + const putOpt = (out3: rt.ByteVec, v2: rt.U128 | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); @@ -399,17 +401,17 @@ export function u128Op(op: number, data: Uint8Array, out: rt.ByteVec): void { } else if (value === 4) { put(out, rt.u128.rem(a, b)); } else if (value === 5) { - put(out, a & b); + put(out, rt.u128.and(a, b)); } else if (value === 6) { - put(out, a | b); + put(out, rt.u128.or(a, b)); } else if (value === 7) { - put(out, a ^ b); + put(out, rt.u128.xor(a, b)); } else if (value === 8) { put(out, rt.u128.shl(a, shift)); } else if (value === 9) { put(out, rt.u128.shr(a, shift)); } else if (value === 10) { - put(out, BigInt.asUintN(128, ~a)); + put(out, rt.u128.not(a)); } else if (value === 11) { putOpt(out, rt.u128.checkedAdd(a, b)); } else if (value === 12) { @@ -429,25 +431,27 @@ export function u128Op(op: number, data: Uint8Array, out: rt.ByteVec): void { } else if (value === 19) { out.extendFromSlice(rt.u32.toBeBytes(rt.u128.leadingZeros(a))); } else if (value === 20) { - put(out, rt.max(a, b)); + put(out, rt.u128.max(a, b)); } else if (value === 21) { - put(out, rt.min(a, b)); + put(out, rt.u128.min(a, b)); } else if (value === 22) { - out.push(a === b ? 1 : 0); - out.push(a < b ? 1 : 0); - out.push(a >= b ? 1 : 0); + out.push(rt.u128.eq(a, b) ? 1 : 0); + out.push(rt.u128.lt(a, b) ? 1 : 0); + out.push(rt.u128.ge(a, b) ? 1 : 0); } else { - let x: bigint = a; + let x: rt.U128 = a; x = rt.u128.add(x, b); - x = rt.u128.sub(x, 1n); - x = rt.u128.mul(x, 2n); + const value2 = rt.u128.of(1); + x = rt.u128.sub(x, value2); + const value3 = rt.u128.of(2); + x = rt.u128.mul(x, value3); x = rt.u128.shr(x, 1); x = rt.u128.shl(x, 1); - const value2 = BigInt.asUintN(128, 1n << 3n); - x = x | value2; - x = x ^ b; - const value3 = BigInt.asUintN(128, ~BigInt.asUintN(128, 1n << 2n)); - x = x & value3; + const value4 = rt.u128.shl(rt.u128.of(1), 3); + x = rt.u128.or(x, value4); + x = rt.u128.xor(x, b); + const value5 = rt.u128.not(rt.u128.shl(rt.u128.of(1), 2)); + x = rt.u128.and(x, value5); put(out, x); } } @@ -466,13 +470,13 @@ export function usizeOp(op: number, data: Uint8Array, out: rt.ByteVec): void { 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))); + out2.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v))); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.u128.toBeBytes(BigInt(v3))); + out3.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v3))); } else { out3.push(0); } @@ -556,13 +560,13 @@ export function i8Op(op: number, data: Uint8Array, out: rt.ByteVec): void { 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))); + out2.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v))); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.i128.toBeBytes(BigInt(v3))); + out3.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v3))); } else { out3.push(0); } @@ -646,13 +650,13 @@ export function i16Op(op: number, data: Uint8Array, out: rt.ByteVec): void { 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))); + out2.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v))); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.i128.toBeBytes(BigInt(v3))); + out3.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v3))); } else { out3.push(0); } @@ -736,13 +740,13 @@ export function i32Op(op: number, data: Uint8Array, out: rt.ByteVec): void { 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))); + out2.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v))); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.i128.toBeBytes(BigInt(v3))); + out3.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v3))); } else { out3.push(0); } @@ -818,21 +822,21 @@ 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(buf), rt.slice.range(data2, n)] as [bigint, Uint8Array]; + const [a, data3] = [rt.i64.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.I64, 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(buf2), rt.slice.range(data4, n2)] as [bigint, Uint8Array]; + const [b, data5] = [rt.i64.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.I64, 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)); + const put = (out2: rt.ByteVec, v: rt.I64): void => { + out2.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v))); }; - const putOpt = (out3: rt.ByteVec, v2: bigint | undefined): void => { + const putOpt = (out3: rt.ByteVec, v2: rt.I64 | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.i128.toBeBytes(v3)); + out3.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v3))); } else { out3.push(0); } @@ -849,17 +853,17 @@ export function i64Op(op: number, data: Uint8Array, out: rt.ByteVec): void { } else if (value === 4) { put(out, rt.i64.rem(a, b)); } else if (value === 5) { - put(out, a & b); + put(out, rt.i64.and(a, b)); } else if (value === 6) { - put(out, a | b); + put(out, rt.i64.or(a, b)); } else if (value === 7) { - put(out, a ^ b); + put(out, rt.i64.xor(a, b)); } else if (value === 8) { put(out, rt.i64.shl(a, shift)); } else if (value === 9) { put(out, rt.i64.shr(a, shift)); } else if (value === 10) { - put(out, ~a); + put(out, rt.i64.not(a)); } else if (value === 11) { putOpt(out, rt.i64.checkedAdd(a, b)); } else if (value === 12) { @@ -879,25 +883,27 @@ export function i64Op(op: number, data: Uint8Array, out: rt.ByteVec): void { } else if (value === 19) { out.extendFromSlice(rt.u32.toBeBytes(rt.i64.leadingZeros(a))); } else if (value === 20) { - put(out, rt.max(a, b)); + put(out, rt.i64.max(a, b)); } else if (value === 21) { - put(out, rt.min(a, b)); + put(out, rt.i64.min(a, b)); } else if (value === 22) { - out.push(a === b ? 1 : 0); - out.push(a < b ? 1 : 0); - out.push(a >= b ? 1 : 0); + out.push(rt.i64.eq(a, b) ? 1 : 0); + out.push(rt.i64.lt(a, b) ? 1 : 0); + out.push(rt.i64.ge(a, b) ? 1 : 0); } else { - let x: bigint = a; + let x: rt.I64 = a; x = rt.i64.add(x, b); - x = rt.i64.sub(x, 1n); - x = rt.i64.mul(x, 2n); + const value2 = rt.i64.of(1); + x = rt.i64.sub(x, value2); + const value3 = rt.i64.of(2); + x = rt.i64.mul(x, value3); x = rt.i64.shr(x, 1); x = rt.i64.shl(x, 1); - const value2 = 1n << 3n; - x = x | value2; - x = x ^ b; - const value3 = ~(1n << 2n); - x = x & value3; + const value4 = rt.i64.of(8); + x = rt.i64.or(x, value4); + x = rt.i64.xor(x, b); + const value5 = rt.i64.not(rt.i64.of(4)); + x = rt.i64.and(x, value5); put(out, x); } } @@ -908,17 +914,17 @@ 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(buf), rt.slice.range(data2, n)] as [bigint, Uint8Array]; + const [a, data3] = [rt.i128.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.I128, 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(buf2), rt.slice.range(data4, n2)] as [bigint, Uint8Array]; + const [b, data5] = [rt.i128.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.I128, 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 => { + const put = (out2: rt.ByteVec, v: rt.I128): void => { out2.extendFromSlice(rt.i128.toBeBytes(v)); }; - const putOpt = (out3: rt.ByteVec, v2: bigint | undefined): void => { + const putOpt = (out3: rt.ByteVec, v2: rt.I128 | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); @@ -939,17 +945,17 @@ export function i128Op(op: number, data: Uint8Array, out: rt.ByteVec): void { } else if (value === 4) { put(out, rt.i128.rem(a, b)); } else if (value === 5) { - put(out, a & b); + put(out, rt.i128.and(a, b)); } else if (value === 6) { - put(out, a | b); + put(out, rt.i128.or(a, b)); } else if (value === 7) { - put(out, a ^ b); + put(out, rt.i128.xor(a, b)); } else if (value === 8) { put(out, rt.i128.shl(a, shift)); } else if (value === 9) { put(out, rt.i128.shr(a, shift)); } else if (value === 10) { - put(out, ~a); + put(out, rt.i128.not(a)); } else if (value === 11) { putOpt(out, rt.i128.checkedAdd(a, b)); } else if (value === 12) { @@ -969,25 +975,27 @@ export function i128Op(op: number, data: Uint8Array, out: rt.ByteVec): void { } else if (value === 19) { out.extendFromSlice(rt.u32.toBeBytes(rt.i128.leadingZeros(a))); } else if (value === 20) { - put(out, rt.max(a, b)); + put(out, rt.i128.max(a, b)); } else if (value === 21) { - put(out, rt.min(a, b)); + put(out, rt.i128.min(a, b)); } else if (value === 22) { - out.push(a === b ? 1 : 0); - out.push(a < b ? 1 : 0); - out.push(a >= b ? 1 : 0); + out.push(rt.i128.eq(a, b) ? 1 : 0); + out.push(rt.i128.lt(a, b) ? 1 : 0); + out.push(rt.i128.ge(a, b) ? 1 : 0); } else { - let x: bigint = a; + let x: rt.I128 = a; x = rt.i128.add(x, b); - x = rt.i128.sub(x, 1n); - x = rt.i128.mul(x, 2n); + const value2 = rt.i128.of(1); + x = rt.i128.sub(x, value2); + const value3 = rt.i128.of(2); + x = rt.i128.mul(x, value3); x = rt.i128.shr(x, 1); x = rt.i128.shl(x, 1); - const value2 = BigInt.asIntN(128, 1n << 3n); - x = x | value2; - x = x ^ b; - const value3 = ~BigInt.asIntN(128, 1n << 2n); - x = x & value3; + const value4 = rt.i128.shl(rt.i128.of(1), 3); + x = rt.i128.or(x, value4); + x = rt.i128.xor(x, b); + const value5 = rt.i128.not(rt.i128.shl(rt.i128.of(1), 2)); + x = rt.i128.and(x, value5); put(out, x); } } @@ -1006,13 +1014,13 @@ export function isizeOp(op: number, data: Uint8Array, out: rt.ByteVec): void { 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))); + out2.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v))); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.i128.toBeBytes(BigInt(v3))); + out3.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v3))); } else { out3.push(0); } @@ -1089,7 +1097,7 @@ export function i8Neg(data: Uint8Array, out: rt.ByteVec): void { 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; - out.extendFromSlice(rt.i128.toBeBytes(BigInt(rt.i8.neg(a)))); + out.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(rt.i8.neg(a)))); } /** Negate, which overflows at the minimum. */ @@ -1099,7 +1107,7 @@ export function i16Neg(data: Uint8Array, out: rt.ByteVec): void { 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; - out.extendFromSlice(rt.i128.toBeBytes(BigInt(rt.i16.neg(a)))); + out.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(rt.i16.neg(a)))); } /** Negate, which overflows at the minimum. */ @@ -1109,7 +1117,7 @@ export function i32Neg(data: Uint8Array, out: rt.ByteVec): void { 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; - out.extendFromSlice(rt.i128.toBeBytes(BigInt(rt.i32.neg(a)))); + out.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(rt.i32.neg(a)))); } /** Negate, which overflows at the minimum. */ @@ -1118,8 +1126,8 @@ 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(buf), rt.slice.range(data2, n)] as [bigint, Uint8Array]; - out.extendFromSlice(rt.i128.toBeBytes(rt.i64.neg(a))); + const [a, ] = [rt.i64.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.I64, Uint8Array]; + out.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(rt.i64.neg(a)))); } /** Negate, which overflows at the minimum. */ @@ -1128,7 +1136,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(buf), rt.slice.range(data2, n)] as [bigint, Uint8Array]; + const [a, ] = [rt.i128.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.I128, Uint8Array]; out.extendFromSlice(rt.i128.toBeBytes(rt.i128.neg(a))); } @@ -1139,7 +1147,7 @@ export function isizeNeg(data: Uint8Array, out: rt.ByteVec): void { 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; - out.extendFromSlice(rt.i128.toBeBytes(BigInt(rt.isize.neg(a)))); + out.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(rt.isize.neg(a)))); } /** Cast a value of type `from` to type `to`, both indexes into the twelve types. */ @@ -1160,9 +1168,9 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec } else if (value2 === 2) { out2.extendFromSlice(rt.u32.toBeBytes(v)); } else if (value2 === 3) { - out2.extendFromSlice(rt.u64.toBeBytes(BigInt(v))); + out2.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v))); } else if (value2 === 4) { - out2.extendFromSlice(rt.u128.toBeBytes(BigInt(v))); + out2.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v))); } else if (value2 === 5) { out2.extendFromSlice(rt.usize.toBeBytes(v)); } else if (value2 === 6) { @@ -1172,9 +1180,9 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec } else if (value2 === 8) { out2.extendFromSlice(rt.i32.toBeBytes(v)); } else if (value2 === 9) { - out2.extendFromSlice(rt.i64.toBeBytes(BigInt(v))); + out2.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v))); } else if (value2 === 10) { - out2.extendFromSlice(rt.i128.toBeBytes(BigInt(v))); + out2.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v))); } else { out2.extendFromSlice(rt.isize.toBeBytes(v)); } @@ -1193,9 +1201,9 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec } else if (value3 === 2) { out3.extendFromSlice(rt.u32.toBeBytes(v2)); } else if (value3 === 3) { - out3.extendFromSlice(rt.u64.toBeBytes(BigInt(v2))); + out3.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v2))); } else if (value3 === 4) { - out3.extendFromSlice(rt.u128.toBeBytes(BigInt(v2))); + out3.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v2))); } else if (value3 === 5) { out3.extendFromSlice(rt.usize.toBeBytes(v2)); } else if (value3 === 6) { @@ -1205,9 +1213,9 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec } else if (value3 === 8) { out3.extendFromSlice(rt.i32.toBeBytes(v2)); } else if (value3 === 9) { - out3.extendFromSlice(rt.i64.toBeBytes(BigInt(v2))); + out3.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v2))); } else if (value3 === 10) { - out3.extendFromSlice(rt.i128.toBeBytes(BigInt(v2))); + out3.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v2))); } else { out3.extendFromSlice(rt.isize.toBeBytes(v2)); } @@ -1226,9 +1234,9 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec } else if (value4 === 2) { out4.extendFromSlice(rt.u32.toBeBytes(v3)); } else if (value4 === 3) { - out4.extendFromSlice(rt.u64.toBeBytes(BigInt(v3))); + out4.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v3))); } else if (value4 === 4) { - out4.extendFromSlice(rt.u128.toBeBytes(BigInt(v3))); + out4.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v3))); } else if (value4 === 5) { out4.extendFromSlice(rt.usize.toBeBytes(v3)); } else if (value4 === 6) { @@ -1238,9 +1246,9 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec } else if (value4 === 8) { out4.extendFromSlice(rt.i32.toBeBytes(v3 | 0)); } else if (value4 === 9) { - out4.extendFromSlice(rt.i64.toBeBytes(BigInt(v3))); + out4.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v3))); } else if (value4 === 10) { - out4.extendFromSlice(rt.i128.toBeBytes(BigInt(v3))); + out4.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v3))); } else { out4.extendFromSlice(rt.isize.toBeBytes(v3 | 0)); } @@ -1249,66 +1257,66 @@ 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(buf4), rt.slice.range(data5, n4)] as [bigint, Uint8Array])[0]; + const v4 = ([rt.u64.fromBeBytes(buf4), rt.slice.range(data5, n4)] as [rt.U64, Uint8Array])[0]; const out5 = out; const value5 = rt.u8.rem(to, 12); if (value5 === 0) { - out5.extendFromSlice(rt.u8.toBeBytes(Number(BigInt.asUintN(8, v4)))); + out5.extendFromSlice(rt.u8.toBeBytes(v4.lo & 0xff)); } else if (value5 === 1) { - out5.extendFromSlice(rt.u16.toBeBytes(Number(BigInt.asUintN(16, v4)))); + out5.extendFromSlice(rt.u16.toBeBytes(v4.lo & 0xffff)); } else if (value5 === 2) { - out5.extendFromSlice(rt.u32.toBeBytes(Number(BigInt.asUintN(32, v4)))); + out5.extendFromSlice(rt.u32.toBeBytes(v4.lo)); } else if (value5 === 3) { out5.extendFromSlice(rt.u64.toBeBytes(v4)); } else if (value5 === 4) { - out5.extendFromSlice(rt.u128.toBeBytes(v4)); + out5.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v4))); } else if (value5 === 5) { - out5.extendFromSlice(rt.usize.toBeBytes(Number(BigInt.asUintN(32, v4)))); + out5.extendFromSlice(rt.usize.toBeBytes(v4.lo)); } else if (value5 === 6) { - out5.extendFromSlice(rt.i8.toBeBytes(Number(BigInt.asIntN(8, v4)))); + out5.extendFromSlice(rt.i8.toBeBytes((v4.lo << 24) >> 24)); } else if (value5 === 7) { - out5.extendFromSlice(rt.i16.toBeBytes(Number(BigInt.asIntN(16, v4)))); + out5.extendFromSlice(rt.i16.toBeBytes((v4.lo << 16) >> 16)); } else if (value5 === 8) { - out5.extendFromSlice(rt.i32.toBeBytes(Number(BigInt.asIntN(32, v4)))); + out5.extendFromSlice(rt.i32.toBeBytes(v4.lo | 0)); } else if (value5 === 9) { - out5.extendFromSlice(rt.i64.toBeBytes(BigInt.asIntN(64, v4))); + out5.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v4))); } else if (value5 === 10) { - out5.extendFromSlice(rt.i128.toBeBytes(v4)); + out5.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v4))); } else { - out5.extendFromSlice(rt.isize.toBeBytes(Number(BigInt.asIntN(32, v4)))); + out5.extendFromSlice(rt.isize.toBeBytes(v4.lo | 0)); } } else if (value === 4) { const data6 = data; 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(buf5), rt.slice.range(data6, n5)] as [bigint, Uint8Array])[0]; + const v5 = ([rt.u128.fromBeBytes(buf5), rt.slice.range(data6, n5)] as [rt.U128, Uint8Array])[0]; const out6 = out; const value6 = rt.u8.rem(to, 12); if (value6 === 0) { - out6.extendFromSlice(rt.u8.toBeBytes(Number(BigInt.asUintN(8, v5)))); + out6.extendFromSlice(rt.u8.toBeBytes(v5.lo & 0xff)); } else if (value6 === 1) { - out6.extendFromSlice(rt.u16.toBeBytes(Number(BigInt.asUintN(16, v5)))); + out6.extendFromSlice(rt.u16.toBeBytes(v5.lo & 0xffff)); } else if (value6 === 2) { - out6.extendFromSlice(rt.u32.toBeBytes(Number(BigInt.asUintN(32, v5)))); + out6.extendFromSlice(rt.u32.toBeBytes(v5.lo)); } else if (value6 === 3) { - out6.extendFromSlice(rt.u64.toBeBytes(BigInt.asUintN(64, v5))); + out6.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v5))); } else if (value6 === 4) { out6.extendFromSlice(rt.u128.toBeBytes(v5)); } else if (value6 === 5) { - out6.extendFromSlice(rt.usize.toBeBytes(Number(BigInt.asUintN(32, v5)))); + out6.extendFromSlice(rt.usize.toBeBytes(v5.lo)); } else if (value6 === 6) { - out6.extendFromSlice(rt.i8.toBeBytes(Number(BigInt.asIntN(8, v5)))); + out6.extendFromSlice(rt.i8.toBeBytes((v5.lo << 24) >> 24)); } else if (value6 === 7) { - out6.extendFromSlice(rt.i16.toBeBytes(Number(BigInt.asIntN(16, v5)))); + out6.extendFromSlice(rt.i16.toBeBytes((v5.lo << 16) >> 16)); } else if (value6 === 8) { - out6.extendFromSlice(rt.i32.toBeBytes(Number(BigInt.asIntN(32, v5)))); + out6.extendFromSlice(rt.i32.toBeBytes(v5.lo | 0)); } else if (value6 === 9) { - out6.extendFromSlice(rt.i64.toBeBytes(BigInt.asIntN(64, v5))); + out6.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v5))); } else if (value6 === 10) { - out6.extendFromSlice(rt.i128.toBeBytes(BigInt.asIntN(128, v5))); + out6.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v5))); } else { - out6.extendFromSlice(rt.isize.toBeBytes(Number(BigInt.asIntN(32, v5)))); + out6.extendFromSlice(rt.isize.toBeBytes(v5.lo | 0)); } } else if (value === 5) { const data7 = data; @@ -1325,9 +1333,9 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec } else if (value7 === 2) { out7.extendFromSlice(rt.u32.toBeBytes(v6)); } else if (value7 === 3) { - out7.extendFromSlice(rt.u64.toBeBytes(BigInt(v6))); + out7.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v6))); } else if (value7 === 4) { - out7.extendFromSlice(rt.u128.toBeBytes(BigInt(v6))); + out7.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v6))); } else if (value7 === 5) { out7.extendFromSlice(rt.usize.toBeBytes(v6)); } else if (value7 === 6) { @@ -1337,9 +1345,9 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec } else if (value7 === 8) { out7.extendFromSlice(rt.i32.toBeBytes(v6 | 0)); } else if (value7 === 9) { - out7.extendFromSlice(rt.i64.toBeBytes(BigInt(v6))); + out7.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v6))); } else if (value7 === 10) { - out7.extendFromSlice(rt.i128.toBeBytes(BigInt(v6))); + out7.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v6))); } else { out7.extendFromSlice(rt.isize.toBeBytes(v6 | 0)); } @@ -1358,9 +1366,9 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec } else if (value8 === 2) { out8.extendFromSlice(rt.u32.toBeBytes(v7 >>> 0)); } else if (value8 === 3) { - out8.extendFromSlice(rt.u64.toBeBytes(BigInt.asUintN(64, BigInt(v7)))); + out8.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v7))); } else if (value8 === 4) { - out8.extendFromSlice(rt.u128.toBeBytes(BigInt.asUintN(128, BigInt(v7)))); + out8.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v7))); } else if (value8 === 5) { out8.extendFromSlice(rt.usize.toBeBytes(v7 >>> 0)); } else if (value8 === 6) { @@ -1370,9 +1378,9 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec } else if (value8 === 8) { out8.extendFromSlice(rt.i32.toBeBytes(v7)); } else if (value8 === 9) { - out8.extendFromSlice(rt.i64.toBeBytes(BigInt(v7))); + out8.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v7))); } else if (value8 === 10) { - out8.extendFromSlice(rt.i128.toBeBytes(BigInt(v7))); + out8.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v7))); } else { out8.extendFromSlice(rt.isize.toBeBytes(v7)); } @@ -1391,9 +1399,9 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec } else if (value9 === 2) { out9.extendFromSlice(rt.u32.toBeBytes(v8 >>> 0)); } else if (value9 === 3) { - out9.extendFromSlice(rt.u64.toBeBytes(BigInt.asUintN(64, BigInt(v8)))); + out9.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v8))); } else if (value9 === 4) { - out9.extendFromSlice(rt.u128.toBeBytes(BigInt.asUintN(128, BigInt(v8)))); + out9.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v8))); } else if (value9 === 5) { out9.extendFromSlice(rt.usize.toBeBytes(v8 >>> 0)); } else if (value9 === 6) { @@ -1403,9 +1411,9 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec } else if (value9 === 8) { out9.extendFromSlice(rt.i32.toBeBytes(v8)); } else if (value9 === 9) { - out9.extendFromSlice(rt.i64.toBeBytes(BigInt(v8))); + out9.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v8))); } else if (value9 === 10) { - out9.extendFromSlice(rt.i128.toBeBytes(BigInt(v8))); + out9.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v8))); } else { out9.extendFromSlice(rt.isize.toBeBytes(v8)); } @@ -1424,9 +1432,9 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec } else if (value10 === 2) { out10.extendFromSlice(rt.u32.toBeBytes(v9 >>> 0)); } else if (value10 === 3) { - out10.extendFromSlice(rt.u64.toBeBytes(BigInt.asUintN(64, BigInt(v9)))); + out10.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v9))); } else if (value10 === 4) { - out10.extendFromSlice(rt.u128.toBeBytes(BigInt.asUintN(128, BigInt(v9)))); + out10.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v9))); } else if (value10 === 5) { out10.extendFromSlice(rt.usize.toBeBytes(v9 >>> 0)); } else if (value10 === 6) { @@ -1436,9 +1444,9 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec } else if (value10 === 8) { out10.extendFromSlice(rt.i32.toBeBytes(v9)); } else if (value10 === 9) { - out10.extendFromSlice(rt.i64.toBeBytes(BigInt(v9))); + out10.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v9))); } else if (value10 === 10) { - out10.extendFromSlice(rt.i128.toBeBytes(BigInt(v9))); + out10.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v9))); } else { out10.extendFromSlice(rt.isize.toBeBytes(v9)); } @@ -1447,66 +1455,66 @@ 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(buf10), rt.slice.range(data11, n10)] as [bigint, Uint8Array])[0]; + const v10 = ([rt.i64.fromBeBytes(buf10), rt.slice.range(data11, n10)] as [rt.I64, Uint8Array])[0]; const out11 = out; const value11 = rt.u8.rem(to, 12); if (value11 === 0) { - out11.extendFromSlice(rt.u8.toBeBytes(Number(BigInt.asUintN(8, v10)))); + out11.extendFromSlice(rt.u8.toBeBytes(v10.lo & 0xff)); } else if (value11 === 1) { - out11.extendFromSlice(rt.u16.toBeBytes(Number(BigInt.asUintN(16, v10)))); + out11.extendFromSlice(rt.u16.toBeBytes(v10.lo & 0xffff)); } else if (value11 === 2) { - out11.extendFromSlice(rt.u32.toBeBytes(Number(BigInt.asUintN(32, v10)))); + out11.extendFromSlice(rt.u32.toBeBytes(v10.lo)); } else if (value11 === 3) { - out11.extendFromSlice(rt.u64.toBeBytes(BigInt.asUintN(64, v10))); + out11.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v10))); } else if (value11 === 4) { - out11.extendFromSlice(rt.u128.toBeBytes(BigInt.asUintN(128, v10))); + out11.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v10))); } else if (value11 === 5) { - out11.extendFromSlice(rt.usize.toBeBytes(Number(BigInt.asUintN(32, v10)))); + out11.extendFromSlice(rt.usize.toBeBytes(v10.lo)); } else if (value11 === 6) { - out11.extendFromSlice(rt.i8.toBeBytes(Number(BigInt.asIntN(8, v10)))); + out11.extendFromSlice(rt.i8.toBeBytes((v10.lo << 24) >> 24)); } else if (value11 === 7) { - out11.extendFromSlice(rt.i16.toBeBytes(Number(BigInt.asIntN(16, v10)))); + out11.extendFromSlice(rt.i16.toBeBytes((v10.lo << 16) >> 16)); } else if (value11 === 8) { - out11.extendFromSlice(rt.i32.toBeBytes(Number(BigInt.asIntN(32, v10)))); + out11.extendFromSlice(rt.i32.toBeBytes(v10.lo | 0)); } else if (value11 === 9) { out11.extendFromSlice(rt.i64.toBeBytes(v10)); } else if (value11 === 10) { - out11.extendFromSlice(rt.i128.toBeBytes(v10)); + out11.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v10))); } else { - out11.extendFromSlice(rt.isize.toBeBytes(Number(BigInt.asIntN(32, v10)))); + out11.extendFromSlice(rt.isize.toBeBytes(v10.lo | 0)); } } else if (value === 10) { const data12 = data; 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(buf11), rt.slice.range(data12, n11)] as [bigint, Uint8Array])[0]; + const v11 = ([rt.i128.fromBeBytes(buf11), rt.slice.range(data12, n11)] as [rt.I128, Uint8Array])[0]; const out12 = out; const value12 = rt.u8.rem(to, 12); if (value12 === 0) { - out12.extendFromSlice(rt.u8.toBeBytes(Number(BigInt.asUintN(8, v11)))); + out12.extendFromSlice(rt.u8.toBeBytes(v11.lo & 0xff)); } else if (value12 === 1) { - out12.extendFromSlice(rt.u16.toBeBytes(Number(BigInt.asUintN(16, v11)))); + out12.extendFromSlice(rt.u16.toBeBytes(v11.lo & 0xffff)); } else if (value12 === 2) { - out12.extendFromSlice(rt.u32.toBeBytes(Number(BigInt.asUintN(32, v11)))); + out12.extendFromSlice(rt.u32.toBeBytes(v11.lo)); } else if (value12 === 3) { - out12.extendFromSlice(rt.u64.toBeBytes(BigInt.asUintN(64, v11))); + out12.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v11))); } else if (value12 === 4) { - out12.extendFromSlice(rt.u128.toBeBytes(BigInt.asUintN(128, v11))); + out12.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v11))); } else if (value12 === 5) { - out12.extendFromSlice(rt.usize.toBeBytes(Number(BigInt.asUintN(32, v11)))); + out12.extendFromSlice(rt.usize.toBeBytes(v11.lo)); } else if (value12 === 6) { - out12.extendFromSlice(rt.i8.toBeBytes(Number(BigInt.asIntN(8, v11)))); + out12.extendFromSlice(rt.i8.toBeBytes((v11.lo << 24) >> 24)); } else if (value12 === 7) { - out12.extendFromSlice(rt.i16.toBeBytes(Number(BigInt.asIntN(16, v11)))); + out12.extendFromSlice(rt.i16.toBeBytes((v11.lo << 16) >> 16)); } else if (value12 === 8) { - out12.extendFromSlice(rt.i32.toBeBytes(Number(BigInt.asIntN(32, v11)))); + out12.extendFromSlice(rt.i32.toBeBytes(v11.lo | 0)); } else if (value12 === 9) { - out12.extendFromSlice(rt.i64.toBeBytes(BigInt.asIntN(64, v11))); + out12.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v11))); } else if (value12 === 10) { out12.extendFromSlice(rt.i128.toBeBytes(v11)); } else { - out12.extendFromSlice(rt.isize.toBeBytes(Number(BigInt.asIntN(32, v11)))); + out12.extendFromSlice(rt.isize.toBeBytes(v11.lo | 0)); } } else { const data13 = data; @@ -1523,9 +1531,9 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec } else if (value13 === 2) { out13.extendFromSlice(rt.u32.toBeBytes(v12 >>> 0)); } else if (value13 === 3) { - out13.extendFromSlice(rt.u64.toBeBytes(BigInt.asUintN(64, BigInt(v12)))); + out13.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v12))); } else if (value13 === 4) { - out13.extendFromSlice(rt.u128.toBeBytes(BigInt.asUintN(128, BigInt(v12)))); + out13.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v12))); } else if (value13 === 5) { out13.extendFromSlice(rt.usize.toBeBytes(v12 >>> 0)); } else if (value13 === 6) { @@ -1535,9 +1543,9 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec } else if (value13 === 8) { out13.extendFromSlice(rt.i32.toBeBytes(v12)); } else if (value13 === 9) { - out13.extendFromSlice(rt.i64.toBeBytes(BigInt(v12))); + out13.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v12))); } else if (value13 === 10) { - out13.extendFromSlice(rt.i128.toBeBytes(BigInt(v12))); + out13.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v12))); } else { out13.extendFromSlice(rt.isize.toBeBytes(v12)); } @@ -1550,7 +1558,7 @@ 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(buf), rt.slice.range(data2, n)] as [bigint, Uint8Array]; + const [wide, data3] = [rt.u64.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.U64, Uint8Array]; const data4 = data3; let buf2: Uint8Array = new Uint8Array(4); const n2 = rt.min(data4.length, buf2.length); @@ -1578,8 +1586,8 @@ export function convert(data: Uint8Array, out: rt.ByteVec): void { } else { out.push(0xfd); } - const widened = BigInt(Number(BigInt.asUintN(16, wide))); - const into = BigInt(signed); + const widened = rt.u64.cast(wide.lo & 0xffff); + const into = rt.i64.cast(signed); out.extendFromSlice(rt.u64.toBeBytes(widened)); out.extendFromSlice(rt.i64.toBeBytes(into)); out.push(rt.u8.add(true ? 1 : 0, false ? 1 : 0)); diff --git a/ts/fixtures/lang/expected/shapes.ts b/ts/fixtures/lang/expected/shapes.ts index 99ab7a303..009df7232 100644 --- a/ts/fixtures/lang/expected/shapes.ts +++ b/ts/fixtures/lang/expected/shapes.ts @@ -37,9 +37,9 @@ export class Point { /** A tuple struct. */ export class Meters { - _0: bigint; + _0: rt.U64; - constructor(_0: bigint) { + constructor(_0: rt.U64) { this._0 = _0; } @@ -49,7 +49,7 @@ export class Meters { out.extendFromSlice(rt.u64.toBeBytes(this._0)); } - area(): bigint { + area(): rt.U64 { return this._0; } } @@ -67,15 +67,15 @@ export const Shape = { out.extendFromSlice(rt.u64.toBeBytes(Shape.area(self))); }, - area(self: Shape): bigint { + area(self: Shape): rt.U64 { if (self.tag === "Dot") { - return 0n; + return rt.u64.of(0); } else if (self.tag === "Line") { - return 0n; + return rt.u64.of(0); } else { const width = self.width; const height = self.height; - return rt.u64.mul(BigInt(width), BigInt(height)); + return rt.u64.mul(rt.u64.cast(width), rt.u64.cast(height)); } }, @@ -102,8 +102,8 @@ export type Signed = "Neg" | "Min" | "Pos"; export type Dir = "North" | "East" | "South" | "West"; -export function total$Shape(items: Shape[], out: rt.ByteVec): bigint { - let sum: bigint = 0n; +export function total$Shape(items: Shape[], out: rt.ByteVec): rt.U64 { + let sum: rt.U64 = rt.u64.of(0); for (const item of items) { Shape.describe(item, out); sum = rt.u64.saturatingAdd(sum, Shape.area(item)); @@ -111,8 +111,8 @@ export function total$Shape(items: Shape[], out: rt.ByteVec): bigint { return sum; } -export function total$Meters(items: Meters[], out: rt.ByteVec): bigint { - let sum: bigint = 0n; +export function total$Meters(items: Meters[], out: rt.ByteVec): rt.U64 { + let sum: rt.U64 = rt.u64.of(0); for (const item of items) { item.describe(out); sum = rt.u64.saturatingAdd(sum, item.area()); @@ -193,12 +193,12 @@ export function copies(data: Uint8Array, out: rt.ByteVec): void { a = Point.new(5, 6); const old2 = old; out.extendFromSlice(Uint8Array.of(a.x & 0xff, b.x & 0xff, old2.x & 0xff)); - let opt: Meters | undefined = new Meters(9n); + let opt: Meters | undefined = new Meters(rt.u64.of(9)); const taken = opt; opt = undefined; const taken2 = taken; out.push(opt === undefined ? 1 : 0); - out.extendFromSlice(rt.u64.toBeBytes(rt.option.mapOr(rt.copy(taken2), 0n, (m: Meters): bigint => { + out.extendFromSlice(rt.u64.toBeBytes(rt.option.mapOr(rt.copy(taken2), rt.u64.of(0), (m: Meters): rt.U64 => { return m._0; }))); const res = rt.Ok(rt.copy(p)); @@ -221,12 +221,12 @@ export function discriminants(data: Uint8Array, out: rt.ByteVec): void { out.push({ Low: 1, High: 44, Top: 255 }[wide]); out.push({ Low: 1, High: 44, Top: -1 }[wide] & 0xff); out.extendFromSlice(rt.i16.toBeBytes({ Low: 1, High: 300, Top: -1 }[wide])); - out.extendFromSlice(rt.u64.toBeBytes({ Low: 1n, High: 300n, Top: 65535n }[wide])); + out.extendFromSlice(rt.u64.toBeBytes({ Low: rt.u64.of(1), High: rt.u64.of(300), Top: rt.u64.of(65535) }[wide])); out.push({ Neg: 255, Min: 128, Pos: 100 }[signed]); out.extendFromSlice(rt.u16.toBeBytes({ Neg: 65535, Min: 65408, Pos: 100 }[signed])); out.extendFromSlice(rt.u32.toBeBytes({ Neg: 4294967295, Min: 4294967168, Pos: 100 }[signed])); - out.extendFromSlice(rt.i64.toBeBytes({ Neg: -1n, Min: -128n, Pos: 100n }[signed])); - out.extendFromSlice(rt.u128.toBeBytes({ Neg: 340282366920938463463374607431768211455n, Min: 340282366920938463463374607431768211328n, Pos: 100n }[signed])); + out.extendFromSlice(rt.i64.toBeBytes({ Neg: rt.i64.of(-1), Min: rt.i64.of(-128), Pos: rt.i64.of(100) }[signed])); + out.extendFromSlice(rt.u128.toBeBytes({ Neg: rt.u128.of(340282366920938463463374607431768211455n), Min: rt.u128.of(340282366920938463463374607431768211328n), Pos: rt.u128.of(100) }[signed])); } } @@ -272,15 +272,15 @@ export function loops(data: Uint8Array, out: rt.ByteVec): void { } else { out.push(0xff); } - let n: bigint = BigInt(data.length); + let n: rt.U64 = rt.u64.cast(data.length); let steps: number; while (true) { - if (n <= 1n) { + if (rt.u64.le(n, rt.u64.of(1))) { steps = 0; break; } - n = rt.u64.rem(n, 2n) === 0n ? rt.u64.div(n, 2n) : rt.u64.add(rt.u64.mul(3n, n), 1n); - if (n > 100n) { + n = rt.u64.eq(rt.u64.rem(n, rt.u64.of(2)), rt.u64.of(0)) ? rt.u64.div(n, rt.u64.of(2)) : rt.u64.add(rt.u64.mul(rt.u64.of(3), n), rt.u64.of(1)); + if (rt.u64.gt(n, rt.u64.of(100))) { steps = 1; break; } @@ -564,32 +564,32 @@ export function patterns(data: Uint8Array, out: rt.ByteVec): void { out.push(0xee); } const shape_ = shape(data); - let area: bigint; + let area: rt.I64; arms3: { if ((shape_.tag === "Rect" && shape_.width === 0) || shape_.tag === "Dot") { - area = 0n; + area = rt.i64.of(0); break arms3; } if (shape_.tag === "Rect") { const x = shape_.corner.x; const width = shape_.width; if (x > 0) { - area = rt.i64.mul(BigInt(width), BigInt(x)); + area = rt.i64.mul(rt.i64.cast(width), rt.i64.cast(x)); break arms3; } } if (shape_.tag === "Rect") { const width2 = shape_.width; const height = shape_.height; - area = rt.i64.neg(rt.i64.mul(BigInt(width2), BigInt(height))); + area = rt.i64.neg(rt.i64.mul(rt.i64.cast(width2), rt.i64.cast(height))); break arms3; } const a2 = rt.copy(shape_._0); const b3 = rt.copy(shape_._1); - area = rt.i64.mul(BigInt(rt.i32.sub(a2.x, b3.x)), BigInt(rt.i32.sub(a2.y, b3.y))); + area = rt.i64.mul(rt.i64.cast(rt.i32.sub(a2.x, b3.x)), rt.i64.cast(rt.i32.sub(a2.y, b3.y))); } out.extendFromSlice(rt.i64.toBeBytes(area)); - const total = rt.u64.add(total$Shape([shape_, { tag: "Dot" } as Shape], out), total$Meters([new Meters(3n), new Meters(18446744073709551615n)], out)); + const total = rt.u64.add(total$Shape([shape_, { tag: "Dot" } as Shape], out), total$Meters([new Meters(rt.u64.of(3)), new Meters(rt.u64.of(18446744073709551615n))], out)); out.extendFromSlice(rt.u64.toBeBytes(total)); const parsed = rt.Ok(data.length & 0xff); let a3: number; diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index 6847a9868..06c28959d 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -1,8 +1,8 @@ // The runtime of rust2-ts output: what Rust's std does that plain JS does not. // -// Integers up to 32 bits are numbers; wider ones are bigints. Arithmetic is checked as -// a debug build checks it: overflow panics, it never wraps silently. Every panic's -// message is the one native Rust panics with. +// Integers up to 32 bits are numbers; wider ones are immutable objects of 32-bit limbs. +// Arithmetic is checked as a debug build checks it: overflow panics, it never wraps +// silently. Every panic's message is the one native Rust panics with. /** A Rust panic. */ export class Panic extends Error { @@ -33,8 +33,21 @@ export function assertFailed(op: string, left: Fmt, right: Fmt, args?: Arguments // Integers +declare const brand: unique symbol; + +/** A `u64`: its high and low 32 bits, each a uint32. Immutable. */ +export type U64 = { readonly hi: number; readonly lo: number; readonly [brand]: "u64" }; +/** An `i64`: its high 32 bits as an int32, and its low 32 bits as a uint32. Immutable. */ +export type I64 = { readonly hi: number; readonly lo: number; readonly [brand]: "i64" }; +/** A `u128`: four 32-bit limbs, `hi`, `w2`, `w1`, and `lo`, each a uint32. Immutable. */ +export type U128 = { readonly hi: number; readonly w2: number; readonly w1: number; readonly lo: number; readonly [brand]: "u128" }; +/** An `i128`: as a `u128`, but `hi` is an int32. Immutable. */ +export type I128 = { readonly hi: number; readonly w2: number; readonly w1: number; readonly lo: number; readonly [brand]: "i128" }; +/** An integer wider than 32 bits. Its value is its limbs', `hi` read with the type's sign. */ +export type Wide = U64 | I64 | U128 | I128; + /** The checks and methods of one integer type. */ -export interface Int { +export interface Int { readonly MIN: T; readonly MAX: T; readonly BITS: number; @@ -44,8 +57,8 @@ export interface Int { neg(a: T): T; div(a: T, b: T): T; rem(a: T, b: T): T; - shl(a: T, b: number | bigint): T; - shr(a: T, b: number | bigint): T; + shl(a: T, b: number | Wide): T; + shr(a: T, b: number | Wide): T; checkedAdd(a: T, b: T): T | undefined; checkedSub(a: T, b: T): T | undefined; checkedMul(a: T, b: T): T | undefined; @@ -57,15 +70,54 @@ export interface Int { leadingZeros(a: T): number; fromBeBytes(bytes: Uint8Array): T; toBeBytes(a: T): Uint8Array; - tryFrom(value: number | bigint): Result; + tryFrom(value: number | Wide): Result; /** `Display`, and `Debug`, which writes the same. */ display(a: T, f: Formatter): FmtResult; lowerHex(a: T, f: Formatter): FmtResult; upperHex(a: T, f: Formatter): FmtResult; } -function shift(b: number | bigint, bits: number, direction: string): number { - const n = Number(b); +/** A wide integer type: what JS operators do for numbers, as methods on its objects. */ +export interface WideInt extends Int { + /** A literal: a number when it is a safe integer, else a bigint. */ + of(value: number | bigint): T; + /** `value as T`. */ + cast(value: number | Wide): T; + eq(a: T, b: T): boolean; + ne(a: T, b: T): boolean; + lt(a: T, b: T): boolean; + le(a: T, b: T): boolean; + gt(a: T, b: T): boolean; + ge(a: T, b: T): boolean; + max(a: T, b: T): T; + min(a: T, b: T): T; + not(a: T): T; + and(a: T, b: T): T; + or(a: T, b: T): T; + xor(a: T, b: T): T; + /** `start..end`, or `start..=end`. */ + range(start: T, end: T, inclusive: boolean): Iterable; +} + +const TWO32 = 4294967296; + +/** A wide integer's value as a number: exact up to 2^53 in magnitude, rounded past it. */ +function approx(v: Wide): number { + return "w1" in v ? ((v.hi * TWO32 + v.w2) * TWO32 + v.w1) * TWO32 + v.lo : v.hi * TWO32 + v.lo; +} + +/** A wide integer's exact value. */ +function exact(v: Wide): bigint { + if ("w1" in v) return (((((BigInt(v.hi) << 32n) + BigInt(v.w2)) << 32n) + BigInt(v.w1)) << 32n) + BigInt(v.lo); + return (BigInt(v.hi) << 32n) + BigInt(v.lo); +} + +function safe(n: number): boolean { + return n <= Number.MAX_SAFE_INTEGER && n >= -Number.MAX_SAFE_INTEGER; +} + +function shift(b: number | Wide, bits: number, direction: string): number { + const n = typeof b === "number" ? b : approx(b); if (n < 0 || n >= bits) panic(`attempt to shift ${direction} with overflow`); return n; } @@ -132,8 +184,10 @@ function small(bits: number, signed: boolean): Int { return out; }, tryFrom(value) { - if (value >= min && value <= max) return Ok(Number(value)); - return Err(new TryFromIntError(value < min ? "NegOverflow" : "PosOverflow")); + // Rounding past 2^53 keeps the order, so the bounds still test exactly. + const n = typeof value === "number" ? value : approx(value); + if (n >= min && n <= max) return Ok(n); + return Err(new TryFromIntError(n < min ? "NegOverflow" : "PosOverflow")); }, display: (a, f) => f.padIntegral(a >= 0, "", String(a < 0 ? -a : a)), lowerHex: (a, f) => f.padIntegral(true, "0x", unsigned(a).toString(16)), @@ -141,55 +195,60 @@ function small(bits: number, signed: boolean): Int { }; } -/** An integer wider than 32 bits, in a bigint. */ -function wide(bits: number, signed: boolean): Int { +/** + * A wide integer type, exact through bigints: `box` wraps a value into the type's range, + * and `compare` orders two of its objects. + */ +function wide(bits: number, signed: boolean, box: (v: bigint) => T, compare: (a: T, b: T) => number): WideInt { 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 => { + const big = exact as (a: T) => bigint; + const fits = (value: bigint) => value >= min && value <= max; + const checked = (value: bigint, op: string): T => { if (!fits(value)) panic(`attempt to ${op} with overflow`); - return value; + return box(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, + const clamp = (value: bigint) => box(value < min ? min : value > max ? max : value); + const inRange = (value: bigint) => (fits(value) ? box(value) : undefined); + const unsigned = (a: T) => BigInt.asUintN(bits, big(a)); + const leading = (value: bigint) => { + let n = 0; + for (let bit = BigInt(bits - 1); bit >= 0n && (value >> bit) & 1n; bit--) n++; + return n; + }; + const lt = (a: T, b: T) => compare(a, b) < 0; + const one = box(1n); + const int: WideInt = { + MIN: box(min), + MAX: box(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"), + add: (a, b) => checked(big(a) + big(b), "add"), + sub: (a, b) => checked(big(a) - big(b), "subtract"), + mul: (a, b) => checked(big(a) * big(b), "multiply"), + neg: (a) => checked(-big(a), "negate"), div(a, b) { - if (b === 0n) panic("attempt to divide by zero"); - return checked(a / b, "divide"); + const d = big(b); + if (d === 0n) panic("attempt to divide by zero"); + return checked(big(a) / d, "divide"); }, 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++; - return n; - }, - leadingZeros(a) { - let n = 0; - for (let bit = BigInt(bits - 1); bit >= 0n && !((unsigned(a) >> bit) & 1n); bit--) n++; - return n; + const d = big(b); + if (d === 0n) panic("attempt to calculate the remainder with a divisor of zero"); + const n = big(a); + if (signed && d === -1n && n === min) panic("attempt to calculate the remainder with overflow"); + return box(n % d); }, + shl: (a, b) => box(big(a) << BigInt(shift(b, bits, "left"))), + shr: (a, b) => box((signed ? big(a) : unsigned(a)) >> BigInt(shift(b, bits, "right"))), + checkedAdd: (a, b) => inRange(big(a) + big(b)), + checkedSub: (a, b) => inRange(big(a) - big(b)), + checkedMul: (a, b) => inRange(big(a) * big(b)), + saturatingAdd: (a, b) => clamp(big(a) + big(b)), + saturatingSub: (a, b) => clamp(big(a) - big(b)), + wrappingAdd: (a, b) => box(big(a) + big(b)), + wrappingSub: (a, b) => box(big(a) - big(b)), + leadingOnes: (a) => leading(unsigned(a)), + leadingZeros: (a) => leading(BigInt.asUintN(bits, ~big(a))), // A 32-bit word at a time: one bigint step per word, not per byte. fromBeBytes(bytes) { let value = 0n; @@ -197,7 +256,7 @@ function wide(bits: number, signed: boolean): Int { 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); + return box(value); }, toBeBytes(a) { const out = new Uint8Array(bits / 8); @@ -213,13 +272,219 @@ function wide(bits: number, signed: boolean): Int { return out; }, tryFrom(value) { - if (fits(value)) return Ok(BigInt(value)); - return Err(new TryFromIntError(value < min ? "NegOverflow" : "PosOverflow")); + const v = typeof value === "number" ? BigInt(value) : exact(value); + if (fits(v)) return Ok(box(v)); + return Err(new TryFromIntError(v < min ? "NegOverflow" : "PosOverflow")); + }, + display(a, f) { + const v = big(a); + return f.padIntegral(v >= 0n, "", (v < 0n ? -v : v).toString()); }, - 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()), + of: (value) => box(BigInt(value)), + cast: (value) => box(typeof value === "number" ? BigInt(value) : exact(value)), + eq: (a, b) => compare(a, b) === 0, + ne: (a, b) => compare(a, b) !== 0, + lt, + le: (a, b) => compare(a, b) <= 0, + gt: (a, b) => compare(a, b) > 0, + ge: (a, b) => compare(a, b) >= 0, + max: (a, b) => (lt(b, a) ? a : b), + min: (a, b) => (lt(b, a) ? b : a), + not: (a) => box(~big(a)), + and: (a, b) => box(big(a) & big(b)), + or: (a, b) => box(big(a) | big(b)), + xor: (a, b) => box(big(a) ^ big(b)), + *range(start, end, inclusive) { + for (let i = start; inclusive ? !lt(end, i) : lt(i, end); i = int.add(i, one)) { + yield i; + // Stepping past `end` could overflow at the type's maximum. + if (compare(i, end) === 0) return; + } + }, + }; + return int; +} + +/** + * `u64` or `i64`: exact through bigints, with the common operations computed on the + * halves, which is several times faster. + */ +function wide64(signed: boolean): WideInt { + // The high half's range, and its value from any 32 bits. + const hmin = signed ? -0x80000000 : 0; + const hmax = signed ? 0x7fffffff : 0xffffffff; + const high = signed ? (h: number) => h | 0 : (h: number) => h >>> 0; + const make = (hi: number, lo: number) => ({ hi, lo }) as T; + const box = (v: bigint) => make(Number(signed ? BigInt.asIntN(32, v >> 32n) : BigInt.asUintN(32, v >> 32n)), Number(BigInt.asUintN(32, v))); + // An exact integer of at most 2^53 in magnitude. + const fromNumber = (n: number) => make(high(Math.floor(n / TWO32)), n >>> 0); + const compare = (a: T, b: T) => (a.hi !== b.hi ? (a.hi < b.hi ? -1 : 1) : a.lo === b.lo ? 0 : a.lo < b.lo ? -1 : 1); + const lt = (a: T, b: T) => a.hi < b.hi || (a.hi === b.hi && a.lo < b.lo); + const isZero = (a: T) => a.hi === 0 && a.lo === 0; + const base = wide(64, signed, box, compare); + + // The high half of the last `mul32`. + let mulHi = 0; + /** The full product of two uint32s: its low half, with the high one in `mulHi`. */ + const mul32 = (a: number, b: number) => { + const a0 = a & 0xffff; + const a1 = a >>> 16; + const b0 = b & 0xffff; + const b1 = b >>> 16; + const p00 = a0 * b0; + const p01 = a0 * b1; + const p10 = a1 * b0; + const mid = (p00 >>> 16) + (p01 & 0xffff) + (p10 & 0xffff); + mulHi = a1 * b1 + (p01 >>> 16) + (p10 >>> 16) + (mid >>> 16); + return (((mid & 0xffff) << 16) | (p00 & 0xffff)) >>> 0; + }; + + return { + ...base, + add(a, b) { + const lo = a.lo + b.lo; + const hi = a.hi + b.hi + (lo >= TWO32 ? 1 : 0); + if (hi < hmin || hi > hmax) panic("attempt to add with overflow"); + return make(hi, lo >>> 0); + }, + sub(a, b) { + const lo = a.lo - b.lo; + const hi = a.hi - b.hi - (lo < 0 ? 1 : 0); + if (hi < hmin || hi > hmax) panic("attempt to subtract with overflow"); + return make(hi, lo >>> 0); + }, + wrappingAdd(a, b) { + const lo = a.lo + b.lo; + return make(high(a.hi + b.hi + (lo >= TWO32 ? 1 : 0)), lo >>> 0); + }, + wrappingSub(a, b) { + const lo = a.lo - b.lo; + return make(high(a.hi - b.hi - (lo < 0 ? 1 : 0)), lo >>> 0); + }, + mul(a, b) { + const n = approx(a) * approx(b); + if (safe(n)) return fromNumber(n); + if (!signed && (a.hi === 0 || b.hi === 0)) { + // One factor below 2^32: its product with the other's high half must be too. + const cross = a.hi * b.lo + a.lo * b.hi; + const lo = mul32(a.lo, b.lo); + const hi = mulHi + cross; + if (hi > 0xffffffff) panic("attempt to multiply with overflow"); + return make(hi, lo); + } + return base.mul(a, b); + }, + div(a, b) { + if (isZero(b)) panic("attempt to divide by zero"); + const n = approx(a); + const d = approx(b); + // Below 2^53, a quotient of doubles truncates to the exact one. + if (safe(n) && safe(d)) return fromNumber(Math.trunc(n / d)); + if (!signed && b.hi === 0) { + // Long division by a uint32 in 32- and 16-bit digits, every step exact. + const d = b.lo; + const qh = Math.floor(a.hi / d); + let r = a.hi - qh * d; + const t1 = r * 65536 + (a.lo >>> 16); + const q1 = Math.floor(t1 / d); + r = t1 - q1 * d; + const q2 = Math.floor((r * 65536 + (a.lo & 0xffff)) / d); + return make(qh, (q1 * 65536 + q2) >>> 0); + } + return base.div(a, b); + }, + rem(a, b) { + const n = approx(a); + const d = approx(b); + if (safe(n) && safe(d) && d !== 0) return fromNumber(n % d); + return base.rem(a, b); + }, + neg(a) { + if (!signed || (a.hi === hmin && a.lo === 0)) return base.neg(a); + const lo = -a.lo >>> 0; + return make((~a.hi + (lo === 0 ? 1 : 0)) | 0, lo); + }, + not: (a) => make(high(~a.hi), ~a.lo >>> 0), + and: (a, b) => make(high(a.hi & b.hi), (a.lo & b.lo) >>> 0), + or: (a, b) => make(high(a.hi | b.hi), (a.lo | b.lo) >>> 0), + xor: (a, b) => make(high(a.hi ^ b.hi), (a.lo ^ b.lo) >>> 0), + shl(a, b) { + const n = shift(b, 64, "left"); + if (n === 0) return a; + if (n < 32) return make(high((a.hi << n) | (a.lo >>> (32 - n))), (a.lo << n) >>> 0); + return make(high(a.lo << (n - 32)), 0); + }, + shr(a, b) { + const n = shift(b, 64, "right"); + if (n === 0) return a; + if (signed) { + if (n < 32) return make(a.hi >> n, ((a.lo >>> n) | (a.hi << (32 - n))) >>> 0); + return make(a.hi >> 31, (a.hi >> (n - 32)) >>> 0); + } + if (n < 32) return make(a.hi >>> n, ((a.lo >>> n) | (a.hi << (32 - n))) >>> 0); + return make(0, a.hi >>> (n - 32)); + }, + leadingZeros(a) { + const hi = a.hi >>> 0; + return hi !== 0 ? Math.clz32(hi) : 32 + Math.clz32(a.lo); + }, + leadingOnes(a) { + const hi = ~a.hi >>> 0; + return hi !== 0 ? Math.clz32(hi) : 32 + Math.clz32(~a.lo >>> 0); + }, + fromBeBytes: (b) => + make(high((b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]), ((b[4] << 24) | (b[5] << 16) | (b[6] << 8) | b[7]) >>> 0), + toBeBytes(a) { + const out = new Uint8Array(8); + const { hi, lo } = a; + out[0] = hi >>> 24; + out[1] = hi >>> 16; + out[2] = hi >>> 8; + out[3] = hi; + out[4] = lo >>> 24; + out[5] = lo >>> 16; + out[6] = lo >>> 8; + out[7] = lo; + return out; + }, + display(a, f) { + const n = approx(a); + if (!safe(n)) return base.display(a, f); + return f.padIntegral(n >= 0, "", String(n < 0 ? -n : n)); + }, + of: (value) => (typeof value === "number" ? fromNumber(value) : box(value)), + cast(value) { + if (typeof value === "number") return make(high(value < 0 ? -1 : 0), value >>> 0); + return make(high("w1" in value ? value.w1 : value.hi), value.lo); + }, + eq: (a, b) => a.hi === b.hi && a.lo === b.lo, + ne: (a, b) => a.hi !== b.hi || a.lo !== b.lo, + lt, + le: (a, b) => !lt(b, a), + gt: (a, b) => lt(b, a), + ge: (a, b) => !lt(a, b), + max: (a, b) => (lt(b, a) ? a : b), + min: (a, b) => (lt(b, a) ? b : a), + }; +} + +/** `u128` or `i128`: four limbs, computed through bigints. */ +function wide128(signed: boolean): WideInt { + const limb = (v: bigint, at: bigint) => Number(BigInt.asUintN(32, v >> at)); + const box = (v: bigint) => { + const top = v >> 96n; + const hi = Number(signed ? BigInt.asIntN(32, top) : BigInt.asUintN(32, top)); + return { hi, w2: limb(v, 64n), w1: limb(v, 32n), lo: limb(v, 0n) } as T; + }; + const compare = (a: T, b: T) => { + if (a.hi !== b.hi) return a.hi < b.hi ? -1 : 1; + if (a.w2 !== b.w2) return a.w2 < b.w2 ? -1 : 1; + if (a.w1 !== b.w1) return a.w1 < b.w1 ? -1 : 1; + return a.lo === b.lo ? 0 : a.lo < b.lo ? -1 : 1; }; + return wide(128, signed, box, compare); } export const u8: Int = small(8, false); @@ -232,18 +497,18 @@ export const i32: Int = small(32, true); export const usize: Int = u32; /** 32 bits, as on wasm32. */ export const isize: Int = i32; -export const u64: Int = wide(64, false); -export const i64: Int = wide(64, true); -export const u128: Int = wide(128, false); -export const i128: Int = wide(128, true); +export const u64: WideInt = wide64(false); +export const i64: WideInt = wide64(true); +export const u128: WideInt = wide128(false); +export const i128: WideInt = wide128(true); /** The larger of two integers. */ -export function max(a: T, b: T): T { +export function max(a: number, b: number): number { return a >= b ? a : b; } /** The smaller of two integers. */ -export function min(a: T, b: T): T { +export function min(a: number, b: number): number { return a <= b ? a : b; } diff --git a/ts/src/lower/body.rs b/ts/src/lower/body.rs index 53453d0f7..bbc74afdf 100644 --- a/ts/src/lower/body.rs +++ b/ts/src/lower/body.rs @@ -1365,11 +1365,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { self.spill(end, "end", out) } }; - Some(Iterable::Range { - start, - end, - inclusive: false, - }) + Some(range_of(ty, start, end, false)) } ty::Adt(adt, _) if tcx.is_lang_item(adt.did(), rustc_attr_ir::LangItem::RangeInclusiveStruct) => { // `a..=b` is `RangeInclusive::new(a, b)`. @@ -1391,11 +1387,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { self.spill(end, "end", out) } }; - Some(Iterable::Range { - start, - end, - inclusive: true, - }) + Some(range_of(ty, start, end, true)) } // An array by value iterates its elements, already copied. ty::Array(..) => Some(Iterable::Of(self.operand(id, out))), @@ -2413,6 +2405,18 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { } } +/// A range of `ty` as a `for` loop iterates it: counting in a number, or through the +/// runtime for a wide integer, which is an object. +fn range_of(ty: Ty<'_>, start: Expr, end: Expr, inclusive: bool) -> Iterable { + let ty::Adt(_, args) = ty.kind() else { + unreachable!("a range is a struct") + }; + match super::num::Int::of(args.type_at(0)) { + Some(int) if int.big() => Iterable::Of(int.method("range", vec![start, end, Expr::lit(inclusive.to_string())])), + _ => Iterable::Range { start, end, inclusive }, + } +} + enum Iterable { Range { start: Expr, end: Expr, inclusive: bool }, Of(Expr), diff --git a/ts/src/lower/consts.rs b/ts/src/lower/consts.rs index 8a6bf1b0d..843fd47ca 100644 --- a/ts/src/lower/consts.rs +++ b/ts/src/lower/consts.rs @@ -27,8 +27,8 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { }; let value = n.get() as i128; let value = if *neg { -value } else { value }; - match self.literal_text(span, int, *neg) { - Some(text) => Expr::lit(text), + match self.literal_text(span, *neg) { + Some(text) => int.spelled(text, value), None => int.lit(value), } } @@ -87,7 +87,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { } /// A literal as the source spells it, when that is also valid JS. - fn literal_text(&self, span: Span, int: Int, neg: bool) -> Option { + fn literal_text(&self, span: Span, neg: bool) -> Option { if span.from_expansion() { return None; } @@ -125,8 +125,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { return None; } let sign = if neg { "-" } else { "" }; - let suffix = if int.big() { "n" } else { "" }; - Some(format!("{sign}{prefix}{cleaned}{suffix}")) + Some(format!("{sign}{prefix}{cleaned}")) } /// A constant's value tree as a JS value of type `ty`. diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 2b5cd8830..540a018e0 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -288,11 +288,17 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Args<'_>, }; let values = b.args(args, out); let [l, r] = <[Expr; 2]>::try_from(values).ok()?; - l.binary(js, r) + match Int::of(targ(gargs, 0).peel_refs()) { + Some(int) => int.compare(js, l, r), + None => l.binary(js, r), + } } - ("Ord", "max" | "min") if Int::of(targ(gargs, 0)).is_some() => { + ("Ord", "max" | "min") if let Some(int) = Int::of(targ(gargs, 0)) => { let values = b.args(args, out); - Expr::rt(if name == "max" { "max" } else { "min" }, values) + match int.big() { + true => int.method(name.as_str(), values), + false => Expr::rt(if name == "max" { "max" } else { "min" }, values), + } } ("Clone", "clone") => { let ty = targ(gargs, 0); diff --git a/ts/src/lower/mod.rs b/ts/src/lower/mod.rs index b6dc553d6..b90b1dae1 100644 --- a/ts/src/lower/mod.rs +++ b/ts/src/lower/mod.rs @@ -804,10 +804,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { let tcx = self.tcx; match ty.kind() { ty::Bool => Type::Builtin("boolean"), - ty::Int(_) | ty::Uint(_) => match num::Int::of(ty).is_some_and(|int| int.big()) { - true => Type::Builtin("bigint"), - false => Type::Builtin("number"), - }, + ty::Int(_) | ty::Uint(_) => num::Int::of(ty).unwrap().ty(), ty::Str => Type::Builtin("string"), ty::Never => Type::Builtin("never"), ty::Tuple(tys) if tys.is_empty() => Type::Builtin("undefined"), @@ -1112,8 +1109,6 @@ pub fn escape(name: &str) -> String { "rt", // Globals the output calls. "Array", - "BigInt", - "Number", "Object", "Uint8Array", ]; diff --git a/ts/src/lower/num.rs b/ts/src/lower/num.rs index 49515845a..b71d292a5 100644 --- a/ts/src/lower/num.rs +++ b/ts/src/lower/num.rs @@ -2,15 +2,16 @@ //! //! Integers up to 32 bits are JS numbers, in which their sums, differences, and //! products are exact or provably out of range, so each is checked as computed. Wider -//! integers are bigints. Every operation that can overflow is checked, as a debug build -//! checks it: none wraps silently. +//! integers are the runtime's immutable objects of 32-bit limbs, which every operator, +//! comparison included, goes through the runtime for. Every operation that can overflow +//! is checked, as a debug build checks it: none wraps silently. use rustc_middle::mir::{BinOp, UnOp}; use rustc_middle::ty::{self, Ty}; use rustc_span::Span; use super::body::Body; -use crate::ts::Expr; +use crate::ts::{Expr, Type}; /// An integer type, by width and signedness. `usize` and `isize` are 32 bits. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -37,11 +38,22 @@ impl Int { Some(Self { bits, signed, pointer }) } - /// Whether values are bigints rather than numbers. + /// Whether values are runtime objects rather than numbers. pub fn big(self) -> bool { self.bits > 32 } + /// The TypeScript type of a value. + pub fn ty(self) -> Type { + match (self.signed, self.bits) { + (_, 8..=32) => Type::Builtin("number"), + (false, 64) => Type::Rt("U64", Vec::new()), + (true, 64) => Type::Rt("I64", Vec::new()), + (false, _) => Type::Rt("U128", Vec::new()), + (true, _) => Type::Rt("I128", Vec::new()), + } + } + pub fn min(self) -> i128 { match (self.signed, self.bits) { (true, 128) => i128::MIN, @@ -92,10 +104,19 @@ impl Int { true => value.to_string(), false => (value as u128).to_string(), }; - match self.big() { - true => Expr::lit(format!("{text}n")), - false => Expr::lit(text), + self.spelled(text, value) + } + + /// A literal of this type spelled `text`, JS number digits of `value`: `5`, or + /// `rt.u64.of(5)` when wide. A wide value past 2^53 is spelled as a bigint, + /// `rt.u64.of(0xffffffffffffffffn)`, since a number can't hold it exactly. + pub fn spelled(self, text: String, value: i128) -> Expr { + if !self.big() { + return Expr::lit(text); } + let safe = (1 - (1i128 << 53)..(1 << 53)).contains(&value) && (self.signed || value >= 0); + let text = if safe { text } else { format!("{text}n") }; + self.method("of", vec![Expr::lit(text)]) } /// The runtime method `rt..`. @@ -103,26 +124,35 @@ impl Int { Expr::Rt(self.rt()).member(name).call(args) } - /// `value`, which is exact, wrapped into this type's range. + /// `value`, an exact integer of at most 32 bits, wrapped into this narrow type's range. 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), - (false, false, 32) => value.binary(">>>", Expr::lit("0")), - (false, true, 32) => value.binary("|", Expr::lit("0")), - (false, false, bits) => value.binary("&", Expr::lit(format!("0x{:x}", (1u64 << bits) - 1))), - (false, true, bits) => { + debug_assert!(!self.big()); + match (self.signed, self.bits) { + (false, 32) => value.binary(">>>", Expr::lit("0")), + (true, 32) => value.binary("|", Expr::lit("0")), + (false, bits) => value.binary("&", Expr::lit(format!("0x{:x}", (1u64 << bits) - 1))), + (true, bits) => { let shift = Expr::lit((32 - bits).to_string()); value.binary("<<", shift.clone()).binary(">>", shift) } } } -} -fn big_int(method: &str, bits: u32, value: Expr) -> Expr { - Expr::ident("BigInt") - .member(method) - .call(vec![Expr::lit(bits.to_string()), value]) + /// A comparison, `===` or `<` for instance, of two values of this type. + pub fn compare(self, js: &'static str, l: Expr, r: Expr) -> Expr { + if !self.big() { + return l.binary(js, r); + } + let name = match js { + "===" => "eq", + "!==" => "ne", + "<" => "lt", + "<=" => "le", + ">" => "gt", + _ => "ge", + }; + self.method(name, vec![l, r]) + } } /// An operand, and its value when it is a constant. @@ -181,7 +211,8 @@ pub fn binary<'tcx>( }; if let Some(js) = compare { return match lty.kind() { - ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Char => l.expr.binary(js, r.expr), + ty::Int(_) | ty::Uint(_) => Int::of(lty).unwrap().compare(js, l.expr, r.expr), + ty::Bool | ty::Char => l.expr.binary(js, r.expr), _ => b.err(span, format!("comparing `{lty}`")), }; } @@ -206,21 +237,15 @@ pub fn binary<'tcx>( if let (Some(a), Some(c)) = (l.value, r.value) && let Some(value) = fold(op, a, c, int) { - let exact = match int.big() { - true => true, - false => match op { + let exact = !int.big() + && match op { BinOp::Add | BinOp::Sub | BinOp::Mul => value.abs() < (1 << 53), // JS bitwise operators compute in 32-bit signed integers. _ => (i32::MIN as i128..=i32::MAX as i128).contains(&value) && a >= 0 && c >= 0, - }, - }; + }; if !exact { return int.lit(value); } - let rhs = match (op, int.big()) { - (BinOp::Shl | BinOp::Shr, true) => Expr::lit(format!("{c}n")), - _ => r.expr, - }; let js = match op { BinOp::Add => "+", BinOp::Sub => "-", @@ -233,7 +258,29 @@ pub fn binary<'tcx>( BinOp::Shr => ">>", _ => return int.lit(value), }; - return l.expr.binary(js, rhs); + return l.expr.binary(js, r.expr); + } + + if int.big() { + let name = match op { + BinOp::Add => "add", + BinOp::Sub => "sub", + BinOp::Mul => "mul", + BinOp::Div => "div", + BinOp::Rem => "rem", + BinOp::BitAnd => "and", + BinOp::BitOr => "or", + BinOp::BitXor => "xor", + BinOp::Shl => "shl", + BinOp::Shr => "shr", + _ => return b.err(span, format!("unsupported operator `{op:?}`")), + }; + // A constant shift amount is a number, whatever its type. + let rhs = match (op, r.value) { + (BinOp::Shl | BinOp::Shr, Some(c)) => Expr::lit(c.to_string()), + _ => r.expr, + }; + return int.method(name, vec![l.expr, rhs]); } match op { @@ -250,8 +297,8 @@ pub fn binary<'tcx>( }; let value = l.expr.binary(js, r.expr); // Only an unsigned 32-bit result can land outside a JS int32. - match (int.big(), int.signed, int.bits) { - (false, false, 32) => value.binary(">>>", Expr::lit("0")), + match (int.signed, int.bits) { + (false, 32) => value.binary(">>>", Expr::lit("0")), _ => value, } } @@ -264,12 +311,10 @@ pub fn binary<'tcx>( let name = if op == BinOp::Shl { "shl" } else { "shr" }; return int.method(name, vec![l.expr, r.expr]); }; - match (op, int.big()) { - (BinOp::Shl, false) => int.wrap(l.expr.binary("<<", Expr::lit(c.to_string()))), - (BinOp::Shl, true) => int.wrap(l.expr.binary("<<", Expr::lit(format!("{c}n")))), - (_, false) if !int.signed && int.bits == 32 => l.expr.binary(">>>", Expr::lit(c.to_string())), - (_, false) => l.expr.binary(">>", Expr::lit(c.to_string())), - (_, true) => l.expr.binary(">>", Expr::lit(format!("{c}n"))), + match op { + BinOp::Shl => int.wrap(l.expr.binary("<<", Expr::lit(c.to_string()))), + _ if !int.signed && int.bits == 32 => l.expr.binary(">>>", Expr::lit(c.to_string())), + _ => l.expr.binary(">>", Expr::lit(c.to_string())), } } _ => b.err(span, format!("unsupported operator `{op:?}`")), @@ -282,10 +327,13 @@ pub fn unary<'tcx>(b: &mut Body<'_, '_, 'tcx>, op: UnOp, value: Expr, ty: Ty<'tc (UnOp::Not, ty::Bool) => value.not(), (UnOp::Not, ty::Int(_) | ty::Uint(_)) => { let int = Int::of(ty).unwrap(); + if int.big() { + return int.method("not", vec![value]); + } let inverted = Expr::unary("~", value); - match (int.signed, int.big()) { - (true, _) => inverted, - (false, _) => int.wrap(inverted), + match int.signed { + true => inverted, + false => int.wrap(inverted), } } (UnOp::Neg, ty::Int(_)) => { @@ -302,15 +350,7 @@ pub fn cast<'tcx>(b: &mut Body<'_, '_, 'tcx>, value: Operand, from: Ty<'tcx>, to return b.err(span, format!("a cast to `{to}`")); }; if let ty::Bool = from.kind() { - let (one, zero) = match target.big() { - true => ("1n", "0n"), - false => ("1", "0"), - }; - return Expr::Cond( - Box::new(value.expr), - Box::new(Expr::lit(one)), - Box::new(Expr::lit(zero)), - ); + return Expr::Cond(Box::new(value.expr), Box::new(target.lit(1)), Box::new(target.lit(0))); } if let ty::Adt(adt, _) = from.kind() && adt.is_enum() @@ -343,18 +383,12 @@ pub fn cast<'tcx>(b: &mut Body<'_, '_, 'tcx>, value: Operand, from: Ty<'tcx>, to match (source.big(), target.big()) { (false, false) if fits => value.expr, (false, false) => target.wrap(value.expr), - (false, true) => { - let widened = Expr::ident("BigInt").call(vec![value.expr]); - match fits { - true => widened, - false => target.wrap(widened), - } - } - (true, false) => { - let method = if target.signed { "asIntN" } else { "asUintN" }; - Expr::ident("Number").call(vec![big_int(method, target.bits, value.expr)]) - } - (true, true) if fits => value.expr, - (true, true) => target.wrap(value.expr), + // The low limb holds a narrow target's bits, as a uint32. + (true, false) => match (target.signed, target.bits) { + (false, 32) => value.expr.member("lo"), + _ => target.wrap(value.expr.member("lo")), + }, + (true, true) if source.rt() == target.rt() => value.expr, + _ => target.method("cast", vec![value.expr]), } } diff --git a/ts/src/lower/pat.rs b/ts/src/lower/pat.rs index fba5bb208..052bbfbcb 100644 --- a/ts/src/lower/pat.rs +++ b/ts/src/lower/pat.rs @@ -9,6 +9,7 @@ use rustc_middle::ty::{self, Ty}; use super::AdtRepr; use super::body::Body; use super::library::Std; +use super::num::Int; use crate::ts::{Expr, Stmt, StmtKind}; impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { @@ -72,7 +73,10 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { match (&constant, ty.peel_refs().kind()) { (Expr::Lit(t), ty::Bool) if t == "true" => Some(scrut.clone()), (Expr::Lit(t), ty::Bool) if t == "false" => Some(scrut.clone().not()), - (_, ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Str) => Some(scrut.clone().binary("===", constant)), + (_, ty::Int(_) | ty::Uint(_)) => { + Some(Int::of(ty.peel_refs()).unwrap().compare("===", scrut.clone(), constant)) + } + (_, ty::Bool | ty::Str) => Some(scrut.clone().binary("===", constant)), _ => { self.t.err(pat.span, format!("a constant pattern of type `{ty}`")); None @@ -81,9 +85,13 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { } PatKind::Range(range) => { let mut tests = Vec::new(); + let compare = |op: &'static str, l: Expr, r: Expr| match Int::of(range.ty) { + Some(int) => int.compare(op, l, r), + None => l.binary(op, r), + }; if let PatRangeBoundary::Finite(lo) = range.lo { let lo = self.value(range.ty, lo, pat.span); - tests.push(lo.binary("<=", scrut.clone())); + tests.push(compare("<=", lo, scrut.clone())); } if let PatRangeBoundary::Finite(hi) = range.hi { let hi = self.value(range.ty, hi, pat.span); @@ -91,7 +99,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { rustc_hir::RangeEnd::Included => "<=", rustc_hir::RangeEnd::Excluded => "<", }; - tests.push(scrut.clone().binary(op, hi)); + tests.push(compare(op, scrut.clone(), hi)); } all(tests) } diff --git a/ts/tests/differential.ts b/ts/tests/differential.ts index 0cb5722dc..8030ac4d6 100644 --- a/ts/tests/differential.ts +++ b/ts/tests/differential.ts @@ -1,7 +1,10 @@ // Runs a fixture's harness both ways on the same inputs and fails on any difference: // natively, as the fixture's wasm32 build, and as its TypeScript translation. // -// usage: node differential.ts +// usage: node differential.ts [cases] [wide] +// +// `wide` adds a sweep of the `lang` fixture's 64- and 128-bit cases over operands that +// random bytes rarely make: every pair of values next to a power of two. // // A harness, or a hand-written caller wrapping one, exports `run(input: Uint8Array): // Vec`; the wasm build exports `input`, `run`, and `output` around the crate's @@ -15,8 +18,8 @@ import { Wasm } from "./wasm.ts"; type Outcome = Uint8Array | { panic: string }; -const [wasmPath, harnessPath, countArg] = process.argv.slice(2); -if (!wasmPath || !harnessPath) throw new Error("usage: differential.ts [cases]"); +const [wasmPath, harnessPath, countArg, wideArg] = process.argv.slice(2); +if (!wasmPath || !harnessPath) throw new Error("usage: differential.ts [cases] [wide]"); const count = Number(countArg ?? "20000"); const harness = await import(pathToFileURL(harnessPath).href); @@ -81,6 +84,45 @@ for (let op = 0; op < 8; op++) { } } } +if (wideArg === "wide") { + // 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([1_000_000n, 0x9e3779b97f4a7c15n, 0x0123456789abcdefn, 0xfedcba9876543210n]); + 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); + } + 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 < 24; kind++) { + for (const op of [3, 9]) cases.push(Uint8Array.of(op, kind, ...be(x), ...be(y), shift)); + for (const op of [4, 10]) cases.push(Uint8Array.of(op, kind, ...be(x), ...be(y), ...be(u), ...be(v), shift)); + } + // Negating an `i64` and an `i128`. + cases.push(Uint8Array.of(12, 3, ...be(x)), Uint8Array.of(12, 4, ...be(x), ...be(y))); + } + 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(13, from, to, ...be(x), ...be(y))); + cases.push(Uint8Array.of(21, from, ...be(x), ...be(y))); + } + } + } +} for (let i = 0; i < count; i++) { const len = Number(next() % 48n); const input = new Uint8Array(len + 1); diff --git a/ts/tests/fixtures.rs b/ts/tests/fixtures.rs index 357c0e142..fe7deaac4 100644 --- a/ts/tests/fixtures.rs +++ b/ts/tests/fixtures.rs @@ -140,8 +140,9 @@ fn typecheck(out: &Path) { } /// Run the harness natively, as wasm32, and translated, on the same inputs: the -/// translated side through `entry`, a module in `out`. -fn differential(package: &str, out: &Path, entry: &str, cases: u32) { +/// translated side through `entry`, a module in `out`. `extra` are `differential.ts`'s +/// arguments after the case count. +fn differential(package: &str, out: &Path, entry: &str, cases: u32, extra: &[&str]) { let target_dir = scratch().join("wasm"); let status = cargo() .args([ @@ -169,6 +170,7 @@ fn differential(package: &str, out: &Path, entry: &str, cases: u32) { .arg(&wasm) .arg(out.join(entry)) .arg(cases.to_string()) + .args(extra) .output() .expect("running node, which the dev shell provides"); let stdout = String::from_utf8_lossy(&output.stdout); @@ -185,7 +187,7 @@ fn codec() { let out = translated("codec"); snapshot("codec", &out); typecheck(&out); - differential("codec-fixture", &out, "harness.ts", 20_000); + differential("codec-fixture", &out, "harness.ts", 20_000, &[]); } #[test] @@ -195,7 +197,7 @@ fn lang() { // A hand-written caller, type-checked against the output it calls. std::fs::copy(manifest_dir().join("fixtures/lang/caller.ts"), out.join("caller.ts")).unwrap(); typecheck(&out); - differential("lang-fixture", &out, "caller.ts", 50_000); + differential("lang-fixture", &out, "caller.ts", 50_000, &["wide"]); } /// Each construct outside the subset fails with its span, and nothing is written. From 7afff4ad5ba765d4f8db09aa10d371f2ef4411a1 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 06:38:07 -0700 Subject: [PATCH 3/8] feat(ts): wide integers are numbers while safe, limbs past 2^53 (checkpoint) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- quest/m2/README.md | 1 - quest/m2/ts-byte-views.md | 4 - quest/m2/ts-collections.md | 6 +- quest/m2/ts-model.md | 2 +- quest/m2/ts-wide-ints.md | 33 - ts/README.md | 11 +- ts/fixtures/codec/expected/bench.ts | 16 +- ts/fixtures/codec/expected/coding/decode.ts | 2 +- ts/fixtures/codec/expected/coding/encode.ts | 8 +- ts/fixtures/codec/expected/coding/varint.ts | 20 +- ts/fixtures/codec/expected/coding/version.ts | 7 +- ts/fixtures/codec/expected/harness.ts | 22 +- ts/fixtures/codec/expected/lite/group.ts | 4 +- ts/fixtures/codec/expected/lite/probe.ts | 12 +- ts/fixtures/lang/expected/fmt.ts | 16 +- ts/fixtures/lang/expected/ints.ts | 708 +++++++++++++------ ts/fixtures/lang/expected/shapes.ts | 38 +- ts/fixtures/lang/src/ints.rs | 35 +- ts/runtime/rt.ts | 203 ++++-- ts/src/lower/num.rs | 43 +- ts/src/ts.rs | 8 + 21 files changed, 791 insertions(+), 408 deletions(-) delete mode 100644 quest/m2/ts-wide-ints.md diff --git a/quest/m2/README.md b/quest/m2/README.md index 95906e41e..b035bfad4 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -15,7 +15,6 @@ deletes the quests that follow it. ## Required - [Async and drop spike](/quest/m2/ts-async-spike.md) - generators from THIR against MIR state machines, deciding async, drop points, and the lowering -- [Wide integer spike](/quest/m2/ts-wide-ints.md) - `bigint` against boxed and unboxed hi/lo picks the 64- and 128-bit representation - [Cheaper byte slices](/quest/m2/ts-byte-views.md) - splitting a `&[u8]` stops making a `Uint8Array` view per split - [Drop in the TypeScript translator](/quest/m2/ts-drop.md) - a `Drop` type's `drop()` runs where Rust drops it - [Async in the TypeScript translator](/quest/m2/ts-async.md) - async code translates with Rust's lazy futures diff --git a/quest/m2/ts-byte-views.md b/quest/m2/ts-byte-views.md index 3bb8c7028..aa5320811 100644 --- a/quest/m2/ts-byte-views.md +++ b/quest/m2/ts-byte-views.md @@ -26,7 +26,3 @@ faster, and V8 gives a lone small array its own buffer, about a microsecond, the first time it is viewed. Measure Node, Bun, and Chrome. Public API: the translator's output for byte slices. - -## Related - -- [Wide integer spike](/quest/m2/ts-wide-ints.md) - the other half of decode's remaining gap diff --git a/quest/m2/ts-collections.md b/quest/m2/ts-collections.md index 490d021c3..102d335ba 100644 --- a/quest/m2/ts-collections.md +++ b/quest/m2/ts-collections.md @@ -16,13 +16,11 @@ by the translated `Ord`, and `VecDeque` a ring buffer. - `HashMap` iteration order is unspecified in Rust, so insertion order is a valid refinement; differential tests must not compare it. -- Which keys are primitive depends on the - [wide-integer representation](/quest/m2/ts-wide-ints.md): a hi/lo `u64` is - not one. +- A 64- or 128-bit integer is an object of 32-bit limbs + ([representation](/ts/README.md#design)), so it is not a primitive key. Public API: the translator's output for collections. ## Required -- [Wide integer spike](/quest/m2/ts-wide-ints.md) - which keys lower to primitives - [Iterators in the TypeScript translator](/quest/m2/ts-iter.md) - collections iterate through the same protocol diff --git a/quest/m2/ts-model.md b/quest/m2/ts-model.md index 23ff5d609..297a85c9a 100644 --- a/quest/m2/ts-model.md +++ b/quest/m2/ts-model.md @@ -23,7 +23,7 @@ Decided with the maintainer on 2026-10-23, for the subset: - Out for good: `unsafe` and raw pointers (all in `model/frame.rs`'s `Segment`), and trait objects except at boundaries (`Box`, transport erasure). Those bind to hand-written TS through config instead of translating. -- `u128` stays, on the [wide-integer](/quest/m2/ts-wide-ints.md) representation. +- `u128` stays, on the [wide-integer](/ts/README.md#design) representation. - What the census (`just ts census`, nightly) still ranks after the required quests (`Bytes`, `Duration`/`Instant`, cross-crate generics) lands here or splits into its own quest. diff --git a/quest/m2/ts-wide-ints.md b/quest/m2/ts-wide-ints.md deleted file mode 100644 index 8de723a6e..000000000 --- a/quest/m2/ts-wide-ints.md +++ /dev/null @@ -1,33 +0,0 @@ -# [M] Wide integer spike - -## Goal - -A report, from the [TS benchmark](/ts/README.md#bench), picks how -[rust2-ts](/ts/README.md) represents `u64`, `i64`, `u128`, and `i128`, and the -translator emits the winner for all four, exact at full width and checked on -overflow, proven by the existing differential tests. - -## Plan - -Decided with the maintainer on 2026-10-23: the merged design keeps `bigint` -until measured; this is that measurement. Candidates: - -- `bigint` with `BigInt.asUintN`/`asIntN`, today's baseline. TurboFan lowers a - visibly truncated 64-bit op to native word64, but plain `bigint` arithmetic - stays heap-allocated. -- A boxed immutable hi/lo object. Simple to emit; it allocates per op (Scala.js's - boxed `Long` measured 3-5x slower than `Int`). -- Unboxed hi/lo: the translator, which knows every type, scalar-replaces each - value into two `uint32` numbers in locals, fields, and arguments, as Scala.js - 1.20 does. An object appears only at the API edge, typed `{ hi: number; lo: - number }` plus a type-level `unique symbol` brand so `tsc` tells `u64` from - `i64` at zero runtime cost. -- `u128` and `i128` use the same family with more limbs (4x32 bits, or fewer - wider limbs if measured faster), rather than a separate path. moq-net uses - them for `Duration` millis and cache accounting. - -Measure the codec's varint and message paths and an arithmetic-heavy `lang` -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. diff --git a/ts/README.md b/ts/README.md index c2bc536b2..e30962e6c 100644 --- a/ts/README.md +++ b/ts/README.md @@ -52,7 +52,7 @@ runtime: what std does that JS does not. | --- | --- | | `bool`, `str`, `String` | `boolean`, `string` | | `u8`-`u32`, `i8`-`i32`, `usize`, `isize` | `number`, checked: overflow panics | -| `u64`, `i64`, `u128`, `i128` | `bigint`, checked the same way | +| `u64`, `i64`, `u128`, `i128` | `rt.U64`, `rt.I64`, `rt.U128`, `rt.I128`: immutable objects of 32-bit limbs, checked the same way | | `struct` | a class; methods take `this`, trait methods included | | `enum` without data | a union of strings: `"Lite01" \| "Lite02"`, or `never` without variants | | `enum` | a union of objects tagged by variant, with a companion object of functions | @@ -67,6 +67,11 @@ runtime: what std does that JS does not. a debug build checks it, so translated code panics where native code would: it never wraps silently. A panic throws `rt.Panic`, with the message native Rust panics with. +A wider integer is an object, so its operators and comparisons are runtime calls: +`rt.u64.add(a, b)`, `rt.u64.lt(a, b)`, `rt.u64.of(5)` for a literal, and +`rt.u64.cast(x)` for `x as u64`. Its fields are 32-bit limbs from `hi` down to `lo`, with +`hi` signed in a signed type; a caller builds one with `of`. + `format!`, `write!`, `panic!`, `assert!`, and the other macros of `format_args!` translate, as do `Display` and `Debug` impls, derived and thiserror's included. The runtime ports `core::fmt`, so the text matches native Rust's byte for byte. A placeholder @@ -132,7 +137,9 @@ built natively for wasm32 and run beside its translation on the same inputs by and writes its errors' `Display` and `Debug`. - `lang` covers every integer width's arithmetic and casts, control flow, patterns, `Copy` aggregates, and formatting. Its differential test runs through `caller.ts`, a - hand-written caller that also prints a `printable` error nothing translated prints. + hand-written caller that also prints a `printable` error nothing translated prints, + and adds every 64- and 128-bit case over each pair of values next to a power of two, + where the runtime's limbs carry, borrow, and switch paths. - `reject` holds constructs outside the subset; its expected errors are `expected.txt`, and its expected census is `census.md`. diff --git a/ts/fixtures/codec/expected/bench.ts b/ts/fixtures/codec/expected/bench.ts index 7b1a1f487..4d504ca59 100644 --- a/ts/fixtures/codec/expected/bench.ts +++ b/ts/fixtures/codec/expected/bench.ts @@ -58,7 +58,7 @@ export function encodeVarints(data: Uint8Array, form_: Form, out: rt.ByteVec): r const [count, data2] = takeU64(data); const [value, ] = takeU64(data2); let w: Encoder = Encoder.new(out, form_); - const end = count.lo; + const end = rt.u64.low(count); for (let _ = 0; _ < end; _++) { const varint = w.varint(value); if (!varint.ok) { @@ -72,7 +72,7 @@ export function encodeVarints(data: Uint8Array, form_: Form, out: rt.ByteVec): r export function decodeVarints(wire: Uint8Array, form_: Form, out: rt.ByteVec): rt.Result { let r: Decoder = Decoder.new(wire, form_); let count: number = 0; - let last: rt.U64 = rt.u64.of(0); + let last: rt.U64 = 0; while (!r.isEmpty()) { const varint = r.varint(); if (!varint.ok) { @@ -98,14 +98,14 @@ export function encodeFrames(data: Uint8Array, version: Version, out: rt.ByteVec const [size, data5] = takeU64(data4); const [keyEvery, data6] = takeU64(data5); const [keySize, ] = takeU64(data6); - const [delta2, keyEvery2] = [rt.i64.cast(delta), keyEvery.lo] as [rt.I64, number]; + const [delta2, keyEvery2] = [rt.i64.cast(delta), rt.u64.low(keyEvery)] as [rt.I64, number]; let w: Encoder = Encoder.new(out, Form.from(version)); - const end = count.lo; + const end = rt.u64.low(count); for (let n = 0; n < end; n++) { let value: [rt.I64, rt.U64]; arms: { if (n === 0) { - value = [rt.i64.of(0), first]; + value = [0, first]; break arms; } const n2 = n; @@ -132,8 +132,8 @@ export function encodeFrames(data: Uint8Array, version: Version, out: rt.ByteVec export function decodeFrames(wire: Uint8Array, version: Version, out: rt.ByteVec): rt.Result { let r: Decoder = Decoder.new(wire, Form.from(version)); let count: number = 0; - let total: rt.U64 = rt.u64.of(0); - let last: rt.I64 = rt.i64.of(0); + let total: rt.U64 = 0; + let last: rt.I64 = 0; while (!r.isEmpty()) { const varint = r.varint(); if (!varint.ok) { @@ -157,7 +157,7 @@ export function decodeFrames(wire: Uint8Array, version: Version, out: rt.ByteVec export function encodeGroup(data: Uint8Array, version: Version, out: rt.ByteVec): rt.Result { const [subscribe, data2] = takeU64(data); const [sequence, ] = takeU64(data2); - const group = new Group({ subscribe, sequence, frameStart: rt.u64.of(0) }); + const group = new Group({ subscribe, sequence, frameStart: 0 }); return group.encode(Encoder.new(out, Form.from(version)), version); } diff --git a/ts/fixtures/codec/expected/coding/decode.ts b/ts/fixtures/codec/expected/coding/decode.ts index 4902c56be..2b107e3fd 100644 --- a/ts/fixtures/codec/expected/coding/decode.ts +++ b/ts/fixtures/codec/expected/coding/decode.ts @@ -255,7 +255,7 @@ export class Decoder { if (!varint.ok) { return rt.Err(varint.error); } - return rt.Ok(rt.u64.checkedSub(varint.value, rt.u64.of(1))); + return rt.Ok(rt.u64.checkedSub(varint.value, 1)); } /** Read a varint length, then that many raw bytes. */ diff --git a/ts/fixtures/codec/expected/coding/encode.ts b/ts/fixtures/codec/expected/coding/encode.ts index ada80089e..377cbb4f0 100644 --- a/ts/fixtures/codec/expected/coding/encode.ts +++ b/ts/fixtures/codec/expected/coding/encode.ts @@ -92,20 +92,20 @@ export class Encoder { let v2: rt.U64; if (v !== undefined) { const v3 = v; - const okOr = rt.option.okOr(rt.u64.checkedAdd(v3, rt.u64.of(1)), "TooLarge" as EncodeError); + const okOr = rt.option.okOr(rt.u64.checkedAdd(v3, 1), "TooLarge" as EncodeError); if (!okOr.ok) { return rt.Err(okOr.error); } v2 = okOr.value; } else { - v2 = rt.u64.of(0); + v2 = 0; } return this.varint(v2); } /** Write a varint length, then the raw bytes. */ bytes(v: Uint8Array): rt.Result { - const varint = this.varint(rt.u64.cast(v.length)); + const varint = this.varint(v.length); if (!varint.ok) { return rt.Err(varint.error); } @@ -153,7 +153,7 @@ export class Encoder { const size = mapErr.value; rt.slice.copyFromSlice(rt.slice.range(this.buf.view(), rt.usize.sub(body, 2), body), rt.u16.toBeBytes(size)); } else { - const write_ = write(rt.u64.cast(rt.usize.sub(end, body)), this.form, this.buf); + const write_ = write(rt.usize.sub(end, body), this.form, this.buf); if (!write_.ok) { return rt.Err(EncodeError.from(write_.error)); } diff --git a/ts/fixtures/codec/expected/coding/varint.ts b/ts/fixtures/codec/expected/coding/varint.ts index baf70306b..e7404eb4e 100644 --- a/ts/fixtures/codec/expected/coding/varint.ts +++ b/ts/fixtures/codec/expected/coding/varint.ts @@ -31,12 +31,12 @@ export const Form = { /** The high and low 32 bits: the only place the codec splits a `u64`. */ export function toHalves(value: rt.U64): [number, number] { - return [rt.u64.shr(value, 32).lo, value.lo]; + return [rt.u64.low(rt.u64.shr(value, 32)), rt.u64.low(value)]; } /** The inverse of [`to_halves`]. */ export function fromHalves(hi: number, lo: number): rt.U64 { - return rt.u64.or(rt.u64.shl(rt.u64.cast(hi), 32), rt.u64.cast(lo)); + return rt.u64.or(rt.u64.shl(hi, 32), lo); } /** The bytes `value` takes on the wire in `form`, or [`BoundsExceeded`] if it does not fit. */ @@ -166,7 +166,7 @@ export function readQuic(buf: Uint8Array): rt.Result<[rt.U64, Uint8Array], Decod const first = splitFirst[0]; const rest = splitFirst[1]; if (first < 0x40) { - return rt.Ok([rt.u64.cast(first), rest]); + return rt.Ok([first, rest]); } else if (first < 0x80) { const okOr = rt.option.okOr(rt.slice.splitFirstChunk(buf, 2), { tag: "Short" } as DecodeError); if (!okOr.ok) { @@ -175,7 +175,7 @@ export function readQuic(buf: Uint8Array): rt.Result<[rt.U64, Uint8Array], Decod const a = okOr.value[0][0]; const b = okOr.value[0][1]; const rest2 = okOr.value[1]; - return rt.Ok([rt.u64.cast(((a & 0x3f) << 8) | b), rest2]); + return rt.Ok([((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) { @@ -186,7 +186,7 @@ export function readQuic(buf: Uint8Array): rt.Result<[rt.U64, Uint8Array], Decod const c = okOr2.value[0][2]; const d = okOr2.value[0][3]; const rest3 = okOr2.value[1]; - return rt.Ok([rt.u64.cast((((a2 & 0x3f) << 24) | (b2 << 16) | (c << 8) | d) >>> 0), rest3]); + return rt.Ok([(((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) { @@ -212,7 +212,7 @@ export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[rt. let value: [rt.U64, Uint8Array]; const leadingOnes = rt.u8.leadingOnes(first); if (leadingOnes === 0) { - value = [rt.u64.cast(first), rest]; + value = [first, rest]; } else if (leadingOnes === 1) { const okOr = rt.option.okOr(rt.slice.splitFirstChunk(buf, 2), { tag: "Short" } as DecodeError); if (!okOr.ok) { @@ -221,7 +221,7 @@ export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[rt. const a = okOr.value[0][0]; const b = okOr.value[0][1]; const rest2 = okOr.value[1]; - value = [rt.u64.cast(((a & 0x3f) << 8) | b), rest2]; + value = [((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) { @@ -231,7 +231,7 @@ export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[rt. const b2 = okOr2.value[0][1]; const c = okOr2.value[0][2]; const rest3 = okOr2.value[1]; - value = [rt.u64.cast(((a2 & 0x1f) << 16) | (b2 << 8) | c), rest3]; + value = [((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) { @@ -242,7 +242,7 @@ export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[rt. const c2 = okOr3.value[0][2]; const d = okOr3.value[0][3]; const rest4 = okOr3.value[1]; - value = [rt.u64.cast((((a3 & 0x0f) << 24) | (b3 << 16) | (c2 << 8) | d) >>> 0), rest4]; + value = [(((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) { @@ -313,7 +313,7 @@ export function zigzag(signed: rt.I64): rt.U64 { /** The inverse of [`zigzag`]. */ export function unzigzag(value: rt.U64): rt.I64 { - return rt.i64.xor(rt.i64.cast(rt.u64.shr(value, 1)), rt.i64.neg(rt.i64.cast(rt.u64.and(value, rt.u64.of(1))))); + return rt.i64.xor(rt.i64.cast(rt.u64.shr(value, 1)), rt.i64.neg(rt.i64.cast(rt.u64.and(value, 1)))); } //# sourceMappingURL=varint.ts.map diff --git a/ts/fixtures/codec/expected/coding/version.ts b/ts/fixtures/codec/expected/coding/version.ts index 542b72c43..f465490bd 100644 --- a/ts/fixtures/codec/expected/coding/version.ts +++ b/ts/fixtures/codec/expected/coding/version.ts @@ -63,7 +63,7 @@ export class Versions { /** Encode the version list. */ encode$Form(w: Encoder, version: Form): rt.Result { - const varint = w.varint(rt.u64.cast(this._0.length)); + const varint = w.varint(this._0.length); if (!varint.ok) { return rt.Err(varint.error); } @@ -82,7 +82,7 @@ export class Versions { /** Encode the version list. */ encode$Version(w: Encoder, version: version_Version): rt.Result { - const varint = w.varint(rt.u64.cast(this._0.length)); + const varint = w.varint(this._0.length); if (!varint.ok) { return rt.Err(varint.error); } @@ -103,9 +103,8 @@ export class Versions { } const count = varint.value; let vs: Version[] = []; - const start = rt.u64.of(0); const end = count; - for (const _ of rt.u64.range(start, end, false)) { + for (const _ of rt.u64.range(0, end, false)) { const decode = Version.decode(r, version); if (!decode.ok) { return rt.Err(decode.error); diff --git a/ts/fixtures/codec/expected/harness.ts b/ts/fixtures/codec/expected/harness.ts index 03a7a9114..ba5250f43 100644 --- a/ts/fixtures/codec/expected/harness.ts +++ b/ts/fixtures/codec/expected/harness.ts @@ -77,7 +77,7 @@ export function putU64(out: rt.ByteVec, value: rt.U64): void { } export function putUsize(out: rt.ByteVec, value: number): void { - putU64(out, rt.u64.cast(value)); + putU64(out, value); } /** Take up to 8 big-endian bytes as a `u64`, zero-padded on the right. */ @@ -305,20 +305,20 @@ export function encodeScript(data: Uint8Array, out: rt.ByteVec): void { let result: rt.Result; const value2 = rt.u8.rem(step, 10); if (value2 === 0) { - w.u8(value.lo & 0xff); + w.u8(rt.u64.low(value) & 0xff); result = rt.Ok(undefined); } else if (value2 === 1) { - w.u16(value.lo & 0xffff); + w.u16(rt.u64.low(value) & 0xffff); result = rt.Ok(undefined); } else if (value2 === 2) { - w.bool(rt.u64.eq(rt.u64.and(value, rt.u64.of(1)), rt.u64.of(1))); + w.bool(rt.u64.and(value, 1) === 1); result = rt.Ok(undefined); } else if (value2 === 3) { result = w.varint(value); } else if (value2 === 4) { const arg = w; - const value3 = rt.u64.rem(value, rt.u64.of(3)); - result = arg.varintOpt(rt.u64.eq(value3, rt.u64.of(0)) ? undefined : rt.u64.eq(value3, rt.u64.of(1)) ? value : rt.u64.of(18446744073709551615n)); + const value3 = rt.u64.rem(value, 3); + result = arg.varintOpt(value3 === 0 ? undefined : value3 === 1 ? value : rt.u64.of(18446744073709551615n)); } else if (value2 === 5) { result = w.bytes(rt.slice.range(rt.u64.toBeBytes(value), 0, rt.u8.rem(rt.u8.div(step, 10), 9))); } else if (value2 === 6) { @@ -330,7 +330,7 @@ export function encodeScript(data: Uint8Array, out: rt.ByteVec): void { result = w.string("not utf-8"); } } else if (value2 === 7) { - prefixes.push(rt.u64.eq(rt.u64.rem(value, rt.u64.of(2)), rt.u64.of(0)) ? w.prefixVarint() : w.prefixU16()); + prefixes.push(rt.u64.rem(value, 2) === 0 ? w.prefixVarint() : w.prefixU16()); result = rt.Ok(undefined); } else if (value2 === 8) { const pop = prefixes.pop(); @@ -490,9 +490,9 @@ export function encodeMessages(data: Uint8Array, out: rt.ByteVec): void { const [frameStart, data5] = takeU64(data4); const [bitrate, data6] = takeU64(data5); const [rtt, ] = takeU64(data6); - const group = new Group({ subscribe, sequence, frameStart: rt.u64.rem(frameStart, rt.u64.of(4)) }); + const group = new Group({ subscribe, sequence, frameStart: rt.u64.rem(frameStart, 4) }); encodeMessage$Group(group, version, out); - const probe = new Probe({ bitrate: rt.u64.checkedSub(bitrate, rt.u64.of(1)), rtt: rt.thenSome(rt.u64.ne(rt.u64.rem(rtt, rt.u64.of(3)), rt.u64.of(0)), rtt) }); + const probe = new Probe({ bitrate: rt.u64.checkedSub(bitrate, 1), rtt: rt.thenSome(rt.u64.rem(rtt, 3) !== 0, rtt) }); encodeMessage$Probe(probe, version, out); const versions = Versions.from([new Version(subscribe), new Version(sequence)]); let buf: rt.ByteVec = new rt.ByteVec(); @@ -547,9 +547,9 @@ export function errors(data: Uint8Array, out: rt.ByteVec): void { if (value2 === 0) { err = { tag: "InvalidMessage", _0: value }; } else if (value2 === 1) { - err = { tag: "MessageTooLarge", size: value.lo, max: rt.u64.shr(value, 32).lo }; + err = { tag: "MessageTooLarge", size: rt.u64.low(value), max: rt.u64.low(rt.u64.shr(value, 32)) }; } else if (value2 === 2) { - err = { tag: "Session", _0: { tag: "App", _0: value.lo & 0xffff } as SessionError }; + err = { tag: "Session", _0: { tag: "App", _0: rt.u64.low(value) & 0xffff } as SessionError }; } else if (value2 === 3) { err = { tag: "Session", _0: { tag: "ProtocolViolation" } as SessionError }; } else if (value2 === 4) { diff --git a/ts/fixtures/codec/expected/lite/group.ts b/ts/fixtures/codec/expected/lite/group.ts index b28cbc7fa..12315bb7c 100644 --- a/ts/fixtures/codec/expected/lite/group.ts +++ b/ts/fixtures/codec/expected/lite/group.ts @@ -70,7 +70,7 @@ export class Group { if (!varint3.ok) { return rt.Err(varint3.error); } - } else if (rt.u64.ne(this.frameStart, rt.u64.of(0))) { + } else if (this.frameStart !== 0) { return rt.Err("Version"); } return rt.Ok(undefined); @@ -96,7 +96,7 @@ export class Group { } frameStart = varint3.value; } else { - frameStart = rt.u64.of(0); + frameStart = 0; } return rt.Ok(new Group({ subscribe, sequence, frameStart })); } diff --git a/ts/fixtures/codec/expected/lite/probe.ts b/ts/fixtures/codec/expected/lite/probe.ts index 8729adb78..d1c219d11 100644 --- a/ts/fixtures/codec/expected/lite/probe.ts +++ b/ts/fixtures/codec/expected/lite/probe.ts @@ -66,16 +66,16 @@ export class Probe { return rt.Err("Version"); } const wire = rt.option.unwrapOr(rt.option.map(this.bitrate, (v: rt.U64): rt.U64 => { - return rt.u64.max(v, rt.u64.of(1)); - }), rt.u64.of(0)); + return rt.u64.max(v, 1); + }), 0); const varint = w.varint(wire); if (!varint.ok) { return rt.Err(varint.error); } if (Version.hasProbeRtt(version)) { const wire2 = rt.option.unwrapOr(rt.option.map(this.rtt, (v2: rt.U64): rt.U64 => { - return rt.u64.max(v2, rt.u64.of(1)); - }), rt.u64.of(0)); + return rt.u64.max(v2, 1); + }), 0); const varint2 = w.varint(wire2); if (!varint2.ok) { return rt.Err(varint2.error); @@ -94,7 +94,7 @@ export class Probe { if (!varint.ok) { return rt.Err(varint.error); } - if (rt.u64.eq(varint.value, rt.u64.of(0))) { + if (varint.value === 0) { bitrate = undefined; } else { const v = varint.value; @@ -108,7 +108,7 @@ export class Probe { if (!varint2.ok) { return rt.Err(varint2.error); } - if (rt.u64.eq(varint2.value, rt.u64.of(0))) { + if (varint2.value === 0) { rtt = undefined; } else { const v2 = varint2.value; diff --git a/ts/fixtures/lang/expected/fmt.ts b/ts/fixtures/lang/expected/fmt.ts index b7882ea35..0970ad45e 100644 --- a/ts/fixtures/lang/expected/fmt.ts +++ b/ts/fixtures/lang/expected/fmt.ts @@ -323,8 +323,8 @@ export function nested(data: Uint8Array): Nested { const arg4 = arg2; const arg5 = value2 === 0 ? { tag: "Dot" } as Shape : value2 === 1 ? { tag: "Line", _0: new Point({ x: 1, y: 2 }), _1: new Point({ x: -3, y: 4 }) } as Shape : { tag: "Rect", corner: new Point({ x: 0, y: 0 }), width: at(3), height: 9 } as Shape; const arg6 = rt.slice.toByteVec(rt.slice.range(data, 0, rt.min(data.length, 4))); - const arg7 = rt.slice.toVec([new Pair(at(4), rt.i64.of(-1)), new Pair(2, rt.i64.of(-9223372036854775808n))]); - const arg8 = rt.u8.rem(at(5), 2) === 0 ? new Pair(at(5), rt.i64.of(5)) : undefined; + const arg7 = rt.slice.toVec([new Pair(at(4), -1), new Pair(2, rt.i64.of(-9223372036854775808n))]); + const arg8 = rt.u8.rem(at(5), 2) === 0 ? new Pair(at(5), 5) : undefined; const arg9 = rt.u8.rem(at(6), 2) === 0 ? rt.Ok(at(6)) : rt.Err("no"); const arg10 = [rt.i16.sub(at(7), 100), 32767]; const arg11 = [at(8), true, "t"] as [number, boolean, string]; @@ -351,7 +351,7 @@ export function derived(data: Uint8Array, out: rt.ByteVec): void { put(out, rt.fmt.format([rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => wide.fmt(f)), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => six.fmt(f))])); const point = new Point({ x: at(2), y: -1 }); const arg5 = out; - const arg4 = new Pair(at(3), rt.i64.of(-9)); + const arg4 = new Pair(at(3), -9); put(arg5, rt.fmt.format([rt.fmt.spec({ width: 5 }, (f: rt.Formatter) => point.fmt(f)), "|", rt.fmt.spec({ align: "<", width: 4 }, (f: rt.Formatter) => point.fmt(f)), "|", rt.fmt.spec({ alternate: true, width: 4 }, (f: rt.Formatter) => point.fmt(f)), "|", rt.fmt.spec({ align: ">", width: 3 }, (f: rt.Formatter) => arg4.fmt(f))])); const kinds = [{ tag: "Empty" } as Kind, { tag: "One", _0: at(4) } as Kind, { tag: "Braced" } as Kind, { tag: "Bare" } as Kind, { tag: "Many", count: 0, items: new rt.ByteVec() } as Kind]; const arg7 = out; @@ -494,7 +494,7 @@ export function written(data: Uint8Array, out: rt.ByteVec): void { const manual = new Manual({ id: at(1), list: rt.slice.toVec([1, at(2)]) }); const empty = new Manual({ id: 0, list: [] }); put(out, rt.fmt.format([(f: rt.Formatter) => manual.fmt(f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => manual.fmt(f)), " ", (f: rt.Formatter) => empty.fmt(f)])); - const seq = new Seq(rt.slice.toVec([new Pair(at(3), rt.i64.of(0)), new Pair(1, rt.i64.of(-1))])); + const seq = new Seq(rt.slice.toVec([new Pair(at(3), 0), new Pair(1, -1)])); const none = new Seq([]); put(out, rt.fmt.format([(f: rt.Formatter) => seq.fmt(f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => seq.fmt(f)), " ", (f: rt.Formatter) => none.fmt(f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => none.fmt(f))])); const tup = new Tup(at(4), rt.u8.rem(at(5), 2) === 0 ? at(5) : undefined); @@ -518,7 +518,7 @@ export function panics(kind: number, rest: Uint8Array, out: rt.ByteVec): void { rt.panic(["internal error: entered unreachable code: never ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.u8.lowerHex(n, f)), " ", (f: rt.Formatter) => arg.fmt$Display(f)]); } else if (value === 2) { if (!(n > 200)) { - const arg2 = new Pair(n, rt.i64.of(0)); + const arg2 = new Pair(n, 0); rt.panic(["small ", (f: rt.Formatter) => rt.u8.display(n, f), " ", (f: rt.Formatter) => arg2.fmt(f)]); } } else if (value === 3) { @@ -534,7 +534,7 @@ export function panics(kind: number, rest: Uint8Array, out: rt.ByteVec): void { rt.assertFailed(kind2, (f: rt.Formatter) => rt.u8.display(leftVal, f), (f: rt.Formatter) => rt.u8.display(rightVal, f)); } } else if (value === 5) { - const value3 = [new Pair(n, rt.i64.of(1)), new Pair(n, rt.i64.of(1))] as [Pair, Pair]; + const value3 = [new Pair(n, 1), new Pair(n, 1)] as [Pair, Pair]; const leftVal2 = value3[0]; const rightVal2 = value3[1]; if (leftVal2.eq(rightVal2)) { @@ -555,7 +555,7 @@ export function panics(kind: number, rest: Uint8Array, out: rt.ByteVec): void { const arg3 = rt.result.unwrap(r, (value: Kind, f: rt.Formatter) => Kind.fmt(value, f)); put(arg4, rt.fmt.toString((f: rt.Formatter) => rt.u8.display(arg3, f))); } else if (value === 8) { - const r2 = n > 128 ? rt.Ok(n) : rt.Err(new Pair(n, rt.i64.of(-2))); + const r2 = n > 128 ? rt.Ok(n) : rt.Err(new Pair(n, -2)); const arg6 = out; const arg5 = rt.result.expect(rt.copy(r2), "expected a byte", (value: Pair, f: rt.Formatter) => value.fmt(f)); put(arg6, rt.fmt.toString((f: rt.Formatter) => rt.u8.display(arg5, f))); @@ -641,7 +641,7 @@ export const Fault = { } else if (value === 2) { return { tag: "TooLarge", size: (((at(1) << 24) >>> 0) | at(2)) >>> 0, max: at(3) }; } else { - return { tag: "At", _0: new Pair(at(1), rt.i64.shl(rt.i64.cast((at(2) << 24) >> 24), 40)) }; + return { tag: "At", _0: new Pair(at(1), rt.i64.shl((at(2) << 24) >> 24, 40)) }; } }, diff --git a/ts/fixtures/lang/expected/ints.ts b/ts/fixtures/lang/expected/ints.ts index 45e14905f..dc9ac3457 100644 --- a/ts/fixtures/lang/expected/ints.ts +++ b/ts/fixtures/lang/expected/ints.ts @@ -16,13 +16,13 @@ export function u8Op(op: number, data: Uint8Array, out: rt.ByteVec): void { 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(rt.u128.cast(v))); + out2.extendFromSlice(rt.u128.toBeBytes(v)); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v3))); + out3.extendFromSlice(rt.u128.toBeBytes(v3)); } else { out3.push(0); } @@ -76,6 +76,7 @@ export function u8Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(a === b ? 1 : 0); out.push(a < b ? 1 : 0); out.push(a >= b ? 1 : 0); + out.push(rt.u8.wrappingSub(a, b) === 0 ? 1 : 0); } else { let x: number = a; x = rt.u8.add(x, b); @@ -106,13 +107,13 @@ export function u16Op(op: number, data: Uint8Array, out: rt.ByteVec): void { 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(rt.u128.cast(v))); + out2.extendFromSlice(rt.u128.toBeBytes(v)); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v3))); + out3.extendFromSlice(rt.u128.toBeBytes(v3)); } else { out3.push(0); } @@ -166,6 +167,7 @@ export function u16Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(a === b ? 1 : 0); out.push(a < b ? 1 : 0); out.push(a >= b ? 1 : 0); + out.push(rt.u16.wrappingSub(a, b) === 0 ? 1 : 0); } else { let x: number = a; x = rt.u16.add(x, b); @@ -196,13 +198,13 @@ export function u32Op(op: number, data: Uint8Array, out: rt.ByteVec): void { 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(rt.u128.cast(v))); + out2.extendFromSlice(rt.u128.toBeBytes(v)); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v3))); + out3.extendFromSlice(rt.u128.toBeBytes(v3)); } else { out3.push(0); } @@ -256,6 +258,7 @@ export function u32Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(a === b ? 1 : 0); out.push(a < b ? 1 : 0); out.push(a >= b ? 1 : 0); + out.push(rt.u32.wrappingSub(a, b) === 0 ? 1 : 0); } else { let x: number = a; x = rt.u32.add(x, b); @@ -346,20 +349,18 @@ export function u64Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(rt.u64.eq(a, b) ? 1 : 0); out.push(rt.u64.lt(a, b) ? 1 : 0); out.push(rt.u64.ge(a, b) ? 1 : 0); + out.push(rt.u64.wrappingSub(a, b) === 0 ? 1 : 0); } else { let x: rt.U64 = a; x = rt.u64.add(x, b); - const value2 = rt.u64.of(1); - x = rt.u64.sub(x, value2); - const value3 = rt.u64.of(2); - x = rt.u64.mul(x, value3); + x = rt.u64.sub(x, 1); + x = rt.u64.mul(x, 2); x = rt.u64.shr(x, 1); x = rt.u64.shl(x, 1); - const value4 = rt.u64.of(8); - x = rt.u64.or(x, value4); + x = rt.u64.or(x, 8); x = rt.u64.xor(x, b); - const value5 = rt.u64.not(rt.u64.of(4)); - x = rt.u64.and(x, value5); + const value2 = rt.u64.not(4); + x = rt.u64.and(x, value2); put(out, x); } } @@ -438,20 +439,19 @@ export function u128Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(rt.u128.eq(a, b) ? 1 : 0); out.push(rt.u128.lt(a, b) ? 1 : 0); out.push(rt.u128.ge(a, b) ? 1 : 0); + out.push(rt.u128.wrappingSub(a, b) === 0 ? 1 : 0); } else { let x: rt.U128 = a; x = rt.u128.add(x, b); - const value2 = rt.u128.of(1); - x = rt.u128.sub(x, value2); - const value3 = rt.u128.of(2); - x = rt.u128.mul(x, value3); + x = rt.u128.sub(x, 1); + x = rt.u128.mul(x, 2); x = rt.u128.shr(x, 1); x = rt.u128.shl(x, 1); - const value4 = rt.u128.shl(rt.u128.of(1), 3); - x = rt.u128.or(x, value4); + const value2 = rt.u128.shl(1, 3); + x = rt.u128.or(x, value2); x = rt.u128.xor(x, b); - const value5 = rt.u128.not(rt.u128.shl(rt.u128.of(1), 2)); - x = rt.u128.and(x, value5); + const value3 = rt.u128.not(rt.u128.shl(1, 2)); + x = rt.u128.and(x, value3); put(out, x); } } @@ -470,13 +470,13 @@ export function usizeOp(op: number, data: Uint8Array, out: rt.ByteVec): void { 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(rt.u128.cast(v))); + out2.extendFromSlice(rt.u128.toBeBytes(v)); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v3))); + out3.extendFromSlice(rt.u128.toBeBytes(v3)); } else { out3.push(0); } @@ -530,6 +530,7 @@ export function usizeOp(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(a === b ? 1 : 0); out.push(a < b ? 1 : 0); out.push(a >= b ? 1 : 0); + out.push(rt.usize.wrappingSub(a, b) === 0 ? 1 : 0); } else { let x: number = a; x = rt.usize.add(x, b); @@ -560,13 +561,13 @@ export function i8Op(op: number, data: Uint8Array, out: rt.ByteVec): void { 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(rt.i128.cast(v))); + out2.extendFromSlice(rt.i128.toBeBytes(v)); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v3))); + out3.extendFromSlice(rt.i128.toBeBytes(v3)); } else { out3.push(0); } @@ -620,6 +621,7 @@ export function i8Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(a === b ? 1 : 0); out.push(a < b ? 1 : 0); out.push(a >= b ? 1 : 0); + out.push(rt.i8.wrappingSub(a, b) === 0 ? 1 : 0); } else { let x: number = a; x = rt.i8.add(x, b); @@ -650,13 +652,13 @@ export function i16Op(op: number, data: Uint8Array, out: rt.ByteVec): void { 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(rt.i128.cast(v))); + out2.extendFromSlice(rt.i128.toBeBytes(v)); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v3))); + out3.extendFromSlice(rt.i128.toBeBytes(v3)); } else { out3.push(0); } @@ -710,6 +712,7 @@ export function i16Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(a === b ? 1 : 0); out.push(a < b ? 1 : 0); out.push(a >= b ? 1 : 0); + out.push(rt.i16.wrappingSub(a, b) === 0 ? 1 : 0); } else { let x: number = a; x = rt.i16.add(x, b); @@ -740,13 +743,13 @@ export function i32Op(op: number, data: Uint8Array, out: rt.ByteVec): void { 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(rt.i128.cast(v))); + out2.extendFromSlice(rt.i128.toBeBytes(v)); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v3))); + out3.extendFromSlice(rt.i128.toBeBytes(v3)); } else { out3.push(0); } @@ -800,6 +803,7 @@ export function i32Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(a === b ? 1 : 0); out.push(a < b ? 1 : 0); out.push(a >= b ? 1 : 0); + out.push(rt.i32.wrappingSub(a, b) === 0 ? 1 : 0); } else { let x: number = a; x = rt.i32.add(x, b); @@ -890,20 +894,18 @@ export function i64Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(rt.i64.eq(a, b) ? 1 : 0); out.push(rt.i64.lt(a, b) ? 1 : 0); out.push(rt.i64.ge(a, b) ? 1 : 0); + out.push(rt.i64.wrappingSub(a, b) === 0 ? 1 : 0); } else { let x: rt.I64 = a; x = rt.i64.add(x, b); - const value2 = rt.i64.of(1); - x = rt.i64.sub(x, value2); - const value3 = rt.i64.of(2); - x = rt.i64.mul(x, value3); + x = rt.i64.sub(x, 1); + x = rt.i64.mul(x, 2); x = rt.i64.shr(x, 1); x = rt.i64.shl(x, 1); - const value4 = rt.i64.of(8); - x = rt.i64.or(x, value4); + x = rt.i64.or(x, 8); x = rt.i64.xor(x, b); - const value5 = rt.i64.not(rt.i64.of(4)); - x = rt.i64.and(x, value5); + const value2 = rt.i64.not(4); + x = rt.i64.and(x, value2); put(out, x); } } @@ -982,20 +984,19 @@ export function i128Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(rt.i128.eq(a, b) ? 1 : 0); out.push(rt.i128.lt(a, b) ? 1 : 0); out.push(rt.i128.ge(a, b) ? 1 : 0); + out.push(rt.i128.wrappingSub(a, b) === 0 ? 1 : 0); } else { let x: rt.I128 = a; x = rt.i128.add(x, b); - const value2 = rt.i128.of(1); - x = rt.i128.sub(x, value2); - const value3 = rt.i128.of(2); - x = rt.i128.mul(x, value3); + x = rt.i128.sub(x, 1); + x = rt.i128.mul(x, 2); x = rt.i128.shr(x, 1); x = rt.i128.shl(x, 1); - const value4 = rt.i128.shl(rt.i128.of(1), 3); - x = rt.i128.or(x, value4); + const value2 = rt.i128.shl(1, 3); + x = rt.i128.or(x, value2); x = rt.i128.xor(x, b); - const value5 = rt.i128.not(rt.i128.shl(rt.i128.of(1), 2)); - x = rt.i128.and(x, value5); + const value3 = rt.i128.not(rt.i128.shl(1, 2)); + x = rt.i128.and(x, value3); put(out, x); } } @@ -1014,13 +1015,13 @@ export function isizeOp(op: number, data: Uint8Array, out: rt.ByteVec): void { 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(rt.i128.cast(v))); + out2.extendFromSlice(rt.i128.toBeBytes(v)); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v3))); + out3.extendFromSlice(rt.i128.toBeBytes(v3)); } else { out3.push(0); } @@ -1074,6 +1075,7 @@ export function isizeOp(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(a === b ? 1 : 0); out.push(a < b ? 1 : 0); out.push(a >= b ? 1 : 0); + out.push(rt.isize.wrappingSub(a, b) === 0 ? 1 : 0); } else { let x: number = a; x = rt.isize.add(x, b); @@ -1097,7 +1099,7 @@ export function i8Neg(data: Uint8Array, out: rt.ByteVec): void { 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; - out.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(rt.i8.neg(a)))); + out.extendFromSlice(rt.i128.toBeBytes(rt.i8.neg(a))); } /** Negate, which overflows at the minimum. */ @@ -1107,7 +1109,7 @@ export function i16Neg(data: Uint8Array, out: rt.ByteVec): void { 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; - out.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(rt.i16.neg(a)))); + out.extendFromSlice(rt.i128.toBeBytes(rt.i16.neg(a))); } /** Negate, which overflows at the minimum. */ @@ -1117,7 +1119,7 @@ export function i32Neg(data: Uint8Array, out: rt.ByteVec): void { 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; - out.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(rt.i32.neg(a)))); + out.extendFromSlice(rt.i128.toBeBytes(rt.i32.neg(a))); } /** Negate, which overflows at the minimum. */ @@ -1147,7 +1149,7 @@ export function isizeNeg(data: Uint8Array, out: rt.ByteVec): void { 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; - out.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(rt.isize.neg(a)))); + out.extendFromSlice(rt.i128.toBeBytes(rt.isize.neg(a))); } /** Cast a value of type `from` to type `to`, both indexes into the twelve types. */ @@ -1162,392 +1164,680 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec const out2 = out; const value2 = rt.u8.rem(to, 12); if (value2 === 0) { - out2.extendFromSlice(rt.u8.toBeBytes(v)); + const v2 = v; + out2.extendFromSlice(rt.u8.toBeBytes(v2)); + out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u8.display(v2, f)))); } else if (value2 === 1) { - out2.extendFromSlice(rt.u16.toBeBytes(v)); + const v3 = v; + out2.extendFromSlice(rt.u16.toBeBytes(v3)); + out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u16.display(v3, f)))); } else if (value2 === 2) { - out2.extendFromSlice(rt.u32.toBeBytes(v)); + const v4 = v; + out2.extendFromSlice(rt.u32.toBeBytes(v4)); + out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u32.display(v4, f)))); } else if (value2 === 3) { - out2.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v))); + const v5 = v; + out2.extendFromSlice(rt.u64.toBeBytes(v5)); + out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u64.display(v5, f)))); } else if (value2 === 4) { - out2.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v))); + const v6 = v; + out2.extendFromSlice(rt.u128.toBeBytes(v6)); + out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u128.display(v6, f)))); } else if (value2 === 5) { - out2.extendFromSlice(rt.usize.toBeBytes(v)); + const v7 = v; + out2.extendFromSlice(rt.usize.toBeBytes(v7)); + out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.usize.display(v7, f)))); } else if (value2 === 6) { - out2.extendFromSlice(rt.i8.toBeBytes((v << 24) >> 24)); + const v8 = (v << 24) >> 24; + out2.extendFromSlice(rt.i8.toBeBytes(v8)); + out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i8.display(v8, f)))); } else if (value2 === 7) { - out2.extendFromSlice(rt.i16.toBeBytes(v)); + const v9 = v; + out2.extendFromSlice(rt.i16.toBeBytes(v9)); + out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i16.display(v9, f)))); } else if (value2 === 8) { - out2.extendFromSlice(rt.i32.toBeBytes(v)); + const v10 = v; + out2.extendFromSlice(rt.i32.toBeBytes(v10)); + out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v10, f)))); } else if (value2 === 9) { - out2.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v))); + const v11 = v; + out2.extendFromSlice(rt.i64.toBeBytes(v11)); + out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v11, f)))); } else if (value2 === 10) { - out2.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v))); + const v12 = v; + out2.extendFromSlice(rt.i128.toBeBytes(v12)); + out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v12, f)))); } else { - out2.extendFromSlice(rt.isize.toBeBytes(v)); + const v13 = v; + out2.extendFromSlice(rt.isize.toBeBytes(v13)); + out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.isize.display(v13, f)))); } } else if (value === 1) { const data3 = data; 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(buf2), rt.slice.range(data3, n2)] as [number, Uint8Array])[0]; + const v14 = ([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) { - out3.extendFromSlice(rt.u8.toBeBytes(v2 & 0xff)); + const v15 = v14 & 0xff; + out3.extendFromSlice(rt.u8.toBeBytes(v15)); + out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u8.display(v15, f)))); } else if (value3 === 1) { - out3.extendFromSlice(rt.u16.toBeBytes(v2)); + const v16 = v14; + out3.extendFromSlice(rt.u16.toBeBytes(v16)); + out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u16.display(v16, f)))); } else if (value3 === 2) { - out3.extendFromSlice(rt.u32.toBeBytes(v2)); + const v17 = v14; + out3.extendFromSlice(rt.u32.toBeBytes(v17)); + out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u32.display(v17, f)))); } else if (value3 === 3) { - out3.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v2))); + const v18 = v14; + out3.extendFromSlice(rt.u64.toBeBytes(v18)); + out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u64.display(v18, f)))); } else if (value3 === 4) { - out3.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v2))); + const v19 = v14; + out3.extendFromSlice(rt.u128.toBeBytes(v19)); + out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u128.display(v19, f)))); } else if (value3 === 5) { - out3.extendFromSlice(rt.usize.toBeBytes(v2)); + const v20 = v14; + out3.extendFromSlice(rt.usize.toBeBytes(v20)); + out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.usize.display(v20, f)))); } else if (value3 === 6) { - out3.extendFromSlice(rt.i8.toBeBytes((v2 << 24) >> 24)); + const v21 = (v14 << 24) >> 24; + out3.extendFromSlice(rt.i8.toBeBytes(v21)); + out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i8.display(v21, f)))); } else if (value3 === 7) { - out3.extendFromSlice(rt.i16.toBeBytes((v2 << 16) >> 16)); + const v22 = (v14 << 16) >> 16; + out3.extendFromSlice(rt.i16.toBeBytes(v22)); + out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i16.display(v22, f)))); } else if (value3 === 8) { - out3.extendFromSlice(rt.i32.toBeBytes(v2)); + const v23 = v14; + out3.extendFromSlice(rt.i32.toBeBytes(v23)); + out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v23, f)))); } else if (value3 === 9) { - out3.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v2))); + const v24 = v14; + out3.extendFromSlice(rt.i64.toBeBytes(v24)); + out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v24, f)))); } else if (value3 === 10) { - out3.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v2))); + const v25 = v14; + out3.extendFromSlice(rt.i128.toBeBytes(v25)); + out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v25, f)))); } else { - out3.extendFromSlice(rt.isize.toBeBytes(v2)); + const v26 = v14; + out3.extendFromSlice(rt.isize.toBeBytes(v26)); + out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.isize.display(v26, f)))); } } else if (value === 2) { const data4 = data; 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(buf3), rt.slice.range(data4, n3)] as [number, Uint8Array])[0]; + const v27 = ([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) { - out4.extendFromSlice(rt.u8.toBeBytes(v3 & 0xff)); + const v28 = v27 & 0xff; + out4.extendFromSlice(rt.u8.toBeBytes(v28)); + out4.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u8.display(v28, f)))); } else if (value4 === 1) { - out4.extendFromSlice(rt.u16.toBeBytes(v3 & 0xffff)); + const v29 = v27 & 0xffff; + out4.extendFromSlice(rt.u16.toBeBytes(v29)); + out4.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u16.display(v29, f)))); } else if (value4 === 2) { - out4.extendFromSlice(rt.u32.toBeBytes(v3)); + const v30 = v27; + out4.extendFromSlice(rt.u32.toBeBytes(v30)); + out4.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u32.display(v30, f)))); } else if (value4 === 3) { - out4.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v3))); + const v31 = v27; + out4.extendFromSlice(rt.u64.toBeBytes(v31)); + out4.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u64.display(v31, f)))); } else if (value4 === 4) { - out4.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v3))); + const v32 = v27; + out4.extendFromSlice(rt.u128.toBeBytes(v32)); + out4.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u128.display(v32, f)))); } else if (value4 === 5) { - out4.extendFromSlice(rt.usize.toBeBytes(v3)); + const v33 = v27; + out4.extendFromSlice(rt.usize.toBeBytes(v33)); + out4.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.usize.display(v33, f)))); } else if (value4 === 6) { - out4.extendFromSlice(rt.i8.toBeBytes((v3 << 24) >> 24)); + const v34 = (v27 << 24) >> 24; + out4.extendFromSlice(rt.i8.toBeBytes(v34)); + out4.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i8.display(v34, f)))); } else if (value4 === 7) { - out4.extendFromSlice(rt.i16.toBeBytes((v3 << 16) >> 16)); + const v35 = (v27 << 16) >> 16; + out4.extendFromSlice(rt.i16.toBeBytes(v35)); + out4.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i16.display(v35, f)))); } else if (value4 === 8) { - out4.extendFromSlice(rt.i32.toBeBytes(v3 | 0)); + const v36 = v27 | 0; + out4.extendFromSlice(rt.i32.toBeBytes(v36)); + out4.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v36, f)))); } else if (value4 === 9) { - out4.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v3))); + const v37 = v27; + out4.extendFromSlice(rt.i64.toBeBytes(v37)); + out4.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v37, f)))); } else if (value4 === 10) { - out4.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v3))); + const v38 = v27; + out4.extendFromSlice(rt.i128.toBeBytes(v38)); + out4.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v38, f)))); } else { - out4.extendFromSlice(rt.isize.toBeBytes(v3 | 0)); + const v39 = v27 | 0; + out4.extendFromSlice(rt.isize.toBeBytes(v39)); + out4.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.isize.display(v39, f)))); } } else if (value === 3) { const data5 = data; 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(buf4), rt.slice.range(data5, n4)] as [rt.U64, Uint8Array])[0]; + const v40 = ([rt.u64.fromBeBytes(buf4), rt.slice.range(data5, n4)] as [rt.U64, Uint8Array])[0]; const out5 = out; const value5 = rt.u8.rem(to, 12); if (value5 === 0) { - out5.extendFromSlice(rt.u8.toBeBytes(v4.lo & 0xff)); + const v41 = rt.u64.low(v40) & 0xff; + out5.extendFromSlice(rt.u8.toBeBytes(v41)); + out5.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u8.display(v41, f)))); } else if (value5 === 1) { - out5.extendFromSlice(rt.u16.toBeBytes(v4.lo & 0xffff)); + const v42 = rt.u64.low(v40) & 0xffff; + out5.extendFromSlice(rt.u16.toBeBytes(v42)); + out5.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u16.display(v42, f)))); } else if (value5 === 2) { - out5.extendFromSlice(rt.u32.toBeBytes(v4.lo)); + const v43 = rt.u64.low(v40); + out5.extendFromSlice(rt.u32.toBeBytes(v43)); + out5.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u32.display(v43, f)))); } else if (value5 === 3) { - out5.extendFromSlice(rt.u64.toBeBytes(v4)); + const v44 = v40; + out5.extendFromSlice(rt.u64.toBeBytes(v44)); + out5.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u64.display(v44, f)))); } else if (value5 === 4) { - out5.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v4))); + const v45 = rt.u128.cast(v40); + out5.extendFromSlice(rt.u128.toBeBytes(v45)); + out5.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u128.display(v45, f)))); } else if (value5 === 5) { - out5.extendFromSlice(rt.usize.toBeBytes(v4.lo)); + const v46 = rt.u64.low(v40); + out5.extendFromSlice(rt.usize.toBeBytes(v46)); + out5.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.usize.display(v46, f)))); } else if (value5 === 6) { - out5.extendFromSlice(rt.i8.toBeBytes((v4.lo << 24) >> 24)); + const v47 = (rt.u64.low(v40) << 24) >> 24; + out5.extendFromSlice(rt.i8.toBeBytes(v47)); + out5.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i8.display(v47, f)))); } else if (value5 === 7) { - out5.extendFromSlice(rt.i16.toBeBytes((v4.lo << 16) >> 16)); + const v48 = (rt.u64.low(v40) << 16) >> 16; + out5.extendFromSlice(rt.i16.toBeBytes(v48)); + out5.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i16.display(v48, f)))); } else if (value5 === 8) { - out5.extendFromSlice(rt.i32.toBeBytes(v4.lo | 0)); + const v49 = rt.u64.low(v40) | 0; + out5.extendFromSlice(rt.i32.toBeBytes(v49)); + out5.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v49, f)))); } else if (value5 === 9) { - out5.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v4))); + const v50 = rt.i64.cast(v40); + out5.extendFromSlice(rt.i64.toBeBytes(v50)); + out5.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v50, f)))); } else if (value5 === 10) { - out5.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v4))); + const v51 = rt.i128.cast(v40); + out5.extendFromSlice(rt.i128.toBeBytes(v51)); + out5.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v51, f)))); } else { - out5.extendFromSlice(rt.isize.toBeBytes(v4.lo | 0)); + const v52 = rt.u64.low(v40) | 0; + out5.extendFromSlice(rt.isize.toBeBytes(v52)); + out5.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.isize.display(v52, f)))); } } else if (value === 4) { const data6 = data; 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(buf5), rt.slice.range(data6, n5)] as [rt.U128, Uint8Array])[0]; + const v53 = ([rt.u128.fromBeBytes(buf5), rt.slice.range(data6, n5)] as [rt.U128, Uint8Array])[0]; const out6 = out; const value6 = rt.u8.rem(to, 12); if (value6 === 0) { - out6.extendFromSlice(rt.u8.toBeBytes(v5.lo & 0xff)); + const v54 = rt.u128.low(v53) & 0xff; + out6.extendFromSlice(rt.u8.toBeBytes(v54)); + out6.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u8.display(v54, f)))); } else if (value6 === 1) { - out6.extendFromSlice(rt.u16.toBeBytes(v5.lo & 0xffff)); + const v55 = rt.u128.low(v53) & 0xffff; + out6.extendFromSlice(rt.u16.toBeBytes(v55)); + out6.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u16.display(v55, f)))); } else if (value6 === 2) { - out6.extendFromSlice(rt.u32.toBeBytes(v5.lo)); + const v56 = rt.u128.low(v53); + out6.extendFromSlice(rt.u32.toBeBytes(v56)); + out6.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u32.display(v56, f)))); } else if (value6 === 3) { - out6.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v5))); + const v57 = rt.u64.cast(v53); + out6.extendFromSlice(rt.u64.toBeBytes(v57)); + out6.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u64.display(v57, f)))); } else if (value6 === 4) { - out6.extendFromSlice(rt.u128.toBeBytes(v5)); + const v58 = v53; + out6.extendFromSlice(rt.u128.toBeBytes(v58)); + out6.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u128.display(v58, f)))); } else if (value6 === 5) { - out6.extendFromSlice(rt.usize.toBeBytes(v5.lo)); + const v59 = rt.u128.low(v53); + out6.extendFromSlice(rt.usize.toBeBytes(v59)); + out6.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.usize.display(v59, f)))); } else if (value6 === 6) { - out6.extendFromSlice(rt.i8.toBeBytes((v5.lo << 24) >> 24)); + const v60 = (rt.u128.low(v53) << 24) >> 24; + out6.extendFromSlice(rt.i8.toBeBytes(v60)); + out6.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i8.display(v60, f)))); } else if (value6 === 7) { - out6.extendFromSlice(rt.i16.toBeBytes((v5.lo << 16) >> 16)); + const v61 = (rt.u128.low(v53) << 16) >> 16; + out6.extendFromSlice(rt.i16.toBeBytes(v61)); + out6.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i16.display(v61, f)))); } else if (value6 === 8) { - out6.extendFromSlice(rt.i32.toBeBytes(v5.lo | 0)); + const v62 = rt.u128.low(v53) | 0; + out6.extendFromSlice(rt.i32.toBeBytes(v62)); + out6.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v62, f)))); } else if (value6 === 9) { - out6.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v5))); + const v63 = rt.i64.cast(v53); + out6.extendFromSlice(rt.i64.toBeBytes(v63)); + out6.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v63, f)))); } else if (value6 === 10) { - out6.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v5))); + const v64 = rt.i128.cast(v53); + out6.extendFromSlice(rt.i128.toBeBytes(v64)); + out6.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v64, f)))); } else { - out6.extendFromSlice(rt.isize.toBeBytes(v5.lo | 0)); + const v65 = rt.u128.low(v53) | 0; + out6.extendFromSlice(rt.isize.toBeBytes(v65)); + out6.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.isize.display(v65, f)))); } } else if (value === 5) { const data7 = data; 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(buf6), rt.slice.range(data7, n6)] as [number, Uint8Array])[0]; + const v66 = ([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) { - out7.extendFromSlice(rt.u8.toBeBytes(v6 & 0xff)); + const v67 = v66 & 0xff; + out7.extendFromSlice(rt.u8.toBeBytes(v67)); + out7.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u8.display(v67, f)))); } else if (value7 === 1) { - out7.extendFromSlice(rt.u16.toBeBytes(v6 & 0xffff)); + const v68 = v66 & 0xffff; + out7.extendFromSlice(rt.u16.toBeBytes(v68)); + out7.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u16.display(v68, f)))); } else if (value7 === 2) { - out7.extendFromSlice(rt.u32.toBeBytes(v6)); + const v69 = v66; + out7.extendFromSlice(rt.u32.toBeBytes(v69)); + out7.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u32.display(v69, f)))); } else if (value7 === 3) { - out7.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v6))); + const v70 = v66; + out7.extendFromSlice(rt.u64.toBeBytes(v70)); + out7.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u64.display(v70, f)))); } else if (value7 === 4) { - out7.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v6))); + const v71 = v66; + out7.extendFromSlice(rt.u128.toBeBytes(v71)); + out7.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u128.display(v71, f)))); } else if (value7 === 5) { - out7.extendFromSlice(rt.usize.toBeBytes(v6)); + const v72 = v66; + out7.extendFromSlice(rt.usize.toBeBytes(v72)); + out7.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.usize.display(v72, f)))); } else if (value7 === 6) { - out7.extendFromSlice(rt.i8.toBeBytes((v6 << 24) >> 24)); + const v73 = (v66 << 24) >> 24; + out7.extendFromSlice(rt.i8.toBeBytes(v73)); + out7.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i8.display(v73, f)))); } else if (value7 === 7) { - out7.extendFromSlice(rt.i16.toBeBytes((v6 << 16) >> 16)); + const v74 = (v66 << 16) >> 16; + out7.extendFromSlice(rt.i16.toBeBytes(v74)); + out7.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i16.display(v74, f)))); } else if (value7 === 8) { - out7.extendFromSlice(rt.i32.toBeBytes(v6 | 0)); + const v75 = v66 | 0; + out7.extendFromSlice(rt.i32.toBeBytes(v75)); + out7.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v75, f)))); } else if (value7 === 9) { - out7.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v6))); + const v76 = v66; + out7.extendFromSlice(rt.i64.toBeBytes(v76)); + out7.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v76, f)))); } else if (value7 === 10) { - out7.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v6))); + const v77 = v66; + out7.extendFromSlice(rt.i128.toBeBytes(v77)); + out7.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v77, f)))); } else { - out7.extendFromSlice(rt.isize.toBeBytes(v6 | 0)); + const v78 = v66 | 0; + out7.extendFromSlice(rt.isize.toBeBytes(v78)); + out7.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.isize.display(v78, f)))); } } else if (value === 6) { const data8 = data; 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(buf7), rt.slice.range(data8, n7)] as [number, Uint8Array])[0]; + const v79 = ([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) { - out8.extendFromSlice(rt.u8.toBeBytes(v7 & 0xff)); + const v80 = v79 & 0xff; + out8.extendFromSlice(rt.u8.toBeBytes(v80)); + out8.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u8.display(v80, f)))); } else if (value8 === 1) { - out8.extendFromSlice(rt.u16.toBeBytes(v7 & 0xffff)); + const v81 = v79 & 0xffff; + out8.extendFromSlice(rt.u16.toBeBytes(v81)); + out8.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u16.display(v81, f)))); } else if (value8 === 2) { - out8.extendFromSlice(rt.u32.toBeBytes(v7 >>> 0)); + const v82 = v79 >>> 0; + out8.extendFromSlice(rt.u32.toBeBytes(v82)); + out8.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u32.display(v82, f)))); } else if (value8 === 3) { - out8.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v7))); + const v83 = rt.u64.cast(v79); + out8.extendFromSlice(rt.u64.toBeBytes(v83)); + out8.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u64.display(v83, f)))); } else if (value8 === 4) { - out8.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v7))); + const v84 = rt.u128.cast(v79); + out8.extendFromSlice(rt.u128.toBeBytes(v84)); + out8.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u128.display(v84, f)))); } else if (value8 === 5) { - out8.extendFromSlice(rt.usize.toBeBytes(v7 >>> 0)); + const v85 = v79 >>> 0; + out8.extendFromSlice(rt.usize.toBeBytes(v85)); + out8.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.usize.display(v85, f)))); } else if (value8 === 6) { - out8.extendFromSlice(rt.i8.toBeBytes(v7)); + const v86 = v79; + out8.extendFromSlice(rt.i8.toBeBytes(v86)); + out8.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i8.display(v86, f)))); } else if (value8 === 7) { - out8.extendFromSlice(rt.i16.toBeBytes(v7)); + const v87 = v79; + out8.extendFromSlice(rt.i16.toBeBytes(v87)); + out8.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i16.display(v87, f)))); } else if (value8 === 8) { - out8.extendFromSlice(rt.i32.toBeBytes(v7)); + const v88 = v79; + out8.extendFromSlice(rt.i32.toBeBytes(v88)); + out8.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v88, f)))); } else if (value8 === 9) { - out8.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v7))); + const v89 = v79; + out8.extendFromSlice(rt.i64.toBeBytes(v89)); + out8.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v89, f)))); } else if (value8 === 10) { - out8.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v7))); + const v90 = v79; + out8.extendFromSlice(rt.i128.toBeBytes(v90)); + out8.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v90, f)))); } else { - out8.extendFromSlice(rt.isize.toBeBytes(v7)); + const v91 = v79; + out8.extendFromSlice(rt.isize.toBeBytes(v91)); + out8.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.isize.display(v91, f)))); } } else if (value === 7) { const data9 = data; 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(buf8), rt.slice.range(data9, n8)] as [number, Uint8Array])[0]; + const v92 = ([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) { - out9.extendFromSlice(rt.u8.toBeBytes(v8 & 0xff)); + const v93 = v92 & 0xff; + out9.extendFromSlice(rt.u8.toBeBytes(v93)); + out9.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u8.display(v93, f)))); } else if (value9 === 1) { - out9.extendFromSlice(rt.u16.toBeBytes(v8 & 0xffff)); + const v94 = v92 & 0xffff; + out9.extendFromSlice(rt.u16.toBeBytes(v94)); + out9.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u16.display(v94, f)))); } else if (value9 === 2) { - out9.extendFromSlice(rt.u32.toBeBytes(v8 >>> 0)); + const v95 = v92 >>> 0; + out9.extendFromSlice(rt.u32.toBeBytes(v95)); + out9.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u32.display(v95, f)))); } else if (value9 === 3) { - out9.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v8))); + const v96 = rt.u64.cast(v92); + out9.extendFromSlice(rt.u64.toBeBytes(v96)); + out9.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u64.display(v96, f)))); } else if (value9 === 4) { - out9.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v8))); + const v97 = rt.u128.cast(v92); + out9.extendFromSlice(rt.u128.toBeBytes(v97)); + out9.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u128.display(v97, f)))); } else if (value9 === 5) { - out9.extendFromSlice(rt.usize.toBeBytes(v8 >>> 0)); + const v98 = v92 >>> 0; + out9.extendFromSlice(rt.usize.toBeBytes(v98)); + out9.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.usize.display(v98, f)))); } else if (value9 === 6) { - out9.extendFromSlice(rt.i8.toBeBytes((v8 << 24) >> 24)); + const v99 = (v92 << 24) >> 24; + out9.extendFromSlice(rt.i8.toBeBytes(v99)); + out9.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i8.display(v99, f)))); } else if (value9 === 7) { - out9.extendFromSlice(rt.i16.toBeBytes(v8)); + const v100 = v92; + out9.extendFromSlice(rt.i16.toBeBytes(v100)); + out9.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i16.display(v100, f)))); } else if (value9 === 8) { - out9.extendFromSlice(rt.i32.toBeBytes(v8)); + const v101 = v92; + out9.extendFromSlice(rt.i32.toBeBytes(v101)); + out9.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v101, f)))); } else if (value9 === 9) { - out9.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v8))); + const v102 = v92; + out9.extendFromSlice(rt.i64.toBeBytes(v102)); + out9.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v102, f)))); } else if (value9 === 10) { - out9.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v8))); + const v103 = v92; + out9.extendFromSlice(rt.i128.toBeBytes(v103)); + out9.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v103, f)))); } else { - out9.extendFromSlice(rt.isize.toBeBytes(v8)); + const v104 = v92; + out9.extendFromSlice(rt.isize.toBeBytes(v104)); + out9.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.isize.display(v104, f)))); } } else if (value === 8) { const data10 = data; 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(buf9), rt.slice.range(data10, n9)] as [number, Uint8Array])[0]; + const v105 = ([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) { - out10.extendFromSlice(rt.u8.toBeBytes(v9 & 0xff)); + const v106 = v105 & 0xff; + out10.extendFromSlice(rt.u8.toBeBytes(v106)); + out10.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u8.display(v106, f)))); } else if (value10 === 1) { - out10.extendFromSlice(rt.u16.toBeBytes(v9 & 0xffff)); + const v107 = v105 & 0xffff; + out10.extendFromSlice(rt.u16.toBeBytes(v107)); + out10.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u16.display(v107, f)))); } else if (value10 === 2) { - out10.extendFromSlice(rt.u32.toBeBytes(v9 >>> 0)); + const v108 = v105 >>> 0; + out10.extendFromSlice(rt.u32.toBeBytes(v108)); + out10.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u32.display(v108, f)))); } else if (value10 === 3) { - out10.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v9))); + const v109 = rt.u64.cast(v105); + out10.extendFromSlice(rt.u64.toBeBytes(v109)); + out10.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u64.display(v109, f)))); } else if (value10 === 4) { - out10.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v9))); + const v110 = rt.u128.cast(v105); + out10.extendFromSlice(rt.u128.toBeBytes(v110)); + out10.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u128.display(v110, f)))); } else if (value10 === 5) { - out10.extendFromSlice(rt.usize.toBeBytes(v9 >>> 0)); + const v111 = v105 >>> 0; + out10.extendFromSlice(rt.usize.toBeBytes(v111)); + out10.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.usize.display(v111, f)))); } else if (value10 === 6) { - out10.extendFromSlice(rt.i8.toBeBytes((v9 << 24) >> 24)); + const v112 = (v105 << 24) >> 24; + out10.extendFromSlice(rt.i8.toBeBytes(v112)); + out10.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i8.display(v112, f)))); } else if (value10 === 7) { - out10.extendFromSlice(rt.i16.toBeBytes((v9 << 16) >> 16)); + const v113 = (v105 << 16) >> 16; + out10.extendFromSlice(rt.i16.toBeBytes(v113)); + out10.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i16.display(v113, f)))); } else if (value10 === 8) { - out10.extendFromSlice(rt.i32.toBeBytes(v9)); + const v114 = v105; + out10.extendFromSlice(rt.i32.toBeBytes(v114)); + out10.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v114, f)))); } else if (value10 === 9) { - out10.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v9))); + const v115 = v105; + out10.extendFromSlice(rt.i64.toBeBytes(v115)); + out10.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v115, f)))); } else if (value10 === 10) { - out10.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v9))); + const v116 = v105; + out10.extendFromSlice(rt.i128.toBeBytes(v116)); + out10.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v116, f)))); } else { - out10.extendFromSlice(rt.isize.toBeBytes(v9)); + const v117 = v105; + out10.extendFromSlice(rt.isize.toBeBytes(v117)); + out10.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.isize.display(v117, f)))); } } else if (value === 9) { const data11 = data; 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(buf10), rt.slice.range(data11, n10)] as [rt.I64, Uint8Array])[0]; + const v118 = ([rt.i64.fromBeBytes(buf10), rt.slice.range(data11, n10)] as [rt.I64, Uint8Array])[0]; const out11 = out; const value11 = rt.u8.rem(to, 12); if (value11 === 0) { - out11.extendFromSlice(rt.u8.toBeBytes(v10.lo & 0xff)); + const v119 = rt.i64.low(v118) & 0xff; + out11.extendFromSlice(rt.u8.toBeBytes(v119)); + out11.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u8.display(v119, f)))); } else if (value11 === 1) { - out11.extendFromSlice(rt.u16.toBeBytes(v10.lo & 0xffff)); + const v120 = rt.i64.low(v118) & 0xffff; + out11.extendFromSlice(rt.u16.toBeBytes(v120)); + out11.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u16.display(v120, f)))); } else if (value11 === 2) { - out11.extendFromSlice(rt.u32.toBeBytes(v10.lo)); + const v121 = rt.i64.low(v118); + out11.extendFromSlice(rt.u32.toBeBytes(v121)); + out11.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u32.display(v121, f)))); } else if (value11 === 3) { - out11.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v10))); + const v122 = rt.u64.cast(v118); + out11.extendFromSlice(rt.u64.toBeBytes(v122)); + out11.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u64.display(v122, f)))); } else if (value11 === 4) { - out11.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v10))); + const v123 = rt.u128.cast(v118); + out11.extendFromSlice(rt.u128.toBeBytes(v123)); + out11.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u128.display(v123, f)))); } else if (value11 === 5) { - out11.extendFromSlice(rt.usize.toBeBytes(v10.lo)); + const v124 = rt.i64.low(v118); + out11.extendFromSlice(rt.usize.toBeBytes(v124)); + out11.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.usize.display(v124, f)))); } else if (value11 === 6) { - out11.extendFromSlice(rt.i8.toBeBytes((v10.lo << 24) >> 24)); + const v125 = (rt.i64.low(v118) << 24) >> 24; + out11.extendFromSlice(rt.i8.toBeBytes(v125)); + out11.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i8.display(v125, f)))); } else if (value11 === 7) { - out11.extendFromSlice(rt.i16.toBeBytes((v10.lo << 16) >> 16)); + const v126 = (rt.i64.low(v118) << 16) >> 16; + out11.extendFromSlice(rt.i16.toBeBytes(v126)); + out11.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i16.display(v126, f)))); } else if (value11 === 8) { - out11.extendFromSlice(rt.i32.toBeBytes(v10.lo | 0)); + const v127 = rt.i64.low(v118) | 0; + out11.extendFromSlice(rt.i32.toBeBytes(v127)); + out11.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v127, f)))); } else if (value11 === 9) { - out11.extendFromSlice(rt.i64.toBeBytes(v10)); + const v128 = v118; + out11.extendFromSlice(rt.i64.toBeBytes(v128)); + out11.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v128, f)))); } else if (value11 === 10) { - out11.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v10))); + const v129 = rt.i128.cast(v118); + out11.extendFromSlice(rt.i128.toBeBytes(v129)); + out11.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v129, f)))); } else { - out11.extendFromSlice(rt.isize.toBeBytes(v10.lo | 0)); + const v130 = rt.i64.low(v118) | 0; + out11.extendFromSlice(rt.isize.toBeBytes(v130)); + out11.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.isize.display(v130, f)))); } } else if (value === 10) { const data12 = data; 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(buf11), rt.slice.range(data12, n11)] as [rt.I128, Uint8Array])[0]; + const v131 = ([rt.i128.fromBeBytes(buf11), rt.slice.range(data12, n11)] as [rt.I128, Uint8Array])[0]; const out12 = out; const value12 = rt.u8.rem(to, 12); if (value12 === 0) { - out12.extendFromSlice(rt.u8.toBeBytes(v11.lo & 0xff)); + const v132 = rt.i128.low(v131) & 0xff; + out12.extendFromSlice(rt.u8.toBeBytes(v132)); + out12.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u8.display(v132, f)))); } else if (value12 === 1) { - out12.extendFromSlice(rt.u16.toBeBytes(v11.lo & 0xffff)); + const v133 = rt.i128.low(v131) & 0xffff; + out12.extendFromSlice(rt.u16.toBeBytes(v133)); + out12.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u16.display(v133, f)))); } else if (value12 === 2) { - out12.extendFromSlice(rt.u32.toBeBytes(v11.lo)); + const v134 = rt.i128.low(v131); + out12.extendFromSlice(rt.u32.toBeBytes(v134)); + out12.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u32.display(v134, f)))); } else if (value12 === 3) { - out12.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v11))); + const v135 = rt.u64.cast(v131); + out12.extendFromSlice(rt.u64.toBeBytes(v135)); + out12.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u64.display(v135, f)))); } else if (value12 === 4) { - out12.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v11))); + const v136 = rt.u128.cast(v131); + out12.extendFromSlice(rt.u128.toBeBytes(v136)); + out12.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u128.display(v136, f)))); } else if (value12 === 5) { - out12.extendFromSlice(rt.usize.toBeBytes(v11.lo)); + const v137 = rt.i128.low(v131); + out12.extendFromSlice(rt.usize.toBeBytes(v137)); + out12.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.usize.display(v137, f)))); } else if (value12 === 6) { - out12.extendFromSlice(rt.i8.toBeBytes((v11.lo << 24) >> 24)); + const v138 = (rt.i128.low(v131) << 24) >> 24; + out12.extendFromSlice(rt.i8.toBeBytes(v138)); + out12.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i8.display(v138, f)))); } else if (value12 === 7) { - out12.extendFromSlice(rt.i16.toBeBytes((v11.lo << 16) >> 16)); + const v139 = (rt.i128.low(v131) << 16) >> 16; + out12.extendFromSlice(rt.i16.toBeBytes(v139)); + out12.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i16.display(v139, f)))); } else if (value12 === 8) { - out12.extendFromSlice(rt.i32.toBeBytes(v11.lo | 0)); + const v140 = rt.i128.low(v131) | 0; + out12.extendFromSlice(rt.i32.toBeBytes(v140)); + out12.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v140, f)))); } else if (value12 === 9) { - out12.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v11))); + const v141 = rt.i64.cast(v131); + out12.extendFromSlice(rt.i64.toBeBytes(v141)); + out12.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v141, f)))); } else if (value12 === 10) { - out12.extendFromSlice(rt.i128.toBeBytes(v11)); + const v142 = v131; + out12.extendFromSlice(rt.i128.toBeBytes(v142)); + out12.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v142, f)))); } else { - out12.extendFromSlice(rt.isize.toBeBytes(v11.lo | 0)); + const v143 = rt.i128.low(v131) | 0; + out12.extendFromSlice(rt.isize.toBeBytes(v143)); + out12.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.isize.display(v143, f)))); } } else { const data13 = data; 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(buf12), rt.slice.range(data13, n12)] as [number, Uint8Array])[0]; + const v144 = ([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) { - out13.extendFromSlice(rt.u8.toBeBytes(v12 & 0xff)); + const v145 = v144 & 0xff; + out13.extendFromSlice(rt.u8.toBeBytes(v145)); + out13.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u8.display(v145, f)))); } else if (value13 === 1) { - out13.extendFromSlice(rt.u16.toBeBytes(v12 & 0xffff)); + const v146 = v144 & 0xffff; + out13.extendFromSlice(rt.u16.toBeBytes(v146)); + out13.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u16.display(v146, f)))); } else if (value13 === 2) { - out13.extendFromSlice(rt.u32.toBeBytes(v12 >>> 0)); + const v147 = v144 >>> 0; + out13.extendFromSlice(rt.u32.toBeBytes(v147)); + out13.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u32.display(v147, f)))); } else if (value13 === 3) { - out13.extendFromSlice(rt.u64.toBeBytes(rt.u64.cast(v12))); + const v148 = rt.u64.cast(v144); + out13.extendFromSlice(rt.u64.toBeBytes(v148)); + out13.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u64.display(v148, f)))); } else if (value13 === 4) { - out13.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v12))); + const v149 = rt.u128.cast(v144); + out13.extendFromSlice(rt.u128.toBeBytes(v149)); + out13.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u128.display(v149, f)))); } else if (value13 === 5) { - out13.extendFromSlice(rt.usize.toBeBytes(v12 >>> 0)); + const v150 = v144 >>> 0; + out13.extendFromSlice(rt.usize.toBeBytes(v150)); + out13.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.usize.display(v150, f)))); } else if (value13 === 6) { - out13.extendFromSlice(rt.i8.toBeBytes((v12 << 24) >> 24)); + const v151 = (v144 << 24) >> 24; + out13.extendFromSlice(rt.i8.toBeBytes(v151)); + out13.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i8.display(v151, f)))); } else if (value13 === 7) { - out13.extendFromSlice(rt.i16.toBeBytes((v12 << 16) >> 16)); + const v152 = (v144 << 16) >> 16; + out13.extendFromSlice(rt.i16.toBeBytes(v152)); + out13.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i16.display(v152, f)))); } else if (value13 === 8) { - out13.extendFromSlice(rt.i32.toBeBytes(v12)); + const v153 = v144; + out13.extendFromSlice(rt.i32.toBeBytes(v153)); + out13.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v153, f)))); } else if (value13 === 9) { - out13.extendFromSlice(rt.i64.toBeBytes(rt.i64.cast(v12))); + const v154 = v144; + out13.extendFromSlice(rt.i64.toBeBytes(v154)); + out13.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v154, f)))); } else if (value13 === 10) { - out13.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v12))); + const v155 = v144; + out13.extendFromSlice(rt.i128.toBeBytes(v155)); + out13.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v155, f)))); } else { - out13.extendFromSlice(rt.isize.toBeBytes(v12)); + const v156 = v144; + out13.extendFromSlice(rt.isize.toBeBytes(v156)); + out13.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.isize.display(v156, f)))); } } } @@ -1586,8 +1876,8 @@ export function convert(data: Uint8Array, out: rt.ByteVec): void { } else { out.push(0xfd); } - const widened = rt.u64.cast(wide.lo & 0xffff); - const into = rt.i64.cast(signed); + const widened = rt.u64.low(wide) & 0xffff; + const into = signed; out.extendFromSlice(rt.u64.toBeBytes(widened)); out.extendFromSlice(rt.i64.toBeBytes(into)); out.push(rt.u8.add(true ? 1 : 0, false ? 1 : 0)); diff --git a/ts/fixtures/lang/expected/shapes.ts b/ts/fixtures/lang/expected/shapes.ts index 009df7232..01c3bb0d5 100644 --- a/ts/fixtures/lang/expected/shapes.ts +++ b/ts/fixtures/lang/expected/shapes.ts @@ -69,13 +69,13 @@ export const Shape = { area(self: Shape): rt.U64 { if (self.tag === "Dot") { - return rt.u64.of(0); + return 0; } else if (self.tag === "Line") { - return rt.u64.of(0); + return 0; } else { const width = self.width; const height = self.height; - return rt.u64.mul(rt.u64.cast(width), rt.u64.cast(height)); + return rt.u64.mul(width, height); } }, @@ -103,7 +103,7 @@ export type Signed = "Neg" | "Min" | "Pos"; export type Dir = "North" | "East" | "South" | "West"; export function total$Shape(items: Shape[], out: rt.ByteVec): rt.U64 { - let sum: rt.U64 = rt.u64.of(0); + let sum: rt.U64 = 0; for (const item of items) { Shape.describe(item, out); sum = rt.u64.saturatingAdd(sum, Shape.area(item)); @@ -112,7 +112,7 @@ export function total$Shape(items: Shape[], out: rt.ByteVec): rt.U64 { } export function total$Meters(items: Meters[], out: rt.ByteVec): rt.U64 { - let sum: rt.U64 = rt.u64.of(0); + let sum: rt.U64 = 0; for (const item of items) { item.describe(out); sum = rt.u64.saturatingAdd(sum, item.area()); @@ -193,12 +193,12 @@ export function copies(data: Uint8Array, out: rt.ByteVec): void { a = Point.new(5, 6); const old2 = old; out.extendFromSlice(Uint8Array.of(a.x & 0xff, b.x & 0xff, old2.x & 0xff)); - let opt: Meters | undefined = new Meters(rt.u64.of(9)); + let opt: Meters | undefined = new Meters(9); const taken = opt; opt = undefined; const taken2 = taken; out.push(opt === undefined ? 1 : 0); - out.extendFromSlice(rt.u64.toBeBytes(rt.option.mapOr(rt.copy(taken2), rt.u64.of(0), (m: Meters): rt.U64 => { + out.extendFromSlice(rt.u64.toBeBytes(rt.option.mapOr(rt.copy(taken2), 0, (m: Meters): rt.U64 => { return m._0; }))); const res = rt.Ok(rt.copy(p)); @@ -221,12 +221,12 @@ export function discriminants(data: Uint8Array, out: rt.ByteVec): void { out.push({ Low: 1, High: 44, Top: 255 }[wide]); out.push({ Low: 1, High: 44, Top: -1 }[wide] & 0xff); out.extendFromSlice(rt.i16.toBeBytes({ Low: 1, High: 300, Top: -1 }[wide])); - out.extendFromSlice(rt.u64.toBeBytes({ Low: rt.u64.of(1), High: rt.u64.of(300), Top: rt.u64.of(65535) }[wide])); + out.extendFromSlice(rt.u64.toBeBytes({ Low: 1, High: 300, Top: 65535 }[wide])); out.push({ Neg: 255, Min: 128, Pos: 100 }[signed]); out.extendFromSlice(rt.u16.toBeBytes({ Neg: 65535, Min: 65408, Pos: 100 }[signed])); out.extendFromSlice(rt.u32.toBeBytes({ Neg: 4294967295, Min: 4294967168, Pos: 100 }[signed])); - out.extendFromSlice(rt.i64.toBeBytes({ Neg: rt.i64.of(-1), Min: rt.i64.of(-128), Pos: rt.i64.of(100) }[signed])); - out.extendFromSlice(rt.u128.toBeBytes({ Neg: rt.u128.of(340282366920938463463374607431768211455n), Min: rt.u128.of(340282366920938463463374607431768211328n), Pos: rt.u128.of(100) }[signed])); + out.extendFromSlice(rt.i64.toBeBytes({ Neg: -1, Min: -128, Pos: 100 }[signed])); + out.extendFromSlice(rt.u128.toBeBytes({ Neg: rt.u128.of(340282366920938463463374607431768211455n), Min: rt.u128.of(340282366920938463463374607431768211328n), Pos: 100 }[signed])); } } @@ -272,15 +272,15 @@ export function loops(data: Uint8Array, out: rt.ByteVec): void { } else { out.push(0xff); } - let n: rt.U64 = rt.u64.cast(data.length); + let n: rt.U64 = data.length; let steps: number; while (true) { - if (rt.u64.le(n, rt.u64.of(1))) { + if (rt.u64.le(n, 1)) { steps = 0; break; } - n = rt.u64.eq(rt.u64.rem(n, rt.u64.of(2)), rt.u64.of(0)) ? rt.u64.div(n, rt.u64.of(2)) : rt.u64.add(rt.u64.mul(rt.u64.of(3), n), rt.u64.of(1)); - if (rt.u64.gt(n, rt.u64.of(100))) { + n = rt.u64.rem(n, 2) === 0 ? rt.u64.div(n, 2) : rt.u64.add(rt.u64.mul(3, n), 1); + if (rt.u64.gt(n, 100)) { steps = 1; break; } @@ -567,29 +567,29 @@ export function patterns(data: Uint8Array, out: rt.ByteVec): void { let area: rt.I64; arms3: { if ((shape_.tag === "Rect" && shape_.width === 0) || shape_.tag === "Dot") { - area = rt.i64.of(0); + area = 0; break arms3; } if (shape_.tag === "Rect") { const x = shape_.corner.x; const width = shape_.width; if (x > 0) { - area = rt.i64.mul(rt.i64.cast(width), rt.i64.cast(x)); + area = rt.i64.mul(width, x); break arms3; } } if (shape_.tag === "Rect") { const width2 = shape_.width; const height = shape_.height; - area = rt.i64.neg(rt.i64.mul(rt.i64.cast(width2), rt.i64.cast(height))); + area = rt.i64.neg(rt.i64.mul(width2, height)); break arms3; } const a2 = rt.copy(shape_._0); const b3 = rt.copy(shape_._1); - area = rt.i64.mul(rt.i64.cast(rt.i32.sub(a2.x, b3.x)), rt.i64.cast(rt.i32.sub(a2.y, b3.y))); + area = rt.i64.mul(rt.i32.sub(a2.x, b3.x), rt.i32.sub(a2.y, b3.y)); } out.extendFromSlice(rt.i64.toBeBytes(area)); - const total = rt.u64.add(total$Shape([shape_, { tag: "Dot" } as Shape], out), total$Meters([new Meters(rt.u64.of(3)), new Meters(rt.u64.of(18446744073709551615n))], out)); + const total = rt.u64.add(total$Shape([shape_, { tag: "Dot" } as Shape], out), total$Meters([new Meters(3), new Meters(rt.u64.of(18446744073709551615n))], out)); out.extendFromSlice(rt.u64.toBeBytes(total)); const parsed = rt.Ok(data.length & 0xff); let a3: number; diff --git a/ts/fixtures/lang/src/ints.rs b/ts/fixtures/lang/src/ints.rs index fe6b925ed..fa7273088 100644 --- a/ts/fixtures/lang/src/ints.rs +++ b/ts/fixtures/lang/src/ints.rs @@ -53,6 +53,8 @@ macro_rules! ops { out.push((a == b) as u8); out.push((a < b) as u8); out.push((a >= b) as u8); + // Against a literal, which a wide value equals only in one form. + out.push((a.wrapping_sub(b) == 0) as u8); } _ => { // Compound assignment, with constants folded beside variables. @@ -102,23 +104,32 @@ neg!(i64_neg, i64); neg!(i128_neg, i128); neg!(isize_neg, isize); +/// A cast's bytes, then its text, which shows the sign the result reads with. +macro_rules! put_cast { + ($out:expr, $v:expr) => {{ + let v = $v; + $out.extend_from_slice(&v.to_be_bytes()); + $out.extend_from_slice(v.to_string().as_bytes()); + }}; +} + macro_rules! cast_to { ($v:expr, $to:expr, $out:expr) => {{ let v = $v; let out: &mut Vec = $out; match $to % 12 { - 0 => out.extend_from_slice(&(v as u8).to_be_bytes()), - 1 => out.extend_from_slice(&(v as u16).to_be_bytes()), - 2 => out.extend_from_slice(&(v as u32).to_be_bytes()), - 3 => out.extend_from_slice(&(v as u64).to_be_bytes()), - 4 => out.extend_from_slice(&(v as u128).to_be_bytes()), - 5 => out.extend_from_slice(&(v as usize).to_be_bytes()), - 6 => out.extend_from_slice(&(v as i8).to_be_bytes()), - 7 => out.extend_from_slice(&(v as i16).to_be_bytes()), - 8 => out.extend_from_slice(&(v as i32).to_be_bytes()), - 9 => out.extend_from_slice(&(v as i64).to_be_bytes()), - 10 => out.extend_from_slice(&(v as i128).to_be_bytes()), - _ => out.extend_from_slice(&(v as isize).to_be_bytes()), + 0 => put_cast!(out, v as u8), + 1 => put_cast!(out, v as u16), + 2 => put_cast!(out, v as u32), + 3 => put_cast!(out, v as u64), + 4 => put_cast!(out, v as u128), + 5 => put_cast!(out, v as usize), + 6 => put_cast!(out, v as i8), + 7 => put_cast!(out, v as i16), + 8 => put_cast!(out, v as i32), + 9 => put_cast!(out, v as i64), + 10 => put_cast!(out, v as i128), + _ => put_cast!(out, v as isize), } }}; } diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index 06c28959d..e3d43b387 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -1,8 +1,8 @@ // The runtime of rust2-ts output: what Rust's std does that plain JS does not. // -// Integers up to 32 bits are numbers; wider ones are immutable objects of 32-bit limbs. -// Arithmetic is checked as a debug build checks it: overflow panics, it never wraps -// silently. Every panic's message is the one native Rust panics with. +// Integers are numbers, but a 64- or 128-bit one past 2^53 is an immutable object of +// 32-bit limbs. Arithmetic is checked as a debug build checks it: overflow panics, it +// never wraps silently. Every panic's message is the one native Rust panics with. /** A Rust panic. */ export class Panic extends Error { @@ -35,19 +35,32 @@ export function assertFailed(op: string, left: Fmt, right: Fmt, args?: Arguments declare const brand: unique symbol; -/** A `u64`: its high and low 32 bits, each a uint32. Immutable. */ -export type U64 = { readonly hi: number; readonly lo: number; readonly [brand]: "u64" }; -/** An `i64`: its high 32 bits as an int32, and its low 32 bits as a uint32. Immutable. */ -export type I64 = { readonly hi: number; readonly lo: number; readonly [brand]: "i64" }; -/** A `u128`: four 32-bit limbs, `hi`, `w2`, `w1`, and `lo`, each a uint32. Immutable. */ -export type U128 = { readonly hi: number; readonly w2: number; readonly w1: number; readonly lo: number; readonly [brand]: "u128" }; -/** An `i128`: as a `u128`, but `hi` is an int32. Immutable. */ -export type I128 = { readonly hi: number; readonly w2: number; readonly w1: number; readonly lo: number; readonly [brand]: "i128" }; -/** An integer wider than 32 bits. Its value is its limbs', `hi` read with the type's sign. */ -export type Wide = U64 | I64 | U128 | I128; +/** A `u64` past 2^53: its high and low 32 bits, each a uint32. */ +export type U64Limbs = { readonly hi: number; readonly lo: number; readonly [brand]: "u64" }; +/** An `i64` past 2^53 in magnitude: its high 32 bits as an int32, and its low as a uint32. */ +export type I64Limbs = { readonly hi: number; readonly lo: number; readonly [brand]: "i64" }; +/** A `u128` past 2^53: four 32-bit limbs, `hi`, `w2`, `w1`, and `lo`, each a uint32. */ +export type U128Limbs = { readonly hi: number; readonly w2: number; readonly w1: number; readonly lo: number; readonly [brand]: "u128" }; +/** An `i128` past 2^53 in magnitude: as a `u128`'s, but `hi` is an int32. */ +export type I128Limbs = { readonly hi: number; readonly w2: number; readonly w1: number; readonly lo: number; readonly [brand]: "i128" }; +/** Limbs, whose value is theirs with `hi` read in the type's sign. */ +export type Limbs = U64Limbs | I64Limbs | U128Limbs | I128Limbs; + +/** + * A `u64`: a number while it is a safe integer, at most 2^53 - 1, and its limbs past + * that. Each value has one form, so two numbers compare with `===`, and a number never + * equals limbs. + */ +export type U64 = number | U64Limbs; +/** An `i64`: a number while it is a safe integer, and its limbs past that. */ +export type I64 = number | I64Limbs; +/** A `u128`: a number while it is a safe integer, and its limbs past that. */ +export type U128 = number | U128Limbs; +/** An `i128`: a number while it is a safe integer, and its limbs past that. */ +export type I128 = number | I128Limbs; /** The checks and methods of one integer type. */ -export interface Int { +export interface Int { readonly MIN: T; readonly MAX: T; readonly BITS: number; @@ -57,8 +70,8 @@ export interface Int { neg(a: T): T; div(a: T, b: T): T; rem(a: T, b: T): T; - shl(a: T, b: number | Wide): T; - shr(a: T, b: number | Wide): T; + shl(a: T, b: number | Limbs): T; + shr(a: T, b: number | Limbs): T; checkedAdd(a: T, b: T): T | undefined; checkedSub(a: T, b: T): T | undefined; checkedMul(a: T, b: T): T | undefined; @@ -70,19 +83,21 @@ export interface Int { leadingZeros(a: T): number; fromBeBytes(bytes: Uint8Array): T; toBeBytes(a: T): Uint8Array; - tryFrom(value: number | Wide): Result; + tryFrom(value: number | Limbs): Result; /** `Display`, and `Debug`, which writes the same. */ display(a: T, f: Formatter): FmtResult; lowerHex(a: T, f: Formatter): FmtResult; upperHex(a: T, f: Formatter): FmtResult; } -/** A wide integer type: what JS operators do for numbers, as methods on its objects. */ -export interface WideInt extends Int { - /** A literal: a number when it is a safe integer, else a bigint. */ +/** A 64- or 128-bit integer type: what JS operators do for numbers, as methods. */ +export interface WideInt extends Int { + /** A value from a safe integer, or from a bigint of any size, wrapped into range. */ of(value: number | bigint): T; /** `value as T`. */ - cast(value: number | Wide): T; + cast(value: number | Limbs): T; + /** The low 32 bits, as a uint32. */ + low(a: T): number; eq(a: T, b: T): boolean; ne(a: T, b: T): boolean; lt(a: T, b: T): boolean; @@ -102,12 +117,12 @@ export interface WideInt extends Int { const TWO32 = 4294967296; /** A wide integer's value as a number: exact up to 2^53 in magnitude, rounded past it. */ -function approx(v: Wide): number { +function approx(v: Limbs): number { return "w1" in v ? ((v.hi * TWO32 + v.w2) * TWO32 + v.w1) * TWO32 + v.lo : v.hi * TWO32 + v.lo; } /** A wide integer's exact value. */ -function exact(v: Wide): bigint { +function exact(v: Limbs): bigint { if ("w1" in v) return (((((BigInt(v.hi) << 32n) + BigInt(v.w2)) << 32n) + BigInt(v.w1)) << 32n) + BigInt(v.lo); return (BigInt(v.hi) << 32n) + BigInt(v.lo); } @@ -116,7 +131,7 @@ function safe(n: number): boolean { return n <= Number.MAX_SAFE_INTEGER && n >= -Number.MAX_SAFE_INTEGER; } -function shift(b: number | Wide, bits: number, direction: string): number { +function shift(b: number | Limbs, bits: number, direction: string): number { const n = typeof b === "number" ? b : approx(b); if (n < 0 || n >= bits) panic(`attempt to shift ${direction} with overflow`); return n; @@ -199,7 +214,7 @@ function small(bits: number, signed: boolean): Int { * A wide integer type, exact through bigints: `box` wraps a value into the type's range, * and `compare` orders two of its objects. */ -function wide(bits: number, signed: boolean, box: (v: bigint) => T, compare: (a: T, b: T) => number): WideInt { +function wide(bits: number, signed: boolean, box: (v: bigint) => T, compare: (a: T, b: T) => number): WideInt { const min = signed ? -(1n << BigInt(bits - 1)) : 0n; const max = signed ? (1n << BigInt(bits - 1)) - 1n : (1n << BigInt(bits)) - 1n; const big = exact as (a: T) => bigint; @@ -284,6 +299,7 @@ function wide(bits: number, signed: boolean, box: (v: bigint) => upperHex: (a, f) => f.padIntegral(true, "0x", unsigned(a).toString(16).toUpperCase()), of: (value) => box(BigInt(value)), cast: (value) => box(typeof value === "number" ? BigInt(value) : exact(value)), + low: (a) => a.lo, eq: (a, b) => compare(a, b) === 0, ne: (a, b) => compare(a, b) !== 0, lt, @@ -311,14 +327,14 @@ function wide(bits: number, signed: boolean, box: (v: bigint) => * `u64` or `i64`: exact through bigints, with the common operations computed on the * halves, which is several times faster. */ -function wide64(signed: boolean): WideInt { +function wide64(signed: boolean): WideInt { // The high half's range, and its value from any 32 bits. const hmin = signed ? -0x80000000 : 0; const hmax = signed ? 0x7fffffff : 0xffffffff; const high = signed ? (h: number) => h | 0 : (h: number) => h >>> 0; const make = (hi: number, lo: number) => ({ hi, lo }) as T; const box = (v: bigint) => make(Number(signed ? BigInt.asIntN(32, v >> 32n) : BigInt.asUintN(32, v >> 32n)), Number(BigInt.asUintN(32, v))); - // An exact integer of at most 2^53 in magnitude. + // A safe integer, wrapped into range. const fromNumber = (n: number) => make(high(Math.floor(n / TWO32)), n >>> 0); const compare = (a: T, b: T) => (a.hi !== b.hi ? (a.hi < b.hi ? -1 : 1) : a.lo === b.lo ? 0 : a.lo < b.lo ? -1 : 1); const lt = (a: T, b: T) => a.hi < b.hi || (a.hi === b.hi && a.lo < b.lo); @@ -364,8 +380,6 @@ function wide64(signed: boolean): WideInt { return make(high(a.hi - b.hi - (lo < 0 ? 1 : 0)), lo >>> 0); }, mul(a, b) { - const n = approx(a) * approx(b); - if (safe(n)) return fromNumber(n); if (!signed && (a.hi === 0 || b.hi === 0)) { // One factor below 2^32: its product with the other's high half must be too. const cross = a.hi * b.lo + a.lo * b.hi; @@ -378,10 +392,6 @@ function wide64(signed: boolean): WideInt { }, div(a, b) { if (isZero(b)) panic("attempt to divide by zero"); - const n = approx(a); - const d = approx(b); - // Below 2^53, a quotient of doubles truncates to the exact one. - if (safe(n) && safe(d)) return fromNumber(Math.trunc(n / d)); if (!signed && b.hi === 0) { // Long division by a uint32 in 32- and 16-bit digits, every step exact. const d = b.lo; @@ -395,12 +405,6 @@ function wide64(signed: boolean): WideInt { } return base.div(a, b); }, - rem(a, b) { - const n = approx(a); - const d = approx(b); - if (safe(n) && safe(d) && d !== 0) return fromNumber(n % d); - return base.rem(a, b); - }, neg(a) { if (!signed || (a.hi === hmin && a.lo === 0)) return base.neg(a); const lo = -a.lo >>> 0; @@ -449,14 +453,9 @@ function wide64(signed: boolean): WideInt { out[7] = lo; return out; }, - display(a, f) { - const n = approx(a); - if (!safe(n)) return base.display(a, f); - return f.padIntegral(n >= 0, "", String(n < 0 ? -n : n)); - }, of: (value) => (typeof value === "number" ? fromNumber(value) : box(value)), cast(value) { - if (typeof value === "number") return make(high(value < 0 ? -1 : 0), value >>> 0); + if (typeof value === "number") return fromNumber(value); return make(high("w1" in value ? value.w1 : value.hi), value.lo); }, eq: (a, b) => a.hi === b.hi && a.lo === b.lo, @@ -471,7 +470,7 @@ function wide64(signed: boolean): WideInt { } /** `u128` or `i128`: four limbs, computed through bigints. */ -function wide128(signed: boolean): WideInt { +function wide128(signed: boolean): WideInt { const limb = (v: bigint, at: bigint) => Number(BigInt.asUintN(32, v >> at)); const box = (v: bigint) => { const top = v >> 96n; @@ -487,6 +486,110 @@ function wide128(signed: boolean): WideInt { return wide(128, signed, box, compare); } +/** + * A wide integer type, its values numbers while they are safe integers: their arithmetic + * stays in numbers while its result does, and falls back on `limbs` past that. + */ +function dual(limbs: WideInt, signed: boolean): WideInt { + type V = number | T; + const most = Number.MAX_SAFE_INTEGER; + const least = signed ? -most : 0; + const big = (v: V): T => (typeof v === "number" ? limbs.of(v) : v); + // The one form of a value: a number if it is a safe integer. + const one = (v: T): V => { + const n = approx(v); + return safe(n) ? n : v; + }; + const fits = (n: number) => n <= most && n >= least; + const lt = (a: V, b: V) => (typeof a === "number" && typeof b === "number" ? a < b : limbs.lt(big(a), big(b))); + const eq = (a: V, b: V) => a === b || (typeof a !== "number" && typeof b !== "number" && limbs.eq(a, b)); + // A bitwise operator on two safe integers, a 32-bit half at a time. + const halves = (a: number, b: number, op: (x: number, y: number) => number) => + op(Math.floor(a / TWO32), Math.floor(b / TWO32)) * TWO32 + (op(a >>> 0, b >>> 0) >>> 0); + const binary = (f: (a: T, b: T) => T) => (a: V, b: V) => one(f(big(a), big(b))); + const checked = (f: (a: T, b: T) => T | undefined) => (a: V, b: V) => { + const value = f(big(a), big(b)); + return value === undefined ? undefined : one(value); + }; + return { + MIN: one(limbs.MIN), + MAX: one(limbs.MAX), + BITS: limbs.BITS, + add(a, b) { + if (typeof a === "number" && typeof b === "number" && fits(a + b)) return a + b; + return one(limbs.add(big(a), big(b))); + }, + sub(a, b) { + if (typeof a === "number" && typeof b === "number" && fits(a - b)) return a - b; + return one(limbs.sub(big(a), big(b))); + }, + mul(a, b) { + // `+ 0` turns -0 into 0. + if (typeof a === "number" && typeof b === "number" && safe(a * b)) return a * b + 0; + return one(limbs.mul(big(a), big(b))); + }, + div(a, b) { + // Below 2^53, a quotient of doubles truncates to the exact one. + if (typeof a === "number" && typeof b === "number" && b !== 0) return Math.trunc(a / b) + 0; + return one(limbs.div(big(a), big(b))); + }, + rem(a, b) { + if (typeof a === "number" && typeof b === "number" && b !== 0) return (a % b) + 0; + return one(limbs.rem(big(a), big(b))); + }, + neg(a) { + if (signed && typeof a === "number") return -a + 0; + return one(limbs.neg(big(a))); + }, + shl(a, b) { + const n = shift(b, limbs.BITS, "left"); + if (typeof a === "number" && safe(a * 2 ** n)) return a * 2 ** n; + return one(limbs.shl(big(a), n)); + }, + shr(a, b) { + const n = shift(b, limbs.BITS, "right"); + if (typeof a === "number") return Math.floor(a / 2 ** n) + 0; + return one(limbs.shr(big(a), n)); + }, + and: (a, b) => (typeof a === "number" && typeof b === "number" ? halves(a, b, (x, y) => x & y) : one(limbs.and(big(a), big(b)))), + or: (a, b) => (typeof a === "number" && typeof b === "number" ? halves(a, b, (x, y) => x | y) : one(limbs.or(big(a), big(b)))), + xor: (a, b) => (typeof a === "number" && typeof b === "number" ? halves(a, b, (x, y) => x ^ y) : one(limbs.xor(big(a), big(b)))), + not: (a) => (signed && typeof a === "number" ? -a - 1 : one(limbs.not(big(a)))), + checkedAdd: checked(limbs.checkedAdd), + checkedSub: checked(limbs.checkedSub), + checkedMul: checked(limbs.checkedMul), + saturatingAdd: binary(limbs.saturatingAdd), + saturatingSub: binary(limbs.saturatingSub), + wrappingAdd: binary(limbs.wrappingAdd), + wrappingSub: binary(limbs.wrappingSub), + leadingOnes: (a) => limbs.leadingOnes(big(a)), + leadingZeros: (a) => limbs.leadingZeros(big(a)), + fromBeBytes: (bytes) => one(limbs.fromBeBytes(bytes)), + toBeBytes: (a) => limbs.toBeBytes(big(a)), + tryFrom(value) { + const result = limbs.tryFrom(value); + return result.ok ? Ok(one(result.value)) : result; + }, + display: (a, f) => (typeof a === "number" ? f.padIntegral(a >= 0, "", String(a < 0 ? -a : a)) : limbs.display(a, f)), + lowerHex: (a, f) => limbs.lowerHex(big(a), f), + upperHex: (a, f) => limbs.upperHex(big(a), f), + of: (value) => (typeof value === "number" ? value : one(limbs.of(value))), + cast: (value) => (typeof value === "number" && fits(value) ? value : one(limbs.cast(value))), + low: (a) => (typeof a === "number" ? a >>> 0 : a.lo), + eq, + ne: (a, b) => !eq(a, b), + lt, + le: (a, b) => !lt(b, a), + gt: (a, b) => lt(b, a), + ge: (a, b) => !lt(a, b), + max: (a, b) => (lt(b, a) ? a : b), + min: (a, b) => (lt(b, a) ? b : a), + *range(start, end, inclusive) { + for (const i of limbs.range(big(start), big(end), inclusive)) yield one(i); + }, + }; +} + export const u8: Int = small(8, false); export const u16: Int = small(16, false); export const u32: Int = small(32, false); @@ -497,10 +600,10 @@ export const i32: Int = small(32, true); export const usize: Int = u32; /** 32 bits, as on wasm32. */ export const isize: Int = i32; -export const u64: WideInt = wide64(false); -export const i64: WideInt = wide64(true); -export const u128: WideInt = wide128(false); -export const i128: WideInt = wide128(true); +export const u64: WideInt = dual(wide64(false), false); +export const i64: WideInt = dual(wide64(true), true); +export const u128: WideInt = dual(wide128(false), false); +export const i128: WideInt = dual(wide128(true), true); /** The larger of two integers. */ export function max(a: number, b: number): number { diff --git a/ts/src/lower/num.rs b/ts/src/lower/num.rs index b71d292a5..8a4aec5ba 100644 --- a/ts/src/lower/num.rs +++ b/ts/src/lower/num.rs @@ -1,10 +1,11 @@ //! Integers: literals, arithmetic, and casts with Rust's semantics. //! //! Integers up to 32 bits are JS numbers, in which their sums, differences, and -//! products are exact or provably out of range, so each is checked as computed. Wider -//! integers are the runtime's immutable objects of 32-bit limbs, which every operator, -//! comparison included, goes through the runtime for. Every operation that can overflow -//! is checked, as a debug build checks it: none wraps silently. +//! products are exact or provably out of range, so each is checked as computed. A wider +//! integer is a number too while it is a safe integer, and the runtime's immutable +//! object of 32-bit limbs past that, so every operator and comparison on one goes +//! through the runtime. Every operation that can overflow is checked, as a debug build +//! checks it: none wraps silently. use rustc_middle::mir::{BinOp, UnOp}; use rustc_middle::ty::{self, Ty}; @@ -38,7 +39,7 @@ impl Int { Some(Self { bits, signed, pointer }) } - /// Whether values are runtime objects rather than numbers. + /// Whether values past 2^53 are runtime objects rather than numbers. pub fn big(self) -> bool { self.bits > 32 } @@ -107,16 +108,14 @@ impl Int { self.spelled(text, value) } - /// A literal of this type spelled `text`, JS number digits of `value`: `5`, or - /// `rt.u64.of(5)` when wide. A wide value past 2^53 is spelled as a bigint, - /// `rt.u64.of(0xffffffffffffffffn)`, since a number can't hold it exactly. + /// A literal of this type spelled `text`, JS number digits of `value`. A wide value + /// past 2^53 is limbs, made from a bigint: `rt.u64.of(0xffffffffffffffffn)`. pub fn spelled(self, text: String, value: i128) -> Expr { - if !self.big() { - return Expr::lit(text); - } let safe = (1 - (1i128 << 53)..(1 << 53)).contains(&value) && (self.signed || value >= 0); - let text = if safe { text } else { format!("{text}n") }; - self.method("of", vec![Expr::lit(text)]) + match safe { + true => Expr::lit(text), + false => self.method("of", vec![Expr::lit(format!("{text}n"))]), + } } /// The runtime method `rt..`. @@ -140,7 +139,9 @@ impl Int { /// A comparison, `===` or `<` for instance, of two values of this type. pub fn compare(self, js: &'static str, l: Expr, r: Expr) -> Expr { - if !self.big() { + // A wide value has one form, so it equals a number literal only as that number. + let literal = |e: &Expr| matches!(e, Expr::Lit(_)); + if !self.big() || (matches!(js, "===" | "!==") && (literal(&l) || literal(&r))) { return l.binary(js, r); } let name = match js { @@ -383,11 +384,15 @@ pub fn cast<'tcx>(b: &mut Body<'_, '_, 'tcx>, value: Operand, from: Ty<'tcx>, to match (source.big(), target.big()) { (false, false) if fits => value.expr, (false, false) => target.wrap(value.expr), - // The low limb holds a narrow target's bits, as a uint32. - (true, false) => match (target.signed, target.bits) { - (false, 32) => value.expr.member("lo"), - _ => target.wrap(value.expr.member("lo")), - }, + (true, false) => { + let low = source.method("low", vec![value.expr]); + match (target.signed, target.bits) { + (false, 32) => low, + _ => target.wrap(low), + } + } + // A narrow integer is already a wide one's number. + (false, true) if fits => value.expr, (true, true) if source.rt() == target.rt() => value.expr, _ => target.method("cast", vec![value.expr]), } diff --git a/ts/src/ts.rs b/ts/src/ts.rs index d33587d3e..fe1ffde0e 100644 --- a/ts/src/ts.rs +++ b/ts/src/ts.rs @@ -136,6 +136,14 @@ impl Expr { match self { Self::Lit(_) | Self::Str(_) | Self::This | Self::Sym(_) | Self::Rt(_) => true, Self::Located(_, e) => e.is_constant(), + // A wide integer literal past 2^53, `rt.u64.of(0xffffffffffffffffn)`, which makes + // immutable limbs. + Self::Call(f, args) => match (&**f, args.as_slice()) { + (Self::Member(int, Key::Name(of)), [Self::Lit(_)]) => { + matches!(**int, Self::Rt("u64" | "i64" | "u128" | "i128")) && of == "of" + } + _ => false, + }, _ => false, } } From d4fff932ea7004d94b89e428d24b6a9601002a1a Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 07:09:54 -0700 Subject: [PATCH 4/8] docs(ts): record the wide integer measurement Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/README.md | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/ts/README.md b/ts/README.md index e30962e6c..e736fb9d9 100644 --- a/ts/README.md +++ b/ts/README.md @@ -52,7 +52,7 @@ runtime: what std does that JS does not. | --- | --- | | `bool`, `str`, `String` | `boolean`, `string` | | `u8`-`u32`, `i8`-`i32`, `usize`, `isize` | `number`, checked: overflow panics | -| `u64`, `i64`, `u128`, `i128` | `rt.U64`, `rt.I64`, `rt.U128`, `rt.I128`: immutable objects of 32-bit limbs, checked the same way | +| `u64`, `i64`, `u128`, `i128` | `rt.U64` and so on: a `number` while a safe integer, 32-bit limbs past 2^53, checked the same way | | `struct` | a class; methods take `this`, trait methods included | | `enum` without data | a union of strings: `"Lite01" \| "Lite02"`, or `never` without variants | | `enum` | a union of objects tagged by variant, with a companion object of functions | @@ -67,10 +67,12 @@ runtime: what std does that JS does not. a debug build checks it, so translated code panics where native code would: it never wraps silently. A panic throws `rt.Panic`, with the message native Rust panics with. -A wider integer is an object, so its operators and comparisons are runtime calls: -`rt.u64.add(a, b)`, `rt.u64.lt(a, b)`, `rt.u64.of(5)` for a literal, and -`rt.u64.cast(x)` for `x as u64`. Its fields are 32-bit limbs from `hi` down to `lo`, with -`hi` signed in a signed type; a caller builds one with `of`. +A wider integer is a `number` up to 2^53 - 1 in magnitude, and past that an immutable +object of 32-bit limbs, `{ hi, lo }`, with `w2` and `w1` between them at 128 bits and +`hi` signed in a signed type. Each value has one form, so `x === 0` is exact; a caller +makes one past 2^53 with `rt.u64.of(18446744073709551615n)`. Its other operators and +comparisons are runtime calls, `rt.u64.add(a, b)` and `rt.u64.lt(a, b)`, and `x as u32` +is `rt.u64.low(x)`. `format!`, `write!`, `panic!`, `assert!`, and the other macros of `format_args!` translate, as do `Display` and `Debug` impls, derived and thiserror's included. The @@ -95,8 +97,18 @@ Imports name the `.ts` file, which Node, Bun, and bundlers run as is. A `tsc` bu ## Design -- 64-bit integers are native `bigint`: lossless and readable. A pair of `u32` halves is - faster but waits until `just ts bench` measures `bigint` as the cost. +- 64- and 128-bit integers are numbers while they are safe integers, which most values + are: lengths, IDs, sequence numbers, and timestamps. `just ts bench` measured it + against `bigint`, against limbs for every value, and against unboxed halves, two + numbers in locals, fields, and arguments. It ties unboxed halves on the codec and + beats `bigint` there in Node, Bun, and Chrome: varint encode 13-65% faster, frame + decode 8-32%, and message decode 22-31%, though Chrome's 8-byte varint decode is + 25-45% slower. A loop of sums and comparisons past 2^32 measured 2-6x faster. Limbs + for every value allocate an object each, which costs what a `bigint` does in V8, so + they lost decode; unboxed halves would lower every value to two. Past 2^53 an + operation allocates limbs: a product past 2^53 divided back down is 1.5-1.9x slower + than `bigint`, and 128-bit arithmetic there goes through `bigint` inside the runtime. + The runtime adds 6 KB to the codec's bundle, 2 KB gzipped. - `Result` is a plain `{ ok, value } | { ok, error }` object rather than an exception, so `?` 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. @@ -139,7 +151,7 @@ built natively for wasm32 and run beside its translation on the same inputs by `Copy` aggregates, and formatting. Its differential test runs through `caller.ts`, a hand-written caller that also prints a `printable` error nothing translated prints, and adds every 64- and 128-bit case over each pair of values next to a power of two, - where the runtime's limbs carry, borrow, and switch paths. + where the runtime switches between numbers and limbs, and its limbs carry and borrow. - `reject` holds constructs outside the subset; its expected errors are `expected.txt`, and its expected census is `census.md`. From 691b800bf78ffc3696222e157f854095c571aab8 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 07:35:42 -0700 Subject: [PATCH 5/8] fix(ts): a wide integer from an unsafe or out-of-range number is refused or wrapped Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/runtime/rt.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index 02ce032fd..8b6009fcb 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -564,7 +564,13 @@ function dual(limbs: WideInt, signed: boolean): WideInt (typeof a === "number" ? f.padIntegral(a >= 0, "", String(a < 0 ? -a : a)) : limbs.display(a, f)), lowerHex: (a, f) => limbs.lowerHex(big(a), f), upperHex: (a, f) => limbs.upperHex(big(a), f), - of: (value) => (typeof value === "number" ? value : one(limbs.of(value))), + of(value) { + if (typeof value === "number") { + if (!Number.isSafeInteger(value)) throw new RangeError(`rust2-ts: ${value} is not a safe integer`); + if (fits(value)) return value; + } + return one(limbs.of(value)); + }, cast: (value) => (typeof value === "number" && fits(value) ? value : one(limbs.cast(value))), low: (a) => (typeof a === "number" ? a >>> 0 : a.lo), eq, From e4d46bad1bd4fb0449d5db6c4e6fbe120df28b9f Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 07:47:58 -0700 Subject: [PATCH 6/8] fix(ts): a signed bitwise result of -2^53 takes limbs `!`, `&`, and `^` on two safe `i64` or `i128` numbers can make -2^53, which was left a number, so it compared unequal to the same value as limbs. The lang fixture now checks each result equals its bytes read back, and the wide sweep adds small and safe-edge negative operands. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/fixtures/lang/expected/ints.ts | 12 ++++++++++++ ts/fixtures/lang/src/ints.rs | 6 +++++- ts/runtime/rt.ts | 20 +++++++++++++------- ts/tests/differential.ts | 4 ++++ 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/ts/fixtures/lang/expected/ints.ts b/ts/fixtures/lang/expected/ints.ts index dc9ac3457..165a0c232 100644 --- a/ts/fixtures/lang/expected/ints.ts +++ b/ts/fixtures/lang/expected/ints.ts @@ -17,6 +17,7 @@ export function u8Op(op: number, data: Uint8Array, out: rt.ByteVec): void { 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(v)); + out2.push(rt.u8.fromBeBytes(rt.u8.toBeBytes(v)) === v ? 1 : 0); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { @@ -108,6 +109,7 @@ export function u16Op(op: number, data: Uint8Array, out: rt.ByteVec): void { 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(v)); + out2.push(rt.u16.fromBeBytes(rt.u16.toBeBytes(v)) === v ? 1 : 0); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { @@ -199,6 +201,7 @@ export function u32Op(op: number, data: Uint8Array, out: rt.ByteVec): void { 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(v)); + out2.push(rt.u32.fromBeBytes(rt.u32.toBeBytes(v)) === v ? 1 : 0); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { @@ -290,6 +293,7 @@ export function u64Op(op: number, data: Uint8Array, out: rt.ByteVec): void { const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0), rt.u32.mul(2, 64)); const put = (out2: rt.ByteVec, v: rt.U64): void => { out2.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v))); + out2.push(rt.u64.eq(rt.u64.fromBeBytes(rt.u64.toBeBytes(v)), v) ? 1 : 0); }; const putOpt = (out3: rt.ByteVec, v2: rt.U64 | undefined): void => { if (v2 !== undefined) { @@ -380,6 +384,7 @@ export function u128Op(op: number, data: Uint8Array, out: rt.ByteVec): void { const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0), rt.u32.mul(2, 128)); const put = (out2: rt.ByteVec, v: rt.U128): void => { out2.extendFromSlice(rt.u128.toBeBytes(v)); + out2.push(rt.u128.eq(rt.u128.fromBeBytes(rt.u128.toBeBytes(v)), v) ? 1 : 0); }; const putOpt = (out3: rt.ByteVec, v2: rt.U128 | undefined): void => { if (v2 !== undefined) { @@ -471,6 +476,7 @@ export function usizeOp(op: number, data: Uint8Array, out: rt.ByteVec): void { 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(v)); + out2.push(rt.usize.fromBeBytes(rt.usize.toBeBytes(v)) === v ? 1 : 0); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { @@ -562,6 +568,7 @@ export function i8Op(op: number, data: Uint8Array, out: rt.ByteVec): void { 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(v)); + out2.push(rt.i8.fromBeBytes(rt.i8.toBeBytes(v)) === v ? 1 : 0); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { @@ -653,6 +660,7 @@ export function i16Op(op: number, data: Uint8Array, out: rt.ByteVec): void { 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(v)); + out2.push(rt.i16.fromBeBytes(rt.i16.toBeBytes(v)) === v ? 1 : 0); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { @@ -744,6 +752,7 @@ export function i32Op(op: number, data: Uint8Array, out: rt.ByteVec): void { 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(v)); + out2.push(rt.i32.fromBeBytes(rt.i32.toBeBytes(v)) === v ? 1 : 0); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { @@ -835,6 +844,7 @@ export function i64Op(op: number, data: Uint8Array, out: rt.ByteVec): void { const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0), rt.u32.mul(2, 64)); const put = (out2: rt.ByteVec, v: rt.I64): void => { out2.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v))); + out2.push(rt.i64.eq(rt.i64.fromBeBytes(rt.i64.toBeBytes(v)), v) ? 1 : 0); }; const putOpt = (out3: rt.ByteVec, v2: rt.I64 | undefined): void => { if (v2 !== undefined) { @@ -925,6 +935,7 @@ export function i128Op(op: number, data: Uint8Array, out: rt.ByteVec): void { const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0), rt.u32.mul(2, 128)); const put = (out2: rt.ByteVec, v: rt.I128): void => { out2.extendFromSlice(rt.i128.toBeBytes(v)); + out2.push(rt.i128.eq(rt.i128.fromBeBytes(rt.i128.toBeBytes(v)), v) ? 1 : 0); }; const putOpt = (out3: rt.ByteVec, v2: rt.I128 | undefined): void => { if (v2 !== undefined) { @@ -1016,6 +1027,7 @@ export function isizeOp(op: number, data: Uint8Array, out: rt.ByteVec): void { 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(v)); + out2.push(rt.isize.fromBeBytes(rt.isize.toBeBytes(v)) === v ? 1 : 0); }; const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { if (v2 !== undefined) { diff --git a/ts/fixtures/lang/src/ints.rs b/ts/fixtures/lang/src/ints.rs index fa7273088..307fc9541 100644 --- a/ts/fixtures/lang/src/ints.rs +++ b/ts/fixtures/lang/src/ints.rs @@ -18,7 +18,11 @@ macro_rules! ops { let (a, data) = take!($t, data); let (b, data) = take!($t, data); let shift = data.first().copied().unwrap_or(0) as u32 % (2 * <$t>::BITS); - let put = |out: &mut Vec, v: $t| out.extend_from_slice(&(v as $wide).to_be_bytes()); + let put = |out: &mut Vec, v: $t| { + out.extend_from_slice(&(v as $wide).to_be_bytes()); + // A wide value equals its bytes read back only in its one form. + out.push((<$t>::from_be_bytes(v.to_be_bytes()) == v) as u8); + }; let put_opt = |out: &mut Vec, v: Option<$t>| match v { Some(v) => { out.push(1); diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index 8b6009fcb..ac397d989 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -494,9 +494,15 @@ function dual(limbs: WideInt, signed: boolean): WideInt n <= most && n >= least; const lt = (a: V, b: V) => (typeof a === "number" && typeof b === "number" ? a < b : limbs.lt(big(a), big(b))); const eq = (a: V, b: V) => a === b || (typeof a !== "number" && typeof b !== "number" && limbs.eq(a, b)); - // A bitwise operator on two safe integers, a 32-bit half at a time. - const halves = (a: number, b: number, op: (x: number, y: number) => number) => - op(Math.floor(a / TWO32), Math.floor(b / TWO32)) * TWO32 + (op(a >>> 0, b >>> 0) >>> 0); + // A bitwise operator, on two safe integers a 32-bit half at a time. Signed ones can + // make -2^53, the one result past the safe integers, which takes limbs. + const bitwise = (f: (a: T, b: T) => T, op: (x: number, y: number) => number) => (a: V, b: V) => { + if (typeof a === "number" && typeof b === "number") { + const n = op(Math.floor(a / TWO32), Math.floor(b / TWO32)) * TWO32 + (op(a >>> 0, b >>> 0) >>> 0); + if (fits(n)) return n; + } + return one(f(big(a), big(b))); + }; const binary = (f: (a: T, b: T) => T) => (a: V, b: V) => one(f(big(a), big(b))); const checked = (f: (a: T, b: T) => T | undefined) => (a: V, b: V) => { const value = f(big(a), big(b)); @@ -542,10 +548,10 @@ function dual(limbs: WideInt, signed: boolean): WideInt (typeof a === "number" && typeof b === "number" ? halves(a, b, (x, y) => x & y) : one(limbs.and(big(a), big(b)))), - or: (a, b) => (typeof a === "number" && typeof b === "number" ? halves(a, b, (x, y) => x | y) : one(limbs.or(big(a), big(b)))), - xor: (a, b) => (typeof a === "number" && typeof b === "number" ? halves(a, b, (x, y) => x ^ y) : one(limbs.xor(big(a), big(b)))), - not: (a) => (signed && typeof a === "number" ? -a - 1 : one(limbs.not(big(a)))), + and: bitwise(limbs.and, (x, y) => x & y), + or: bitwise(limbs.or, (x, y) => x | y), + xor: bitwise(limbs.xor, (x, y) => x ^ y), + not: (a) => (signed && typeof a === "number" && a !== most ? -a - 1 : one(limbs.not(big(a)))), checkedAdd: checked(limbs.checkedAdd), checkedSub: checked(limbs.checkedSub), checkedMul: checked(limbs.checkedMul), diff --git a/ts/tests/differential.ts b/ts/tests/differential.ts index 8030ac4d6..4ddfa2107 100644 --- a/ts/tests/differential.ts +++ b/ts/tests/differential.ts @@ -91,6 +91,10 @@ if (wideArg === "wide") { 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, and where negative safe numbers end, as signed values. + for (const k of [1n, 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); From a0b6771204a5bb8bf4a501e0582b6786fb40bf36 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 07:58:39 -0700 Subject: [PATCH 7/8] feat(ts): brand each integer type with its Rust name Every integer type is its own TypeScript type, `rt.u8` through `rt.i128` and `rt.usize`, a number with a type-only brand (limbs past 2^53 for the wide four), so `tsc` refuses one type's value where another's goes. The brand is optional, so a plain number such as a literal passes for any type. A cast that keeps the value is now `x as rt.u16`. The lang fixture's `brands.ts` holds mixes `tsc` must refuse. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/README.md | 14 +++++++--- ts/fixtures/lang/brands.ts | 21 +++++++++++++++ ts/runtime/rt.ts | 52 +++++++++++++++++++++++++------------- ts/src/lower/num.rs | 19 +++++++------- ts/src/ts.rs | 5 ++-- ts/tests/fixtures.rs | 7 +++-- 6 files changed, 84 insertions(+), 34 deletions(-) create mode 100644 ts/fixtures/lang/brands.ts diff --git a/ts/README.md b/ts/README.md index 1aaa4e879..ac0ba8860 100644 --- a/ts/README.md +++ b/ts/README.md @@ -51,8 +51,8 @@ runtime: what std does that JS does not, with iterators in `rt/iter.ts`. | Rust | TypeScript | | --- | --- | | `bool`, `str`, `String` | `boolean`, `string` | -| `u8`-`u32`, `i8`-`i32`, `usize`, `isize` | `number`, checked: overflow panics | -| `u64`, `i64`, `u128`, `i128` | `rt.U64` and so on: a `number` while a safe integer, 32-bit limbs past 2^53, checked the same way | +| `u8`-`u32`, `i8`-`i32`, `usize`, `isize` | `rt.u8` and so on: a `number` branded with its type, checked: overflow panics | +| `u64`, `i64`, `u128`, `i128` | `rt.u64` and so on: a branded `number` while a safe integer, 32-bit limbs past 2^53, checked the same way | | `struct` | a class; methods take `this`, trait methods included | | `enum` without data | a union of strings: `"Lite01" \| "Lite02"`, or `never` without variants | | `enum` | a union of objects tagged by variant, with a companion object of functions | @@ -69,6 +69,11 @@ runtime: what std does that JS does not, with iterators in `rt/iter.ts`. a debug build checks it, so translated code panics where native code would: it never wraps silently. A panic throws `rt.Panic`, with the message native Rust panics with. +Each integer type is its own type to `tsc`, named as Rust's: `rt.u8` is a `number` with a +type-only brand, so a `u16` where a `u8` goes fails the type check, while a plain +number, such as a literal, passes for any. A cast that keeps the value is `x as rt.u16`, +which costs nothing at run time. + A wider integer is a `number` up to 2^53 - 1 in magnitude, and past that an immutable object of 32-bit limbs, `{ hi, lo }`, with `w2` and `w1` between them at 128 bits and `hi` signed in a signed type. Each value has one form, so `x === 0` is exact; a caller @@ -127,7 +132,9 @@ Imports name the `.ts` file, which Node, Bun, and bundlers run as is. A `tsc` bu they lost decode; unboxed halves would lower every value to two. Past 2^53 an operation allocates limbs: a product past 2^53 divided back down is 1.5-1.9x slower than `bigint`, and 128-bit arithmetic there goes through `bigint` inside the runtime. - The runtime adds 6 KB to the codec's bundle, 2 KB gzipped. + The runtime adds 6 KB to the codec's bundle, 2 KB gzipped. Brands tell `u64` from + `i64` and `u32` from `usize` at no cost, since TypeScript erases them; a required + brand would cast every literal, so a plain number passes for any type. - A generic type is one class or union, the type TS callers name, and a method on it is one instance's, typed by its `this`. A class per instance (`Wrap$Pair`) would rename the type for callers, and TS generics with trait dictionaries would undo @@ -184,6 +191,7 @@ built natively for wasm32 and run beside its translation on the same inputs by hand-written caller that also prints a `printable` error nothing translated prints, and adds every 64- and 128-bit case over each pair of values next to a power of two, where the runtime switches between numbers and limbs, and its limbs carry and borrow. + `brands.ts`, another caller, mixes integer types where `tsc` must refuse them. - `reject` holds constructs outside the subset; its expected errors are `expected.txt`, and its expected census is `census.md`. diff --git a/ts/fixtures/lang/brands.ts b/ts/fixtures/lang/brands.ts new file mode 100644 index 000000000..9eb217e29 --- /dev/null +++ b/ts/fixtures/lang/brands.ts @@ -0,0 +1,21 @@ +// A hand-written caller that mixes integer types, which `tsc` must refuse: each line after +// `@ts-expect-error` fails the type check if it is accepted. A plain number, such as a +// literal, passes for any integer type, and a cast picks one. + +import * as rt from "./rt.ts"; +import { Meters, Point, returnedNever, tailBreak } from "./shapes.ts"; + +export function mixed(data: Uint8Array): void { + returnedNever(7, 1); + new Meters(tailBreak(data) as rt.u64); + // @ts-expect-error A `u16` is not a `u8`. + returnedNever(tailBreak(data), 1); + // @ts-expect-error A `u32` is not a `usize`. + returnedNever(1, rt.u32.MAX); + // @ts-expect-error A `u64` is not a `u8`, even as a number. + returnedNever(new Meters(1).area(), 1); + // @ts-expect-error An `i64` is not a `u64`. + new Meters(rt.i64.MAX); + // @ts-expect-error A `u32` is not an `i32`. + Point.new(rt.u32.MAX, 0); +} diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index ac397d989..c12c94828 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -48,18 +48,35 @@ export type I128Limbs = { readonly hi: number; readonly w2: number; readonly w1: /** Limbs, whose value is theirs with `hi` read in the type's sign. */ export type Limbs = U64Limbs | I64Limbs | U128Limbs | I128Limbs; +/** + * A number of the Rust integer type `T`. Its brand is type-only, so `tsc` refuses one + * type's value where another's goes, while a plain number, such as a literal, passes + * for any. + */ +type Num = number & { readonly [brand]?: T }; + +export type u8 = Num<"u8">; +export type u16 = Num<"u16">; +export type u32 = Num<"u32">; +/** 32 bits, as on wasm32. */ +export type usize = Num<"usize">; +export type i8 = Num<"i8">; +export type i16 = Num<"i16">; +export type i32 = Num<"i32">; +/** 32 bits, as on wasm32. */ +export type isize = Num<"isize">; /** * A `u64`: a number while it is a safe integer, at most 2^53 - 1, and its limbs past * that. Each value has one form, so two numbers compare with `===`, and a number never * equals limbs. */ -export type U64 = number | U64Limbs; +export type u64 = Num<"u64"> | U64Limbs; /** An `i64`: a number while it is a safe integer, and its limbs past that. */ -export type I64 = number | I64Limbs; +export type i64 = Num<"i64"> | I64Limbs; /** A `u128`: a number while it is a safe integer, and its limbs past that. */ -export type U128 = number | U128Limbs; +export type u128 = Num<"u128"> | U128Limbs; /** An `i128`: a number while it is a safe integer, and its limbs past that. */ -export type I128 = number | I128Limbs; +export type i128 = Num<"i128"> | I128Limbs; /** The checks and methods of one integer type. */ export interface Int { @@ -590,20 +607,20 @@ function dual(limbs: WideInt, signed: boolean): WideInt = small(8, false); -export const u16: Int = small(16, false); -export const u32: Int = small(32, false); -export const i8: Int = small(8, true); -export const i16: Int = small(16, true); -export const i32: Int = small(32, true); +export const u8: Int = small(8, false); +export const u16: Int = small(16, false); +export const u32: Int = small(32, false); +export const i8: Int = small(8, true); +export const i16: Int = small(16, true); +export const i32: Int = small(32, true); /** 32 bits, as on wasm32. */ -export const usize: Int = u32; +export const usize: Int = u32 as Int; /** 32 bits, as on wasm32. */ -export const isize: Int = i32; -export const u64: WideInt = dual(wide64(false), false); -export const i64: WideInt = dual(wide64(true), true); -export const u128: WideInt = dual(wide128(false), false); -export const i128: WideInt = dual(wide128(true), true); +export const isize: Int = i32 as Int; +export const u64: WideInt = dual(wide64(false), false); +export const i64: WideInt = dual(wide64(true), true); +export const u128: WideInt = dual(wide128(false), false); +export const i128: WideInt = dual(wide128(true), true); /** `a < b` of two integers of one type: numbers, or a wide type's limbs. */ export function lt(a: number | Limbs, b: number | Limbs): boolean { @@ -700,7 +717,8 @@ export const option = { unwrapOr(o: T | undefined, fallback: T): T { return o === undefined ? fallback : o; }, - mapOr(o: T | undefined, fallback: U, f: (value: T) => U): U { + // `f` decides `U`, since a literal fallback would infer a literal type. + mapOr(o: T | undefined, fallback: NoInfer, f: (value: T) => U): U { return o === undefined ? fallback : f(o); }, okOr(o: T | undefined, error: E): Result { diff --git a/ts/src/lower/num.rs b/ts/src/lower/num.rs index 8a4aec5ba..8ffa548ce 100644 --- a/ts/src/lower/num.rs +++ b/ts/src/lower/num.rs @@ -44,15 +44,9 @@ impl Int { self.bits > 32 } - /// The TypeScript type of a value. + /// The TypeScript type of a value, named as the runtime's: `rt.u32`. pub fn ty(self) -> Type { - match (self.signed, self.bits) { - (_, 8..=32) => Type::Builtin("number"), - (false, 64) => Type::Rt("U64", Vec::new()), - (true, 64) => Type::Rt("I64", Vec::new()), - (false, _) => Type::Rt("U128", Vec::new()), - (true, _) => Type::Rt("I128", Vec::new()), - } + Type::Rt(self.rt(), Vec::new()) } pub fn min(self) -> i128 { @@ -381,8 +375,13 @@ pub fn cast<'tcx>(b: &mut Body<'_, '_, 'tcx>, value: Operand, from: Ty<'tcx>, to return target.lit(target.fold(c)); } let fits = target.min() <= source.min() && source.max() <= target.max(); + // A cast that keeps the value still changes the type `tsc` checks it as. + let retyped = |value: Expr| match source.rt() == target.rt() { + true => value, + false => Expr::As(Box::new(value), target.ty()), + }; match (source.big(), target.big()) { - (false, false) if fits => value.expr, + (false, false) if fits => retyped(value.expr), (false, false) => target.wrap(value.expr), (true, false) => { let low = source.method("low", vec![value.expr]); @@ -392,7 +391,7 @@ pub fn cast<'tcx>(b: &mut Body<'_, '_, 'tcx>, value: Operand, from: Ty<'tcx>, to } } // A narrow integer is already a wide one's number. - (false, true) if fits => value.expr, + (false, true) if fits => retyped(value.expr), (true, true) if source.rt() == target.rt() => value.expr, _ => target.method("cast", vec![value.expr]), } diff --git a/ts/src/ts.rs b/ts/src/ts.rs index fe1ffde0e..ed2d6862e 100644 --- a/ts/src/ts.rs +++ b/ts/src/ts.rs @@ -47,7 +47,8 @@ pub enum Expr { /// An arrow function whose body is one expression, its return type inferred: /// `(f: rt.Formatter) => f.pad(s)`. Lambda(Vec, Box), - /// `e as T`, for a literal whose type TypeScript would widen. + /// `e as T`: a literal whose type TypeScript would widen, or an integer cast to + /// another integer type with the same value. As(Box, Type), Located(Loc, Box), } @@ -120,7 +121,7 @@ impl Expr { /// This without an outer `as`, where the context already gives the type. pub fn typed(self) -> Self { match self { - Self::As(e, _) => *e, + Self::As(e, _) if matches!(*e, Self::Str(_) | Self::Array(_) | Self::Object(_)) => *e, Self::Located(loc, e) => Self::Located(loc, Box::new(e.typed())), // `rt.Ok(v)` and `rt.Err(e)` infer their type from the context too. Self::Call(f, args) if matches!(*f, Self::Rt("Ok" | "Err")) => { diff --git a/ts/tests/fixtures.rs b/ts/tests/fixtures.rs index b6b09bd30..2e98ee679 100644 --- a/ts/tests/fixtures.rs +++ b/ts/tests/fixtures.rs @@ -256,8 +256,11 @@ fn lines(text: &str) -> Vec { fn lang() { let out = translated("lang"); snapshot("lang", &out); - // A hand-written caller, type-checked against the output it calls. - std::fs::copy(manifest_dir().join("fixtures/lang/caller.ts"), out.join("caller.ts")).unwrap(); + // Hand-written callers, type-checked against the output they call: one runs it, and + // one mixes integer types, which `tsc` must refuse. + for file in ["caller.ts", "brands.ts"] { + std::fs::copy(manifest_dir().join("fixtures/lang").join(file), out.join(file)).unwrap(); + } typecheck(&out); differential("lang-fixture", &out, "caller.ts", 50_000, &["wide"]); } From 64931f5150d56abd85b7579560a8cb9a9c90872c Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 24 Oct 2026 07:58:39 -0700 Subject: [PATCH 8/8] chore(ts): regenerate snapshots for branded integer types Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/fixtures/codec/expected/bench.ts | 38 +-- ts/fixtures/codec/expected/coding/decode.ts | 34 +- ts/fixtures/codec/expected/coding/encode.ts | 20 +- ts/fixtures/codec/expected/coding/varint.ts | 44 +-- ts/fixtures/codec/expected/coding/version.ts | 10 +- ts/fixtures/codec/expected/harness.ts | 24 +- ts/fixtures/codec/expected/lib.ts | 4 +- ts/fixtures/codec/expected/lite/group.ts | 12 +- ts/fixtures/codec/expected/lite/message.ts | 2 +- ts/fixtures/codec/expected/lite/probe.ts | 16 +- ts/fixtures/lang/expected/fmt.ts | 130 +++---- ts/fixtures/lang/expected/generics.ts | 92 ++--- ts/fixtures/lang/expected/harness.ts | 2 +- ts/fixtures/lang/expected/ints.ts | 340 +++++++++---------- ts/fixtures/lang/expected/iters.ts | 230 ++++++------- ts/fixtures/lang/expected/shapes.ts | 174 +++++----- 16 files changed, 587 insertions(+), 585 deletions(-) diff --git a/ts/fixtures/codec/expected/bench.ts b/ts/fixtures/codec/expected/bench.ts index 9293daba1..941aee3af 100644 --- a/ts/fixtures/codec/expected/bench.ts +++ b/ts/fixtures/codec/expected/bench.ts @@ -73,8 +73,8 @@ export function encodeVarints(data: Uint8Array, form_: Form, out: rt.ByteVec): r /** Every varint on the wire: how many, and the last. */ export function decodeVarints(wire: Uint8Array, form_: Form, out: rt.ByteVec): rt.Result { let r: Decoder = Decoder.new(wire, form_); - let count: number = 0; - let last: rt.U64 = 0; + let count: rt.usize = 0; + let last: rt.u64 = 0; while (!r.isEmpty()) { const varint = r.varint(); if (!varint.ok) { @@ -100,11 +100,11 @@ export function encodeFrames(data: Uint8Array, version: Version, out: rt.ByteVec const [size, data5] = takeU64(data4); const [keyEvery, data6] = takeU64(data5); const [keySize, ] = takeU64(data6); - const [delta2, keyEvery2] = [rt.i64.cast(delta), rt.u64.low(keyEvery)] as [rt.I64, number]; + const [delta2, keyEvery2] = [rt.i64.cast(delta), rt.u64.low(keyEvery)] as [rt.i64, rt.usize]; let w: Encoder = Encoder.new(out, Form.from(version)); const end = rt.u64.low(count); for (let n = 0; n < end; n++) { - let value: [rt.I64, rt.U64]; + let value: [rt.i64, rt.u64]; arms: { if (n === 0) { value = [0, first]; @@ -133,9 +133,9 @@ export function encodeFrames(data: Uint8Array, version: Version, out: rt.ByteVec /** Every FRAME header on the wire: how many, their total size, and the last delta. */ export function decodeFrames(wire: Uint8Array, version: Version, out: rt.ByteVec): rt.Result { let r: Decoder = Decoder.new(wire, Form.from(version)); - let count: number = 0; - let total: rt.U64 = 0; - let last: rt.I64 = 0; + let count: rt.usize = 0; + let total: rt.u64 = 0; + let last: rt.i64 = 0; while (!r.isEmpty()) { const varint = r.varint(); if (!varint.ok) { @@ -167,38 +167,38 @@ export function encodeGroup(data: Uint8Array, version: Version, out: rt.ByteVec) * One iterator chain over every byte, picked by `chain`, with closures that cost little * next to the iterator protocol they run through. */ -export function iterate(chain: number, data: Uint8Array, out: rt.ByteVec): void { +export function iterate(chain: rt.u8, data: Uint8Array, out: rt.ByteVec): void { if (chain === 0) { - putUsize(out, rt.iter.slice(data).map((arg: number): number => { + putUsize(out, rt.iter.slice(data).map((arg: rt.u8): rt.u8 => { const b = arg; return b >> 1; - }).filter((arg2: number): boolean => { + }).filter((arg2: rt.u8): boolean => { const b2 = arg2; return (b2 & 1) === 0; }).count()); } else if (chain === 1) { - out.push(rt.iter.slice(data).any((arg3: number): boolean => { + out.push(rt.iter.slice(data).any((arg3: rt.u8): boolean => { const b3 = arg3; return b3 === 0xff; }) ? 1 : 0); } else if (chain === 2) { - const odd = rt.iter.slice(data).filter((arg4: number): boolean => { + const odd = rt.iter.slice(data).filter((arg4: rt.u8): boolean => { const b4 = arg4; return (b4 & 1) === 1; }).toByteVec(); putUsize(out, odd.length); } else if (chain === 3) { - putU64(out, rt.iter.slice(data).map((arg5: number): number => { + putU64(out, rt.iter.slice(data).map((arg5: rt.u8): rt.u32 => { const b5 = arg5; - return b5; - }).sum(rt.u32.add, 0)); + return b5 as rt.u32; + }).sum(rt.u32.add, 0) as rt.u64); } else if (chain === 4) { - putU64(out, rt.iter.slice(data).skip(16).take(512).fold(0, (acc: number, arg6: number): number => { + putU64(out, rt.iter.slice(data).skip(16).take(512).fold(0, (acc: rt.u32, arg6: rt.u8): rt.u32 => { const b6 = arg6; - return (acc ^ b6) >>> 0; - })); + return (acc ^ b6 as rt.u32) >>> 0; + }) as rt.u64); } else { - let last: number = 0; + let last: rt.usize = 0; const iter = rt.iter.slice(data).enumerate(); while (true) { const item = iter.next(); diff --git a/ts/fixtures/codec/expected/coding/decode.ts b/ts/fixtures/codec/expected/coding/decode.ts index a6eb650dc..146ecbd25 100644 --- a/ts/fixtures/codec/expected/coding/decode.ts +++ b/ts/fixtures/codec/expected/coding/decode.ts @@ -10,13 +10,13 @@ export type DecodeError = | { tag: "Short" } | { tag: "Long" } | { tag: "InvalidString"; _0: rt.FromUtf8Error } - | { tag: "InvalidMessage"; _0: rt.U64 } + | { tag: "InvalidMessage"; _0: rt.u64 } | { tag: "InvalidSubscribeLocation" } | { tag: "InvalidValue" } | { tag: "TooMany" } | { tag: "BoundsExceeded" } | { tag: "ExpectedEnd" } - | { tag: "MessageTooLarge"; size: number; max: number } + | { tag: "MessageTooLarge"; size: rt.usize; max: rt.usize } | { tag: "ExpectedData" } | { tag: "Duplicate" } | { tag: "Missing" } @@ -37,7 +37,7 @@ export const DecodeError = { return __formatter.writeStr("invalid string"); } else if (self.tag === "InvalidMessage") { const _0 = self._0; - const value = [_0] as [rt.U64]; + const value = [_0] as [rt.u64]; const __field0 = value[0]; return __formatter.writeFmt(["invalid message: ", (f: rt.Formatter) => rt.u64.display(__field0, f)]); } else if (self.tag === "InvalidSubscribeLocation") { @@ -53,7 +53,7 @@ export const DecodeError = { } else if (self.tag === "MessageTooLarge") { const size = self.size; const max = self.max; - const value2 = [size, max] as [number, number]; + const value2 = [size, max] as [rt.usize, rt.usize]; const __displaySize = value2[0]; const __displayMax = value2[1]; return __formatter.writeFmt(["message too large: ", (f: rt.Formatter) => rt.usize.display(__displaySize, f), " bytes exceeds ", (f: rt.Formatter) => rt.usize.display(__displayMax, f), " byte limit"]); @@ -155,18 +155,18 @@ export class Decoder { buf: Uint8Array; form: Form; /** The length of the buffer this decoder started with. */ - len: number; + len: rt.usize; /** See [`Self::need`]. */ - _need: number; + _need: rt.usize; /** The control message read, counted once the read is consumed. */ control: Control | undefined; /** * Leading bytes left out of the `control` count: a type selector the sender writes * apart from the message, and so never counts. */ - uncounted: number; + uncounted: rt.usize; - constructor(fields: { buf: Uint8Array; form: Form; len: number; _need: number; control: Control | undefined; uncounted: number }) { + constructor(fields: { buf: Uint8Array; form: Form; len: rt.usize; _need: rt.usize; control: Control | undefined; uncounted: rt.usize }) { this.buf = fields.buf; this.form = fields.form; this.len = fields.len; @@ -185,19 +185,19 @@ export class Decoder { * needed: at least one more than the buffer when it can't tell, and 0 if no read of * this decoder ran short. */ - need(): number { + need(): rt.usize { return this._need; } /** Record that a read wanted `more` bytes past the current position. */ - short(more: number): DecodeError { + short(more: rt.usize): DecodeError { const need = rt.usize.saturatingAdd(rt.usize.sub(this.len, this.buf.length), more); this._need = rt.max(this._need, need); return { tag: "Short" }; } /** The number of unread bytes. */ - remaining(): number { + remaining(): rt.usize { return this.buf.length; } @@ -207,7 +207,7 @@ export class Decoder { } /** Read `len` raw bytes. */ - slice(len: number): rt.Result { + slice(len: rt.usize): rt.Result { const splitAtChecked = rt.slice.splitAtChecked(this.buf, len); if (splitAtChecked === undefined) { return rt.Err(this.short(len)); @@ -226,7 +226,7 @@ export class Decoder { } /** Split off the next `len` bytes as their own decoder, e.g. a size-prefixed body. */ - sub(len: number): rt.Result { + sub(len: rt.usize): rt.Result { const slice = this.slice(len); if (!slice.ok) { return rt.Err(slice.error); @@ -235,7 +235,7 @@ export class Decoder { } /** Read a single byte. */ - u8(): rt.Result { + u8(): rt.Result { const slice = this.slice(1); if (!slice.ok) { return rt.Err(slice.error); @@ -244,7 +244,7 @@ export class Decoder { } /** Read a big-endian `u16`. */ - u16(): rt.Result { + u16(): rt.Result { const slice = this.slice(2); if (!slice.ok) { return rt.Err(slice.error); @@ -269,7 +269,7 @@ export class Decoder { } /** Read a varint. */ - varint(): rt.Result { + varint(): rt.Result { const read_ = read(this.buf, this.form); if (read_.ok) { const value = read_.value[0]; @@ -285,7 +285,7 @@ export class Decoder { } /** Read an optional varint: 0 is `None`, and `n + 1` is `Some(n)`. */ - varintOpt(): rt.Result { + varintOpt(): rt.Result { const varint = this.varint(); if (!varint.ok) { return rt.Err(varint.error); diff --git a/ts/fixtures/codec/expected/coding/encode.ts b/ts/fixtures/codec/expected/coding/encode.ts index deefd5d53..544f3e84c 100644 --- a/ts/fixtures/codec/expected/coding/encode.ts +++ b/ts/fixtures/codec/expected/coding/encode.ts @@ -69,12 +69,12 @@ export class Encoder { } /** Write a single byte. */ - u8(v: number): void { + u8(v: rt.u8): void { this.buf.push(v); } /** Write a big-endian `u16`. */ - u16(v: number): void { + u16(v: rt.u16): void { this.buf.extendFromSlice(rt.u16.toBeBytes(v)); } @@ -84,7 +84,7 @@ export class Encoder { } /** Write a varint, or fail with [`EncodeError::BoundsExceeded`] if the form cannot carry it. */ - varint(v: rt.U64): rt.Result { + varint(v: rt.u64): rt.Result { const write_ = write(v, this.form, this.buf); if (!write_.ok) { return rt.Err(EncodeError.from(write_.error)); @@ -93,8 +93,8 @@ export class Encoder { } /** Write an optional varint: `None` as 0, and `Some(n)` as `n + 1`. */ - varintOpt(v: rt.U64 | undefined): rt.Result { - let v2: rt.U64; + varintOpt(v: rt.u64 | undefined): rt.Result { + let v2: rt.u64; if (v !== undefined) { const v3 = v; const okOr = rt.option.okOr(rt.u64.checkedAdd(v3, 1), "TooLarge" as EncodeError); @@ -110,7 +110,7 @@ export class Encoder { /** Write a varint length, then the raw bytes. */ bytes(v: Uint8Array): rt.Result { - const varint = this.varint(v.length); + const varint = this.varint(v.length as rt.u64); if (!varint.ok) { return rt.Err(varint.error); } @@ -140,7 +140,7 @@ export class Encoder { } /** The number of bytes written since `prefix` was reserved. */ - since(prefix: Prefix): number { + since(prefix: Prefix): rt.usize { return rt.usize.sub(this.buf.length, prefix.body); } @@ -164,7 +164,7 @@ export class Encoder { const size = mapErr.value; rt.slice.copyFromSlice(rt.slice.range(this.buf.view(), rt.usize.sub(body, 2), body), rt.u16.toBeBytes(size)); } else { - const write_ = write(rt.usize.sub(end, body), this.form, this.buf); + const write_ = write(rt.usize.sub(end, body) as rt.u64, this.form, this.buf); if (!write_.ok) { return rt.Err(EncodeError.from(write_.error)); } @@ -190,10 +190,10 @@ export class Encoder { /** A size prefix reserved ahead of a body, sized by [`Encoder::fill`] once it is written. */ export class Prefix { /** Where the body starts, just past the reserved bytes. */ - body: number; + body: rt.usize; kind: PrefixKind; - constructor(fields: { body: number; kind: PrefixKind }) { + constructor(fields: { body: rt.usize; kind: PrefixKind }) { this.body = fields.body; this.kind = fields.kind; } diff --git a/ts/fixtures/codec/expected/coding/varint.ts b/ts/fixtures/codec/expected/coding/varint.ts index cd4f2963a..df7933a77 100644 --- a/ts/fixtures/codec/expected/coding/varint.ts +++ b/ts/fixtures/codec/expected/coding/varint.ts @@ -30,19 +30,19 @@ export const Form = { }; /** The high and low 32 bits: the only place the codec splits a `u64`. */ -export function toHalves(value: rt.U64): [number, number] { +export function toHalves(value: rt.u64): [rt.u32, rt.u32] { return [rt.u64.low(rt.u64.shr(value, 32)), rt.u64.low(value)]; } /** The inverse of [`to_halves`]. */ -export function fromHalves(hi: number, lo: number): rt.U64 { - return rt.u64.or(rt.u64.shl(hi, 32), lo); +export function fromHalves(hi: rt.u32, lo: rt.u32): rt.u64 { + return rt.u64.or(rt.u64.shl(hi as rt.u64, 32), lo as rt.u64); } /** The bytes `value` takes on the wire in `form`, or [`BoundsExceeded`] if it does not fit. */ -export function size(value: rt.U64, form: Form): rt.Result { +export function size(value: rt.u64, form: Form): rt.Result { const [hi, lo] = toHalves(value); - let value2: number; + let value2: rt.usize; if (form.tag === "Quic" && hi === 0 && lo < (1 << 6)) { value2 = 1; } else if (form.tag === "Quic" && hi === 0 && lo < (1 << 14)) { @@ -78,7 +78,7 @@ export function size(value: rt.U64, form: Form): rt.Result { +export function write(value: rt.u64, form: Form, out: rt.ByteVec): rt.Result { if (form.tag === "Quic") { return writeQuic(value, out); } else { @@ -87,7 +87,7 @@ export function write(value: rt.U64, form: Form, out: rt.ByteVec): rt.Result { +export function writeQuic(value: rt.u64, out: rt.ByteVec): rt.Result { const [hi, lo] = toHalves(value); if (hi === 0 && lo < (1 << 6)) { out.push(lo & 0xff); @@ -113,7 +113,7 @@ export function writeQuic(value: rt.U64, out: rt.ByteVec): rt.Result { +export function read(buf: Uint8Array, form: Form): rt.Result<[rt.u64, Uint8Array], DecodeError> { if (form.tag === "Quic") { return readQuic(buf); } else { @@ -158,7 +158,7 @@ export function read(buf: Uint8Array, form: Form): rt.Result<[rt.U64, Uint8Array } } -export function readQuic(buf: Uint8Array): rt.Result<[rt.U64, Uint8Array], DecodeError> { +export function readQuic(buf: Uint8Array): rt.Result<[rt.u64, Uint8Array], DecodeError> { const splitFirst = rt.slice.splitFirst(buf); if (splitFirst === undefined) { return rt.Err({ tag: "Short" }); @@ -166,7 +166,7 @@ export function readQuic(buf: Uint8Array): rt.Result<[rt.U64, Uint8Array], Decod const first = splitFirst[0]; const rest = splitFirst[1]; if (first < 0x40) { - return rt.Ok([first, rest]); + return rt.Ok([first as rt.u64, rest]); } else if (first < 0x80) { const okOr = rt.option.okOr(rt.slice.splitFirstChunk(buf, 2), { tag: "Short" } as DecodeError); if (!okOr.ok) { @@ -175,7 +175,7 @@ export function readQuic(buf: Uint8Array): rt.Result<[rt.U64, Uint8Array], Decod const a = okOr.value[0][0]; const b = okOr.value[0][1]; const rest2 = okOr.value[1]; - return rt.Ok([((a & 0x3f) << 8) | b, rest2]); + return rt.Ok([((((a & 0x3f) << 8) | b)) as rt.u64, rest2]); } else if (first < 0xc0) { const okOr2 = rt.option.okOr(rt.slice.splitFirstChunk(buf, 4), { tag: "Short" } as DecodeError); if (!okOr2.ok) { @@ -186,7 +186,7 @@ export function readQuic(buf: Uint8Array): rt.Result<[rt.U64, Uint8Array], Decod const c = okOr2.value[0][2]; const d = okOr2.value[0][3]; const rest3 = okOr2.value[1]; - return rt.Ok([(((a2 & 0x3f) << 24) | (b2 << 16) | (c << 8) | d) >>> 0, rest3]); + return rt.Ok([(((a2 & 0x3f) << 24) | (b2 << 16) | (c << 8) | d) >>> 0 as rt.u64, rest3]); } else { const okOr3 = rt.option.okOr(rt.slice.splitFirstChunk(buf, 8), { tag: "Short" } as DecodeError); if (!okOr3.ok) { @@ -202,17 +202,17 @@ export function readQuic(buf: Uint8Array): rt.Result<[rt.U64, Uint8Array], Decod } } -export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[rt.U64, Uint8Array], DecodeError> { +export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[rt.u64, Uint8Array], DecodeError> { const splitFirst = rt.slice.splitFirst(buf); if (splitFirst === undefined) { return rt.Err({ tag: "Short" }); } const first = splitFirst[0]; const rest = splitFirst[1]; - let value: [rt.U64, Uint8Array]; + let value: [rt.u64, Uint8Array]; const leadingOnes = rt.u8.leadingOnes(first); if (leadingOnes === 0) { - value = [first, rest]; + value = [first as rt.u64, rest]; } else if (leadingOnes === 1) { const okOr = rt.option.okOr(rt.slice.splitFirstChunk(buf, 2), { tag: "Short" } as DecodeError); if (!okOr.ok) { @@ -221,7 +221,7 @@ export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[rt. const a = okOr.value[0][0]; const b = okOr.value[0][1]; const rest2 = okOr.value[1]; - value = [((a & 0x3f) << 8) | b, rest2]; + value = [((((a & 0x3f) << 8) | b)) as rt.u64, rest2]; } else if (leadingOnes === 2) { const okOr2 = rt.option.okOr(rt.slice.splitFirstChunk(buf, 3), { tag: "Short" } as DecodeError); if (!okOr2.ok) { @@ -231,7 +231,7 @@ export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[rt. const b2 = okOr2.value[0][1]; const c = okOr2.value[0][2]; const rest3 = okOr2.value[1]; - value = [((a2 & 0x1f) << 16) | (b2 << 8) | c, rest3]; + value = [((((a2 & 0x1f) << 16) | (b2 << 8) | c)) as rt.u64, rest3]; } else if (leadingOnes === 3) { const okOr3 = rt.option.okOr(rt.slice.splitFirstChunk(buf, 4), { tag: "Short" } as DecodeError); if (!okOr3.ok) { @@ -242,7 +242,7 @@ export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[rt. const c2 = okOr3.value[0][2]; const d = okOr3.value[0][3]; const rest4 = okOr3.value[1]; - value = [(((a3 & 0x0f) << 24) | (b3 << 16) | (c2 << 8) | d) >>> 0, rest4]; + value = [(((a3 & 0x0f) << 24) | (b3 << 16) | (c2 << 8) | d) >>> 0 as rt.u64, rest4]; } else if (leadingOnes === 4) { const okOr4 = rt.option.okOr(rt.slice.splitFirstChunk(buf, 5), { tag: "Short" } as DecodeError); if (!okOr4.ok) { @@ -251,7 +251,7 @@ export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[rt. 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(lo)), rest5]; + value = [fromHalves(((a4 & 0x07)) as rt.u32, 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) { @@ -307,12 +307,12 @@ export function readLeadingOnes(buf: Uint8Array, seven: boolean): rt.Result<[rt. * * Small magnitudes stay small (-1 -> 1, 1 -> 2, -2 -> 3, ...), and all of `i64` fits. */ -export function zigzag(signed: rt.I64): rt.U64 { +export function zigzag(signed: rt.i64): rt.u64 { return rt.u64.cast(rt.i64.xor(rt.i64.shl(signed, 1), rt.i64.shr(signed, 63))); } /** The inverse of [`zigzag`]. */ -export function unzigzag(value: rt.U64): rt.I64 { +export function unzigzag(value: rt.u64): rt.i64 { return rt.i64.xor(rt.i64.cast(rt.u64.shr(value, 1)), rt.i64.neg(rt.i64.cast(rt.u64.and(value, 1)))); } diff --git a/ts/fixtures/codec/expected/coding/version.ts b/ts/fixtures/codec/expected/coding/version.ts index 1709b1f90..e19868763 100644 --- a/ts/fixtures/codec/expected/coding/version.ts +++ b/ts/fixtures/codec/expected/coding/version.ts @@ -8,9 +8,9 @@ import { type Version as version_Version } from "../lite/version.ts"; /** A version number negotiated during the setup. */ export class Version { - _0: rt.U64; + _0: rt.u64; - constructor(_0: rt.U64) { + constructor(_0: rt.u64) { this._0 = _0; } @@ -51,7 +51,7 @@ export class Versions { } /** Decode the value from the front of `buf`, returning it and the bytes it took. */ - static decodeSlice(buf: Uint8Array, version: Form): rt.Result<[Versions, number], DecodeError> { + static decodeSlice(buf: Uint8Array, version: Form): rt.Result<[Versions, rt.usize], DecodeError> { let r: Decoder = Decoder.new(buf, version); const decode = Versions.decode(r, version); if (!decode.ok) { @@ -83,7 +83,7 @@ export class Versions { /** Encode the version list. */ encode$Form(w: Encoder, version: Form): rt.Result { - const varint = w.varint(this._0.length); + const varint = w.varint(this._0.length as rt.u64); if (!varint.ok) { return rt.Err(varint.error); } @@ -98,7 +98,7 @@ export class Versions { /** Encode the version list. */ encode$Version(w: Encoder, version: version_Version): rt.Result { - const varint = w.varint(this._0.length); + const varint = w.varint(this._0.length as rt.u64); if (!varint.ok) { return rt.Err(varint.error); } diff --git a/ts/fixtures/codec/expected/harness.ts b/ts/fixtures/codec/expected/harness.ts index fb45e500f..9746efb8e 100644 --- a/ts/fixtures/codec/expected/harness.ts +++ b/ts/fixtures/codec/expected/harness.ts @@ -42,7 +42,7 @@ export function run(input: Uint8Array): rt.ByteVec { return out; } -export function form(selector: number): Form { +export function form(selector: rt.u8): Form { const value = rt.u8.rem(selector, 3); if (value === 0) { return { tag: "Quic" }; @@ -53,7 +53,7 @@ export function form(selector: number): Form { } } -export function liteVersion(selector: number): version_Version { +export function liteVersion(selector: rt.u8): version_Version { const value = rt.u8.rem(selector, 7); if (value === 0) { return "Lite01"; @@ -72,16 +72,16 @@ export function liteVersion(selector: number): version_Version { } } -export function putU64(out: rt.ByteVec, value: rt.U64): void { +export function putU64(out: rt.ByteVec, value: rt.u64): void { out.extendFromSlice(rt.u64.toBeBytes(value)); } -export function putUsize(out: rt.ByteVec, value: number): void { - putU64(out, value); +export function putUsize(out: rt.ByteVec, value: rt.usize): void { + putU64(out, value as rt.u64); } /** Take up to 8 big-endian bytes as a `u64`, zero-padded on the right. */ -export function takeU64(data: Uint8Array): [rt.U64, Uint8Array] { +export function takeU64(data: Uint8Array): [rt.u64, 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)); @@ -179,7 +179,7 @@ export function decodeScript(data: Uint8Array, out: rt.ByteVec): void { } const len = splitFirst2[0]; const data3 = splitFirst2[1]; - const [script, wire] = rt.slice.splitAt(data3, rt.min(data3.length, len)); + const [script, wire] = rt.slice.splitAt(data3, rt.min(data3.length, len as rt.usize)); let r: Decoder = Decoder.new(wire, form(selector)); for (const item of script) { const step = item; @@ -197,7 +197,7 @@ export function decodeScript(data: Uint8Array, out: rt.ByteVec): void { out.push(r.isEmpty() ? 1 : 0); } -export function decodeStep(r: Decoder, step: number, out: rt.ByteVec): rt.Result { +export function decodeStep(r: Decoder, step: rt.u8, out: rt.ByteVec): rt.Result { const value = rt.u8.rem(step, 10); if (value === 0) { const arg = out; @@ -255,14 +255,14 @@ export function decodeStep(r: Decoder, step: number, out: rt.ByteVec): rt.Result const s = string.value; out.extendFromSlice(rt.string.asBytes(s)); } else if (value === 7) { - const slice = r.slice(rt.u8.div(step, 10)); + const slice = r.slice(rt.u8.div(step, 10) as rt.usize); if (!slice.ok) { return rt.Err(slice.error); } const slice2 = slice.value; out.extendFromSlice(slice2); } else if (value === 8) { - const sub = r.sub(rt.u8.div(step, 10)); + const sub = r.sub(rt.u8.div(step, 10) as rt.usize); if (!sub.ok) { return rt.Err(sub.error); } @@ -294,7 +294,7 @@ export function encodeScript(data: Uint8Array, out: rt.ByteVec): void { } const len = splitFirst2[0]; const data3 = splitFirst2[1]; - let [script, values] = rt.slice.splitAt(data3, rt.min(data3.length, len)); + let [script, values] = rt.slice.splitAt(data3, rt.min(data3.length, len as rt.usize)); let buf: rt.ByteVec = new rt.ByteVec(); let w: Encoder = Encoder.new(buf, form(selector)); let prefixes: Prefix[] = []; @@ -320,7 +320,7 @@ export function encodeScript(data: Uint8Array, out: rt.ByteVec): void { const value3 = rt.u64.rem(value, 3); result = arg.varintOpt(value3 === 0 ? undefined : value3 === 1 ? value : rt.u64.of(18446744073709551615n)); } else if (value2 === 5) { - result = w.bytes(rt.slice.range(rt.u64.toBeBytes(value), 0, rt.u8.rem(rt.u8.div(step, 10), 9))); + result = w.bytes(rt.slice.range(rt.u64.toBeBytes(value), 0, rt.u8.rem(rt.u8.div(step, 10), 9) as rt.usize)); } else if (value2 === 6) { const fromUtf8 = rt.string.fromUtf8(rt.slice.toByteVec(rt.u64.toBeBytes(value))); if (fromUtf8.ok) { diff --git a/ts/fixtures/codec/expected/lib.ts b/ts/fixtures/codec/expected/lib.ts index 59f2a0bcf..7a79567cf 100644 --- a/ts/fixtures/codec/expected/lib.ts +++ b/ts/fixtures/codec/expected/lib.ts @@ -3,7 +3,7 @@ import * as rt from "./rt.ts"; /** moq-net's session error, trimmed to a few variants: the codec only carries it. */ -export type SessionError = { tag: "ProtocolViolation" } | { tag: "App"; _0: number }; +export type SessionError = { tag: "ProtocolViolation" } | { tag: "App"; _0: rt.u16 }; export const SessionError = { fmt$Display(self: SessionError, __formatter: rt.Formatter): rt.Result { @@ -11,7 +11,7 @@ export const SessionError = { return __formatter.writeStr("protocol violation"); } else { const _0 = self._0; - const value = [_0] as [number]; + const value = [_0] as [rt.u16]; const __display0 = value[0]; return __formatter.writeFmt(["app code=", (f: rt.Formatter) => rt.u16.display(__display0, f)]); } diff --git a/ts/fixtures/codec/expected/lite/group.ts b/ts/fixtures/codec/expected/lite/group.ts index 96ee4be73..96e580552 100644 --- a/ts/fixtures/codec/expected/lite/group.ts +++ b/ts/fixtures/codec/expected/lite/group.ts @@ -8,18 +8,18 @@ import { decodeWith$Group } from "../lite/message.ts"; import { Version } from "../lite/version.ts"; export class Group { - subscribe: rt.U64; - sequence: rt.U64; - frameStart: rt.U64; + subscribe: rt.u64; + sequence: rt.u64; + frameStart: rt.u64; - constructor(fields: { subscribe: rt.U64; sequence: rt.U64; frameStart: rt.U64 }) { + constructor(fields: { subscribe: rt.u64; sequence: rt.u64; frameStart: rt.u64 }) { this.subscribe = fields.subscribe; this.sequence = fields.sequence; this.frameStart = fields.frameStart; } /** Decode the value from the front of `buf`, returning it and the bytes it took. */ - static decodeSlice(buf: Uint8Array, version: Version): rt.Result<[Group, number], DecodeError> { + static decodeSlice(buf: Uint8Array, version: Version): rt.Result<[Group, rt.usize], DecodeError> { let r: Decoder = Decoder.new(buf, Form.from(version)); const decode = Group.decode(r, version); if (!decode.ok) { @@ -41,7 +41,7 @@ export class Group { return rt.Err(varint2.error); } const sequence = varint2.value; - let frameStart: rt.U64; + let frameStart: rt.u64; if (Version.hasFrameBounds(version)) { const varint3 = r.varint(); if (!varint3.ok) { diff --git a/ts/fixtures/codec/expected/lite/message.ts b/ts/fixtures/codec/expected/lite/message.ts index 7e334bc3b..1020cc5d0 100644 --- a/ts/fixtures/codec/expected/lite/message.ts +++ b/ts/fixtures/codec/expected/lite/message.ts @@ -7,7 +7,7 @@ import { type Probe } from "../lite/probe.ts"; import { type Control } from "../stats.ts"; /** Read a lite message's varint size prefix, refusing one past `max`. */ -export function decodeSize(r: Decoder, max: number): rt.Result { +export function decodeSize(r: Decoder, max: rt.usize): rt.Result { const varint = r.varint(); if (!varint.ok) { return rt.Err(varint.error); diff --git a/ts/fixtures/codec/expected/lite/probe.ts b/ts/fixtures/codec/expected/lite/probe.ts index 10201e47f..635ccb322 100644 --- a/ts/fixtures/codec/expected/lite/probe.ts +++ b/ts/fixtures/codec/expected/lite/probe.ts @@ -15,16 +15,16 @@ import { type Control } from "../stats.ts"; * On the wire, 0 means unknown (None). Some(0) is rounded up to Some(1). */ export class Probe { - bitrate: rt.U64 | undefined; - rtt: rt.U64 | undefined; + bitrate: rt.u64 | undefined; + rtt: rt.u64 | undefined; - constructor(fields: { bitrate: rt.U64 | undefined; rtt: rt.U64 | undefined }) { + constructor(fields: { bitrate: rt.u64 | undefined; rtt: rt.u64 | undefined }) { this.bitrate = fields.bitrate; this.rtt = fields.rtt; } /** Decode the value from the front of `buf`, returning it and the bytes it took. */ - static decodeSlice(buf: Uint8Array, version: Version): rt.Result<[Probe, number], DecodeError> { + static decodeSlice(buf: Uint8Array, version: Version): rt.Result<[Probe, rt.usize], DecodeError> { let r: Decoder = Decoder.new(buf, Form.from(version)); const decode = Probe.decode(r, version); if (!decode.ok) { @@ -65,7 +65,7 @@ export class Probe { if (version === "Lite01" || version === "Lite02") { return rt.Err({ tag: "Version" }); } - let bitrate: rt.U64 | undefined; + let bitrate: rt.u64 | undefined; const varint = r.varint(); if (!varint.ok) { return rt.Err(varint.error); @@ -76,7 +76,7 @@ export class Probe { const v = varint.value; bitrate = v; } - let rtt: rt.U64 | undefined; + let rtt: rt.u64 | undefined; if (!Version.hasProbeRtt(version)) { rtt = undefined; } else { @@ -99,7 +99,7 @@ export class Probe { if (version === "Lite01" || version === "Lite02") { return rt.Err("Version"); } - const wire = rt.option.unwrapOr(rt.option.map(this.bitrate, (v: rt.U64): rt.U64 => { + const wire = rt.option.unwrapOr(rt.option.map(this.bitrate, (v: rt.u64): rt.u64 => { return rt.u64.max(v, 1); }), 0); const varint = w.varint(wire); @@ -107,7 +107,7 @@ export class Probe { return rt.Err(varint.error); } if (Version.hasProbeRtt(version)) { - const wire2 = rt.option.unwrapOr(rt.option.map(this.rtt, (v2: rt.U64): rt.U64 => { + const wire2 = rt.option.unwrapOr(rt.option.map(this.rtt, (v2: rt.u64): rt.u64 => { return rt.u64.max(v2, 1); }), 0); const varint2 = w.varint(wire2); diff --git a/ts/fixtures/lang/expected/fmt.ts b/ts/fixtures/lang/expected/fmt.ts index e0bb1a0e9..6d60ea630 100644 --- a/ts/fixtures/lang/expected/fmt.ts +++ b/ts/fixtures/lang/expected/fmt.ts @@ -9,7 +9,7 @@ export function put(out: rt.ByteVec, s: string): void { } /** Integers of every width, with each spec. */ -export function ints(kind: number, data: Uint8Array, out: rt.ByteVec): void { +export function ints(kind: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const value = rt.u8.rem(kind, 12); if (value === 0) { const data2 = data; @@ -115,7 +115,7 @@ export function strings(data: Uint8Array, out: rt.ByteVec): void { for (const item of rt.slice.range(data, 0, rt.min(data.length, 3))) { const b = item; const value = ["", "a", "héllo", "日本語", "tab\tnew\nline\r", "quote\"back\\slash'", "\u0000\u0001\u001f", "€Ÿ ­", "é", "‍ᅟ\u2028 ", "͸􏿽", "😀 🇦", "゙゚ン", "{}"]; - const s = rt.at(value, rt.usize.rem(b, ["", "a", "héllo", "日本語", "tab\tnew\nline\r", "quote\"back\\slash'", "\u0000\u0001\u001f", "€Ÿ ­", "é", "‍ᅟ\u2028 ", "͸􏿽", "😀 🇦", "゙゚ン", "{}"].length)); + const s = rt.at(value, rt.usize.rem(b as rt.usize, ["", "a", "héllo", "日本語", "tab\tnew\nline\r", "quote\"back\\slash'", "\u0000\u0001\u001f", "€Ÿ ­", "é", "‍ᅟ\u2028 ", "͸􏿽", "😀 🇦", "゙゚ン", "{}"].length)); put(out, rt.fmt.format([(f: rt.Formatter) => f.pad(s), "|", rt.fmt.spec({ width: 8 }, (f: rt.Formatter) => f.pad(s)), "|", rt.fmt.spec({ align: ">", width: 8 }, (f: rt.Formatter) => f.pad(s)), "|", rt.fmt.spec({ align: "^", width: 8 }, (f: rt.Formatter) => f.pad(s)), "|", rt.fmt.spec({ fill: "-", align: "<", width: 3 }, (f: rt.Formatter) => f.pad(s)), "|", (f: rt.Formatter) => rt.fmt.str(s, f), "|", rt.fmt.spec({ width: 12 }, (f: rt.Formatter) => rt.fmt.str(s, f)), "|", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.fmt.str(s, f)), "|{", (f: rt.Formatter) => f.pad(s), "}"])); } const fromUtf8 = rt.string.fromUtf8(rt.slice.toByteVec(data)); @@ -180,10 +180,10 @@ export class Bare { } export class Pair { - _0: number; - _1: rt.I64; + _0: rt.u8; + _1: rt.i64; - constructor(_0: number, _1: rt.I64) { + constructor(_0: rt.u8, _1: rt.i64) { this._0 = _0; this._1 = _1; } @@ -199,14 +199,14 @@ export class Pair { /** Past five fields, derived `Debug` takes the fields as slices. */ export class Wide { - a: number; - b: number; - c: number; - d: rt.U64; - e: number; + a: rt.u8; + b: rt.u16; + c: rt.u32; + d: rt.u64; + e: rt.i8; f: boolean; - constructor(fields: { a: number; b: number; c: number; d: rt.U64; e: number; f: boolean }) { + constructor(fields: { a: rt.u8; b: rt.u16; c: rt.u32; d: rt.u64; e: rt.i8; f: boolean }) { this.a = fields.a; this.b = fields.b; this.c = fields.c; @@ -223,14 +223,14 @@ export class Wide { } export class Six { - _0: number; - _1: number; - _2: number; - _3: number; - _4: number; - _5: number; - - constructor(_0: number, _1: number, _2: number, _3: number, _4: number, _5: number) { + _0: rt.u8; + _1: rt.u8; + _2: rt.u8; + _3: rt.u8; + _4: rt.u8; + _5: rt.u8; + + constructor(_0: rt.u8, _1: rt.u8, _2: rt.u8, _3: rt.u8, _4: rt.u8, _5: rt.u8) { this._0 = _0; this._1 = _1; this._2 = _2; @@ -249,8 +249,8 @@ export type Kind = | { tag: "Empty" } | { tag: "Braced" } | { tag: "Bare" } - | { tag: "One"; _0: number } - | { tag: "Many"; count: number; items: rt.ByteVec }; + | { tag: "One"; _0: rt.u8 } + | { tag: "Many"; count: rt.u32; items: rt.ByteVec }; export const Kind = { fmt(self: Kind, f: rt.Formatter): rt.Result { @@ -278,16 +278,16 @@ export class Nested { tags: rt.ByteVec; pairs: Pair[]; maybe: Pair | undefined; - none: number | undefined; - result: rt.Result; - list: number[]; - tuple: [number, boolean, string]; - single: [number]; + none: rt.u8 | undefined; + result: rt.Result; + list: rt.i16[]; + tuple: [rt.u8, boolean, string]; + single: [rt.u8]; empty: rt.ByteVec; unit: undefined; boxed: Kind; - constructor(fields: { name: string; point: Point; shape: Shape; tags: rt.ByteVec; pairs: Pair[]; maybe: Pair | undefined; none: number | undefined; result: rt.Result; list: number[]; tuple: [number, boolean, string]; single: [number]; empty: rt.ByteVec; unit: undefined; boxed: Kind }) { + constructor(fields: { name: string; point: Point; shape: Shape; tags: rt.ByteVec; pairs: Pair[]; maybe: Pair | undefined; none: rt.u8 | undefined; result: rt.Result; list: rt.i16[]; tuple: [rt.u8, boolean, string]; single: [rt.u8]; empty: rt.ByteVec; unit: undefined; boxed: Kind }) { this.name = fields.name; this.point = fields.point; this.shape = fields.shape; @@ -312,23 +312,23 @@ export class Nested { } export function nested(data: Uint8Array): Nested { - const at = (i: number): number => { + const at = (i: rt.usize): rt.u8 => { return rt.option.unwrapOr(rt.slice.get(data, i), 0); }; const value = ["", "a", "héllo", "日本語", "tab\tnew\nline\r", "quote\"back\\slash'", "\u0000\u0001\u001f", "€Ÿ ­", "é", "‍ᅟ\u2028 ", "͸􏿽", "😀 🇦", "゙゚ン", "{}"]; - const arg = rt.at(value, rt.usize.rem(at(0), ["", "a", "héllo", "日本語", "tab\tnew\nline\r", "quote\"back\\slash'", "\u0000\u0001\u001f", "€Ÿ ­", "é", "‍ᅟ\u2028 ", "͸􏿽", "😀 🇦", "゙゚ン", "{}"].length)); - const arg2 = new Point({ x: rt.i32.sub(at(1), 128), y: -7 }); + const arg = rt.at(value, rt.usize.rem(at(0) as rt.usize, ["", "a", "héllo", "日本語", "tab\tnew\nline\r", "quote\"back\\slash'", "\u0000\u0001\u001f", "€Ÿ ­", "é", "‍ᅟ\u2028 ", "͸􏿽", "😀 🇦", "゙゚ン", "{}"].length)); + const arg2 = new Point({ x: rt.i32.sub(at(1) as rt.i32, 128), y: -7 }); const value2 = rt.u8.rem(at(2), 3); const arg3 = arg; const arg4 = arg2; - const arg5 = value2 === 0 ? { tag: "Dot" } as Shape : value2 === 1 ? { tag: "Line", _0: new Point({ x: 1, y: 2 }), _1: new Point({ x: -3, y: 4 }) } as Shape : { tag: "Rect", corner: new Point({ x: 0, y: 0 }), width: at(3), height: 9 } as Shape; + const arg5 = value2 === 0 ? { tag: "Dot" } as Shape : value2 === 1 ? { tag: "Line", _0: new Point({ x: 1, y: 2 }), _1: new Point({ x: -3, y: 4 }) } as Shape : { tag: "Rect", corner: new Point({ x: 0, y: 0 }), width: at(3) as rt.u16, height: 9 } as Shape; const arg6 = rt.slice.toByteVec(rt.slice.range(data, 0, rt.min(data.length, 4))); const arg7 = rt.slice.toVec([new Pair(at(4), -1), new Pair(2, rt.i64.of(-9223372036854775808n))]); const arg8 = rt.u8.rem(at(5), 2) === 0 ? new Pair(at(5), 5) : undefined; const arg9 = rt.u8.rem(at(6), 2) === 0 ? rt.Ok(at(6)) : rt.Err("no"); - const arg10 = [rt.i16.sub(at(7), 100), 32767]; - const arg11 = [at(8), true, "t"] as [number, boolean, string]; - const arg12 = [at(9)] as [number]; + const arg10 = [rt.i16.sub(at(7) as rt.i16, 100), 32767]; + const arg11 = [at(8), true, "t"] as [rt.u8, boolean, string]; + const arg12 = [at(9)] as [rt.u8]; const arg13 = new rt.ByteVec(); const value3 = rt.u8.rem(at(10), 3); return new Nested({ name: arg3, point: arg4, shape: arg5, tags: arg6, pairs: arg7, maybe: arg8, none: undefined, result: arg9, list: arg10, tuple: arg11, single: arg12, empty: arg13, unit: undefined, boxed: value3 === 0 ? { tag: "Empty" } as Kind : value3 === 1 ? { tag: "One", _0: at(11) } as Kind : { tag: "Many", count: 2, items: rt.slice.toByteVec(Uint8Array.of(at(11), at(12))) } as Kind }); @@ -336,7 +336,7 @@ export function nested(data: Uint8Array): Nested { /** Derived `Debug` of every shape, compact and pretty, with options passed to fields. */ export function derived(data: Uint8Array, out: rt.ByteVec): void { - const at = (i: number): number => { + const at = (i: rt.usize): rt.u8 => { return rt.option.unwrapOr(rt.slice.get(data, i), 0); }; const value = nested(data); @@ -349,7 +349,7 @@ export function derived(data: Uint8Array, out: rt.ByteVec): void { const arg2 = new Unit(); put(arg3, rt.fmt.format([(f: rt.Formatter) => wide.fmt(f), " ", (f: rt.Formatter) => six.fmt(f), " ", (f: rt.Formatter) => arg.fmt(f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => arg2.fmt(f))])); put(out, rt.fmt.format([rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => wide.fmt(f)), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => six.fmt(f))])); - const point = new Point({ x: at(2), y: -1 }); + const point = new Point({ x: at(2) as rt.i32, y: -1 }); const arg5 = out; const arg4 = new Pair(at(3), -9); put(arg5, rt.fmt.format([rt.fmt.spec({ width: 5 }, (f: rt.Formatter) => point.fmt(f)), "|", rt.fmt.spec({ align: "<", width: 4 }, (f: rt.Formatter) => point.fmt(f)), "|", rt.fmt.spec({ alternate: true, width: 4 }, (f: rt.Formatter) => point.fmt(f)), "|", rt.fmt.spec({ align: ">", width: 3 }, (f: rt.Formatter) => arg4.fmt(f))])); @@ -359,7 +359,7 @@ export function derived(data: Uint8Array, out: rt.ByteVec): void { put(arg7, rt.fmt.format([(f: rt.Formatter) => rt.fmt.list(kinds, f, (value: Kind, f: rt.Formatter) => Kind.fmt(value, f)), " ", (f: rt.Formatter) => Kind.fmt(arg6, f)])); put(out, rt.fmt.format([rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.fmt.list(kinds, f, (value: Kind, f: rt.Formatter) => Kind.fmt(value, f)))])); const bytes = data; - const words = [at(5), 0xffff]; + const words = [at(5) as rt.u16, 0xffff]; const arg10 = out; const arg8 = rt.slice.first(data); const arg9 = rt.slice.get(data, 100); @@ -375,9 +375,9 @@ export function derived(data: Uint8Array, out: rt.ByteVec): void { /** `Display` written with `write!`, which ignores the outer options, unless `#`. */ export class Hex { - _0: number; + _0: rt.u32; - constructor(_0: number) { + constructor(_0: rt.u32) { this._0 = _0; } @@ -418,9 +418,9 @@ export class Padded { /** `Display` that fails after writing, for odd values. */ export class Fails { - _0: number; + _0: rt.u8; - constructor(_0: number) { + constructor(_0: rt.u8) { this._0 = _0; } @@ -438,10 +438,10 @@ export class Fails { /** The builders, written out. */ export class Manual { - id: number; - list: number[]; + id: rt.u8; + list: rt.u16[]; - constructor(fields: { id: number; list: number[] }) { + constructor(fields: { id: rt.u8; list: rt.u16[] }) { this.id = fields.id; this.list = fields.list; } @@ -464,10 +464,10 @@ export class Seq { } export class Tup { - _0: number; - _1: number | undefined; + _0: rt.u8; + _1: rt.u8 | undefined; - constructor(_0: number, _1: number | undefined) { + constructor(_0: rt.u8, _1: rt.u8 | undefined) { this._0 = _0; this._1 = _1; } @@ -483,15 +483,15 @@ export class Tup { /** Written `Display` and `Debug`, and a `Display` that fails inside `format!`. */ export function written(data: Uint8Array, out: rt.ByteVec): void { - const at = (i: number): number => { + const at = (i: rt.usize): rt.u8 => { return rt.option.unwrapOr(rt.slice.get(data, i), 0); }; - const hex = new Hex(rt.u32.mul(at(0), 0x1001)); + const hex = new Hex(rt.u32.mul(at(0) as rt.u32, 0x1001)); put(out, rt.fmt.format([(f: rt.Formatter) => hex.fmt$Display(f), "|", rt.fmt.spec({ align: ">", width: 12 }, (f: rt.Formatter) => hex.fmt$Display(f)), "|", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => hex.fmt$Display(f)), "|", (f: rt.Formatter) => hex.fmt$Debug(f), "|", rt.fmt.spec({ align: ">", width: 6 }, (f: rt.Formatter) => hex.fmt$Debug(f)), "|", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => hex.fmt$Debug(f))])); put(out, rt.fmt.toString((f: rt.Formatter) => hex.fmt$Display(f))); const padded = new Padded("pad"); put(out, rt.fmt.format([(f: rt.Formatter) => padded.fmt(f), "|", rt.fmt.spec({ align: ">", width: 6 }, (f: rt.Formatter) => padded.fmt(f)), "|", rt.fmt.spec({ fill: "_", align: "^", width: 9 }, (f: rt.Formatter) => padded.fmt(f)), "|", rt.fmt.spec({ width: 30 }, (f: rt.Formatter) => padded.fmt(f)), "|"])); - const manual = new Manual({ id: at(1), list: rt.slice.toVec([1, at(2)]) }); + const manual = new Manual({ id: at(1), list: rt.slice.toVec([1, at(2) as rt.u16]) }); const empty = new Manual({ id: 0, list: [] }); put(out, rt.fmt.format([(f: rt.Formatter) => manual.fmt(f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => manual.fmt(f)), " ", (f: rt.Formatter) => empty.fmt(f)])); const seq = new Seq(rt.slice.toVec([new Pair(at(3), 0), new Pair(1, -1)])); @@ -508,13 +508,13 @@ export function written(data: Uint8Array, out: rt.ByteVec): void { } /** Panics, each with the message native Rust formats. */ -export function panics(kind: number, rest: Uint8Array, out: rt.ByteVec): void { +export function panics(kind: rt.u8, rest: Uint8Array, out: rt.ByteVec): void { const n = rt.option.unwrapOr(rt.slice.first(rest), 0); const value = rt.u8.rem(kind, 19); if (value === 0) { rt.panic(["bad ", (f: rt.Formatter) => rt.u8.display(n, f), ": ", (f: rt.Formatter) => rt.fmt.list(rest, f, rt.u8.display)]); } else if (value === 1) { - const arg = new Hex(n); + const arg = new Hex(n as rt.u32); rt.panic(["internal error: entered unreachable code: never ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.u8.lowerHex(n, f)), " ", (f: rt.Formatter) => arg.fmt$Display(f)]); } else if (value === 2) { if (!(n > 200)) { @@ -526,7 +526,7 @@ export function panics(kind: number, rest: Uint8Array, out: rt.ByteVec): void { rt.panic("assertion failed: n > 200"); } } else if (value === 4) { - const value2 = [n, 7] as [number, number]; + const value2 = [n, 7] as [rt.u8, rt.u8]; const leftVal = value2[0]; const rightVal = value2[1]; if (leftVal !== rightVal) { @@ -542,7 +542,7 @@ export function panics(kind: number, rest: Uint8Array, out: rt.ByteVec): void { rt.assertFailed(kind3, (f: rt.Formatter) => leftVal2.fmt(f), (f: rt.Formatter) => rightVal2.fmt(f), ["same ", (f: rt.Formatter) => rt.u8.display(n, f)]); } } else if (value === 6) { - const value4 = [n, 9] as [number, number]; + const value4 = [n, 9] as [rt.u8, rt.u8]; const leftVal3 = value4[0]; const rightVal3 = value4[1]; if (leftVal3 !== rightVal3) { @@ -560,7 +560,7 @@ export function panics(kind: number, rest: Uint8Array, out: rt.ByteVec): void { const arg5 = rt.result.expect(rt.copy(r2), "expected a byte", (value: Pair, f: rt.Formatter) => value.fmt(f)); put(arg6, rt.fmt.toString((f: rt.Formatter) => rt.u8.display(arg5, f))); } else if (value === 9) { - const r3 = rt.Ok(new Point({ x: n, y: 0 })); + const r3 = rt.Ok(new Point({ x: n as rt.i32, y: 0 })); const arg8 = out; const arg7 = rt.result.unwrapErr(rt.copy(r3), (value: Point, f: rt.Formatter) => value.fmt(f)); put(arg8, rt.fmt.toString((f: rt.Formatter) => rt.u8.display(arg7, f))); @@ -578,11 +578,11 @@ export function panics(kind: number, rest: Uint8Array, out: rt.ByteVec): void { const arg13 = new Fails(n); put(arg14, rt.fmt.toString((f: rt.Formatter) => arg13.fmt(f))); } else if (value === 13) { - const arg15 = new Hex(n); + const arg15 = new Hex(n as rt.u32); const arg16 = new Fails(n); rt.panic([(f: rt.Formatter) => arg15.fmt$Display(f), " then ", (f: rt.Formatter) => arg16.fmt(f)]); } else if (value === 14) { - const [start, end] = [rt.u8.rem(n, 8), rt.u8.div(n, 32)] as [number, number]; + const [start, end] = [rt.u8.rem(n, 8) as rt.usize, rt.u8.div(n, 32) as rt.usize] as [rt.usize, rt.usize]; const arg19 = out; const arg17 = rt.slice.range(rest, start); const arg18 = rt.slice.range(rest, start, end); @@ -592,7 +592,7 @@ export function panics(kind: number, rest: Uint8Array, out: rt.ByteVec): void { rt.slice.copyFromSlice(buf, rt.slice.range(rest, 0, rt.min(rest.length, 4))); put(out, rt.fmt.format([(f: rt.Formatter) => rt.fmt.list(buf, f, rt.u8.display)])); } else if (value === 16) { - let r4: rt.Result; + let r4: rt.Result; const fromUtf8 = rt.string.fromUtf8(rt.slice.toByteVec(rest)); if (fromUtf8.ok) { const s = fromUtf8.value; @@ -606,8 +606,8 @@ export function panics(kind: number, rest: Uint8Array, out: rt.ByteVec): void { put(arg21, rt.fmt.toString((f: rt.Formatter) => rt.u8.display(arg20, f))); } else if (value === 17) { let buf2: Uint8Array = Uint8Array.of(1, 2, 3, 4); - const dest = rt.usize.rem(rt.option.unwrapOr(rt.slice.get(rest, 1), 0), 4); - rt.slice.copyWithin(buf2, rt.u8.rem(n, 6), rt.u8.rem(rt.u8.div(n, 6), 6), dest); + const dest = rt.usize.rem(rt.option.unwrapOr(rt.slice.get(rest, 1), 0) as rt.usize, 4); + rt.slice.copyWithin(buf2, rt.u8.rem(n, 6) as rt.usize, rt.u8.rem(rt.u8.div(n, 6), 6) as rt.usize, dest); put(out, rt.fmt.format([(f: rt.Formatter) => rt.fmt.list(buf2, f, rt.u8.display)])); } else { const r5 = rt.u8.rem(n, 2) === 0 ? rt.Err(n) : rt.Ok(n); @@ -623,8 +623,8 @@ export function panics(kind: number, rest: Uint8Array, out: rt.ByteVec): void { */ export type Fault = | { tag: "Short" } - | { tag: "Code"; _0: number } - | { tag: "TooLarge"; size: number; max: number } + | { tag: "Code"; _0: rt.u16 } + | { tag: "TooLarge"; size: rt.usize; max: rt.usize } | { tag: "At"; _0: Pair }; export const Fault = { @@ -662,7 +662,7 @@ export const Fault = { /** The fault `data` picks. */ new(data: Uint8Array): Fault { - const at = (i: number): number => { + const at = (i: rt.usize): rt.u8 => { return rt.option.unwrapOr(rt.slice.get(data, i), 0); }; const value = rt.u8.rem(at(0), 4); @@ -671,9 +671,9 @@ export const Fault = { } else if (value === 1) { 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) }; + return { tag: "TooLarge", size: (((((at(1) as rt.usize)) << 24) >>> 0) | at(2) as rt.usize) >>> 0, max: at(3) as rt.usize }; } else { - return { tag: "At", _0: new Pair(at(1), rt.i64.shl((at(2) << 24) >> 24, 40)) }; + return { tag: "At", _0: new Pair(at(1), rt.i64.shl((at(2) << 24) >> 24 as rt.i64, 40)) }; } }, }; diff --git a/ts/fixtures/lang/expected/generics.ts b/ts/fixtures/lang/expected/generics.ts index 617cb409c..85a2fa50f 100644 --- a/ts/fixtures/lang/expected/generics.ts +++ b/ts/fixtures/lang/expected/generics.ts @@ -7,14 +7,14 @@ import { Point, Meters, Shape } from "./shapes.ts"; /** A generic struct, with methods on every instance, on some, and on one. */ export class Wrap { value: T; - count: number; + count: rt.u8; - constructor(fields: { value: T; count: number }) { + constructor(fields: { value: T; count: rt.u8 }) { this.value = fields.value; this.count = fields.count; } - fmt$Option_u8(this: Wrap, f: rt.Formatter): rt.Result { + fmt$Option_u8(this: Wrap, f: rt.Formatter): rt.Result { return f.debugStruct("Wrap").field("value", (f: rt.Formatter) => rt.fmt.option(this.value, f, rt.u8.display)).field("count", (f: rt.Formatter) => rt.u8.display(this.count, f)).finish(); } @@ -26,7 +26,7 @@ export class Wrap { return f.debugStruct("Wrap").field("value", (f: rt.Formatter) => this.value.fmt(f)).field("count", (f: rt.Formatter) => rt.u8.display(this.count, f)).finish(); } - fmt$Result_u8_Shape(this: Wrap>, f: rt.Formatter): rt.Result { + fmt$Result_u8_Shape(this: Wrap>, f: rt.Formatter): rt.Result { return f.debugStruct("Wrap").field("value", (f: rt.Formatter) => rt.fmt.result(this.value, f, rt.u8.display, (value: Shape, f: rt.Formatter) => Shape.fmt(value, f))).field("count", (f: rt.Formatter) => rt.u8.display(this.count, f)).finish(); } @@ -34,7 +34,7 @@ export class Wrap { return f.debugStruct("Wrap").field("value", (f: rt.Formatter) => Shape.fmt(this.value, f)).field("count", (f: rt.Formatter) => rt.u8.display(this.count, f)).finish(); } - fmt$Vec_u16(this: Wrap, f: rt.Formatter): rt.Result { + fmt$Vec_u16(this: Wrap, f: rt.Formatter): rt.Result { return f.debugStruct("Wrap").field("value", (f: rt.Formatter) => rt.fmt.list(this.value, f, rt.u16.display)).field("count", (f: rt.Formatter) => rt.u8.display(this.count, f)).finish(); } @@ -42,11 +42,11 @@ export class Wrap { return f.debugStruct("Wrap").field("value", (f: rt.Formatter) => this.value.fmt$Pair(f)).field("count", (f: rt.Formatter) => rt.u8.display(this.count, f)).finish(); } - fmt$i16(this: Wrap, f: rt.Formatter): rt.Result { + fmt$i16(this: Wrap, f: rt.Formatter): rt.Result { return f.debugStruct("Wrap").field("value", (f: rt.Formatter) => rt.i16.display(this.value, f)).field("count", (f: rt.Formatter) => rt.u8.display(this.count, f)).finish(); } - fmt$u8(this: Wrap, f: rt.Formatter): rt.Result { + fmt$u8(this: Wrap, f: rt.Formatter): rt.Result { return f.debugStruct("Wrap").field("value", (f: rt.Formatter) => rt.u8.display(this.value, f)).field("count", (f: rt.Formatter) => rt.u8.display(this.count, f)).finish(); } @@ -54,7 +54,7 @@ export class Wrap { return this.count === other.count && this.value.eq(other.value); } - static new$Option_u8(value: number | undefined): Wrap { + static new$Option_u8(value: rt.u8 | undefined): Wrap { return new Wrap({ value, count: 0 }); } @@ -74,15 +74,15 @@ export class Wrap { return new Wrap({ value: rt.copy(value), count: 0 }); } - static new$i16(value: number): Wrap { + static new$i16(value: rt.i16): Wrap { return new Wrap({ value, count: 0 }); } - static new$u8(value: number): Wrap { + static new$u8(value: rt.u8): Wrap { return new Wrap({ value, count: 0 }); } - get$Option_u8(this: Wrap): number | undefined { + get$Option_u8(this: Wrap): rt.u8 | undefined { return this.value; } @@ -94,21 +94,21 @@ export class Wrap { return this.value; } - get$u8(this: Wrap): number { + get$u8(this: Wrap): rt.u8 { return this.value; } - bump$Point(this: Wrap): number { + bump$Point(this: Wrap): rt.u8 { this.count = rt.u8.wrappingAdd(this.count, 1); return this.count; } - bump$Vec_u16(this: Wrap): number { + bump$Vec_u16(this: Wrap): rt.u8 { this.count = rt.u8.wrappingAdd(this.count, 1); return this.count; } - bump$u8(this: Wrap): number { + bump$u8(this: Wrap): rt.u8 { this.count = rt.u8.wrappingAdd(this.count, 1); return this.count; } @@ -118,27 +118,27 @@ export class Wrap { } /** A method with its own type parameter. */ - map(this: Wrap, f: (a0: number) => Pair): Wrap { + map(this: Wrap, f: (a0: rt.u8) => Pair): Wrap { return new Wrap({ value: f(this.value), count: this.count }); } - scaled$Meters(this: Wrap): rt.U64 { - return rt.u64.saturatingAdd(this.value.area(), this.count); + scaled$Meters(this: Wrap): rt.u64 { + return rt.u64.saturatingAdd(this.value.area(), this.count as rt.u64); } - scaled$Shape(this: Wrap): rt.U64 { - return rt.u64.saturatingAdd(Shape.area(this.value), this.count); + scaled$Shape(this: Wrap): rt.u64 { + return rt.u64.saturatingAdd(Shape.area(this.value), this.count as rt.u64); } - double(this: Wrap): number { - return rt.u16.mul(this.value, 2); + double(this: Wrap): rt.u16 { + return rt.u16.mul(this.value as rt.u16, 2); } - area$Meters(this: Wrap): rt.U64 { + area$Meters(this: Wrap): rt.u64 { return this.value.area(); } - area$Shape(this: Wrap): rt.U64 { + area$Shape(this: Wrap): rt.u64 { return Shape.area(this.value); } @@ -159,7 +159,7 @@ export class Wrap { export type Either = { tag: "Left"; _0: L } | { tag: "Right"; value: R } | { tag: "Neither" }; export const Either = { - fmt$Point_u8(self: Either, f: rt.Formatter): rt.Result { + fmt$Point_u8(self: Either, f: rt.Formatter): rt.Result { if (self.tag === "Left") { const __self0 = self._0; return f.debugTuple("Left").field((f: rt.Formatter) => __self0.fmt(f)).finish(); @@ -171,7 +171,7 @@ export const Either = { } }, - fmt$u8_Point(self: Either, f: rt.Formatter): rt.Result { + fmt$u8_Point(self: Either, f: rt.Formatter): rt.Result { if (self.tag === "Left") { const __self0 = self._0; return f.debugTuple("Left").field((f: rt.Formatter) => rt.u8.display(__self0, f)).finish(); @@ -183,7 +183,7 @@ export const Either = { } }, - fmt$u8_u16(self: Either, f: rt.Formatter): rt.Result { + fmt$u8_u16(self: Either, f: rt.Formatter): rt.Result { if (self.tag === "Left") { const __self0 = self._0; return f.debugTuple("Left").field((f: rt.Formatter) => rt.u8.display(__self0, f)).finish(); @@ -195,7 +195,7 @@ export const Either = { } }, - left$Point_u8(self: Either): Point | undefined { + left$Point_u8(self: Either): Point | undefined { if (self.tag === "Left") { const l = rt.copy(self._0); return rt.copy(l); @@ -204,7 +204,7 @@ export const Either = { } }, - left$u8_Point(self: Either): number | undefined { + left$u8_Point(self: Either): rt.u8 | undefined { if (self.tag === "Left") { const l = self._0; return l; @@ -213,7 +213,7 @@ export const Either = { } }, - left$u8_u16(self: Either): number | undefined { + left$u8_u16(self: Either): rt.u8 | undefined { if (self.tag === "Left") { const l = self._0; return l; @@ -222,7 +222,7 @@ export const Either = { } }, - flip(self: Either): Either { + flip(self: Either): Either { if (self.tag === "Left") { const l = self._0; return { tag: "Right", value: l }; @@ -247,28 +247,28 @@ export class Tagged { this.all = fields.all; } - fmt(this: Tagged, number>, f: rt.Formatter): rt.Result { - return f.debugStruct("Tagged").field("key", (f: rt.Formatter) => Either.fmt$u8_u16(this.key, f)).field("inner", (f: rt.Formatter) => this.inner.fmt$i16(f)).field("all", (f: rt.Formatter) => rt.fmt.list(this.all, f, (value: Wrap, f: rt.Formatter) => value.fmt$i16(f))).finish(); + fmt(this: Tagged, rt.i16>, f: rt.Formatter): rt.Result { + return f.debugStruct("Tagged").field("key", (f: rt.Formatter) => Either.fmt$u8_u16(this.key, f)).field("inner", (f: rt.Formatter) => this.inner.fmt$i16(f)).field("all", (f: rt.Formatter) => rt.fmt.list(this.all, f, (value: Wrap, f: rt.Formatter) => value.fmt$i16(f))).finish(); } - first(this: Tagged, number>): Wrap | undefined { + first(this: Tagged, rt.i16>): Wrap | undefined { return rt.copy(rt.slice.first(this.all)); } } /** Each method once per instance: `Wrap` of a struct, an enum, a number, and itself. */ export function methods(data: Uint8Array, out: rt.ByteVec): void { - const at = (i: number): number => { + const at = (i: rt.usize): rt.u8 => { return rt.option.unwrapOr(rt.slice.get(data, i), 0); }; - let point: Wrap = Wrap.new$Point(new Point({ x: at(0), y: -2 })); + let point: Wrap = Wrap.new$Point(new Point({ x: at(0) as rt.i32, y: -2 })); point.bump$Point(); - let byte: Wrap = Wrap.new$u8(at(1)); + let byte: Wrap = Wrap.new$u8(at(1)); byte.bump$u8(); byte.bump$u8(); const nested = Wrap.new$Wrap_Pair(Wrap.new$Pair(new Pair(at(2), -3))); - const meters = new Wrap({ value: new Meters(at(3)), count: at(4) }); - const shape = Wrap.new$Shape({ tag: "Rect", corner: rt.copy(point).intoInner(), width: at(5), height: 3 }); + const meters = new Wrap({ value: new Meters(at(3) as rt.u64), count: at(4) }); + const shape = Wrap.new$Shape({ tag: "Rect", corner: rt.copy(point).intoInner(), width: at(5) as rt.u16, height: 3 }); put(out, rt.fmt.format([(f: rt.Formatter) => point.fmt$Point(f), " ", (f: rt.Formatter) => byte.fmt$u8(f), " ", (f: rt.Formatter) => nested.fmt$Wrap_Pair(f), " ", (f: rt.Formatter) => shape.fmt$Shape(f)])); put(out, rt.fmt.format([rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => nested.fmt$Wrap_Pair(f))])); out.extendFromSlice(rt.u16.toBeBytes(byte.double())); @@ -278,13 +278,13 @@ export function methods(data: Uint8Array, out: rt.ByteVec): void { out.extendFromSlice(rt.u64.toBeBytes(shape.scaled$Shape())); meters.describe$Meters(out); shape.describe$Shape(out); - const mapped = rt.copy(byte).map((b: number): Pair => { - return new Pair(b, at(6)); + const mapped = rt.copy(byte).map((b: rt.u8): Pair => { + return new Pair(b, at(6) as rt.i64); }); const arg2 = out; const arg = mapped.eq(Wrap.new$Pair(new Pair(at(1), 0))); put(arg2, rt.fmt.format([(f: rt.Formatter) => mapped.fmt$Pair(f), " ", (f: rt.Formatter) => rt.fmt.bool(arg, f)])); - const eithers = [{ tag: "Left", _0: at(7) } as Either, { tag: "Right", value: Point.new(1, -1) } as Either, { tag: "Neither" } as Either]; + const eithers = [{ tag: "Left", _0: at(7) } as Either, { tag: "Right", value: Point.new(1, -1) } as Either, { tag: "Neither" } as Either]; for (const either of rt.copy(eithers)) { const flipped = Either.flip(either); const arg5 = out; @@ -292,8 +292,8 @@ export function methods(data: Uint8Array, out: rt.ByteVec): void { const arg4 = Either.left$Point_u8(flipped); put(arg5, rt.fmt.format([(f: rt.Formatter) => Either.fmt$u8_Point(either, f), " ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => Either.fmt$Point_u8(flipped, f)), " ", (f: rt.Formatter) => rt.fmt.option(arg3, f, rt.u8.display), " ", (f: rt.Formatter) => rt.fmt.option(arg4, f, (value: Point, f: rt.Formatter) => value.fmt(f))])); } - let list: Wrap = new Wrap({ value: [], count: 1 }); - list.value.push(at(10)); + let list: Wrap = new Wrap({ value: [], count: 1 }); + list.value.push(at(10) as rt.u16); list.bump$Vec_u16(); const none = new Wrap({ value: undefined, count: 0 }); const some = Wrap.new$Option_u8(rt.slice.get(data, 11)); @@ -301,10 +301,10 @@ export function methods(data: Uint8Array, out: rt.ByteVec): void { const arg7 = out; const arg6 = rt.option.unwrapOr(none.get$Option_u8(), some.count); put(arg7, rt.fmt.format([(f: rt.Formatter) => list.fmt$Vec_u16(f), " ", (f: rt.Formatter) => none.fmt$Option_u8(f), " ", (f: rt.Formatter) => some.fmt$Option_u8(f), " ", (f: rt.Formatter) => err.fmt$Result_u8_Shape(f), " ", (f: rt.Formatter) => rt.u8.display(arg6, f)])); - const tagged = new Tagged({ key: { tag: "Right", value: 500 } as Either, inner: Wrap.new$i16(rt.i16.sub(at(8), 100)), all: rt.slice.toVec([Wrap.new$i16(-1), Wrap.new$i16(at(9))]) }); + const tagged = new Tagged({ key: { tag: "Right", value: 500 } as Either, inner: Wrap.new$i16(rt.i16.sub(at(8) as rt.i16, 100)), all: rt.slice.toVec([Wrap.new$i16(-1), Wrap.new$i16(at(9) as rt.i16)]) }); const arg9 = out; const arg8 = tagged.first(); - put(arg9, rt.fmt.format([(f: rt.Formatter) => tagged.fmt(f), " ", (f: rt.Formatter) => rt.fmt.option(arg8, f, (value: Wrap, f: rt.Formatter) => value.fmt$i16(f))])); + put(arg9, rt.fmt.format([(f: rt.Formatter) => tagged.fmt(f), " ", (f: rt.Formatter) => rt.fmt.option(arg8, f, (value: Wrap, f: rt.Formatter) => value.fmt$i16(f))])); out.extendFromSlice(rt.i16.toBeBytes(tagged.inner.value)); out.push(rt.option.unwrapOr(Either.left$u8_u16(tagged.key), 0)); } diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index 7a45c63d4..14d93f721 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -16,7 +16,7 @@ export function run(input: Uint8Array): rt.ByteVec { const op = input[0]; const kind = input[1]; const data = input.subarray(2, input.length - 0); - const [op2, kind2] = [op, kind] as [number, number]; + const [op2, kind2] = [op, kind] as [rt.u8, rt.u8]; const value = rt.u8.rem(op2, 33); if (value === 0) { u8Op(kind2, data, out); diff --git a/ts/fixtures/lang/expected/ints.ts b/ts/fixtures/lang/expected/ints.ts index 165a0c232..23fa18acd 100644 --- a/ts/fixtures/lang/expected/ints.ts +++ b/ts/fixtures/lang/expected/ints.ts @@ -3,27 +3,27 @@ import * as rt from "./rt.ts"; /** Apply operation `op` to operands taken from `data`. */ -export function u8Op(op: number, data: Uint8Array, out: rt.ByteVec): void { +export function u8Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, data3] = [rt.u8.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.u8, 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(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(v)); + const [b, data5] = [rt.u8.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.u8, Uint8Array]; + const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0) as rt.u32, rt.u32.mul(2, 8)); + const put = (out2: rt.ByteVec, v: rt.u8): void => { + out2.extendFromSlice(rt.u128.toBeBytes(v as rt.u128)); out2.push(rt.u8.fromBeBytes(rt.u8.toBeBytes(v)) === v ? 1 : 0); }; - const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { + const putOpt = (out3: rt.ByteVec, v2: rt.u8 | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.u128.toBeBytes(v3)); + out3.extendFromSlice(rt.u128.toBeBytes(v3 as rt.u128)); } else { out3.push(0); } @@ -79,7 +79,7 @@ export function u8Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(a >= b ? 1 : 0); out.push(rt.u8.wrappingSub(a, b) === 0 ? 1 : 0); } else { - let x: number = a; + let x: rt.u8 = a; x = rt.u8.add(x, b); x = rt.u8.sub(x, 1); x = rt.u8.mul(x, 2); @@ -95,27 +95,27 @@ export function u8Op(op: number, data: Uint8Array, out: rt.ByteVec): void { } /** Apply operation `op` to operands taken from `data`. */ -export function u16Op(op: number, data: Uint8Array, out: rt.ByteVec): void { +export function u16Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, data3] = [rt.u16.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.u16, 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(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(v)); + const [b, data5] = [rt.u16.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.u16, Uint8Array]; + const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0) as rt.u32, rt.u32.mul(2, 16)); + const put = (out2: rt.ByteVec, v: rt.u16): void => { + out2.extendFromSlice(rt.u128.toBeBytes(v as rt.u128)); out2.push(rt.u16.fromBeBytes(rt.u16.toBeBytes(v)) === v ? 1 : 0); }; - const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { + const putOpt = (out3: rt.ByteVec, v2: rt.u16 | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.u128.toBeBytes(v3)); + out3.extendFromSlice(rt.u128.toBeBytes(v3 as rt.u128)); } else { out3.push(0); } @@ -171,7 +171,7 @@ export function u16Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(a >= b ? 1 : 0); out.push(rt.u16.wrappingSub(a, b) === 0 ? 1 : 0); } else { - let x: number = a; + let x: rt.u16 = a; x = rt.u16.add(x, b); x = rt.u16.sub(x, 1); x = rt.u16.mul(x, 2); @@ -187,27 +187,27 @@ export function u16Op(op: number, data: Uint8Array, out: rt.ByteVec): void { } /** Apply operation `op` to operands taken from `data`. */ -export function u32Op(op: number, data: Uint8Array, out: rt.ByteVec): void { +export function u32Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, data3] = [rt.u32.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.u32, 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(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(v)); + const [b, data5] = [rt.u32.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.u32, Uint8Array]; + const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0) as rt.u32, rt.u32.mul(2, 32)); + const put = (out2: rt.ByteVec, v: rt.u32): void => { + out2.extendFromSlice(rt.u128.toBeBytes(v as rt.u128)); out2.push(rt.u32.fromBeBytes(rt.u32.toBeBytes(v)) === v ? 1 : 0); }; - const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { + const putOpt = (out3: rt.ByteVec, v2: rt.u32 | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.u128.toBeBytes(v3)); + out3.extendFromSlice(rt.u128.toBeBytes(v3 as rt.u128)); } else { out3.push(0); } @@ -263,7 +263,7 @@ export function u32Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(a >= b ? 1 : 0); out.push(rt.u32.wrappingSub(a, b) === 0 ? 1 : 0); } else { - let x: number = a; + let x: rt.u32 = a; x = rt.u32.add(x, b); x = rt.u32.sub(x, 1); x = rt.u32.mul(x, 2); @@ -279,23 +279,23 @@ export function u32Op(op: number, data: Uint8Array, out: rt.ByteVec): void { } /** Apply operation `op` to operands taken from `data`. */ -export function u64Op(op: number, data: Uint8Array, out: rt.ByteVec): void { +export function u64Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; 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(buf), rt.slice.range(data2, n)] as [rt.U64, Uint8Array]; + const [a, data3] = [rt.u64.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.u64, 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(buf2), rt.slice.range(data4, n2)] as [rt.U64, 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: rt.U64): void => { + const [b, data5] = [rt.u64.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.u64, Uint8Array]; + const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0) as rt.u32, rt.u32.mul(2, 64)); + const put = (out2: rt.ByteVec, v: rt.u64): void => { out2.extendFromSlice(rt.u128.toBeBytes(rt.u128.cast(v))); out2.push(rt.u64.eq(rt.u64.fromBeBytes(rt.u64.toBeBytes(v)), v) ? 1 : 0); }; - const putOpt = (out3: rt.ByteVec, v2: rt.U64 | undefined): void => { + const putOpt = (out3: rt.ByteVec, v2: rt.u64 | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); @@ -355,7 +355,7 @@ export function u64Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(rt.u64.ge(a, b) ? 1 : 0); out.push(rt.u64.wrappingSub(a, b) === 0 ? 1 : 0); } else { - let x: rt.U64 = a; + let x: rt.u64 = a; x = rt.u64.add(x, b); x = rt.u64.sub(x, 1); x = rt.u64.mul(x, 2); @@ -370,23 +370,23 @@ export function u64Op(op: number, data: Uint8Array, out: rt.ByteVec): void { } /** Apply operation `op` to operands taken from `data`. */ -export function u128Op(op: number, data: Uint8Array, out: rt.ByteVec): void { +export function u128Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; 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(buf), rt.slice.range(data2, n)] as [rt.U128, Uint8Array]; + const [a, data3] = [rt.u128.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.u128, 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(buf2), rt.slice.range(data4, n2)] as [rt.U128, 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: rt.U128): void => { + const [b, data5] = [rt.u128.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.u128, Uint8Array]; + const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0) as rt.u32, rt.u32.mul(2, 128)); + const put = (out2: rt.ByteVec, v: rt.u128): void => { out2.extendFromSlice(rt.u128.toBeBytes(v)); out2.push(rt.u128.eq(rt.u128.fromBeBytes(rt.u128.toBeBytes(v)), v) ? 1 : 0); }; - const putOpt = (out3: rt.ByteVec, v2: rt.U128 | undefined): void => { + const putOpt = (out3: rt.ByteVec, v2: rt.u128 | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); @@ -446,7 +446,7 @@ export function u128Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(rt.u128.ge(a, b) ? 1 : 0); out.push(rt.u128.wrappingSub(a, b) === 0 ? 1 : 0); } else { - let x: rt.U128 = a; + let x: rt.u128 = a; x = rt.u128.add(x, b); x = rt.u128.sub(x, 1); x = rt.u128.mul(x, 2); @@ -462,27 +462,27 @@ export function u128Op(op: number, data: Uint8Array, out: rt.ByteVec): void { } /** Apply operation `op` to operands taken from `data`. */ -export function usizeOp(op: number, data: Uint8Array, out: rt.ByteVec): void { +export function usizeOp(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, data3] = [rt.usize.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.usize, 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(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(v)); + const [b, data5] = [rt.usize.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.usize, Uint8Array]; + const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0) as rt.u32, rt.u32.mul(2, 32)); + const put = (out2: rt.ByteVec, v: rt.usize): void => { + out2.extendFromSlice(rt.u128.toBeBytes(v as rt.u128)); out2.push(rt.usize.fromBeBytes(rt.usize.toBeBytes(v)) === v ? 1 : 0); }; - const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { + const putOpt = (out3: rt.ByteVec, v2: rt.usize | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.u128.toBeBytes(v3)); + out3.extendFromSlice(rt.u128.toBeBytes(v3 as rt.u128)); } else { out3.push(0); } @@ -538,7 +538,7 @@ export function usizeOp(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(a >= b ? 1 : 0); out.push(rt.usize.wrappingSub(a, b) === 0 ? 1 : 0); } else { - let x: number = a; + let x: rt.usize = a; x = rt.usize.add(x, b); x = rt.usize.sub(x, 1); x = rt.usize.mul(x, 2); @@ -554,27 +554,27 @@ export function usizeOp(op: number, data: Uint8Array, out: rt.ByteVec): void { } /** Apply operation `op` to operands taken from `data`. */ -export function i8Op(op: number, data: Uint8Array, out: rt.ByteVec): void { +export function i8Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, data3] = [rt.i8.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i8, 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(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(v)); + const [b, data5] = [rt.i8.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.i8, Uint8Array]; + const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0) as rt.u32, rt.u32.mul(2, 8)); + const put = (out2: rt.ByteVec, v: rt.i8): void => { + out2.extendFromSlice(rt.i128.toBeBytes(v as rt.i128)); out2.push(rt.i8.fromBeBytes(rt.i8.toBeBytes(v)) === v ? 1 : 0); }; - const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { + const putOpt = (out3: rt.ByteVec, v2: rt.i8 | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.i128.toBeBytes(v3)); + out3.extendFromSlice(rt.i128.toBeBytes(v3 as rt.i128)); } else { out3.push(0); } @@ -630,7 +630,7 @@ export function i8Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(a >= b ? 1 : 0); out.push(rt.i8.wrappingSub(a, b) === 0 ? 1 : 0); } else { - let x: number = a; + let x: rt.i8 = a; x = rt.i8.add(x, b); x = rt.i8.sub(x, 1); x = rt.i8.mul(x, 2); @@ -646,27 +646,27 @@ export function i8Op(op: number, data: Uint8Array, out: rt.ByteVec): void { } /** Apply operation `op` to operands taken from `data`. */ -export function i16Op(op: number, data: Uint8Array, out: rt.ByteVec): void { +export function i16Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, data3] = [rt.i16.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i16, 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(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(v)); + const [b, data5] = [rt.i16.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.i16, Uint8Array]; + const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0) as rt.u32, rt.u32.mul(2, 16)); + const put = (out2: rt.ByteVec, v: rt.i16): void => { + out2.extendFromSlice(rt.i128.toBeBytes(v as rt.i128)); out2.push(rt.i16.fromBeBytes(rt.i16.toBeBytes(v)) === v ? 1 : 0); }; - const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { + const putOpt = (out3: rt.ByteVec, v2: rt.i16 | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.i128.toBeBytes(v3)); + out3.extendFromSlice(rt.i128.toBeBytes(v3 as rt.i128)); } else { out3.push(0); } @@ -722,7 +722,7 @@ export function i16Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(a >= b ? 1 : 0); out.push(rt.i16.wrappingSub(a, b) === 0 ? 1 : 0); } else { - let x: number = a; + let x: rt.i16 = a; x = rt.i16.add(x, b); x = rt.i16.sub(x, 1); x = rt.i16.mul(x, 2); @@ -738,27 +738,27 @@ export function i16Op(op: number, data: Uint8Array, out: rt.ByteVec): void { } /** Apply operation `op` to operands taken from `data`. */ -export function i32Op(op: number, data: Uint8Array, out: rt.ByteVec): void { +export function i32Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, data3] = [rt.i32.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i32, 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(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(v)); + const [b, data5] = [rt.i32.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.i32, Uint8Array]; + const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0) as rt.u32, rt.u32.mul(2, 32)); + const put = (out2: rt.ByteVec, v: rt.i32): void => { + out2.extendFromSlice(rt.i128.toBeBytes(v as rt.i128)); out2.push(rt.i32.fromBeBytes(rt.i32.toBeBytes(v)) === v ? 1 : 0); }; - const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { + const putOpt = (out3: rt.ByteVec, v2: rt.i32 | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.i128.toBeBytes(v3)); + out3.extendFromSlice(rt.i128.toBeBytes(v3 as rt.i128)); } else { out3.push(0); } @@ -814,7 +814,7 @@ export function i32Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(a >= b ? 1 : 0); out.push(rt.i32.wrappingSub(a, b) === 0 ? 1 : 0); } else { - let x: number = a; + let x: rt.i32 = a; x = rt.i32.add(x, b); x = rt.i32.sub(x, 1); x = rt.i32.mul(x, 2); @@ -830,23 +830,23 @@ export function i32Op(op: number, data: Uint8Array, out: rt.ByteVec): void { } /** Apply operation `op` to operands taken from `data`. */ -export function i64Op(op: number, data: Uint8Array, out: rt.ByteVec): void { +export function i64Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; 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(buf), rt.slice.range(data2, n)] as [rt.I64, Uint8Array]; + const [a, data3] = [rt.i64.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i64, 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(buf2), rt.slice.range(data4, n2)] as [rt.I64, 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: rt.I64): void => { + const [b, data5] = [rt.i64.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.i64, Uint8Array]; + const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0) as rt.u32, rt.u32.mul(2, 64)); + const put = (out2: rt.ByteVec, v: rt.i64): void => { out2.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(v))); out2.push(rt.i64.eq(rt.i64.fromBeBytes(rt.i64.toBeBytes(v)), v) ? 1 : 0); }; - const putOpt = (out3: rt.ByteVec, v2: rt.I64 | undefined): void => { + const putOpt = (out3: rt.ByteVec, v2: rt.i64 | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); @@ -906,7 +906,7 @@ export function i64Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(rt.i64.ge(a, b) ? 1 : 0); out.push(rt.i64.wrappingSub(a, b) === 0 ? 1 : 0); } else { - let x: rt.I64 = a; + let x: rt.i64 = a; x = rt.i64.add(x, b); x = rt.i64.sub(x, 1); x = rt.i64.mul(x, 2); @@ -921,23 +921,23 @@ export function i64Op(op: number, data: Uint8Array, out: rt.ByteVec): void { } /** Apply operation `op` to operands taken from `data`. */ -export function i128Op(op: number, data: Uint8Array, out: rt.ByteVec): void { +export function i128Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; 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(buf), rt.slice.range(data2, n)] as [rt.I128, Uint8Array]; + const [a, data3] = [rt.i128.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i128, 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(buf2), rt.slice.range(data4, n2)] as [rt.I128, 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: rt.I128): void => { + const [b, data5] = [rt.i128.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.i128, Uint8Array]; + const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0) as rt.u32, rt.u32.mul(2, 128)); + const put = (out2: rt.ByteVec, v: rt.i128): void => { out2.extendFromSlice(rt.i128.toBeBytes(v)); out2.push(rt.i128.eq(rt.i128.fromBeBytes(rt.i128.toBeBytes(v)), v) ? 1 : 0); }; - const putOpt = (out3: rt.ByteVec, v2: rt.I128 | undefined): void => { + const putOpt = (out3: rt.ByteVec, v2: rt.i128 | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); @@ -997,7 +997,7 @@ export function i128Op(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(rt.i128.ge(a, b) ? 1 : 0); out.push(rt.i128.wrappingSub(a, b) === 0 ? 1 : 0); } else { - let x: rt.I128 = a; + let x: rt.i128 = a; x = rt.i128.add(x, b); x = rt.i128.sub(x, 1); x = rt.i128.mul(x, 2); @@ -1013,27 +1013,27 @@ export function i128Op(op: number, data: Uint8Array, out: rt.ByteVec): void { } /** Apply operation `op` to operands taken from `data`. */ -export function isizeOp(op: number, data: Uint8Array, out: rt.ByteVec): void { +export function isizeOp(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; + const [a, data3] = [rt.isize.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.isize, 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(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(v)); + const [b, data5] = [rt.isize.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.isize, Uint8Array]; + const shift = rt.u32.rem(rt.option.unwrapOr(rt.slice.first(data5), 0) as rt.u32, rt.u32.mul(2, 32)); + const put = (out2: rt.ByteVec, v: rt.isize): void => { + out2.extendFromSlice(rt.i128.toBeBytes(v as rt.i128)); out2.push(rt.isize.fromBeBytes(rt.isize.toBeBytes(v)) === v ? 1 : 0); }; - const putOpt = (out3: rt.ByteVec, v2: number | undefined): void => { + const putOpt = (out3: rt.ByteVec, v2: rt.isize | undefined): void => { if (v2 !== undefined) { const v3 = v2; out3.push(1); - out3.extendFromSlice(rt.i128.toBeBytes(v3)); + out3.extendFromSlice(rt.i128.toBeBytes(v3 as rt.i128)); } else { out3.push(0); } @@ -1089,7 +1089,7 @@ export function isizeOp(op: number, data: Uint8Array, out: rt.ByteVec): void { out.push(a >= b ? 1 : 0); out.push(rt.isize.wrappingSub(a, b) === 0 ? 1 : 0); } else { - let x: number = a; + let x: rt.isize = a; x = rt.isize.add(x, b); x = rt.isize.sub(x, 1); x = rt.isize.mul(x, 2); @@ -1110,8 +1110,8 @@ 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; - out.extendFromSlice(rt.i128.toBeBytes(rt.i8.neg(a))); + const [a, ] = [rt.i8.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i8, Uint8Array]; + out.extendFromSlice(rt.i128.toBeBytes(rt.i8.neg(a) as rt.i128)); } /** Negate, which overflows at the minimum. */ @@ -1120,8 +1120,8 @@ 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; - out.extendFromSlice(rt.i128.toBeBytes(rt.i16.neg(a))); + const [a, ] = [rt.i16.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i16, Uint8Array]; + out.extendFromSlice(rt.i128.toBeBytes(rt.i16.neg(a) as rt.i128)); } /** Negate, which overflows at the minimum. */ @@ -1130,8 +1130,8 @@ 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; - out.extendFromSlice(rt.i128.toBeBytes(rt.i32.neg(a))); + const [a, ] = [rt.i32.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i32, Uint8Array]; + out.extendFromSlice(rt.i128.toBeBytes(rt.i32.neg(a) as rt.i128)); } /** Negate, which overflows at the minimum. */ @@ -1140,7 +1140,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(buf), rt.slice.range(data2, n)] as [rt.I64, Uint8Array]; + const [a, ] = [rt.i64.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i64, Uint8Array]; out.extendFromSlice(rt.i128.toBeBytes(rt.i128.cast(rt.i64.neg(a)))); } @@ -1150,7 +1150,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(buf), rt.slice.range(data2, n)] as [rt.I128, Uint8Array]; + const [a, ] = [rt.i128.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i128, Uint8Array]; out.extendFromSlice(rt.i128.toBeBytes(rt.i128.neg(a))); } @@ -1160,19 +1160,19 @@ 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array]; - out.extendFromSlice(rt.i128.toBeBytes(rt.isize.neg(a))); + const [a, ] = [rt.isize.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.isize, Uint8Array]; + out.extendFromSlice(rt.i128.toBeBytes(rt.isize.neg(a) as rt.i128)); } /** Cast a value of type `from` to type `to`, both indexes into the twelve types. */ -export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec): void { +export function cast(from: rt.u8, to: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const value = rt.u8.rem(from, 12); if (value === 0) { const data2 = data; 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(buf), rt.slice.range(data2, n)] as [number, Uint8Array])[0]; + const v = ([rt.u8.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.u8, Uint8Array])[0]; const out2 = out; const value2 = rt.u8.rem(to, 12); if (value2 === 0) { @@ -1180,23 +1180,23 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec out2.extendFromSlice(rt.u8.toBeBytes(v2)); out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u8.display(v2, f)))); } else if (value2 === 1) { - const v3 = v; + const v3 = v as rt.u16; out2.extendFromSlice(rt.u16.toBeBytes(v3)); out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u16.display(v3, f)))); } else if (value2 === 2) { - const v4 = v; + const v4 = v as rt.u32; out2.extendFromSlice(rt.u32.toBeBytes(v4)); out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u32.display(v4, f)))); } else if (value2 === 3) { - const v5 = v; + const v5 = v as rt.u64; out2.extendFromSlice(rt.u64.toBeBytes(v5)); out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u64.display(v5, f)))); } else if (value2 === 4) { - const v6 = v; + const v6 = v as rt.u128; out2.extendFromSlice(rt.u128.toBeBytes(v6)); out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u128.display(v6, f)))); } else if (value2 === 5) { - const v7 = v; + const v7 = v as rt.usize; out2.extendFromSlice(rt.usize.toBeBytes(v7)); out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.usize.display(v7, f)))); } else if (value2 === 6) { @@ -1204,23 +1204,23 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec out2.extendFromSlice(rt.i8.toBeBytes(v8)); out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i8.display(v8, f)))); } else if (value2 === 7) { - const v9 = v; + const v9 = v as rt.i16; out2.extendFromSlice(rt.i16.toBeBytes(v9)); out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i16.display(v9, f)))); } else if (value2 === 8) { - const v10 = v; + const v10 = v as rt.i32; out2.extendFromSlice(rt.i32.toBeBytes(v10)); out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v10, f)))); } else if (value2 === 9) { - const v11 = v; + const v11 = v as rt.i64; out2.extendFromSlice(rt.i64.toBeBytes(v11)); out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v11, f)))); } else if (value2 === 10) { - const v12 = v; + const v12 = v as rt.i128; out2.extendFromSlice(rt.i128.toBeBytes(v12)); out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v12, f)))); } else { - const v13 = v; + const v13 = v as rt.isize; out2.extendFromSlice(rt.isize.toBeBytes(v13)); out2.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.isize.display(v13, f)))); } @@ -1229,7 +1229,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 v14 = ([rt.u16.fromBeBytes(buf2), rt.slice.range(data3, n2)] as [number, Uint8Array])[0]; + const v14 = ([rt.u16.fromBeBytes(buf2), rt.slice.range(data3, n2)] as [rt.u16, Uint8Array])[0]; const out3 = out; const value3 = rt.u8.rem(to, 12); if (value3 === 0) { @@ -1241,19 +1241,19 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec out3.extendFromSlice(rt.u16.toBeBytes(v16)); out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u16.display(v16, f)))); } else if (value3 === 2) { - const v17 = v14; + const v17 = v14 as rt.u32; out3.extendFromSlice(rt.u32.toBeBytes(v17)); out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u32.display(v17, f)))); } else if (value3 === 3) { - const v18 = v14; + const v18 = v14 as rt.u64; out3.extendFromSlice(rt.u64.toBeBytes(v18)); out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u64.display(v18, f)))); } else if (value3 === 4) { - const v19 = v14; + const v19 = v14 as rt.u128; out3.extendFromSlice(rt.u128.toBeBytes(v19)); out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u128.display(v19, f)))); } else if (value3 === 5) { - const v20 = v14; + const v20 = v14 as rt.usize; out3.extendFromSlice(rt.usize.toBeBytes(v20)); out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.usize.display(v20, f)))); } else if (value3 === 6) { @@ -1265,19 +1265,19 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec out3.extendFromSlice(rt.i16.toBeBytes(v22)); out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i16.display(v22, f)))); } else if (value3 === 8) { - const v23 = v14; + const v23 = v14 as rt.i32; out3.extendFromSlice(rt.i32.toBeBytes(v23)); out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v23, f)))); } else if (value3 === 9) { - const v24 = v14; + const v24 = v14 as rt.i64; out3.extendFromSlice(rt.i64.toBeBytes(v24)); out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v24, f)))); } else if (value3 === 10) { - const v25 = v14; + const v25 = v14 as rt.i128; out3.extendFromSlice(rt.i128.toBeBytes(v25)); out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v25, f)))); } else { - const v26 = v14; + const v26 = v14 as rt.isize; out3.extendFromSlice(rt.isize.toBeBytes(v26)); out3.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.isize.display(v26, f)))); } @@ -1286,7 +1286,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 v27 = ([rt.u32.fromBeBytes(buf3), rt.slice.range(data4, n3)] as [number, Uint8Array])[0]; + const v27 = ([rt.u32.fromBeBytes(buf3), rt.slice.range(data4, n3)] as [rt.u32, Uint8Array])[0]; const out4 = out; const value4 = rt.u8.rem(to, 12); if (value4 === 0) { @@ -1302,15 +1302,15 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec out4.extendFromSlice(rt.u32.toBeBytes(v30)); out4.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u32.display(v30, f)))); } else if (value4 === 3) { - const v31 = v27; + const v31 = v27 as rt.u64; out4.extendFromSlice(rt.u64.toBeBytes(v31)); out4.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u64.display(v31, f)))); } else if (value4 === 4) { - const v32 = v27; + const v32 = v27 as rt.u128; out4.extendFromSlice(rt.u128.toBeBytes(v32)); out4.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u128.display(v32, f)))); } else if (value4 === 5) { - const v33 = v27; + const v33 = v27 as rt.usize; out4.extendFromSlice(rt.usize.toBeBytes(v33)); out4.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.usize.display(v33, f)))); } else if (value4 === 6) { @@ -1326,11 +1326,11 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec out4.extendFromSlice(rt.i32.toBeBytes(v36)); out4.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v36, f)))); } else if (value4 === 9) { - const v37 = v27; + const v37 = v27 as rt.i64; out4.extendFromSlice(rt.i64.toBeBytes(v37)); out4.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v37, f)))); } else if (value4 === 10) { - const v38 = v27; + const v38 = v27 as rt.i128; out4.extendFromSlice(rt.i128.toBeBytes(v38)); out4.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v38, f)))); } else { @@ -1343,7 +1343,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 v40 = ([rt.u64.fromBeBytes(buf4), rt.slice.range(data5, n4)] as [rt.U64, Uint8Array])[0]; + const v40 = ([rt.u64.fromBeBytes(buf4), rt.slice.range(data5, n4)] as [rt.u64, Uint8Array])[0]; const out5 = out; const value5 = rt.u8.rem(to, 12); if (value5 === 0) { @@ -1400,7 +1400,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 v53 = ([rt.u128.fromBeBytes(buf5), rt.slice.range(data6, n5)] as [rt.U128, Uint8Array])[0]; + const v53 = ([rt.u128.fromBeBytes(buf5), rt.slice.range(data6, n5)] as [rt.u128, Uint8Array])[0]; const out6 = out; const value6 = rt.u8.rem(to, 12); if (value6 === 0) { @@ -1457,7 +1457,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 v66 = ([rt.usize.fromBeBytes(buf6), rt.slice.range(data7, n6)] as [number, Uint8Array])[0]; + const v66 = ([rt.usize.fromBeBytes(buf6), rt.slice.range(data7, n6)] as [rt.usize, Uint8Array])[0]; const out7 = out; const value7 = rt.u8.rem(to, 12); if (value7 === 0) { @@ -1469,15 +1469,15 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec out7.extendFromSlice(rt.u16.toBeBytes(v68)); out7.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u16.display(v68, f)))); } else if (value7 === 2) { - const v69 = v66; + const v69 = v66 as rt.u32; out7.extendFromSlice(rt.u32.toBeBytes(v69)); out7.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u32.display(v69, f)))); } else if (value7 === 3) { - const v70 = v66; + const v70 = v66 as rt.u64; out7.extendFromSlice(rt.u64.toBeBytes(v70)); out7.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u64.display(v70, f)))); } else if (value7 === 4) { - const v71 = v66; + const v71 = v66 as rt.u128; out7.extendFromSlice(rt.u128.toBeBytes(v71)); out7.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.u128.display(v71, f)))); } else if (value7 === 5) { @@ -1497,11 +1497,11 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec out7.extendFromSlice(rt.i32.toBeBytes(v75)); out7.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v75, f)))); } else if (value7 === 9) { - const v76 = v66; + const v76 = v66 as rt.i64; out7.extendFromSlice(rt.i64.toBeBytes(v76)); out7.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v76, f)))); } else if (value7 === 10) { - const v77 = v66; + const v77 = v66 as rt.i128; out7.extendFromSlice(rt.i128.toBeBytes(v77)); out7.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v77, f)))); } else { @@ -1514,7 +1514,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 v79 = ([rt.i8.fromBeBytes(buf7), rt.slice.range(data8, n7)] as [number, Uint8Array])[0]; + const v79 = ([rt.i8.fromBeBytes(buf7), rt.slice.range(data8, n7)] as [rt.i8, Uint8Array])[0]; const out8 = out; const value8 = rt.u8.rem(to, 12); if (value8 === 0) { @@ -1546,23 +1546,23 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec out8.extendFromSlice(rt.i8.toBeBytes(v86)); out8.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i8.display(v86, f)))); } else if (value8 === 7) { - const v87 = v79; + const v87 = v79 as rt.i16; out8.extendFromSlice(rt.i16.toBeBytes(v87)); out8.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i16.display(v87, f)))); } else if (value8 === 8) { - const v88 = v79; + const v88 = v79 as rt.i32; out8.extendFromSlice(rt.i32.toBeBytes(v88)); out8.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v88, f)))); } else if (value8 === 9) { - const v89 = v79; + const v89 = v79 as rt.i64; out8.extendFromSlice(rt.i64.toBeBytes(v89)); out8.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v89, f)))); } else if (value8 === 10) { - const v90 = v79; + const v90 = v79 as rt.i128; out8.extendFromSlice(rt.i128.toBeBytes(v90)); out8.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v90, f)))); } else { - const v91 = v79; + const v91 = v79 as rt.isize; out8.extendFromSlice(rt.isize.toBeBytes(v91)); out8.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.isize.display(v91, f)))); } @@ -1571,7 +1571,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 v92 = ([rt.i16.fromBeBytes(buf8), rt.slice.range(data9, n8)] as [number, Uint8Array])[0]; + const v92 = ([rt.i16.fromBeBytes(buf8), rt.slice.range(data9, n8)] as [rt.i16, Uint8Array])[0]; const out9 = out; const value9 = rt.u8.rem(to, 12); if (value9 === 0) { @@ -1607,19 +1607,19 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec out9.extendFromSlice(rt.i16.toBeBytes(v100)); out9.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i16.display(v100, f)))); } else if (value9 === 8) { - const v101 = v92; + const v101 = v92 as rt.i32; out9.extendFromSlice(rt.i32.toBeBytes(v101)); out9.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v101, f)))); } else if (value9 === 9) { - const v102 = v92; + const v102 = v92 as rt.i64; out9.extendFromSlice(rt.i64.toBeBytes(v102)); out9.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v102, f)))); } else if (value9 === 10) { - const v103 = v92; + const v103 = v92 as rt.i128; out9.extendFromSlice(rt.i128.toBeBytes(v103)); out9.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v103, f)))); } else { - const v104 = v92; + const v104 = v92 as rt.isize; out9.extendFromSlice(rt.isize.toBeBytes(v104)); out9.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.isize.display(v104, f)))); } @@ -1628,7 +1628,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 v105 = ([rt.i32.fromBeBytes(buf9), rt.slice.range(data10, n9)] as [number, Uint8Array])[0]; + const v105 = ([rt.i32.fromBeBytes(buf9), rt.slice.range(data10, n9)] as [rt.i32, Uint8Array])[0]; const out10 = out; const value10 = rt.u8.rem(to, 12); if (value10 === 0) { @@ -1668,15 +1668,15 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec out10.extendFromSlice(rt.i32.toBeBytes(v114)); out10.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v114, f)))); } else if (value10 === 9) { - const v115 = v105; + const v115 = v105 as rt.i64; out10.extendFromSlice(rt.i64.toBeBytes(v115)); out10.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v115, f)))); } else if (value10 === 10) { - const v116 = v105; + const v116 = v105 as rt.i128; out10.extendFromSlice(rt.i128.toBeBytes(v116)); out10.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v116, f)))); } else { - const v117 = v105; + const v117 = v105 as rt.isize; out10.extendFromSlice(rt.isize.toBeBytes(v117)); out10.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.isize.display(v117, f)))); } @@ -1685,7 +1685,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 v118 = ([rt.i64.fromBeBytes(buf10), rt.slice.range(data11, n10)] as [rt.I64, Uint8Array])[0]; + const v118 = ([rt.i64.fromBeBytes(buf10), rt.slice.range(data11, n10)] as [rt.i64, Uint8Array])[0]; const out11 = out; const value11 = rt.u8.rem(to, 12); if (value11 === 0) { @@ -1742,7 +1742,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 v131 = ([rt.i128.fromBeBytes(buf11), rt.slice.range(data12, n11)] as [rt.I128, Uint8Array])[0]; + const v131 = ([rt.i128.fromBeBytes(buf11), rt.slice.range(data12, n11)] as [rt.i128, Uint8Array])[0]; const out12 = out; const value12 = rt.u8.rem(to, 12); if (value12 === 0) { @@ -1799,7 +1799,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 v144 = ([rt.isize.fromBeBytes(buf12), rt.slice.range(data13, n12)] as [number, Uint8Array])[0]; + const v144 = ([rt.isize.fromBeBytes(buf12), rt.slice.range(data13, n12)] as [rt.isize, Uint8Array])[0]; const out13 = out; const value13 = rt.u8.rem(to, 12); if (value13 === 0) { @@ -1835,15 +1835,15 @@ export function cast(from: number, to: number, data: Uint8Array, out: rt.ByteVec out13.extendFromSlice(rt.i16.toBeBytes(v152)); out13.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i16.display(v152, f)))); } else if (value13 === 8) { - const v153 = v144; + const v153 = v144 as rt.i32; out13.extendFromSlice(rt.i32.toBeBytes(v153)); out13.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v153, f)))); } else if (value13 === 9) { - const v154 = v144; + const v154 = v144 as rt.i64; out13.extendFromSlice(rt.i64.toBeBytes(v154)); out13.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v154, f)))); } else if (value13 === 10) { - const v155 = v144; + const v155 = v144 as rt.i128; out13.extendFromSlice(rt.i128.toBeBytes(v155)); out13.extendFromSlice(rt.string.asBytes(rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v155, f)))); } else { @@ -1860,12 +1860,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(buf), rt.slice.range(data2, n)] as [rt.U64, Uint8Array]; + const [wide, data3] = [rt.u64.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.u64, 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(buf2), rt.slice.range(data4, n2)] as [number, Uint8Array]; + const [signed, ] = [rt.i32.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.i32, Uint8Array]; out.push(rt.u8.tryFrom(wide).ok ? 1 : 0); const tryFrom = rt.u32.tryFrom(wide); if (tryFrom.ok) { @@ -1888,8 +1888,8 @@ export function convert(data: Uint8Array, out: rt.ByteVec): void { } else { out.push(0xfd); } - const widened = rt.u64.low(wide) & 0xffff; - const into = signed; + const widened = (rt.u64.low(wide) & 0xffff) as rt.u64; + const into = signed as rt.i64; out.extendFromSlice(rt.u64.toBeBytes(widened)); out.extendFromSlice(rt.i64.toBeBytes(into)); out.push(rt.u8.add(true ? 1 : 0, false ? 1 : 0)); diff --git a/ts/fixtures/lang/expected/iters.ts b/ts/fixtures/lang/expected/iters.ts index 64e644b47..1510a6018 100644 --- a/ts/fixtures/lang/expected/iters.ts +++ b/ts/fixtures/lang/expected/iters.ts @@ -3,19 +3,19 @@ import * as rt from "./rt.ts"; /** Adaptor chains over the input bytes, each consumed one way. */ -export function adaptors(kind: number, data: Uint8Array, out: rt.ByteVec): void { +export function adaptors(kind: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const value = rt.u8.rem(kind, 8); if (value === 0) { - const doubled = rt.iter.slice(data).map((arg: number): number => { + const doubled = rt.iter.slice(data).map((arg: rt.u8): rt.u8 => { const b = arg; return rt.u8.div(b, 2); - }).filter((b2: number): boolean => { + }).filter((b2: rt.u8): boolean => { return rt.u8.rem(b2, 3) !== 0; }).toByteVec(); out.extendFromSlice(doubled.view()); - const small = rt.iter.slice(data).filterMap((arg2: number): number | undefined => { + const small = rt.iter.slice(data).filterMap((arg2: rt.u8): rt.u16 | undefined => { const b3 = arg2; - return rt.thenSome(b3 < 100, rt.u16.mul(b3, 200)); + return rt.thenSome(b3 < 100, rt.u16.mul(b3 as rt.u16, 200)); }).toArray(); for (const v of small) { out.extendFromSlice(rt.u16.toBeBytes(v)); @@ -44,7 +44,7 @@ export function adaptors(kind: number, data: Uint8Array, out: rt.ByteVec): void } } else if (value === 2) { const [front, back] = rt.slice.splitAt(data, rt.usize.div(data.length, 2)); - const zipped = rt.iter.slice(front).zip(rt.iter.slice(back).rev()).map((arg3: [number, number]): number => { + const zipped = rt.iter.slice(front).zip(rt.iter.slice(back).rev()).map((arg3: [rt.u8, rt.u8]): rt.u8 => { const a = arg3[0]; const b6 = arg3[1]; return a ^ b6; @@ -53,15 +53,15 @@ export function adaptors(kind: number, data: Uint8Array, out: rt.ByteVec): void const chained = rt.iter.slice(front).chain(rt.iter.slice(back)).rev().toByteVec(); out.extendFromSlice(chained.view()); } else if (value === 3) { - const taken = rt.iter.slice(data).takeWhile((arg4: number): boolean => { + const taken = rt.iter.slice(data).takeWhile((arg4: rt.u8): boolean => { const b7 = arg4; return b7 !== 0; }).toByteVec(); - const skipped = rt.iter.slice(data).skipWhile((arg5: number): boolean => { + const skipped = rt.iter.slice(data).skipWhile((arg5: rt.u8): boolean => { const b8 = arg5; return b8 < 0x80; }).toByteVec(); - const mapped = rt.iter.slice(data).mapWhile((arg6: number): number | undefined => { + const mapped = rt.iter.slice(data).mapWhile((arg6: rt.u8): rt.u8 | undefined => { const b9 = arg6; return rt.u8.checkedAdd(b9, 0x40); }).toByteVec(); @@ -70,25 +70,25 @@ export function adaptors(kind: number, data: Uint8Array, out: rt.ByteVec): void out.extendFromSlice(mapped.view()); } else if (value === 4) { const words = chunksByZero(data); - const flat = rt.iter.slice(words).flatMap((w: rt.ByteVec): rt.Iter => { + const flat = rt.iter.slice(words).flatMap((w: rt.ByteVec): rt.Iter => { return rt.iter.slice(w.view()).rev(); }).toByteVec(); out.extendFromSlice(flat.view()); - const nested = rt.iter.slice(words).map((w2: rt.ByteVec): rt.Iter => { - return rt.iter.slice(w2.view()).map((arg7: number): number => { + const nested = rt.iter.slice(words).map((w2: rt.ByteVec): rt.Iter => { + return rt.iter.slice(w2.view()).map((arg7: rt.u8): rt.u8 => { const b10 = arg7; return rt.u8.wrappingAdd(b10, 3); }); }).flatten(); rt.iter.extend(out, nested.rev()); } else if (value === 5) { - let it: rt.Peekable = rt.iter.slice(data).peekable(); + let it: rt.Peekable = rt.iter.slice(data).peekable(); while (true) { const next = rt.iter.toOption(it.next()); if (next !== undefined) { const b11 = next; - let run: number = 1; - while (it.nextIf((arg8: number): boolean => { + let run: rt.u8 = 1; + while (it.nextIf((arg8: rt.u8): boolean => { const next2 = arg8; return next2 === b11; }) !== undefined) { @@ -106,7 +106,7 @@ export function adaptors(kind: number, data: Uint8Array, out: rt.ByteVec): void } } } else if (value === 6) { - let it2: rt.Iter = rt.iter.slice(data); + let it2: rt.Iter = rt.iter.slice(data); const firsts = it2.take(2).toByteVec(); out.extendFromSlice(firsts.view()); out.push(it2.len() & 0xff); @@ -120,7 +120,7 @@ export function adaptors(kind: number, data: Uint8Array, out: rt.ByteVec): void const rest = it2.rev().stepBy(3).toByteVec(); out.extendFromSlice(rest.view()); } else { - const pairs = rt.iter.slice(data).map((arg9: number): Uint8Array => { + const pairs = rt.iter.slice(data).map((arg9: rt.u8): Uint8Array => { const b12 = arg9; return Uint8Array.of(b12, ~b12 & 0xff); }).toArray(); @@ -137,13 +137,13 @@ export function adaptors(kind: number, data: Uint8Array, out: rt.ByteVec): void } out.extendFromSlice(pair); } - let v2: number[] = []; - rt.iter.extend(v2, rt.iter.slice(data).map((arg10: number): number => { + let v2: rt.u16[] = []; + rt.iter.extend(v2, rt.iter.slice(data).map((arg10: rt.u8): rt.u16 => { const b13 = arg10; - return rt.u16.add(b13, 1); + return rt.u16.add(b13 as rt.u16, 1); })); out.push(v2.length & 0xff); - rt.iter.extend(out, rt.iter.slice(data).filter((arg11: number): boolean => { + rt.iter.extend(out, rt.iter.slice(data).filter((arg11: rt.u8): boolean => { const b14 = arg11; return b14 > 0x80; })); @@ -170,38 +170,38 @@ export function chunksByZero(self: Uint8Array): rt.ByteVec[] { } /** Consumers, and how far each reads: `log` records every item a closure sees. */ -export function consumers(kind: number, data: Uint8Array, out: rt.ByteVec): void { +export function consumers(kind: rt.u8, data: Uint8Array, out: rt.ByteVec): void { let log: rt.ByteVec = new rt.ByteVec(); const value = rt.u8.rem(kind, 6); if (value === 0) { - const any = rt.iter.slice(data).inspect((arg: number): void => { + const any = rt.iter.slice(data).inspect((arg: rt.u8): void => { const b = arg; log.push(b); - }).any((arg2: number): boolean => { + }).any((arg2: rt.u8): boolean => { const b2 = arg2; return b2 > 0xf0; }); out.push(any ? 1 : 0); - const all = rt.iter.slice(data).all((arg3: number): boolean => { + const all = rt.iter.slice(data).all((arg3: rt.u8): boolean => { const b3 = arg3; return b3 !== 0; }); out.push(all ? 1 : 0); out.push(rt.iter.slice(data).count() & 0xff); - out.push(rt.iter.slice(data).filter((arg4: number): boolean => { + out.push(rt.iter.slice(data).filter((arg4: rt.u8): boolean => { const b4 = arg4; return rt.u8.rem(b4, 2) === 0; }).count() & 0xff); } else if (value === 1) { - const found = rt.iter.slice(data).inspect((arg5: number): void => { + const found = rt.iter.slice(data).inspect((arg5: rt.u8): void => { const b5 = arg5; log.push(b5); - }).find((arg6: number): boolean => { + }).find((arg6: rt.u8): boolean => { const b6 = arg6; return rt.u8.rem(b6, 7) === 3; }); out.push(rt.option.unwrapOr(found, 0xff)); - const mapped = rt.iter.slice(data).findMap((arg7: number): number | undefined => { + const mapped = rt.iter.slice(data).findMap((arg7: rt.u8): rt.u8 | undefined => { const b7 = arg7; if (b7 > 0x50) { return rt.u8.checkedMul(b7, 3); @@ -210,24 +210,24 @@ export function consumers(kind: number, data: Uint8Array, out: rt.ByteVec): void } }); out.push(rt.option.unwrapOr(mapped, 0)); - const position = rt.iter.slice(data).position((arg8: number): boolean => { + const position = rt.iter.slice(data).position((arg8: rt.u8): boolean => { const b8 = arg8; return b8 === 0x40; }); - out.push(rt.option.mapOr(position, 0xff, (p: number): number => { + out.push(rt.option.mapOr(position, 0xff, (p: rt.usize): rt.u8 => { return p & 0xff; })); const short = rt.slice.range(data, rt.usize.div(data.length, 2)); - const logged = (b9: number): number => { + const logged = (b9: rt.u8): rt.u8 => { return b9; }; - const pairs = rt.iter.slice(data).map((arg9: number): number => { + const pairs = rt.iter.slice(data).map((arg9: rt.u8): rt.u8 => { const b10 = arg9; log.push(b10); return b10; }).zip(rt.iter.slice(short), true).count(); out.push(pairs & 0xff); - const walked = rt.iter.slice(data).map((arg10: number): number => { + const walked = rt.iter.slice(data).map((arg10: rt.u8): rt.u8 => { const b11 = arg10; log.push(~b11 & 0xff); return b11; @@ -242,7 +242,7 @@ export function consumers(kind: number, data: Uint8Array, out: rt.ByteVec): void const b12 = item[1]; out.push(a ^ b12); } - const iter2 = rt.iter.slice(data).inspect((arg11: number): void => { + const iter2 = rt.iter.slice(data).inspect((arg11: rt.u8): void => { const b13 = arg11; log.push(b13); }).map(logged).zip(rt.iter.slice(short)); @@ -255,7 +255,7 @@ export function consumers(kind: number, data: Uint8Array, out: rt.ByteVec): void const b14 = item2[1]; out.push(a2 ^ b14); } - let back: rt.Iter<[number, number]> = rt.iter.slice(data).map((arg12: number): number => { + let back: rt.Iter<[rt.u8, rt.u8]> = rt.iter.slice(data).map((arg12: rt.u8): rt.u8 => { const b15 = arg12; return rotateFree(b15); }).zip(rt.iter.slice(short), true); @@ -270,53 +270,53 @@ export function consumers(kind: number, data: Uint8Array, out: rt.ByteVec): void } else if (value === 2) { out.push(rt.option.unwrapOr(rt.iter.slice(data).max(), 0)); out.push(rt.option.unwrapOr(rt.iter.slice(data).min(), 0xff)); - out.push(rt.option.unwrapOr(rt.iter.slice(data).maxByKey((arg13: number): number => { + out.push(rt.option.unwrapOr(rt.iter.slice(data).maxByKey((arg13: rt.u8): rt.u8 => { const b17 = arg13; return rt.u8.rem(b17, 16); }), 0xee)); - out.push(rt.option.unwrapOr(rt.iter.slice(data).minByKey((arg14: number): number => { + out.push(rt.option.unwrapOr(rt.iter.slice(data).minByKey((arg14: rt.u8): rt.u8 => { const b18 = arg14; return rt.u8.div(b18, 16); }), 0xee)); out.push(rt.option.unwrapOr(rt.iter.slice(data).last(), 0xdd)); out.push(rt.option.unwrapOr(rt.iter.slice(data).nth(3), 0xcc)); - let it: rt.Iter = rt.iter.slice(data); + let it: rt.Iter = rt.iter.slice(data); out.push(rt.option.unwrapOr(rt.iter.toOption(it.take(rt.usize.rem(data.length, 3)).nthBackItem(1)), 0xbb)); out.push(it.len() & 0xff); out.push(rt.option.unwrapOr(it.take(rt.usize.rem(data.length, 5)).nth(2), 0xbb)); out.push(it.len() & 0xff); } else if (value === 3) { - const wide = rt.iter.slice(data).map((arg15: number): rt.U64 => { + const wide = rt.iter.slice(data).map((arg15: rt.u8): rt.u64 => { const b19 = arg15; - return rt.u64.mul(b19, 0x0101_0101_0101); + return rt.u64.mul(b19 as rt.u64, 0x0101_0101_0101); }).sum(rt.u64.add, 0); out.extendFromSlice(rt.u64.toBeBytes(wide)); - const folded = rt.iter.slice(data).fold(0, (acc: number, arg16: number): number => { + const folded = rt.iter.slice(data).fold(0, (acc: rt.u32, arg16: rt.u8): rt.u32 => { const b20 = arg16; - return rt.u32.wrappingAdd((acc << 3) >>> 0, (b20 ^ acc) >>> 0); + return rt.u32.wrappingAdd((acc << 3) >>> 0, (b20 as rt.u32 ^ acc) >>> 0); }); out.extendFromSlice(rt.u32.toBeBytes(folded)); - rt.iter.slice(data).forEach((arg17: number): void => { + rt.iter.slice(data).forEach((arg17: rt.u8): void => { const b21 = arg17; log.push(rotateFree(b21)); }); } else if (value === 4) { const total = rt.iter.slice(data).sum(rt.u8.add, 0); out.push(total); - const product = rt.iter.slice(data).map((arg18: number): number => { + const product = rt.iter.slice(data).map((arg18: rt.u8): rt.u16 => { const b22 = arg18; - return rt.u16.add(rt.u16.rem(b22, 4), 1); + return rt.u16.add(rt.u16.rem(b22 as rt.u16, 4), 1); }).product(rt.u16.mul, 1); out.extendFromSlice(rt.u16.toBeBytes(product)); } else { - const parsed = rt.iter.slice(data).map((arg19: number): rt.Result => { + const parsed = rt.iter.slice(data).map((arg19: rt.u8): rt.Result => { const b23 = arg19; if (b23 < 0xf0) { return rt.Ok(b23); } else { return rt.Err(b23); } - }).collectResult((values__: rt.Iter) => values__.toByteVec()); + }).collectResult((values__: rt.Iter) => values__.toByteVec()); if (parsed.ok) { const v = parsed.value; out.extendFromSlice(v.view()); @@ -324,21 +324,21 @@ export function consumers(kind: number, data: Uint8Array, out: rt.ByteVec): void const e = parsed.error; out.push(e); } - const some = rt.iter.slice(data).inspect((arg20: number): void => { + const some = rt.iter.slice(data).inspect((arg20: rt.u8): void => { const b24 = arg20; log.push(b24); - }).map((arg21: number): number | undefined => { + }).map((arg21: rt.u8): rt.u16 | undefined => { const b25 = arg21; - return rt.thenSome(b25 !== 0x20, b25); - }).collectOption((values__: rt.Iter) => values__.toArray()); - out.push(rt.option.mapOr(some, 0xff, (v2: number[]): number => { + return rt.thenSome(b25 !== 0x20, b25 as rt.u16); + }).collectOption((values__: rt.Iter) => values__.toArray()); + out.push(rt.option.mapOr(some, 0xff, (v2: rt.u16[]): rt.u8 => { return v2.length & 0xff; })); } out.extendFromSlice(log.view()); } -export function rotateFree(self: number): number { +export function rotateFree(self: rt.u8): rt.u8 { return ((self << 1) & 0xff) | (self >> 7); } @@ -351,7 +351,7 @@ export function ranges(data: Uint8Array, out: rt.ByteVec): void { out.push(range.end); out.push(range.isEmpty() ? 1 : 0); out.push(range.contains(0x40) ? 1 : 0); - let copy: rt.Range = range.clone(); + let copy: rt.Range = range.clone(); out.push(rt.option.unwrapOr(rt.iter.toOption(copy.next()), 0xff)); out.push(range.len() & 0xff); const iter = range.rev().stepBy(3); @@ -377,55 +377,55 @@ export function ranges(data: Uint8Array, out: rt.ByteVec): void { out.push(inclusive.start()); out.push(inclusive.end()); out.push(inclusive.contains(b) ? 1 : 0); - let near: rt.RangeInclusive = new rt.RangeInclusive(rt.max(a, 200), 255, rt.u8); - out.push(rt.option.unwrapOr(near.nth(b), 0xee)); + let near: rt.RangeInclusive = new rt.RangeInclusive(rt.max(a, 200), 255, rt.u8); + out.push(rt.option.unwrapOr(near.nth(b as rt.usize), 0xee)); out.push(near.start()); out.push(near.isEmpty() ? 1 : 0); out.push(near.contains(255) ? 1 : 0); out.push(near.contains(100) ? 1 : 0); - let low: rt.RangeInclusive = new rt.RangeInclusive(0, b, rt.u8); - out.push(rt.option.unwrapOr(rt.iter.toOption(low.nthBackItem(rt.usize.rem(a, 16))), 0xee)); + let low: rt.RangeInclusive = new rt.RangeInclusive(0, b, rt.u8); + out.push(rt.option.unwrapOr(rt.iter.toOption(low.nthBackItem(rt.usize.rem(a as rt.usize, 16))), 0xee)); out.push(low.end()); out.push(low.contains(rt.u8.div(b, 2)) ? 1 : 0); - let reversed: rt.RangeInclusive = new rt.RangeInclusive(a, b, rt.u8); - out.push(rt.option.unwrapOr(reversed.rev().nth(a), 0xee)); + let reversed: rt.RangeInclusive = new rt.RangeInclusive(a, b, rt.u8); + out.push(rt.option.unwrapOr(reversed.rev().nth(a as rt.usize), 0xee)); out.push(reversed.end()); out.push(reversed.contains(a) ? 1 : 0); - let counted: rt.Iter<[number, number]> = new rt.RangeInclusive(a, b, rt.u8).enumerate(); - const [i3, n] = rt.option.unwrapOr(counted.nth(rt.usize.div(b, 4)), [0xdd, 0xdd] as [number, number]); + let counted: rt.Iter<[rt.usize, rt.u8]> = new rt.RangeInclusive(a, b, rt.u8).enumerate(); + const [i3, n] = rt.option.unwrapOr(counted.nth(rt.usize.div(b as rt.usize, 4)), [0xdd, 0xdd] as [rt.usize, rt.u8]); out.push(i3 & 0xff); out.push(n); - out.push(rt.option.mapOr(rt.iter.toOption(counted.next()), 0xcc, (arg: [number, number]): number => { + out.push(rt.option.mapOr(rt.iter.toOption(counted.next()), 0xcc, (arg: [rt.usize, rt.u8]): rt.u8 => { const i4 = arg[0]; return i4 & 0xff; })); - let stepped: rt.RangeInclusive = new rt.RangeInclusive(a, b, rt.u8); + let stepped: rt.RangeInclusive = new rt.RangeInclusive(a, b, rt.u8); out.push(stepped.stepBy(3).count() & 0xff); out.push(stepped.start()); const signed = new rt.RangeInclusive((a << 24) >> 24, (b << 24) >> 24, rt.i8); - rt.iter.extend(out, signed.rev().map((i5: number): number => { + rt.iter.extend(out, signed.rev().map((i5: rt.i8): rt.u8 => { return i5 & 0xff; })); - let wide: rt.Range = new rt.Range(a, rt.u64.mul(b, 3), rt.u64); - out.push(wide.map((n2: rt.U64): rt.U64 => { + let wide: rt.Range = new rt.Range(a as rt.u64, rt.u64.mul(b as rt.u64, 3), rt.u64); + out.push(wide.map((n2: rt.u64): rt.u64 => { return rt.u64.rem(n2, 5); - }).filter((arg2: rt.U64): boolean => { + }).filter((arg2: rt.u64): boolean => { const n3 = arg2; return n3 === 0; }).count() & 0xff); out.push(wide.isEmpty() ? 1 : 0); - const total = new rt.Range(0, a, rt.u64).sum(rt.u64.add, 0); + const total = new rt.Range(0, a as rt.u64, rt.u64).sum(rt.u64.add, 0); out.extendFromSlice(rt.u64.toBeBytes(total)); - const edge = rt.u64.add(9007199254740990, rt.u64.rem(a, 4)); - let past: rt.Range = new rt.Range(edge, rt.u64.add(edge, 4), rt.u64); - out.extendFromSlice(rt.u64.toBeBytes(rt.option.unwrapOr(past.nth(rt.usize.rem(b, 3)), 0))); + const edge = rt.u64.add(9007199254740990, rt.u64.rem(a as rt.u64, 4)); + let past: rt.Range = new rt.Range(edge, rt.u64.add(edge, 4), rt.u64); + out.extendFromSlice(rt.u64.toBeBytes(rt.option.unwrapOr(past.nth(rt.usize.rem(b as rt.usize, 3)), 0))); out.extendFromSlice(rt.u64.toBeBytes(rt.option.unwrapOr(rt.iter.toOption(past.nextBack()), 0))); - out.extendFromSlice(rt.u64.toBeBytes(rt.option.unwrapOr(new rt.RangeInclusive(edge, rt.u64.add(edge, 3), rt.u64).nth(rt.usize.rem(b, 5)), 0))); - const top2 = new rt.RangeInclusive(rt.u64.sub(rt.u64.of(18446744073709551615n), rt.u64.rem(a, 3)), rt.u64.of(18446744073709551615n), rt.u64).map((n4: rt.U64): rt.U64 => { + out.extendFromSlice(rt.u64.toBeBytes(rt.option.unwrapOr(new rt.RangeInclusive(edge, rt.u64.add(edge, 3), rt.u64).nth(rt.usize.rem(b as rt.usize, 5)), 0))); + const top2 = new rt.RangeInclusive(rt.u64.sub(rt.u64.of(18446744073709551615n), rt.u64.rem(a as rt.u64, 3)), rt.u64.of(18446744073709551615n), rt.u64).map((n4: rt.u64): rt.u64 => { return rt.u64.rem(n4, 7); }).sum(rt.u64.add, 0); out.extendFromSlice(rt.u64.toBeBytes(top2)); - const most = rt.option.unwrapOr(new rt.RangeInclusive(edge, rt.u64.add(edge, 2), rt.u64).rev().maxByKey((arg3: rt.U64): rt.U64 => { + const most = rt.option.unwrapOr(new rt.RangeInclusive(edge, rt.u64.add(edge, 2), rt.u64).rev().maxByKey((arg3: rt.u64): rt.u64 => { const n5 = arg3; return n5; }), 0); @@ -440,7 +440,7 @@ export function ranges(data: Uint8Array, out: rt.ByteVec): void { /** Iterators of `Option`, which keep each `None` apart from their end. */ export function options(data: Uint8Array, out: rt.ByteVec): void { - const maybe = (b: number): number | undefined => { + const maybe = (b: rt.u8): rt.u8 | undefined => { if (rt.u8.rem(b, 3) === 0) { return undefined; } else { @@ -462,9 +462,9 @@ export function options(data: Uint8Array, out: rt.ByteVec): void { out.push(0); } } - const flat = rt.iter.slice(data).map(maybe).flatten((value: number | undefined) => rt.iter.option(value)).toByteVec(); + const flat = rt.iter.slice(data).map(maybe).flatten((value: rt.u8 | undefined) => rt.iter.option(value)).toByteVec(); out.extendFromSlice(flat.view()); - const somes = rt.iter.slice(all).filter((o: number | undefined): boolean => { + const somes = rt.iter.slice(all).filter((o: rt.u8 | undefined): boolean => { return o !== undefined; }).count(); out.push(somes & 0xff); @@ -479,8 +479,8 @@ export function options(data: Uint8Array, out: rt.ByteVec): void { out.push(b3); } rt.iter.extend(out, rt.iter.option(first).chain(rt.iter.empty())); - let n: number = 0; - const counted = rt.iter.fromFn((): number | undefined => { + let n: rt.u8 = 0; + const counted = rt.iter.fromFn((): rt.u8 | undefined => { n = rt.u8.add(n, 1); return rt.thenSome(n < 4, n); }).toByteVec(); @@ -491,14 +491,14 @@ export function options(data: Uint8Array, out: rt.ByteVec): void { if (gap === rt.DONE) { break; } - out.push(rt.option.mapOr(gap, 0xaa, (b4: number): number => { + out.push(rt.option.mapOr(gap, 0xaa, (b4: rt.u8): rt.u8 => { return b4; })); } } /** The second item of an iterator of options, through `?`, which tests for its end. */ -export function second(items: rt.Iter): number | undefined { +export function second(items: rt.Iter): rt.u8 | undefined { const next = items.next(); if (next === rt.DONE) { return undefined; @@ -514,14 +514,14 @@ export function second(items: rt.Iter): number | undefined { /** Every byte, or `None` where it is zero. */ export class Gaps { data: Uint8Array; - at: number; + at: rt.usize; - constructor(fields: { data: Uint8Array; at: number }) { + constructor(fields: { data: Uint8Array; at: rt.usize }) { this.data = fields.data; this.at = fields.at; } - next(): number | undefined | rt.Done { + next(): rt.u8 | undefined | rt.Done { const get = rt.slice.get(this.data, this.at); if (get === undefined) { return rt.DONE; @@ -537,15 +537,15 @@ export class Gaps { /** A countdown: the crate's own iterator. */ export class Countdown { - left: number; - step: number; + left: rt.u8; + step: rt.u8; - constructor(fields: { left: number; step: number }) { + constructor(fields: { left: rt.u8; step: rt.u8 }) { this.left = fields.left; this.step = fields.step; } - next(): number | rt.Done { + next(): rt.u8 | rt.Done { if (this.left === 0) { return rt.DONE; } else { @@ -558,13 +558,13 @@ export class Countdown { /** The even bytes, read through another iterator, whose `None` ends this one. */ export class Evens { - inner: rt.Iter; + inner: rt.Iter; - constructor(fields: { inner: rt.Iter }) { + constructor(fields: { inner: rt.Iter }) { this.inner = fields.inner; } - next(): number | rt.Done { + next(): rt.u8 | rt.Done { while (true) { const next = this.inner.next(); if (next === rt.DONE) { @@ -590,8 +590,8 @@ export function own(data: Uint8Array, out: rt.ByteVec): void { } out.push(n); } - const total = rt.iter.from(new Countdown({ left, step }), (it: Countdown) => it.next()).map((n2: number): rt.U64 => { - return rt.u64.div(rt.u64.mul(n2, 1000), 7); + const total = rt.iter.from(new Countdown({ left, step }), (it: Countdown) => it.next()).map((n2: rt.u8): rt.u64 => { + return rt.u64.div(rt.u64.mul(n2 as rt.u64, 1000), 7); }).sum(rt.u64.add, 0); out.extendFromSlice(rt.u64.toBeBytes(total)); let down: Countdown = new Countdown({ left, step }); @@ -613,9 +613,9 @@ export function own(data: Uint8Array, out: rt.ByteVec): void { const firsts = rt.iter.from(byRef, (it: Evens) => it.next()).take(2).toByteVec(); out.extendFromSlice(firsts.view()); out.push(rt.iter.from(byRef, (it: Evens) => it.next()).count() & 0xff); - const halves = rt.iter.from(new Halving({ value: left }), (it: Halving) => it.next$Iterator()).toByteVec(); + const halves = rt.iter.from(new Halving({ value: left }), (it: Halving) => it.next$Iterator()).toByteVec(); out.extendFromSlice(halves.view()); - const total2 = rt.iter.from(new Halving({ value: rt.u64.mul(left, 0x1_0000_0001) }), (it: Halving) => it.next$Iterator$2()).sum(rt.u64.add, 0); + const total2 = rt.iter.from(new Halving({ value: rt.u64.mul(left as rt.u64, 0x1_0000_0001) }), (it: Halving) => it.next$Iterator$2()).sum(rt.u64.add, 0); out.extendFromSlice(rt.u64.toBeBytes(total2)); } @@ -627,13 +627,13 @@ export class Halving { this.value = fields.value; } - next$Iterator(this: Halving): number | rt.Done { + next$Iterator(this: Halving): rt.u8 | rt.Done { const value = this.value; this.value = rt.u8.div(this.value, 2); return rt.iter.fromOption(rt.thenSome(value !== 0, value)); } - next$Iterator$2(this: Halving): rt.U64 | rt.Done { + next$Iterator$2(this: Halving): rt.u64 | rt.Done { if (this.value === 0) { return rt.DONE; } else { @@ -648,13 +648,13 @@ export class Halving { * The last two bytes of `data`, as a `slice::Iter` made from the second argument: a * `for` must iterate it, not the first. */ -export function tail(_head: Uint8Array, data: Uint8Array): rt.Iter { +export function tail(_head: Uint8Array, data: Uint8Array): rt.Iter { return rt.iter.slice(rt.slice.range(data, rt.usize.saturatingSub(data.length, 2))); } /** The odd bytes, from a function returning `impl Iterator`. */ -export function odds(data: Uint8Array): rt.Iter { - return rt.iter.slice(data).filter((b: number): boolean => { +export function odds(data: Uint8Array): rt.Iter { + return rt.iter.slice(data).filter((b: rt.u8): boolean => { return rt.u8.rem(b, 2) === 1; }); } @@ -684,7 +684,7 @@ export function values(data: Uint8Array, out: rt.ByteVec): void { const arg2 = new Doubled(rt.slice.range(data, 0, rt.min(data.length, 2))); const printed = rt.fmt.format([(f: rt.Formatter) => arg.fmt(f), "|", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => arg2.fmt(f))]); out.extendFromSlice(rt.string.asBytes(printed)); - const tags = rt.iter.slice(data).map((arg3: number): Tag => { + const tags = rt.iter.slice(data).map((arg3: rt.u8): Tag => { const b3 = arg3; return new Tag({ bytes: rt.slice.toByteVec(Uint8Array.of(b3, ~b3 & 0xff)) }); }).toArray(); @@ -707,25 +707,25 @@ export function values(data: Uint8Array, out: rt.ByteVec): void { } out.extendFromSlice(tag2.bytes.view()); } - const bumps = rt.iter.slice(data).map((arg4: number): Bumped => { + const bumps = rt.iter.slice(data).map((arg4: rt.u8): Bumped => { const b4 = arg4; return new Bumped(b4); }).toArray(); rt.iter.extend(out, rt.iter.slice(bumps).map((a02: Bumped): Bumped => { return a02.clone(); - }).map((b5: Bumped): number => { + }).map((b5: Bumped): rt.u8 => { return b5._0; })); - rt.iter.extend(out, rt.iter.slice(bumps).map(rt.copy).map((b6: Bumped): number => { + rt.iter.extend(out, rt.iter.slice(bumps).map(rt.copy).map((b6: Bumped): rt.u8 => { return b6._0; })); - const wrapped = rt.iter.slice(data).map((arg5: number): Wrapper => { + const wrapped = rt.iter.slice(data).map((arg5: rt.u8): Wrapper => { const b7 = arg5; return new Wrapper(new Bumped(b7)); }).toArray(); rt.iter.extend(out, rt.iter.slice(wrapped).map((a03: Wrapper): Wrapper => { return a03.clone(); - }).map((w: Wrapper): number => { + }).map((w: Wrapper): rt.u8 => { return w._0._0; })); } @@ -744,9 +744,9 @@ export class Wrapper { /** A `Copy` type whose `Clone` does more than copy. */ export class Bumped { - _0: number; + _0: rt.u8; - constructor(_0: number) { + constructor(_0: rt.u8) { this._0 = _0; } @@ -770,13 +770,13 @@ export class Tag { /** An iterator in a field. */ export class Held { - it: rt.Iter; + it: rt.Iter; - constructor(fields: { it: rt.Iter }) { + constructor(fields: { it: rt.Iter }) { this.it = fields.it; } - skipOne(): number { + skipOne(): rt.u8 { rt.iter.toOption(this.it.next()); return rt.option.unwrapOr(rt.iter.toOption(this.it.next()), 0xbb); } @@ -791,9 +791,9 @@ export class Doubled { } fmt(f: rt.Formatter): rt.Result { - return f.debugList().entries(rt.iter.slice(this._0).map((arg: number): number => { + return f.debugList().entries(rt.iter.slice(this._0).map((arg: rt.u8): rt.u16 => { const b = arg; - return rt.u16.mul(b, 2); + return rt.u16.mul(b as rt.u16, 2); }), rt.u16.display).finish(); } } diff --git a/ts/fixtures/lang/expected/shapes.ts b/ts/fixtures/lang/expected/shapes.ts index d77c858fb..60b6cfc0d 100644 --- a/ts/fixtures/lang/expected/shapes.ts +++ b/ts/fixtures/lang/expected/shapes.ts @@ -4,10 +4,10 @@ import * as rt from "./rt.ts"; /** A `Copy` struct, which Rust copies where JS would share. */ export class Point { - x: number; - y: number; + x: rt.i32; + y: rt.i32; - constructor(fields: { x: number; y: number }) { + constructor(fields: { x: rt.i32; y: rt.i32 }) { this.x = fields.x; this.y = fields.y; } @@ -20,12 +20,12 @@ export class Point { return this.x === other.x && this.y === other.y; } - static new(x: number, y: number): Point { + static new(x: rt.i32, y: rt.i32): Point { return new Point({ x, y }); } /** Changes `self`, which a caller's copy must not see. */ - shift(by: number): Point { + shift(by: rt.i32): Point { this.x = rt.i32.add(this.x, by); return rt.copy(this); } @@ -37,13 +37,13 @@ export class Point { /** A tuple struct. */ export class Meters { - _0: rt.U64; + _0: rt.u64; - constructor(_0: rt.U64) { + constructor(_0: rt.u64) { this._0 = _0; } - area(): rt.U64 { + area(): rt.u64 { return this._0; } @@ -58,7 +58,7 @@ export class Meters { export type Shape = | { tag: "Dot" } | { tag: "Line"; _0: Point; _1: Point } - | { tag: "Rect"; corner: Point; width: number; height: number }; + | { tag: "Rect"; corner: Point; width: rt.u16; height: rt.u16 }; export const Shape = { fmt(self: Shape, f: rt.Formatter): rt.Result { @@ -82,7 +82,7 @@ export const Shape = { out.extendFromSlice(rt.u64.toBeBytes(Shape.area(self))); }, - area(self: Shape): rt.U64 { + area(self: Shape): rt.u64 { if (self.tag === "Dot") { return 0; } else if (self.tag === "Line") { @@ -90,7 +90,7 @@ export const Shape = { } else { const width = self.width; const height = self.height; - return rt.u64.mul(width, height); + return rt.u64.mul(width as rt.u64, height as rt.u64); } }, }; @@ -102,8 +102,8 @@ export type Signed = "Neg" | "Min" | "Pos"; export type Dir = "North" | "East" | "South" | "West"; -export function total$Meters(items: Meters[], out: rt.ByteVec): rt.U64 { - let sum: rt.U64 = 0; +export function total$Meters(items: Meters[], out: rt.ByteVec): rt.u64 { + let sum: rt.u64 = 0; for (const item of items) { item.describe(out); sum = rt.u64.saturatingAdd(sum, item.area()); @@ -111,8 +111,8 @@ export function total$Meters(items: Meters[], out: rt.ByteVec): rt.U64 { return sum; } -export function total$Shape(items: Shape[], out: rt.ByteVec): rt.U64 { - let sum: rt.U64 = 0; +export function total$Shape(items: Shape[], out: rt.ByteVec): rt.u64 { + let sum: rt.u64 = 0; for (const item of items) { Shape.describe(item, out); sum = rt.u64.saturatingAdd(sum, Shape.area(item)); @@ -120,11 +120,11 @@ export function total$Shape(items: Shape[], out: rt.ByteVec): rt.U64 { return sum; } -export function from(dir_: Dir): number { +export function from(dir_: Dir): rt.u8 { return { North: 1, East: 2, South: 4, West: 8 }[dir_]; } -export function dir(byte: number): Dir | undefined { +export function dir(byte: rt.u8): Dir | undefined { const value = rt.u8.rem(byte, 5); if (value === 0) { return "North"; @@ -146,8 +146,8 @@ export function shape(data: Uint8Array): Shape { } const kind = splitFirst[0]; const rest = splitFirst[1]; - const at = (i: number): number => { - return rt.i32.sub(rt.option.unwrapOr(rt.slice.get(rest, i), 0), 128); + const at = (i: rt.usize): rt.i32 => { + return rt.i32.sub(rt.option.unwrapOr(rt.slice.get(rest, i), 0) as rt.i32, 128); }; const value = rt.u8.rem(kind, 3); if (value === 0) { @@ -155,18 +155,18 @@ export function shape(data: Uint8Array): Shape { } else if (value === 1) { return { tag: "Line", _0: Point.new(at(0), at(1)), _1: Point.new(at(2), at(3)) }; } else { - return { tag: "Rect", corner: Point.new(at(0), at(1)), width: rt.u16.mul(rt.option.unwrapOr(rt.slice.get(rest, 2), 0), 300), height: rt.option.unwrapOr(rt.slice.get(rest, 3), 0) }; + return { tag: "Rect", corner: Point.new(at(0), at(1)), width: rt.u16.mul(rt.option.unwrapOr(rt.slice.get(rest, 2), 0) as rt.u16, 300), height: rt.option.unwrapOr(rt.slice.get(rest, 3), 0) as rt.u16 }; } } /** `Copy` aggregates: a copy changes apart from its original. */ export function copies(data: Uint8Array, out: rt.ByteVec): void { - const p = Point.new(rt.option.unwrapOr(rt.slice.first(data), 0), 7); + const p = Point.new(rt.option.unwrapOr(rt.slice.first(data), 0) as rt.i32, 7); let q: Point = rt.copy(p); q.x = rt.i32.add(q.x, 100); q.bump(); const r = rt.copy(p).shift(5); - let pair: [Point, number] = [rt.copy(p), 3]; + let pair: [Point, rt.u8] = [rt.copy(p), 3]; const saved = rt.copy(pair); pair[0].y = -1; pair[1] = rt.u8.add(pair[1], 1); @@ -198,7 +198,7 @@ export function copies(data: Uint8Array, out: rt.ByteVec): void { opt = undefined; const taken2 = taken; out.push(opt === undefined ? 1 : 0); - out.extendFromSlice(rt.u64.toBeBytes(rt.option.mapOr(rt.copy(taken2), 0, (m: Meters): rt.U64 => { + out.extendFromSlice(rt.u64.toBeBytes(rt.option.mapOr(rt.copy(taken2), 0, (m: Meters): rt.u64 => { return m._0; }))); const res = rt.Ok(rt.copy(p)); @@ -236,7 +236,7 @@ export function discriminants(data: Uint8Array, out: rt.ByteVec): void { */ export function loops(data: Uint8Array, out: rt.ByteVec): void { let stack: rt.ByteVec = rt.slice.toByteVec(data); - let popped: number = 0; + let popped: rt.u32 = 0; while (true) { const pop = stack.pop(); if (pop !== undefined) { @@ -244,7 +244,8 @@ export function loops(data: Uint8Array, out: rt.ByteVec): void { if (top === 0) { continue; } - popped = rt.u32.add(popped, top); + const value = top as rt.u32; + popped = rt.u32.add(popped, value); if (popped > 1000) { break; } @@ -253,14 +254,14 @@ export function loops(data: Uint8Array, out: rt.ByteVec): void { } } out.extendFromSlice(rt.u32.toBeBytes(popped)); - let found: [number, number] | undefined = undefined; + let found: [rt.usize, rt.usize] | undefined = undefined; const end = data.length; loop2: for (let i = 0; i < end; i++) { const start = rt.usize.add(i, 1); const end2 = data.length; for (let j = start; j < end2; j++) { if (rt.at(data, i) === rt.at(data, j)) { - found = [i, j] as [number, number]; + found = [i, j] as [rt.usize, rt.usize]; break loop2; } if (rt.at(data, j) === 0) { @@ -275,8 +276,8 @@ export function loops(data: Uint8Array, out: rt.ByteVec): void { } else { out.push(0xff); } - let n: rt.U64 = data.length; - let steps: number; + let n: rt.u64 = data.length as rt.u64; + let steps: rt.u8; while (true) { if (rt.u64.le(n, 1)) { steps = 0; @@ -289,11 +290,11 @@ export function loops(data: Uint8Array, out: rt.ByteVec): void { } } out.push(steps); - let count: number = 0; + let count: rt.u8 = 0; for (let _ = 0; _ <= 3; _++) { count = rt.u8.add(count, 1); } - let i3: number = 10; + let i3: rt.i8 = 10; while (i3 > -3) { i3 = rt.i8.sub(i3, 4); count = rt.u8.add(count, 1); @@ -302,8 +303,8 @@ export function loops(data: Uint8Array, out: rt.ByteVec): void { out.push(firstRepeat(data)); out.extendFromSlice(rt.u16.toBeBytes(tailBreak(data))); out.push(armLoop(data)); - out.extendFromSlice(rt.u32.toBeBytes(position(data, 0))); - const settle = (x: number): number => { + out.extendFromSlice(rt.u32.toBeBytes(position(data, 0) as rt.u32)); + const settle = (x: rt.u8): rt.u8 => { while (true) { if (x < 10) { return x; @@ -327,7 +328,7 @@ export function diverging(data: Uint8Array, out: rt.ByteVec): void { const b = rt.option.unwrapOr(rt.slice.first(head), 0); out.push(returnedNever(b, data.length)); out.push(brokenNever(head)); - const check = (b2: number): number => { + const check = (b2: rt.u8): rt.u8 => { if (b2 === 0xfd) { rt.panic(["closure ", (f: rt.Formatter) => rt.u8.display(b2, f)]); } @@ -340,12 +341,12 @@ export function diverging(data: Uint8Array, out: rt.ByteVec): void { } /** A function that never returns. */ -export function bail(b: number): never { +export function bail(b: rt.u8): never { rt.panic(["failed on ", rt.fmt.spec({ alternate: true }, (f: rt.Formatter) => rt.u8.lowerHex(b, f))]); } /** `return`, and `break` out of the function's labeled block, with values that never exist. */ -export function returnedNever(b: number, len: number): number { +export function returnedNever(b: rt.u8, len: rt.usize): rt.u8 { if (b === 247) { rt.panic(["returned ", (f: rt.Formatter) => rt.u8.display(b, f)]); } else if (b === 248) { @@ -364,7 +365,7 @@ export function returnedNever(b: number, len: number): number { } /** A `return` of a `match` that guards, whose arms fall through to the next. */ -export function guardedReturn(data: Uint8Array): number { +export function guardedReturn(data: Uint8Array): rt.u8 { if (!((data.length === 0))) { arms: { if (data.length === 2) { @@ -387,8 +388,8 @@ export function guardedReturn(data: Uint8Array): number { } /** A loop ending the function, left by `break` with values that never exist. */ -export function brokenNever(data: Uint8Array): number { - let i: number = 0; +export function brokenNever(data: Uint8Array): rt.u8 { + let i: rt.usize = 0; while (true) { if (i >= data.length) { return 0xcc; @@ -410,31 +411,31 @@ export function brokenNever(data: Uint8Array): number { * Values that never exist where a value is needed: bound, and passed. A loop that * only `return` leaves gives the function's result. */ -export function boundNever(data: Uint8Array, out: rt.ByteVec): number { - let i: number = 0; - let found: number; +export function boundNever(data: Uint8Array, out: rt.ByteVec): rt.u8 { + let i: rt.usize = 0; + let found: rt.u8; while (true) { if (i >= data.length) { return 0xbb; } const value = data[rt.idx(data, i)]; if (value === 239) { - let x: number; + let x: rt.u8; rt.panic(["bound at ", (f: rt.Formatter) => rt.usize.display(i, f)]); out.push(x); } else if (value === 240) { const arg = out; - let value2: number; + let value2: rt.u8; bail(rt.at(data, i)); arg.push(value2); } else if (value === 0) { const arg2 = out; - let value3: number; + let value3: rt.u8; return i & 0xff; arg2.push(value3); } else if (value === 128) { const arg3 = out; - let value4: number; + let value4: rt.u8; while (true) { i = rt.usize.add(i, 1); if (i >= data.length || rt.at(data, i) < 0x80) { @@ -450,7 +451,7 @@ export function boundNever(data: Uint8Array, out: rt.ByteVec): number { /** Loops that `break` ending a guarded `match` arm and an `if let` chain branch. */ export function branchLoops(data: Uint8Array, out: rt.ByteVec): void { - let i: number = 0; + let i: rt.usize = 0; while (rt.usize.add(i, 1) < data.length) { const w = rt.slice.range(data, i, rt.usize.add(i, 2)); i = rt.usize.add(i, 2); @@ -459,7 +460,7 @@ export function branchLoops(data: Uint8Array, out: rt.ByteVec): void { const a = w[0]; const b = w[1]; if (a > b) { - let n: number = a; + let n: rt.u8 = a; while (true) { if (n <= b) { break; @@ -481,7 +482,7 @@ export function branchLoops(data: Uint8Array, out: rt.ByteVec): void { const a3 = w[0]; const b2 = w[1]; if (rt.u8.rem(a3, 3) === rt.u8.rem(b2, 3)) { - let n2: number = a3; + let n2: rt.u8 = a3; while (true) { n2 = rt.u8.div(n2, 2); out.push(n2); @@ -498,8 +499,8 @@ export function branchLoops(data: Uint8Array, out: rt.ByteVec): void { } /** A loop that only `return` leaves, returned itself, around loops that `break`. */ -export function returnedLoop(data: Uint8Array): number { - let i: number = 0; +export function returnedLoop(data: Uint8Array): rt.u8 { + let i: rt.usize = 0; while (true) { if (i >= data.length) { return 0xdd; @@ -518,13 +519,13 @@ export function returnedLoop(data: Uint8Array): number { } /** A loop that ends the function, left only by `return` from nested scopes. */ -export function firstRepeat(data: Uint8Array): number { - let i: number = 0; +export function firstRepeat(data: Uint8Array): rt.u8 { + let i: rt.usize = 0; loop: while (true) { if (i >= data.length) { return 0xff; } - let j: number = rt.usize.add(i, 1); + let j: rt.usize = rt.usize.add(i, 1); while (j < data.length) { if (rt.at(data, j) === 0) { i = rt.usize.add(j, 1); @@ -540,8 +541,8 @@ export function firstRepeat(data: Uint8Array): number { } /** A loop that ends the function, left by `break` with a value from nested scopes. */ -export function tailBreak(data: Uint8Array): number { - let i: number = 0; +export function tailBreak(data: Uint8Array): rt.u16 { + let i: rt.usize = 0; while (true) { if (i >= data.length) { return 0x100; @@ -555,13 +556,13 @@ export function tailBreak(data: Uint8Array): number { for (const item of rt.slice.range(data, i)) { const c = item; if (c === b) { - return rt.u16.mul(c, 2); + return rt.u16.mul(c as rt.u16, 2); } } } else if (value === 2 && b > 100) { - return rt.u16.add(b, i & 0xffff); + return rt.u16.add(b as rt.u16, i & 0xffff); } else { - let n: number = b; + let n: rt.u8 = b; while (true) { if (n < 10) { break; @@ -576,9 +577,9 @@ export function tailBreak(data: Uint8Array): number { } /** A loop that only `return` leaves, in a `match` arm that needs a value. */ -export function armLoop(data: Uint8Array): number { - let k: number = 0; - let start: number; +export function armLoop(data: Uint8Array): rt.u8 { + let k: rt.usize = 0; + let start: rt.u8; const first = rt.slice.first(data); arms: { if (first !== undefined) { @@ -602,8 +603,8 @@ export function armLoop(data: Uint8Array): number { } /** A generic function ending in a loop. */ -export function position(items: Uint8Array, want: number): number { - let i: number = 0; +export function position(items: Uint8Array, want: rt.u8): rt.usize { + let i: rt.usize = 0; while (true) { if (i === items.length || items[rt.idx(items, i)] === want) { return i; @@ -614,7 +615,7 @@ export function position(items: Uint8Array, want: number): number { /** A unit function ending in a loop. */ export function drain(data: Uint8Array, out: rt.ByteVec): void { - let i: number = 0; + let i: rt.usize = 0; while (true) { if (i >= data.length) { return; @@ -652,7 +653,7 @@ export function patterns(data: Uint8Array, out: rt.ByteVec): void { } for (const item of rt.slice.range(data, 0, rt.min(data.length, 4))) { const b = item; - let class_: number; + let class_: rt.u8; arms2: { if (b === 0) { class_ = 0; @@ -698,7 +699,7 @@ export function patterns(data: Uint8Array, out: rt.ByteVec): void { out.push(0xee); } const shape_ = shape(data); - let area: rt.I64; + let area: rt.i64; arms3: { if ((shape_.tag === "Rect" && shape_.width === 0) || shape_.tag === "Dot") { area = 0; @@ -708,25 +709,25 @@ export function patterns(data: Uint8Array, out: rt.ByteVec): void { const x = shape_.corner.x; const width = shape_.width; if (x > 0) { - area = rt.i64.mul(width, x); + area = rt.i64.mul(width as rt.i64, x as rt.i64); break arms3; } } if (shape_.tag === "Rect") { const width2 = shape_.width; const height = shape_.height; - area = rt.i64.neg(rt.i64.mul(width2, height)); + area = rt.i64.neg(rt.i64.mul(width2 as rt.i64, height as rt.i64)); break arms3; } const a2 = rt.copy(shape_._0); const b3 = rt.copy(shape_._1); - area = rt.i64.mul(rt.i32.sub(a2.x, b3.x), rt.i32.sub(a2.y, b3.y)); + area = rt.i64.mul(rt.i32.sub(a2.x, b3.x) as rt.i64, rt.i32.sub(a2.y, b3.y) as rt.i64); } out.extendFromSlice(rt.i64.toBeBytes(area)); const total = rt.u64.add(total$Shape([shape_, { tag: "Dot" } as Shape], out), total$Meters([new Meters(3), new Meters(rt.u64.of(18446744073709551615n))], out)); out.extendFromSlice(rt.u64.toBeBytes(total)); const parsed = rt.Ok(data.length & 0xff); - let a3: number; + let a3: rt.u8; if (parsed.ok) { const v = parsed.value; a3 = v; @@ -734,7 +735,7 @@ export function patterns(data: Uint8Array, out: rt.ByteVec): void { const e = parsed.error; a3 = absurd([e, 1]); } - let b4: number; + let b4: rt.u8; if (parsed.ok) { const v2 = parsed.value; b4 = v2; @@ -742,7 +743,7 @@ export function patterns(data: Uint8Array, out: rt.ByteVec): void { const e2 = parsed.error; rt.unreachable(e2); } - let c: number; + let c: rt.u8; const v3 = parsed.value; c = v3; out.extendFromSlice(Uint8Array.of(a3, b4, c, early(a3))); @@ -759,28 +760,29 @@ export const Void = { }; /** A value holding a `Void` can't exist either. */ -export function absurd(v: [Void, number]): number { +export function absurd(v: [Void, rt.u8]): rt.u8 { rt.unreachable(rt.copy(v)); } /** A `match` without arms on a scrutinee that diverges, not on an empty type. */ -export function early(n: number): number { +export function early(n: rt.u8): rt.u8 { return rt.u8.wrappingAdd(n, 1); } /** Closures capturing by reference and by move, and functions as values. */ export function closures(data: Uint8Array, out: rt.ByteVec): void { - let sum: number = 0; - let add: (a0: number) => void = (v: number): void => { - sum = rt.u32.add(sum, v); + let sum: rt.u32 = 0; + let add: (a0: rt.u8) => void = (v: rt.u8): void => { + const value = v as rt.u32; + sum = rt.u32.add(sum, value); }; for (const item of data) { const b = item; add(b); } out.extendFromSlice(rt.u32.toBeBytes(sum)); - const base = data.length; - const scale = (v2: number): number => { + const base = data.length as rt.u32; + const scale = (v2: rt.u32): rt.u32 => { return rt.u32.add(rt.u32.mul(v2, 3), base); }; out.extendFromSlice(rt.u32.toBeBytes(apply(scale, sum))); @@ -796,13 +798,13 @@ export function closures(data: Uint8Array, out: rt.ByteVec): void { out.extendFromSlice(tags.view()); } -export function apply(f: (a0: number) => number, v: number): number { +export function apply(f: (a0: rt.u32) => rt.u32, v: rt.u32): rt.u32 { return f(v); } /** Options, results, `?`, and strings. */ export function library(data: Uint8Array, out: rt.ByteVec): void { - const checked = (a: number, b: number): number | undefined => { + const checked = (a: rt.u8, b: rt.u8): rt.u8 | undefined => { const checkedAdd = rt.u8.checkedAdd(a, b); if (checkedAdd === undefined) { return undefined; @@ -818,16 +820,16 @@ export function library(data: Uint8Array, out: rt.ByteVec): void { const a2 = rt.option.unwrapOr(rt.slice.first(data), 0); const b2 = rt.option.unwrapOr(rt.slice.get(data, 1), 0); out.push(rt.option.unwrapOr(checked(a2, b2), 0xff)); - let parsed: rt.Result; + let parsed: rt.Result; if (a2 === 0) { parsed = rt.Err("zero"); } else { const n = a2; parsed = rt.Ok(n); } - const mapped = rt.result.mapErr(rt.result.map(parsed, (n2: number): number => { + const mapped = rt.result.mapErr(rt.result.map(parsed, (n2: rt.u8): rt.u8 => { return rt.u8.div(n2, 2); - }), (e: string): number => { + }), (e: string): rt.u8 => { return rt.string.len(e) & 0xff; }); if (mapped.ok) { @@ -841,7 +843,7 @@ export function library(data: Uint8Array, out: rt.ByteVec): void { if (fromUtf8.ok) { const s = fromUtf8.value; out.push(1); - out.extendFromSlice(rt.u32.toBeBytes(rt.string.len(s))); + out.extendFromSlice(rt.u32.toBeBytes(rt.string.len(s) as rt.u32)); out.extendFromSlice(rt.string.asBytes(s)); } else { out.push(0);