From d78e189734ee53030b480d79e940d694fb211763 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 1 Nov 2026 01:17:44 -0700 Subject: [PATCH 1/4] chore(quest): claim quest/m2/ts-byte-views/fields.md Quest-Claim: 99771d7d1f6c5da1 From 4ffb5a9bd9e97d60922c57bd53135d26ebac9114 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 1 Nov 2026 01:03:14 -0800 Subject: [PATCH 2/4] perf(ts): hold a struct's private byte slice field as its parts A private &[u8] or &[u8; N] field of a struct is three JS fields, its bytes, start, and length, so a cursor advancing through it makes no object. A read that needs the slice whole makes it there; a write, through a &mut, an rt.Place, or a closure's capture too, writes all three. Co-Authored-By: Claude Opus 5.5 --- ts/MIGRATE.md | 3 + ts/README.md | 7 +- .../codec/expected/coding/decode.Decoder.ts | 49 +++--- ts/fixtures/lang/expected/chunks.Header.ts | 10 +- ts/fixtures/lang/expected/chunks.ts | 8 +- ts/fixtures/lang/expected/clones.Plan.ts | 6 +- ts/fixtures/lang/expected/clones.ts | 2 +- ts/fixtures/lang/expected/core/option.ts | 7 + ts/fixtures/lang/expected/core/result.ts | 14 ++ ts/fixtures/lang/expected/fields.Couple.ts | 33 ++++ ts/fixtures/lang/expected/fields.Exposed.ts | 20 +++ ts/fixtures/lang/expected/fields.Glued.ts | 24 +++ ts/fixtures/lang/expected/fields.Labeled.ts | 26 +++ ts/fixtures/lang/expected/fields.Scanner.ts | 74 +++++++++ ts/fixtures/lang/expected/fields.Sentry.ts | 28 ++++ ts/fixtures/lang/expected/fields.ts | 125 +++++++++++++++ ts/fixtures/lang/expected/harness.ts | 3 +- ts/fixtures/lang/expected/iters.Doubled.ts | 6 +- ts/fixtures/lang/expected/iters.Gaps.ts | 6 +- ts/fixtures/lang/expected/iters.ts | 11 +- ts/fixtures/lang/expected/lib.ts | 5 + ts/fixtures/lang/expected/refs.Cursor.ts | 6 +- ts/fixtures/lang/expected/refs.ts | 2 +- ts/fixtures/lang/expected/returns.Guard.ts | 12 +- ts/fixtures/lang/expected/returns.ts | 2 +- ts/fixtures/lang/src/fields.rs | 151 ++++++++++++++++++ ts/fixtures/lang/src/harness.rs | 9 +- ts/fixtures/lang/src/lib.rs | 3 +- ts/fixtures/stock/expected/buf.Ops.ts | 31 ++-- ts/fixtures/stock/expected/buf.ts | 2 +- ts/src/lower/copy.rs | 2 +- ts/src/lower/drop.rs | 2 +- ts/src/lower/mir/call.rs | 3 +- ts/src/lower/mir/place.rs | 106 +++++++++++- ts/src/lower/mir/rvalue.rs | 31 +++- ts/src/lower/mir/scalar.rs | 29 +++- ts/src/lower/mod.rs | 134 +++++++++++++--- ts/src/lower/unions.rs | 2 +- 38 files changed, 891 insertions(+), 103 deletions(-) create mode 100644 ts/fixtures/lang/expected/fields.Couple.ts create mode 100644 ts/fixtures/lang/expected/fields.Exposed.ts create mode 100644 ts/fixtures/lang/expected/fields.Glued.ts create mode 100644 ts/fixtures/lang/expected/fields.Labeled.ts create mode 100644 ts/fixtures/lang/expected/fields.Scanner.ts create mode 100644 ts/fixtures/lang/expected/fields.Sentry.ts create mode 100644 ts/fixtures/lang/expected/fields.ts create mode 100644 ts/fixtures/lang/src/fields.rs diff --git a/ts/MIGRATE.md b/ts/MIGRATE.md index 29ecf6866..53c0f3e15 100644 --- a/ts/MIGRATE.md +++ b/ts/MIGRATE.md @@ -14,6 +14,9 @@ that, or `undefined` for an error, and leaves the rest, or the error as `err`, in the slot, such as `readOut`. A root still takes an `rt.ByteSlice` and returns the `Result`. +- A struct's private `&[u8]` or `&[u8; N]` field is three fields, its bytes, start, and + length, named for it: `buf` is `bufData`, `bufStart`, and `bufLen`, and a class's + constructor takes the three in its place. A `pub` field is still an `rt.ByteSlice`. - `rt.btreeCollect` takes whether it sorts and the keys' `!ne` after the entries, and `rt.btreeCollectKeys` the keys' `PartialOrd::lt` and `!ne`, as in `rt.btreeCollect(m, entries, true, Key.eq$cmp_PartialEq)`; neither takes a test for keys diff --git a/ts/README.md b/ts/README.md index b6904a224..f7d4118c0 100644 --- a/ts/README.md +++ b/ts/README.md @@ -142,8 +142,11 @@ crate translation reaches is refused, and so are two versions of one crate it re `byteOffset` and `byteLength`, and copy it before transferring it. An `rt.ByteSlice` is a range of a `Uint8Array`: its `data`, from `start`, for `length` -bytes. A caller passes bytes as `rt.byteSliceFrom(array)` and reads a slice it gets as -`rt.byteSliceView(slice)`, a view of it. A mutable part of a slice of other values, as +bytes. A struct's private `&[u8]` or `&[u8; N]` field is its three parts, `bufData`, +`bufStart`, and `bufLen` for `buf`, so a cursor moving through it makes no object; a +read that needs the slice whole makes it there. A caller passes bytes as +`rt.byteSliceFrom(array)` and reads a slice it gets as `rt.byteSliceView(slice)`, a view +of it. A mutable part of a slice of other values, as `&mut v[a..b]`, `split_at_mut`, `chunks_mut`, and `split_mut` lend it, is a `Proxy` of an array whose elements are the slice's, read and written where they are, since a JS array has no views. diff --git a/ts/fixtures/codec/expected/coding/decode.Decoder.ts b/ts/fixtures/codec/expected/coding/decode.Decoder.ts index 18a6a3454..9b0ad40cb 100644 --- a/ts/fixtures/codec/expected/coding/decode.Decoder.ts +++ b/ts/fixtures/codec/expected/coding/decode.Decoder.ts @@ -14,7 +14,9 @@ import type * as Control from "../stats.Control.ts"; * [`DecodeError::Short`] can be retried once more bytes arrive. */ export interface Decoder { - buf: rt.ByteSlice; + bufData: Uint8Array; + bufStart: number; + bufLen: number; form: Form.Form; /** The length of the buffer this decoder started with. */ len: rt.usize; @@ -31,7 +33,7 @@ export interface Decoder { /** Read `buf`, with varints in the given form. */ function new_(buf: rt.ByteSlice, form: Form.Form): Decoder { - return { buf, form, len: buf.length, need: 0, control: undefined, uncounted: 0 }; + return { bufData: buf.data, bufStart: buf.start, bufLen: buf.length, form, len: buf.length, need: 0, control: undefined, uncounted: 0 }; } /** @@ -45,28 +47,32 @@ export function need(self: Decoder): rt.usize { /** Record that a read wanted `more` bytes past the current position. */ export function short(self: Decoder, more: rt.usize): DecodeError.DecodeError { - const need: rt.usize = rt.usizeSaturatingAdd(rt.usizeSub(self.len, self.buf.length), more); + const need: rt.usize = rt.usizeSaturatingAdd(rt.usizeSub(self.len, self.bufLen), more); self.need = rt.max(self.need, need); return { tag: "Short" } as DecodeError.DecodeError; } /** The number of unread bytes. */ export function remaining(self: Decoder): rt.usize { - return self.buf.length; + return self.bufLen; } /** Whether every byte has been read. */ export function isEmpty(self: Decoder): boolean { - return self.buf.length === 0; + return self.bufLen === 0; } /** Read `len` raw bytes. */ export function slice(self: Decoder, len: rt.usize): DecodeError.DecodeError | undefined { - const slice: rt.ByteSlice = self.buf; - if (len <= slice.length) { - self.buf = new rt.ByteSlice(slice.data, slice.start + len, slice.length - len); - sliceOut.pData = slice.data; - sliceOut.pStart = slice.start; + const _8Data: Uint8Array = self.bufData; + const _8Start: number = self.bufStart; + const _8Len: number = self.bufLen; + if (len <= _8Len) { + self.bufData = _8Data; + self.bufStart = _8Start + len; + self.bufLen = _8Len - len; + sliceOut.pData = _8Data; + sliceOut.pStart = _8Start; sliceOut.pLen = len; return undefined; } @@ -77,8 +83,10 @@ export const sliceOut: { pData: Uint8Array; pStart: number; pLen: number } = { p /** Read every remaining byte. */ export function rest(self: Decoder): rt.ByteSlice { - const taken = self.buf; - self.buf = rt.byteSliceFrom(new Uint8Array(0)); + const taken = new rt.ByteSlice(self.bufData, self.bufStart, self.bufLen); + self.bufData = new Uint8Array(0); + self.bufStart = 0; + self.bufLen = 0; return taken; } @@ -147,18 +155,19 @@ export function bool(self: Decoder): rt.Result /** Read a varint. */ export function varint(self: Decoder): rt.Result { let _0!: rt.Result; - const _13: rt.ByteSlice = self.buf; - const part: rt.u64 | undefined = read(_13.data, _13.start, _13.length, self.form); + const part: rt.u64 | undefined = read(self.bufData, self.bufStart, self.bufLen, self.form); const err2: DecodeError.DecodeError | undefined = part === undefined ? readOut.err as DecodeError.DecodeError : undefined; const part2: Uint8Array = readOut.p_1Data; const part3: number = readOut.p_1Start; const part4: number = readOut.p_1Len; if (err2 === undefined) { const value: rt.u64 = part as rt.u64; - self.buf = new rt.ByteSlice(part2, part3, part4); + self.bufData = part2; + self.bufStart = part3; + self.bufLen = part4; _0 = rt.Ok(value); } else if ((err2 as DecodeError.DecodeError).tag === "Short") { - _0 = rt.Err(short(self, rt.usizeAdd(self.buf.length, 1))); + _0 = rt.Err(short(self, rt.usizeAdd(self.bufLen, 1))); } else { _0 = rt.Err(err2 as DecodeError.DecodeError); } @@ -177,7 +186,9 @@ export function varintOpt(self: Decoder): rt.Result; - const start: rt.ByteSlice = self.buf; + const startData: Uint8Array = self.bufData; + const startStart: number = self.bufStart; + const startLen: number = self.bufLen; const _6: rt.Result = varint(self); if (_6.ok) { const _3: rt.Result = result_mapErr$06mnjtr(rt.usizeTryFrom(_6.value), (_2: rt.TryFromIntError): DecodeError.DecodeError => { @@ -189,7 +200,9 @@ export function bytes(self: Decoder): DecodeError.DecodeError | undefined { const part2: number = sliceOut.pStart; const part3: number = sliceOut.pLen; const _15: (a0: DecodeError.DecodeError) => undefined = (_22: DecodeError.DecodeError): undefined => { - self.buf = start; + self.bufData = startData; + self.bufStart = startStart; + self.bufLen = startLen; }; _0 = inspectErr$156koiv((err !== undefined ? rt.Err(err as DecodeError.DecodeError) : rt.Ok(new rt.ByteSlice(part, part2, part3))) as rt.Result, _15); } else { diff --git a/ts/fixtures/lang/expected/chunks.Header.ts b/ts/fixtures/lang/expected/chunks.Header.ts index dc1aa1157..1388b86af 100644 --- a/ts/fixtures/lang/expected/chunks.Header.ts +++ b/ts/fixtures/lang/expected/chunks.Header.ts @@ -1,11 +1,13 @@ // Generated by rust2-ts. Do not edit. -import * as rt from "./rt.ts"; - /** A header split off the front of its input, keeping both parts. */ export interface Header { - tag: rt.ByteSlice; - rest: rt.ByteSlice; + tagData: Uint8Array; + tagStart: number; + tagLen: number; + restData: Uint8Array; + restStart: number; + restLen: number; } //# sourceMappingURL=chunks.Header.ts.map diff --git a/ts/fixtures/lang/expected/chunks.ts b/ts/fixtures/lang/expected/chunks.ts index 04bbaca58..c19d240d7 100644 --- a/ts/fixtures/lang/expected/chunks.ts +++ b/ts/fixtures/lang/expected/chunks.ts @@ -6,7 +6,7 @@ import { mapOr$1jgrjsu as option_mapOr$1jgrjsu } from "./core/option.ts"; export function header(dataData: Uint8Array, dataStart: number, dataLen: number): Header.Header | undefined { if (dataLen >= 2) { - return { tag: new rt.ByteSlice(dataData, dataStart, 2), rest: new rt.ByteSlice(dataData, dataStart + 2, dataLen - 2) }; + return { tagData: dataData, tagStart: dataStart, tagLen: 2, restData: dataData, restStart: dataStart + 2, restLen: dataLen - 2 }; } return undefined; } @@ -64,9 +64,9 @@ export function chunks(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { const _6: Header.Header | undefined = header(data.data, data.start, data.length); if (_6 !== undefined) { const h: Header.Header = _6; - rt.byteVecExtendFromSlice(out, h.tag); - rt.byteVecPush(out, h.tag.data[h.tag.start + 1]); - rt.byteVecPush(out, h.rest.length & 0xff); + rt.byteVecExtendFromSlice(out, new rt.ByteSlice(h.tagData, h.tagStart, h.tagLen)); + rt.byteVecPush(out, h.tagData[h.tagStart + 1]); + rt.byteVecPush(out, h.restLen & 0xff); } const err: rt.u8 | undefined = take4(data.data, data.start, data.length); const part3: Uint8Array = take4Out.p_0Data; diff --git a/ts/fixtures/lang/expected/clones.Plan.ts b/ts/fixtures/lang/expected/clones.Plan.ts index 2b82e35a5..a9c1f193e 100644 --- a/ts/fixtures/lang/expected/clones.Plan.ts +++ b/ts/fixtures/lang/expected/clones.Plan.ts @@ -5,12 +5,14 @@ import { unwrapOr$0r2bmpr as option_unwrapOr$0r2bmpr } from "./core/option.ts"; /** What `turns` reads by: the case's bytes, each step's taken in order. */ export interface Plan { - data: rt.ByteSlice; + dataData: Uint8Array; + dataStart: number; + dataLen: number; at: rt.usize; } export function next(self: Plan): rt.u8 { - const b: rt.u8 = option_unwrapOr$0r2bmpr(rt.byteSliceGet(self.data, rt.usizeRem(self.at, rt.max(self.data.length, 1))), 0); + const b: rt.u8 = option_unwrapOr$0r2bmpr(rt.byteSliceGet(new rt.ByteSlice(self.dataData, self.dataStart, self.dataLen), rt.usizeRem(self.at, rt.max(self.dataLen, 1))), 0); self.at = rt.usizeAdd(self.at, 1); return b ^ rt.u8WrappingMul(self.at & 0xff, 29); } diff --git a/ts/fixtures/lang/expected/clones.ts b/ts/fixtures/lang/expected/clones.ts index 715d8b7ca..3e4756bb7 100644 --- a/ts/fixtures/lang/expected/clones.ts +++ b/ts/fixtures/lang/expected/clones.ts @@ -6603,7 +6603,7 @@ export function iters(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { let _263!: [rt.ByteSlice, rt.ByteSlice]; let _269!: rt.usize; let it!: rt.Peekable; - const plan: Plan.Plan = { data, at: kind as rt.usize }; + const plan: Plan.Plan = { dataData: data.data, dataStart: data.start, dataLen: data.length, at: kind as rt.usize }; const n: rt.usize = option_mapOr$1surhce(rt.byteSliceFirst(data), 1, (_2: rt.u8): rt.u8 => { return rt.u8Add(rt.u8Rem(_2, 4), 1); }) as rt.usize; diff --git a/ts/fixtures/lang/expected/core/option.ts b/ts/fixtures/lang/expected/core/option.ts index d39042e77..2c1e2ab2f 100644 --- a/ts/fixtures/lang/expected/core/option.ts +++ b/ts/fixtures/lang/expected/core/option.ts @@ -1517,6 +1517,13 @@ export function okOr$0tezn0w(self: rt.ByteSlice | undefined, _: undefined): rt.R return rt.Ok(self); } +export function okOr$1w08zpl(self: rt.ByteSlice | undefined, err: rt.usize): rt.Result { + if (self === undefined) { + return rt.Err(err); + } + return rt.Ok(self); +} + export function okOr$0q0ogpn(self: [rt.ByteSlice, rt.ByteSlice] | undefined, err: rt.u8 | undefined): rt.Result<[rt.ByteSlice, rt.ByteSlice], rt.u8 | undefined> { if (self === undefined) { return rt.Err(err); diff --git a/ts/fixtures/lang/expected/core/result.ts b/ts/fixtures/lang/expected/core/result.ts index 806dbe900..22ce59e1a 100644 --- a/ts/fixtures/lang/expected/core/result.ts +++ b/ts/fixtures/lang/expected/core/result.ts @@ -438,6 +438,13 @@ export function isErrAnd$0b01a39(self: rt.Result, f: (a0: rt.u16) return f(self.error); } +export function ok$04yj0hm(self: rt.Result): rt.ByteSlice | undefined { + if (self.ok) { + return self.value; + } + return undefined; +} + export function ok$0lceojf(self: rt.Result<[rt.usize, rt.u8], rt.Arc>): [rt.usize, rt.u8] | undefined { let _0!: [rt.usize, rt.u8] | undefined; if (self.ok) { @@ -718,6 +725,13 @@ export function inspect$16o3gle(self: rt.Result, f: (a0: rt.u8) = return self; } +export function inspectErr$0g3nypv(self: rt.Result, f: (a0: rt.usize) => undefined): rt.Result { + if (!self.ok) { + f(self.error); + } + return self; +} + export function inspectErr$06tv5sa(self: rt.Result, f: (a0: rt.u16) => undefined): rt.Result { if (!self.ok) { f(self.error); diff --git a/ts/fixtures/lang/expected/fields.Couple.ts b/ts/fixtures/lang/expected/fields.Couple.ts new file mode 100644 index 000000000..dc36c07bd --- /dev/null +++ b/ts/fixtures/lang/expected/fields.Couple.ts @@ -0,0 +1,33 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; + +export function fmt$fmt_Debug(self: Couple, f: rt.Formatter): rt.Result { + const arg = new rt.ByteSlice(self.headData, self.headStart, self.headLen); + const _5: rt.Fmt = (f: rt.Formatter) => rt.fmtList(rt.byteSliceView(arg), f, rt.u8Debug); + const arg2 = new rt.ByteSlice(self.wordData, self.wordStart, self.wordLen); + const _3: string = "Couple"; + const _4: string = "head"; + const _7: string = "word"; + const _8: rt.Fmt = (f: rt.Formatter) => rt.fmtList(rt.byteSliceView(arg2), f, rt.u8Debug); + return rt.debugStructFinish(rt.debugStructField(rt.debugStructField(rt.fmtDebugStruct(f, _3), _4, _5), _7, _8)); +} + +export function eq$cmp_PartialEq(self: Couple, other: Couple): boolean { + if (!rt.byteSliceEq(new rt.ByteSlice(self.headData, self.headStart, self.headLen), new rt.ByteSlice(other.headData, other.headStart, other.headLen))) { + return false; + } + return rt.eqBytes(new rt.ByteSlice(self.wordData, self.wordStart, self.wordLen), new rt.ByteSlice(other.wordData, other.wordStart, other.wordLen)); +} + +/** A `Copy` struct of a slice and an array's reference. */ +export interface Couple { + headData: Uint8Array; + headStart: number; + headLen: number; + wordData: Uint8Array; + wordStart: number; + wordLen: number; +} + +//# sourceMappingURL=fields.Couple.ts.map diff --git a/ts/fixtures/lang/expected/fields.Exposed.ts b/ts/fixtures/lang/expected/fields.Exposed.ts new file mode 100644 index 000000000..6626b285d --- /dev/null +++ b/ts/fixtures/lang/expected/fields.Exposed.ts @@ -0,0 +1,20 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; + +export function fmt$fmt_Debug(self: Exposed, f: rt.Formatter): rt.Result { + const _3: string = "Exposed"; + const _4: string = "buf"; + const _5: rt.Fmt = (f: rt.Formatter) => rt.fmtList(rt.byteSliceView(self.buf), f, rt.u8Debug); + const _7: string = "at"; + const _8: rt.Fmt = (f: rt.Formatter) => rt.u8Debug(self.at, f); + return rt.debugStructFinish(rt.debugStructField(rt.debugStructField(rt.fmtDebugStruct(f, _3), _4, _5), _7, _8)); +} + +/** A `pub` field, which stays whole. */ +export interface Exposed { + buf: rt.ByteSlice; + at: rt.u8; +} + +//# sourceMappingURL=fields.Exposed.ts.map diff --git a/ts/fixtures/lang/expected/fields.Glued.ts b/ts/fixtures/lang/expected/fields.Glued.ts new file mode 100644 index 000000000..5c0b69d37 --- /dev/null +++ b/ts/fixtures/lang/expected/fields.Glued.ts @@ -0,0 +1,24 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; + +export function fmt$fmt_Debug(self: Glued, f: rt.Formatter): rt.Result { + const arg = new rt.ByteSlice(self._0Data, self._0Start, self._0Len); + const _4: rt.Fmt = (f: rt.Formatter) => rt.fmtList(rt.byteSliceView(arg), f, rt.u8Debug); + const arg2 = new rt.ByteSlice(self._1Data, self._1Start, self._1Len); + const _3: string = "Glued"; + const _6: rt.Fmt = (f: rt.Formatter) => rt.fmtList(rt.byteSliceView(arg2), f, rt.u8Debug); + return rt.debugTupleFinish(rt.debugTupleField(rt.debugTupleField(rt.fmtDebugTuple(f, _3), _4), _6)); +} + +/** A tuple struct of two slices, one a constant's. */ +export interface Glued { + _0Data: Uint8Array; + _0Start: number; + _0Len: number; + _1Data: Uint8Array; + _1Start: number; + _1Len: number; +} + +//# sourceMappingURL=fields.Glued.ts.map diff --git a/ts/fixtures/lang/expected/fields.Labeled.ts b/ts/fixtures/lang/expected/fields.Labeled.ts new file mode 100644 index 000000000..a9a9b9886 --- /dev/null +++ b/ts/fixtures/lang/expected/fields.Labeled.ts @@ -0,0 +1,26 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; + +/** A generic struct holding a slice beside its parameter. */ +export class Labeled { + tag: T; + bytesData: Uint8Array; + bytesStart: number; + bytesLen: number; + + constructor(tag: T, bytesData: Uint8Array, bytesStart: number, bytesLen: number) { + this.tag = tag; + this.bytesData = bytesData; + this.bytesStart = bytesStart; + this.bytesLen = bytesLen; + } +} + +export function sum$u8(self: Labeled): rt.u32 { + return rt.u32Add(rt.iterSum(rt.iterMap(rt.iterSlice(rt.byteSliceView(new rt.ByteSlice(self.bytesData, self.bytesStart, self.bytesLen))), (_22: rt.u8): rt.u32 => { + return _22 as rt.u32; + }), rt.u32Add, 0), self.tag as rt.u32); +} + +//# sourceMappingURL=fields.Labeled.ts.map diff --git a/ts/fixtures/lang/expected/fields.Scanner.ts b/ts/fixtures/lang/expected/fields.Scanner.ts new file mode 100644 index 000000000..d55deafda --- /dev/null +++ b/ts/fixtures/lang/expected/fields.Scanner.ts @@ -0,0 +1,74 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import { okOr$1w08zpl as option_okOr$1w08zpl } from "./core/option.ts"; +import { ok$04yj0hm, inspectErr$0g3nypv } from "./core/result.ts"; + +export function clone$Clone(self: Scanner): Scanner { + const _2: rt.ByteSlice = new rt.ByteSlice(self.bufData, self.bufStart, self.bufLen); + return { bufData: _2.data, bufStart: _2.start, bufLen: _2.length, read: self.read }; +} + +export function fmt$fmt_Debug(self: Scanner, f: rt.Formatter): rt.Result { + const arg = new rt.ByteSlice(self.bufData, self.bufStart, self.bufLen); + const _3: string = "Scanner"; + const _4: string = "buf"; + const _5: rt.Fmt = (f: rt.Formatter) => rt.fmtList(rt.byteSliceView(arg), f, rt.u8Debug); + const _7: string = "read"; + const _8: rt.Fmt = (f: rt.Formatter) => rt.usizeDebug(self.read, f); + return rt.debugStructFinish(rt.debugStructField(rt.debugStructField(rt.fmtDebugStruct(f, _3), _4, _5), _7, _8)); +} + +export function eq$cmp_PartialEq(self: Scanner, other: Scanner): boolean { + if (!rt.byteSliceEq(new rt.ByteSlice(self.bufData, self.bufStart, self.bufLen), new rt.ByteSlice(other.bufData, other.bufStart, other.bufLen))) { + return false; + } + return self.read === other.read; +} + +/** A cursor through bytes, advanced in place. */ +export interface Scanner { + bufData: Uint8Array; + bufStart: number; + bufLen: number; + read: rt.usize; +} + +export function take(self: Scanner, n: rt.usize): rt.ByteSlice | undefined { + const _10Data: Uint8Array = self.bufData; + const _10Start: number = self.bufStart; + const _10Len: number = self.bufLen; + if (n <= _10Len) { + self.bufData = _10Data; + self.bufStart = _10Start + n; + self.bufLen = _10Len - n; + self.read = rt.usizeAdd(self.read, n); + return new rt.ByteSlice(_10Data, _10Start, n); + } + return undefined; +} + +/** A read undone by a closure that captured the field's `&mut` and the old value. */ +export function takeOrRewind(self: Scanner, n: rt.usize): rt.ByteSlice | undefined { + const startData: Uint8Array = self.bufData; + const startStart: number = self.bufStart; + const startLen: number = self.bufLen; + if (take(self, 1) !== undefined) { + return ok$04yj0hm(inspectErr$0g3nypv(option_okOr$1w08zpl(take(self, n), n), (_2: rt.usize): undefined => { + self.bufData = startData; + self.bufStart = startStart; + self.bufLen = startLen; + })); + } + return undefined; +} + +export function rest(self: Scanner): rt.ByteSlice { + const taken = new rt.ByteSlice(self.bufData, self.bufStart, self.bufLen); + self.bufData = new Uint8Array(0); + self.bufStart = 0; + self.bufLen = 0; + return taken; +} + +//# sourceMappingURL=fields.Scanner.ts.map diff --git a/ts/fixtures/lang/expected/fields.Sentry.ts b/ts/fixtures/lang/expected/fields.Sentry.ts new file mode 100644 index 000000000..e98c76507 --- /dev/null +++ b/ts/fixtures/lang/expected/fields.Sentry.ts @@ -0,0 +1,28 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; + +/** A struct that drops, a class, holding a slice. */ +export class Sentry { + bufData: Uint8Array; + bufStart: number; + bufLen: number; + out: rt.ByteVec; + + constructor(bufData: Uint8Array, bufStart: number, bufLen: number, out: rt.ByteVec) { + this.bufData = bufData; + this.bufStart = bufStart; + this.bufLen = bufLen; + this.out = out; + } + + [Symbol.dispose](): void { + drop$ops_Drop(this); + } +} + +export function drop$ops_Drop(self: Sentry): void { + rt.byteVecPush(self.out, self.bufLen & 0xff); +} + +//# sourceMappingURL=fields.Sentry.ts.map diff --git a/ts/fixtures/lang/expected/fields.ts b/ts/fixtures/lang/expected/fields.ts new file mode 100644 index 000000000..ee3363cd7 --- /dev/null +++ b/ts/fixtures/lang/expected/fields.ts @@ -0,0 +1,125 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import { copy$fields_Couple } from "./lib.ts"; +import { unwrapOr$0r2bmpr as option_unwrapOr$0r2bmpr } from "./core/option.ts"; +import * as Couple from "./fields.Couple.ts"; +import * as Exposed from "./fields.Exposed.ts"; +import * as Glued from "./fields.Glued.ts"; +import * as Labeled from "./fields.Labeled.ts"; +import * as Scanner from "./fields.Scanner.ts"; +import * as Sentry from "./fields.Sentry.ts"; + +/** Advance a slice through a `&mut` to it. */ +export function skipInto(buf: rt.Place, n: rt.usize): void { + buf.v = rt.byteSliceRange(buf.v, rt.min(n, buf.v.length)); +} + +export const SCANNER: Scanner.Scanner = { bufData: Uint8Array.of(120, 121, 122), bufStart: 0, bufLen: 3, read: 1 }; + +export function fields(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + let two!: Glued.Glued; + const n: rt.usize = rt.u8Rem(kind, 5) as rt.usize; + const r: Scanner.Scanner = { bufData: data.data, bufStart: data.start, bufLen: data.length, read: 0 }; + while (true) { + const _8: rt.ByteSlice | undefined = Scanner.take(r, rt.max(n, 1)); + if (_8 !== undefined) { + rt.byteVecExtendFromSlice(out, rt.byteSliceRange(_8, 0, rt.min(_8.length, 1))); + if (r.read > 12) { + break; + } + } else { + break; + } + } + const r2: Scanner.Scanner = { bufData: data.data, bufStart: data.start, bufLen: data.length, read: 0 }; + const _22: rt.ByteSlice | undefined = Scanner.takeOrRewind(r2, rt.usizeAdd(n, 3)); + if (_22 === undefined) { + rt.byteVecPush(out, 200); + } else { + rt.byteVecPush(out, _22.length & 0xff); + } + rt.byteVecPush(out, r2.bufLen & 0xff); + const before: Scanner.Scanner = Scanner.clone$Clone(r2); + Scanner.take(r2, 1); + rt.byteVecPush(out, Scanner.eq$cmp_PartialEq(before, r2) ? 1 : 0); + rt.byteVecPush(out, before.bufLen & 0xff); + rt.byteVecPush(out, Scanner.rest(r2).length & 0xff); + rt.byteVecPush(out, r2.bufLen & 0xff); + const s: Scanner.Scanner = { bufData: data.data, bufStart: data.start, bufLen: data.length, read: 0 }; + const _56: rt.ByteSlice = rt.byteSliceRange(data, rt.usizeDiv(data.length, 2)); + const old2 = new rt.ByteSlice(s.bufData, s.bufStart, s.bufLen); + s.bufData = _56.data; + s.bufStart = _56.start; + s.bufLen = _56.length; + rt.byteVecPush(out, rt.usizeSub(old2.length, s.bufLen) & 0xff); + const swapped = new rt.ByteSlice(r2.bufData, r2.bufStart, r2.bufLen); + r2.bufData = s.bufData; + r2.bufStart = s.bufStart; + r2.bufLen = s.bufLen; + s.bufData = swapped.data; + s.bufStart = swapped.start; + s.bufLen = swapped.length; + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(r2.bufLen & 0xff, s.bufLen & 0xff))); + skipInto(rt.place(() => new rt.ByteSlice(s.bufData, s.bufStart, s.bufLen), (value): void => { + s.bufData = value.data; + s.bufStart = value.start; + s.bufLen = value.length; + }), n); + rt.byteVecPush(out, s.bufLen & 0xff); + skipInto(rt.place(() => new rt.ByteSlice(r2.bufData, r2.bufStart, r2.bufLen), (value2): void => { + r2.bufData = value2.data; + r2.bufStart = value2.start; + r2.bufLen = value2.length; + }), 1); + const _85: rt.ByteSlice = rt.byteSliceRange(new rt.ByteSlice(r2.bufData, r2.bufStart, r2.bufLen), rt.min(new rt.ByteSlice(r2.bufData, r2.bufStart, r2.bufLen).length, 1)); + r2.bufData = _85.data; + r2.bufStart = _85.start; + r2.bufLen = _85.length; + rt.byteVecPush(out, r2.bufLen & 0xff); + const _92: rt.ByteSlice = rt.byteSliceRange(new rt.ByteSlice(r2.bufData, r2.bufStart, r2.bufLen), rt.usizeDiv(r2.bufLen, 2)); + r2.bufData = _92.data; + r2.bufStart = _92.start; + r2.bufLen = _92.length; + rt.byteVecPush(out, r2.bufLen & 0xff); + const _100: rt.ByteSlice | undefined = rt.byteSliceGetRange(data, 0, 2); + if (_100 !== undefined) { + const p: Couple.Couple = { headData: data.data, headStart: data.start, headLen: data.length, wordData: _100.data, wordStart: _100.start, wordLen: _100.length }; + const q: Couple.Couple = copy$fields_Couple(p); + const _105: rt.ByteSlice = rt.byteSliceRange(new rt.ByteSlice(p.headData, p.headStart, p.headLen), 1); + p.headData = _105.data; + p.headStart = _105.start; + p.headLen = _105.length; + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(p.headLen & 0xff, q.headLen & 0xff, Couple.eq$cmp_PartialEq(p, q) ? 1 : 0))); + rt.byteVecExtendFromSlice(out, new rt.ByteSlice(q.wordData, q.wordStart, q.wordLen)); + const args2: rt.Fmt[] = [(f: rt.Formatter) => Couple.fmt$fmt_Debug(p, f)]; + rt.byteVecExtendFromSlice(out, rt.stringAsBytes(rt.fmtFormat([args2[0]]))); + } + const _134: rt.ByteSlice = rt.byteSliceRange(data, 0, rt.min(data.length, 3)); + const open: Exposed.Exposed = { buf: _134, at: kind }; + const args4: rt.Fmt[] = [(f: rt.Formatter) => Exposed.fmt$fmt_Debug(open, f), (f: rt.Formatter) => Scanner.fmt$fmt_Debug(r2, f)]; + rt.byteVecExtendFromSlice(out, rt.stringAsBytes(rt.fmtFormat([args4[0], args4[1]]))); + rt.byteVecPush(out, kind ^ (_134.length & 0xff)); + if (!(rt.u8Rem(kind, 2) === 0)) { + const _161: Glued.Glued = { _0Data: Uint8Array.of(97, 98), _0Start: 0, _0Len: 2, _1Data: Uint8Array.of(), _1Start: 0, _1Len: 0 }; + two = { _0Data: data.data, _0Start: data.start, _0Len: data.length, _1Data: _161._0Data, _1Start: _161._0Start, _1Len: _161._0Len }; + } else { + two = { _0Data: Uint8Array.of(97, 98), _0Start: 0, _0Len: 2, _1Data: Uint8Array.of(), _1Start: 0, _1Len: 0 }; + } + const args6: rt.Fmt[] = [(f: rt.Formatter) => Glued.fmt$fmt_Debug(two, f)]; + rt.byteVecExtendFromSlice(out, rt.stringAsBytes(rt.fmtFormat([args6[0]]))); + rt.byteVecPush(out, rt.usizeAdd(two._0Len, two._1Len) & 0xff); + const _181: Scanner.Scanner = SCANNER; + rt.byteVecExtendFromSlice(out, new rt.ByteSlice(_181.bufData, _181.bufStart, _181.bufLen)); + rt.byteVecPush(out, SCANNER.read & 0xff); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u32ToBeBytes(Labeled.sum$u8(new Labeled.Labeled(kind, data.data, data.start, data.length))).reverse())); + const g: Sentry.Sentry = new Sentry.Sentry(data.data, data.start, data.length, out); + const _194: rt.ByteSlice = rt.byteSliceRange(new rt.ByteSlice(g.bufData, g.bufStart, g.bufLen), rt.min(g.bufLen, 2)); + g.bufData = _194.data; + g.bufStart = _194.start; + g.bufLen = _194.length; + rt.byteVecPush(g.out, option_unwrapOr$0r2bmpr(rt.byteSliceFirst(new rt.ByteSlice(g.bufData, g.bufStart, g.bufLen)), 0)); + g[Symbol.dispose](); +} + +//# sourceMappingURL=fields.ts.map diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index 6c77b4360..60a3fcb3c 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -23,6 +23,7 @@ import { durations } from "./durations.ts"; import { dyns, shared as dyns_shared, anys, fns } from "./dyns.ts"; import { edits as edits_edits, bytes as edits_bytes } from "./edits.ts"; import { eqs } from "./errors.ts"; +import { fields as fields_fields } from "./fields.ts"; import { floats, mulAdds as floats_mulAdds, formats as floats_formats, parses as floats_parses } from "./floats.ts"; import { ints, strings, derived, written as fmt_written, counts as fmt_counts, rounding, panics as fmt_panics, wrapped as fmt_wrapped, collections, enums, iters as fmt_iters, writes as fmt_writes } from "./fmt.ts"; import { folds } from "./folds.ts"; @@ -252,7 +253,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) aimed(data88, out89); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["ascii::parts", ascii_parts as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["boxes::places", boxes_places as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["arrays::flattened", arrays_flattened as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["args::args", args as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["returns::returns", returns as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["borrows::guards", (_2189: rt.u8, data89: rt.ByteSlice, out90: rt.ByteVec): undefined => { guards(data89, out90); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined]]; + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["fields::fields", fields_fields as (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined]]; } /** Run one case. */ diff --git a/ts/fixtures/lang/expected/iters.Doubled.ts b/ts/fixtures/lang/expected/iters.Doubled.ts index a28662611..590455c24 100644 --- a/ts/fixtures/lang/expected/iters.Doubled.ts +++ b/ts/fixtures/lang/expected/iters.Doubled.ts @@ -4,12 +4,14 @@ import * as rt from "./rt.ts"; /** Each byte doubled, as a `Debug` list built from an iterator. */ export interface Doubled { - _0: rt.ByteSlice; + _0Data: Uint8Array; + _0Start: number; + _0Len: number; } export function fmt$fmt_Debug(self: Doubled, f: rt.Formatter): rt.Result { const _5: rt.DebugList = rt.fmtDebugList(f); - const _6: rt.Iter = rt.iterMap(rt.iterSlice(rt.byteSliceView(self._0)), (_2: rt.u8): rt.u16 => { + const _6: rt.Iter = rt.iterMap(rt.iterSlice(rt.byteSliceView(new rt.ByteSlice(self._0Data, self._0Start, self._0Len))), (_2: rt.u8): rt.u16 => { return rt.u16Mul(_2 as rt.u16, 2); }); return rt.debugListFinish(rt.debugListEntries(_5, _6, rt.u16Debug)); diff --git a/ts/fixtures/lang/expected/iters.Gaps.ts b/ts/fixtures/lang/expected/iters.Gaps.ts index 94ae87d79..416a7da17 100644 --- a/ts/fixtures/lang/expected/iters.Gaps.ts +++ b/ts/fixtures/lang/expected/iters.Gaps.ts @@ -4,12 +4,14 @@ import * as rt from "./rt.ts"; /** Every byte, or `None` where it is zero. */ export interface Gaps { - data: rt.ByteSlice; + dataData: Uint8Array; + dataStart: number; + dataLen: number; at: rt.usize; } export function next$iter_Iterator(self: Gaps): rt.u8 | undefined | rt.Done { - const _3: rt.u8 | undefined = rt.byteSliceGet(self.data, self.at); + const _3: rt.u8 | undefined = rt.byteSliceGet(new rt.ByteSlice(self.dataData, self.dataStart, self.dataLen), self.at); if (_3 !== undefined) { const b: rt.u8 = _3; self.at = rt.usizeAdd(self.at, 1); diff --git a/ts/fixtures/lang/expected/iters.ts b/ts/fixtures/lang/expected/iters.ts index 8aafa5244..f12b8bd45 100644 --- a/ts/fixtures/lang/expected/iters.ts +++ b/ts/fixtures/lang/expected/iters.ts @@ -1369,7 +1369,7 @@ export function options(data: rt.ByteSlice, out: rt.ByteVec): void { return _83(b9, calls); })) & 0xff); rt.byteVecExtendFromSlice(out, rt.byteVecSlice(calls)); - const iter3: Gaps.Gaps = { data, at: 0 }; + const iter3: Gaps.Gaps = { dataData: data.data, dataStart: data.start, dataLen: data.length, at: 0 }; while (true) { const _114: rt.u8 | undefined | rt.Done = Gaps.next$iter_Iterator(iter3); if (_114 === rt.DONE) { @@ -1715,8 +1715,9 @@ export function values(data: rt.ByteSlice, out: rt.ByteVec): void { } rt.byteVecPush(out, _21); } - const _31: Doubled.Doubled = { _0: data }; - const _33: Doubled.Doubled = { _0: rt.byteSliceRange(data, 0, rt.min(data.length, 2)) }; + const _34: rt.ByteSlice = rt.byteSliceRange(data, 0, rt.min(data.length, 2)); + const _31: Doubled.Doubled = { _0Data: data.data, _0Start: data.start, _0Len: data.length }; + const _33: Doubled.Doubled = { _0Data: _34.data, _0Start: _34.start, _0Len: _34.length }; const args2: rt.Fmt[] = [(f: rt.Formatter) => Doubled.fmt$fmt_Debug(_31, f), (f: rt.Formatter) => Doubled.fmt$fmt_Debug(_33, f)]; rt.byteVecExtendFromSlice(out, rt.stringAsBytes(rt.fmtFormat([args2[0], "|", rt.fmtSpec({ alternate: true }, args2[1])]))); const tags: Tag.Tag[] = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (_2: rt.u8): Tag.Tag => { @@ -1800,7 +1801,7 @@ export function returned(data: rt.ByteSlice, out: rt.ByteVec): void { return undefined; })); _4(part6, out); - const part7 = first$iters_Gaps({ data, at: n }); + const part7 = first$iters_Gaps({ dataData: data.data, dataStart: data.start, dataLen: data.length, at: n }); _4(part7, out); const part8 = first$iters_Countdown({ left: n & 0xff, step: 1 }); _3(part8, out); @@ -1809,7 +1810,7 @@ export function returned(data: rt.ByteSlice, out: rt.ByteVec): void { })).reverse()), undefined, true); const part9 = byMut$iter_Copied_slice_Iter_u8(rt.iterSlice(rt.byteSliceView(data))); _3(part9, out); - const gaps: Gaps.Gaps = { data, at: 0 }; + const gaps: Gaps.Gaps = { dataData: data.data, dataStart: data.start, dataLen: data.length, at: 0 }; const part10 = byMut$iters_Gaps(gaps); _4(part10, out); const part11 = byMut$iters_Gaps(gaps); diff --git a/ts/fixtures/lang/expected/lib.ts b/ts/fixtures/lang/expected/lib.ts index 601bc539c..ca337b259 100644 --- a/ts/fixtures/lang/expected/lib.ts +++ b/ts/fixtures/lang/expected/lib.ts @@ -13,6 +13,7 @@ import type * as counts_Noisy from "./counts.Noisy.ts"; import type * as RcReleasing from "./counts.RcReleasing.ts"; import type * as RcShared from "./counts.RcShared.ts"; import type * as durations_Window from "./durations.Window.ts"; +import type * as Couple from "./fields.Couple.ts"; import type * as Either from "./generics.Either.ts"; import * as Wrap from "./generics.Wrap.ts"; import type * as Bumped from "./iters.Bumped.ts"; @@ -293,6 +294,10 @@ export function copy$durations_Window(v: durations_Window.Window): durations_Win return { start: v.start, len: v.len }; } +export function copy$fields_Couple(v: Couple.Couple): Couple.Couple { + return { headData: v.headData, headStart: v.headStart, headLen: v.headLen, wordData: v.wordData, wordStart: v.wordStart, wordLen: v.wordLen }; +} + export function copy$generics_Either_u8_shapes_Point(v: Either.Either): Either.Either { if (v.tag === "Left") { return { tag: "Left", _0: v._0 }; diff --git a/ts/fixtures/lang/expected/refs.Cursor.ts b/ts/fixtures/lang/expected/refs.Cursor.ts index 557a528a7..b5fc4892d 100644 --- a/ts/fixtures/lang/expected/refs.Cursor.ts +++ b/ts/fixtures/lang/expected/refs.Cursor.ts @@ -5,11 +5,13 @@ import * as rt from "./rt.ts"; /** A struct that keeps a `&mut` to its position. */ export interface Cursor { at: rt.Place; - data: rt.ByteSlice; + dataData: Uint8Array; + dataStart: number; + dataLen: number; } export function next(self: Cursor): rt.u8 | undefined { - const _3: rt.u8 | undefined = rt.byteSliceGet(self.data, self.at.v); + const _3: rt.u8 | undefined = rt.byteSliceGet(new rt.ByteSlice(self.dataData, self.dataStart, self.dataLen), self.at.v); if (_3 !== undefined) { self.at.v = rt.usizeAdd(self.at.v, 1); return _3; diff --git a/ts/fixtures/lang/expected/refs.ts b/ts/fixtures/lang/expected/refs.ts index 132d76a47..71e1a2200 100644 --- a/ts/fixtures/lang/expected/refs.ts +++ b/ts/fixtures/lang/expected/refs.ts @@ -272,7 +272,7 @@ export function refs(data: rt.ByteSlice, out: rt.ByteVec): void { const _185: rt.u8 = rt.u8WrappingAdd(by, 1); rt.byteVecPush(out, fill(rt.place(() => empty, (value13) => empty = value13), rt.place(() => text, (value14) => text = value14), _185)); let at: rt.usize = 0; - const cursor: Cursor.Cursor = { at: rt.place(() => at, (value15) => at = value15), data }; + const cursor: Cursor.Cursor = { at: rt.place(() => at, (value15) => at = value15), dataData: data.data, dataStart: data.start, dataLen: data.length }; let sum: rt.u8 = 0; while (true) { const _190: rt.u8 | undefined = Cursor.next(cursor); diff --git a/ts/fixtures/lang/expected/returns.Guard.ts b/ts/fixtures/lang/expected/returns.Guard.ts index a9303ba4a..f31417abb 100644 --- a/ts/fixtures/lang/expected/returns.Guard.ts +++ b/ts/fixtures/lang/expected/returns.Guard.ts @@ -6,11 +6,15 @@ import type * as returns_Error from "./returns.Error.ts"; /** A guard whose drop writes the slot after its function's call did. */ export class Guard { - _0: rt.ByteSlice; + _0Data: Uint8Array; + _0Start: number; + _0Len: number; _1: rt.u8; - constructor(_0: rt.ByteSlice, _1: rt.u8) { - this._0 = _0; + constructor(_0Data: Uint8Array, _0Start: number, _0Len: number, _1: rt.u8) { + this._0Data = _0Data; + this._0Start = _0Start; + this._0Len = _0Len; this._1 = _1; } @@ -20,7 +24,7 @@ export class Guard { } export function drop$ops_Drop(self: Guard): void { - const _3: rt.ByteSlice = rt.byteSliceRange(self._0, rt.usizeRem(self._1 as rt.usize, 2)); + const _3: rt.ByteSlice = rt.byteSliceRange(new rt.ByteSlice(self._0Data, self._0Start, self._0Len), rt.usizeRem(self._1 as rt.usize, 2)); narrow(_3.data, _3.start, _3.length) === undefined ? wideOut.err as returns_Error.Error : undefined; } diff --git a/ts/fixtures/lang/expected/returns.ts b/ts/fixtures/lang/expected/returns.ts index 27cb7b559..95fdb309a 100644 --- a/ts/fixtures/lang/expected/returns.ts +++ b/ts/fixtures/lang/expected/returns.ts @@ -182,7 +182,7 @@ export function guarded(dataData: Uint8Array, dataStart: number, dataLen: number const part2: Uint8Array = wideOut.p_1Data; const part3: number = wideOut.p_1Start; const part4: number = wideOut.p_1Len; - new Guard.Guard(other, dataLen & 0xff)[Symbol.dispose](); + new Guard.Guard(other.data, other.start, other.length, dataLen & 0xff)[Symbol.dispose](); if (err !== undefined) { wideOut.err = err as returns_Error.Error; return undefined; diff --git a/ts/fixtures/lang/src/fields.rs b/ts/fixtures/lang/src/fields.rs new file mode 100644 index 000000000..b3f76309e --- /dev/null +++ b/ts/fixtures/lang/src/fields.rs @@ -0,0 +1,151 @@ +//! Byte slices in private fields, which their structs hold as their parts: read, split, +//! written whole and through a `&mut`, taken and swapped, captured, copied, compared, and +//! printed. + +/// A cursor through bytes, advanced in place. +#[derive(Clone, Debug, PartialEq)] +struct Scanner<'a> { + buf: &'a [u8], + read: usize, +} + +impl<'a> Scanner<'a> { + fn take(&mut self, n: usize) -> Option<&'a [u8]> { + let (head, rest) = self.buf.split_at_checked(n)?; + self.buf = rest; + self.read += n; + Some(head) + } + + /// A read undone by a closure that captured the field's `&mut` and the old value. + fn take_or_rewind(&mut self, n: usize) -> Option<&'a [u8]> { + let start = self.buf; + self.take(1)?; + self.take(n).ok_or(n).inspect_err(|_| self.buf = start).ok() + } + + fn rest(&mut self) -> &'a [u8] { + std::mem::take(&mut self.buf) + } +} + +/// Advance a slice through a `&mut` to it. +fn skip_into(buf: &mut &[u8], n: usize) { + *buf = &buf[n.min(buf.len())..]; +} + +/// A `Copy` struct of a slice and an array's reference. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct Couple<'a> { + head: &'a [u8], + word: &'a [u8; 2], +} + +/// A `pub` field, which stays whole. +#[derive(Debug)] +pub struct Exposed<'a> { + pub buf: &'a [u8], + at: u8, +} + +/// A tuple struct of two slices, one a constant's. +#[derive(Debug, Clone, Copy)] +struct Glued<'a>(&'a [u8], &'a [u8]); + +const GLUED: Glued<'static> = Glued(b"ab", &[]); + +static SCANNER: Scanner<'static> = Scanner { buf: b"xyz", read: 1 }; + +/// A generic struct holding a slice beside its parameter. +struct Labeled<'a, T> { + tag: T, + bytes: &'a [u8], +} + +impl> Labeled<'_, T> { + fn sum(&self) -> u32 { + self.bytes.iter().map(|&b| u32::from(b)).sum::() + self.tag.into() + } +} + +/// A struct that drops, a class, holding a slice. +struct Sentry<'a, 'b> { + buf: &'a [u8], + out: &'b mut Vec, +} + +impl Drop for Sentry<'_, '_> { + fn drop(&mut self) { + self.out.push(self.buf.len() as u8); + } +} + +pub fn fields(kind: u8, data: &[u8], out: &mut Vec) { + let n = usize::from(kind % 5); + // Reads, splits, and a closure that writes the field back. + let mut r = Scanner { buf: data, read: 0 }; + while let Some(head) = r.take(n.max(1)) { + out.extend_from_slice(&head[..head.len().min(1)]); + if r.read > 12 { + break; + } + } + let mut r = Scanner { buf: data, read: 0 }; + match r.take_or_rewind(n + 3) { + Some(got) => out.push(got.len() as u8), + None => out.push(200), + } + out.push(r.buf.len() as u8); + // A clone that the original's reads don't move, and equality of the two. + let before = r.clone(); + r.take(1); + out.push(u8::from(before == r)); + out.push(before.buf.len() as u8); + // Taken, then replaced and swapped. + let rest = r.rest(); + out.push(rest.len() as u8); + out.push(r.buf.len() as u8); + let mut s = Scanner { buf: data, read: 0 }; + let old = std::mem::replace(&mut s.buf, &data[data.len() / 2..]); + out.push((old.len() - s.buf.len()) as u8); + std::mem::swap(&mut r.buf, &mut s.buf); + out.extend_from_slice(&[r.buf.len() as u8, s.buf.len() as u8]); + // A `&mut` to the field passed on, and one kept in a variable. + skip_into(&mut s.buf, n); + out.push(s.buf.len() as u8); + let field = &mut r.buf; + skip_into(field, 1); + *field = &field[field.len().min(1)..]; + out.push(r.buf.len() as u8); + // A field whose new value reads it. + r.buf = &r.buf[r.buf.len() / 2..]; + out.push(r.buf.len() as u8); + // A `Copy` struct copied, then the original changed. + if let Some(word) = data.first_chunk::<2>() { + let mut p = Couple { head: data, word }; + let q = p; + p.head = &p.head[1..]; + out.extend_from_slice(&[p.head.len() as u8, q.head.len() as u8, u8::from(p == q)]); + out.extend_from_slice(q.word); + out.extend_from_slice(format!("{p:?}").as_bytes()); + } + // Printed, with the `pub` field left whole. + let open = Exposed { + buf: &data[..data.len().min(3)], + at: kind, + }; + out.extend_from_slice(format!("{open:?}{r:?}").as_bytes()); + out.push(open.at ^ open.buf.len() as u8); + // A constant's and a static's. + let two = if kind % 2 == 0 { GLUED } else { Glued(data, GLUED.0) }; + out.extend_from_slice(format!("{two:?}").as_bytes()); + out.push((two.0.len() + two.1.len()) as u8); + out.extend_from_slice(SCANNER.buf); + out.push(SCANNER.read as u8); + // Generic, and a class. + let tagged = Labeled { tag: kind, bytes: data }; + out.extend_from_slice(&tagged.sum().to_le_bytes()); + let mut g = Sentry { buf: data, out }; + g.buf = &g.buf[g.buf.len().min(2)..]; + g.out.push(g.buf.first().copied().unwrap_or(0)); +} diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index 9a6d553cb..2350e6cce 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -3,10 +3,10 @@ use crate::{ args, arrays, ascii, atomics, borrows, bounds, boxes, captures, cells, chars, chunks, clones, collections, - combinators, counts, cows, cycles, defaults, durations, dyns, edits, errors, floats, fmt, folds, generics, hashes, - hints, holds, ints, iters, joins, keys, lengths, lists, locals, locks, methods, nested, objects, orders, pointers, - refs, returns, saturates, separated, shapes, singles, slices, splits, statics, strs, tuples, utf8, wrappers, wraps, - zeros, + combinators, counts, cows, cycles, defaults, durations, dyns, edits, errors, fields, floats, fmt, folds, generics, + hashes, hints, holds, ints, iters, joins, keys, lengths, lists, locals, locks, methods, nested, objects, orders, + pointers, refs, returns, saturates, separated, shapes, singles, slices, splits, statics, strs, tuples, utf8, + wrappers, wraps, zeros, }; /// The native side's entry: [`run`], except that a case starting with 0xff prints a @@ -269,6 +269,7 @@ fn cases() -> Vec<(&'static str, Case)> { ("args::args", args::args), ("returns::returns", returns::returns), ("borrows::guards", |_, data, out| borrows::guards(data, out)), + ("fields::fields", fields::fields), ] } diff --git a/ts/fixtures/lang/src/lib.rs b/ts/fixtures/lang/src/lib.rs index cec049032..5f2cb06ba 100644 --- a/ts/fixtures/lang/src/lib.rs +++ b/ts/fixtures/lang/src/lib.rs @@ -1,7 +1,7 @@ //! The language the driver translates, exercised against native Rust: every integer //! width's arithmetic, casts, and inherent methods, `f64`, `char`s and the `str` methods over them, //! `Cow`, boxed slices and text, `Duration`, control flow, patterns, byte slices split apart, -//! passed as their parts, and returned in a slot, `Copy` +//! passed as their parts, returned in a slot, and held as their parts in fields, `Copy` //! aggregates, closures and what they write of what they captured, traits, generic //! types, formatting, collections, `LinkedList`s, UTF-8 decoding, `&mut` to values JS copies, whole //! writes through a `&mut` to an object, the standard library calls it maps, `Option`'s @@ -54,6 +54,7 @@ mod durations; mod dyns; mod edits; mod errors; +mod fields; mod floats; mod fmt; mod folds; diff --git a/ts/fixtures/stock/expected/buf.Ops.ts b/ts/fixtures/stock/expected/buf.Ops.ts index b780347da..b181e1dab 100644 --- a/ts/fixtures/stock/expected/buf.Ops.ts +++ b/ts/fixtures/stock/expected/buf.Ops.ts @@ -5,14 +5,20 @@ import { unwrapOr$0r2bmpr as option_unwrapOr$0r2bmpr } from "./core/option.ts"; /** The bytes of a case's input, taken from the front. */ export interface Ops { - _0: rt.ByteSlice; + _0Data: Uint8Array; + _0Start: number; + _0Len: number; } export function next(self: Ops): rt.u8 | undefined { - const slice: rt.ByteSlice = self._0; - if (slice.length !== 0) { - const first: rt.u8 = slice.data[slice.start]; - self._0 = new rt.ByteSlice(slice.data, slice.start + 1, slice.length - 1); + const _8Data: Uint8Array = self._0Data; + const _8Start: number = self._0Start; + const _8Len: number = self._0Len; + if (_8Len !== 0) { + const first: rt.u8 = _8Data[_8Start]; + self._0Data = _8Data; + self._0Start = _8Start + 1; + self._0Len = _8Len - 1; return first; } return undefined; @@ -23,14 +29,17 @@ export function byte(self: Ops): rt.u8 { } export function take(self: Ops, n: rt.usize): rt.ByteSlice { - const _7: rt.ByteSlice = self._0; - const _5: rt.usize = rt.min(n, self._0.length); - const slice: rt.ByteSlice = _7; - if (_5 > slice.length) { + const _7Data: Uint8Array = self._0Data; + const _7Start: number = self._0Start; + const _7Len: number = self._0Len; + const mid: number = rt.min(n, self._0Len); + if (mid > _7Len) { rt.panic("mid > len"); } - const taken: rt.ByteSlice = new rt.ByteSlice(slice.data, slice.start, _5); - self._0 = new rt.ByteSlice(slice.data, slice.start + _5, slice.length - _5); + const taken: rt.ByteSlice = new rt.ByteSlice(_7Data, _7Start, mid); + self._0Data = _7Data; + self._0Start = _7Start + mid; + self._0Len = _7Len - mid; return taken; } diff --git a/ts/fixtures/stock/expected/buf.ts b/ts/fixtures/stock/expected/buf.ts index 5b3f996cb..20715faa8 100644 --- a/ts/fixtures/stock/expected/buf.ts +++ b/ts/fixtures/stock/expected/buf.ts @@ -14,7 +14,7 @@ import { then$1y5f3ge } from "./core/bool.ts"; */ export function bytesMut(data: rt.ByteSlice, out: rt.ByteVec): void { let _80!: rt.usize; - const ops: Ops.Ops = { _0: data }; + const ops: Ops.Ops = { _0Data: data.data, _0Start: data.start, _0Len: data.length }; const buf: rt.BytesMut = rt.bytesMutWithCapacity(rt.usizeRem(Ops.byte(ops) as rt.usize, 24)); const pieces: rt.BytesMut[] = []; const frozen: rt.ByteSlice[] = []; diff --git a/ts/src/lower/copy.rs b/ts/src/lower/copy.rs index 193777dcc..a7bd83a66 100644 --- a/ts/src/lower/copy.rs +++ b/ts/src/lower/copy.rs @@ -275,7 +275,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { let values = (variant.fields.iter().zip(&fields)) .map(|(field, &id)| { let field_ty = self.field_ty(field.did, args); - let read = self.held_field(v().field(id), field.did, args, span); + let read = self.held_field(Translator::read_field(v(), id), field.did, args, span); let copy = self.snapshot(read, part(field_ty), span); self.field_value(copy, adt.did(), field.did, args, span) }) diff --git a/ts/src/lower/drop.rs b/ts/src/lower/drop.rs index 2b00f9209..d82143042 100644 --- a/ts/src/lower/drop.rs +++ b/ts/src/lower/drop.rs @@ -535,7 +535,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { } for (field, &id) in adt.non_enum_variant().fields.iter().zip(fields) { let ty = tcx.type_of(field.did).instantiate_identity().skip_norm_wip(); - if let Some(dropped) = self.drop_value(Expr::This.field(id), ty, span) { + if let Some(dropped) = self.drop_value(Translator::read_field(Expr::This, id), ty, span) { steps.push(StmtKind::Expr(dropped).at(None)); } } diff --git a/ts/src/lower/mir/call.rs b/ts/src/lower/mir/call.rs index 5e115617c..9f4e53c59 100644 --- a/ts/src/lower/mir/call.rs +++ b/ts/src/lower/mir/call.rs @@ -1455,7 +1455,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { fn set_borrowed(&mut self, (read, place): &Borrowed<'tcx>, value: Expr, span: Span, out: &mut Vec) { match place { Some(place) => out.extend(self.store(*place, value, span)), - None => out.push(StmtKind::Expr(read.clone().assign(value)).at(None)), + None => self.assign_to(read.clone(), value, None, out), } } @@ -1738,6 +1738,7 @@ fn continued(ty: Ty<'_>) -> Ty<'_> { /// Whether an assignment can write `e`: a path, an element, or a member of one. fn assignable(e: &Expr) -> bool { match e { + _ if super::place::split_target(e).is_some() => true, Expr::Index(..) => true, Expr::Member(base, _) | Expr::Located(_, base) => assignable(base), e => e.is_path(), diff --git a/ts/src/lower/mir/place.rs b/ts/src/lower/mir/place.rs index 9a32c203c..5832dae53 100644 --- a/ts/src/lower/mir/place.rs +++ b/ts/src/lower/mir/place.rs @@ -17,7 +17,7 @@ use rustc_span::Span; use super::{Env, Fx, Pending, Slot}; use crate::lower::library::{self, Flow, Std}; use crate::lower::num::Int; -use crate::lower::{AdtRepr, ObjectKind, byte_array, bytes_ref}; +use crate::lower::{AdtRepr, ObjectKind, StructField, Translator, byte_array, bytes_ref}; use crate::program::{SymKind, Symbol}; use crate::ts::{Expr, Item, Stmt, StmtKind, SymId}; @@ -209,7 +209,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { } } ProjectionElem::Field(field, _) => { - e = self.field(e, ty, pty.variant_index, field, span); + e = self.field(e, ty, pty.variant_index, field, span, out); } ProjectionElem::Downcast(_, variant) => { let base = Place { @@ -541,7 +541,15 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { } /// A field of `base`, a `ty`, in `variant` if it was downcast to one. - fn field(&mut self, base: Expr, ty: Ty<'tcx>, variant: Option, field: FieldIdx, span: Span) -> Expr { + fn field( + &mut self, + base: Expr, + ty: Ty<'tcx>, + variant: Option, + field: FieldIdx, + span: Span, + out: &mut Vec, + ) -> Expr { let tcx = self.tcx; match ty.kind() { ty::Tuple(_) => base.index(Expr::lit(field.index().to_string())), @@ -615,7 +623,14 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { let info = self.b.t.adt(*adt, span); let def = &adt.variant(variant.unwrap_or(rustc_abi::FIRST_VARIANT)).fields[field]; let read = match info.repr { - AdtRepr::Struct { fields, .. } => base.field(fields[field.index()]), + AdtRepr::Struct { fields, .. } => match fields[field.index()] { + whole @ StructField::Whole(_) => Translator::read_field(base, whole), + // Read from each of its fields, of one object. + parts @ StructField::Slice(_) => { + let base = self.path(base, "object", out); + Translator::read_field(base, parts) + } + }, AdtRepr::Slots(_) => { return self.err(span, format!("a field of union `{ty}` read apart from its place")); } @@ -744,10 +759,40 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { return out; } let target = self.place_expr(place, span, &mut out); - out.push(StmtKind::Expr(target.assign(value)).at(self.b.t.loc(span))); + let loc = self.b.t.loc(span); + self.assign_to(target, value, loc, &mut out); out } + /// `target = value`, where `target` may be a struct's byte slice field held as its + /// parts: each of its fields gets its part, in order, read before any is written where + /// a part reads a field written before it. + pub(super) fn assign_to(&mut self, target: Expr, value: Expr, loc: Option, out: &mut Vec) { + let Some((base, ids)) = split_target(&target) else { + out.push(StmtKind::Expr(target.assign(value)).at(loc)); + return; + }; + let base = base.clone(); + let value = match crate::lower::slice_parts(value.clone()) { + Some(_) => value, + None => self.path(value, "slice", out), + }; + let mut parts = crate::lower::slice_parts(value).expect("a path's parts"); + if (1..3).any(|k| reads_fields(&parts[k], &ids[..k])) { + for part in &mut parts { + if !part.is_constant() { + let name = self.b.fresh("part"); + let value = std::mem::replace(part, Expr::ident(&name)); + let stmt = StmtKind::decl(false, name, None, Some(value)).at(None); + self.emit(vec![stmt], false, out); + } + } + } + for (id, part) in ids.into_iter().zip(parts) { + out.push(StmtKind::Expr(base.clone().field(id).assign(part)).at(loc)); + } + } + /// What `place` is, where it is `*r` for an `r` that is the object it points at, with /// no place known to hold it: neither an alias of a borrowed place, nor an `rt.Place`, /// nor a closure's environment, nor an alias of a reference to no object, such as a @@ -1014,6 +1059,10 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { /// object too unless it is `fixed`, which can't change while borrowed. pub(super) fn stable_path(&mut self, target: Expr, fixed: bool, span: Span, out: &mut Vec) -> Expr { self.materialize(&target, out); + if let Some((base, ids)) = split_target(&target) { + let base = self.stable_object(base.clone(), fixed, out); + return Translator::read_field(base, StructField::Slice(ids)); + } match target { Expr::Located(_, e) => self.stable_path(*e, fixed, span, out), // A payload `Option::insert` lends, read as its type. @@ -1073,7 +1122,17 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { None => target.clone(), }; let get = Expr::Lambda(Vec::new(), Box::new(read)); - let set = Expr::Lambda(vec![param], Box::new(target.assign(Expr::ident(value)))); + let set = match split_target(&target) { + // The value's parts, which are its own. + Some((base, ids)) => { + let parts = crate::lower::slice_parts(Expr::ident(&value)).expect("a name's parts"); + let sets = (ids.into_iter().zip(parts)) + .map(|(id, part)| StmtKind::Expr(base.clone().field(id).assign(part)).at(None)) + .collect(); + Expr::Arrow(vec![param], crate::ts::Type::Builtin("void"), sets) + } + None => Expr::Lambda(vec![param], Box::new(target.assign(Expr::ident(value)))), + }; Expr::rt("place", vec![get, set]) } @@ -1774,6 +1833,41 @@ pub fn slice_member(slice: Expr, key: &str) -> Expr { slice.member(key) } +/// The object and fields of `target`, if it is a struct's byte slice field held as its +/// parts, as [`Translator::read_field`] reads it. +pub fn split_target(target: &Expr) -> Option<(&Expr, [crate::ts::FieldId; 3])> { + fn field(e: &Expr) -> Option<(&Expr, crate::ts::FieldId)> { + match e { + Expr::Member(base, crate::ts::Key::Field(id)) => Some((base, *id)), + _ => None, + } + } + let [data, start, length] = made_slice(target)?; + let ((base, d), (b1, s), (b2, l)) = (field(data)?, field(start)?, field(length)?); + (base == b1 && base == b2).then_some((base, [d, s, l])) +} + +/// Whether `e` may read any of the fields `ids`, of any object. +fn reads_fields(e: &Expr, ids: &[crate::ts::FieldId]) -> bool { + let any = |es: &[Expr]| es.iter().any(|e| reads_fields(e, ids)); + match e { + Expr::Ident(_) | Expr::This | Expr::Sym(_) | Expr::Rt(_) | Expr::Lit(_) | Expr::Str(_) => false, + Expr::Member(base, key) => { + matches!(key, crate::ts::Key::Field(id) if ids.contains(id)) || reads_fields(base, ids) + } + Expr::Array(items) => any(items), + Expr::Object(props) => props.iter().any(|(_, e)| reads_fields(e, ids)), + Expr::Index(a, b) | Expr::Binary(a, _, b) | Expr::Assign(a, b) => reads_fields(a, ids) || reads_fields(b, ids), + Expr::Call(f, args) | Expr::New(f, args) => reads_fields(f, ids) || any(args), + Expr::Cond(c, a, b) => reads_fields(c, ids) || reads_fields(a, ids) || reads_fields(b, ids), + Expr::Unary(_, e) | Expr::Postfix(e, _) | Expr::As(e, _) | Expr::Instantiate(e, _) | Expr::Located(_, e) => { + reads_fields(e, ids) + } + // What a function reads when called is unknown here. + Expr::Arrow(..) | Expr::Lambda(..) => true, + } +} + /// The parts of `slice`, if it is an `rt.ByteSlice` made in place. pub fn made_slice(slice: &Expr) -> Option<&[Expr; 3]> { match slice { diff --git a/ts/src/lower/mir/rvalue.rs b/ts/src/lower/mir/rvalue.rs index e718796f9..78108edf7 100644 --- a/ts/src/lower/mir/rvalue.rs +++ b/ts/src/lower/mir/rvalue.rs @@ -13,7 +13,7 @@ use super::{Capture, Env, Fx, Slot}; use crate::lower::library::Std; use crate::lower::num::{self, Int}; use crate::lower::pointer::Pointee; -use crate::lower::{AdtRepr, byte_array, field_name}; +use crate::lower::{AdtRepr, StructField, byte_array, field_name}; use crate::ts::{Expr, Key, Param, Stmt, StmtKind, Type}; impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { @@ -514,6 +514,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { if let Some(same) = self.rebuilt_result(kind, ops, &values, ty, span, out) { return Some(same); } + let values = self.held_slices(ty, values, span, out); self.adt(*def_id, *variant, args, values, ty, span) } _ => unreachable!(), @@ -701,6 +702,28 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { } } + /// `values`, a struct `ty`'s fields', with each that its struct holds as a byte slice's + /// parts held in a path unless it is made of them, so each part is read once. + fn held_slices(&mut self, ty: Ty<'tcx>, values: Vec, span: Span, out: &mut Vec) -> Vec { + let ty::Adt(adt, _) = ty.kind() else { + return values; + }; + if !adt.is_struct() || Std::of(self.tcx, adt.did()).is_some() || !self.b.t.own(adt.did()) { + return values; + } + let AdtRepr::Struct { fields, .. } = self.b.t.adt(*adt, span).repr else { + return values; + }; + (values.into_iter().zip(fields)) + .map(|(value, field)| match field { + StructField::Slice(_) if crate::lower::slice_parts(value.clone()).is_none() => { + self.path(value, "slice", out) + } + _ => value, + }) + .collect() + } + /// A closure that captures nothing, as an arrow function. pub(super) fn bare_closure( &mut self, @@ -772,9 +795,11 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { ty::Ref(..) => in_var && !self.stable_name(&value), _ => true, }; - // A constant is its own snapshot, unless the closure reassigns it. + // A constant is its own snapshot, unless the closure reassigns it, and so are a + // slice's parts held where they read the same later. let rebinds = rebound.contains(&field); - let constant = value.is_constant(); + let constant = value.is_constant() + || super::place::made_slice(&value).is_some_and(|parts| parts.iter().all(|p| self.steady_expr(p))); let value = match snapshot && (rebinds || !constant) { false => value, true => { diff --git a/ts/src/lower/mir/scalar.rs b/ts/src/lower/mir/scalar.rs index b3eea8221..1317ceea4 100644 --- a/ts/src/lower/mir/scalar.rs +++ b/ts/src/lower/mir/scalar.rs @@ -826,26 +826,39 @@ impl<'tcx> Fx<'_, '_, '_, 'tcx> { /// flag stands for one, a subslice, or a copy of a local held as its parts, alone or /// in a tuple. A tuple of objects alone already folds into its reads. fn parts_def(&self, rvalue: &Rvalue<'tcx>, shapes: &FxHashMap>) -> bool { - let held = |p: &Place<'tcx>| { + let held = |fx: &Self, p: &Place<'tcx>| { shapes .get(&p.local) .is_some_and(|s| walk(s, p.projection).1 == p.projection.len()) + || fx.split_field(*p) }; match rvalue { - Rvalue::Use(Operand::Copy(p) | Operand::Move(p), _) | Rvalue::CopyForDeref(p) => held(p), + Rvalue::Use(Operand::Copy(p) | Operand::Move(p), _) | Rvalue::CopyForDeref(p) => held(self, p), Rvalue::Ref(_, _, p) => self .reborrow(*p) - .is_some_and(|(base, range)| range.is_some() || held(&base)), + .is_some_and(|(base, range)| range.is_some() || held(self, &base)), Rvalue::Aggregate(kind, ops) => { matches!(**kind, AggregateKind::Adt(..)) || ops .iter() - .any(|op| matches!(op, Operand::Copy(p) | Operand::Move(p) if held(p))) + .any(|op| matches!(op, Operand::Copy(p) | Operand::Move(p) if held(self, p))) } _ => false, } } + /// Whether `place` is a struct's byte slice field that its struct holds as its parts. + fn split_field(&self, place: Place<'tcx>) -> bool { + let [base @ .., ProjectionElem::Field(field, _)] = place.projection.as_slice() else { + return false; + }; + let ty = Place::ty_from(place.local, base, &self.mir.local_decls, self.tcx).ty; + matches!(ty.kind(), ty::Adt(adt, _) if adt.is_struct() + && library::Std::of(self.tcx, adt.did()).is_none() + && self.b.t.own(adt.did()) + && self.b.t.split(&adt.non_enum_variant().fields[*field])) + } + /// What a shared borrow of bytes through a reference to them borrows: the reference, /// and the range of them, as `&*s` or `&s[a..b]` of an array or slice pattern make it. fn reborrow(&self, p: Place<'tcx>) -> Option<(Place<'tcx>, Option>)> { @@ -2022,6 +2035,12 @@ impl<'tcx> Fx<'_, '_, '_, 'tcx> { match shape { Shape::Unit => Vec::new(), Shape::Leaf(_) => vec![self.leaf(value)], + // Each part a struct's fields hold may change, so it reads the same later only + // if it is held. + Shape::Slice if let Some(parts) = super::place::made_slice(&value) => parts + .iter() + .map(|part| (part.clone(), self.steady_expr(part))) + .collect(), Shape::Slice => { let (slice, _) = self.steady_value(value, "slice", Type::Rt("ByteSlice", Vec::new()), out); ["data", "start", "length"] @@ -2132,7 +2151,7 @@ impl<'tcx> Fx<'_, '_, '_, 'tcx> { /// Whether `e` reads the same wherever it is read later, and does nothing: a constant, /// a name assigned once and not held back, or what is made of those without reading /// memory, as a cast, a comparison, or an object literal. - fn steady_expr(&self, e: &Expr) -> bool { + pub(super) fn steady_expr(&self, e: &Expr) -> bool { match e { Expr::Lit(_) | Expr::Str(_) | Expr::Rt(_) => true, Expr::Ident(name) => { diff --git a/ts/src/lower/mod.rs b/ts/src/lower/mod.rs index c84d658ca..0549c211a 100644 --- a/ts/src/lower/mod.rs +++ b/ts/src/lower/mod.rs @@ -312,7 +312,7 @@ pub struct Adt { #[derive(Clone)] pub enum AdtRepr { /// A struct: a plain object, or a class if it may drop, whose fields are named at print time. - Struct { ctor: Ctor, fields: Vec }, + Struct { ctor: Ctor, fields: Vec }, /// An enum whose variants carry no data: a string per variant. Tags, /// An enum: an object per variant, tagged by its name. @@ -321,6 +321,15 @@ pub enum AdtRepr { Slots(unions::Fields), } +/// The JS fields of a struct's field. +#[derive(Clone, Copy, Debug)] +pub enum StructField { + Whole(FieldId), + /// A private `&[u8]` or `&[u8; N]`: its bytes, start, and length, so a cursor moving + /// through it makes no object. + Slice([FieldId; 3]), +} + /// A JS object that a `&mut` can change in place. #[derive(Clone, Copy, PartialEq, Eq)] pub enum ObjectKind { @@ -487,22 +496,48 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { self.program.plain.contains(&sym) } - /// A struct's value, from its fields' values in declaration order. - pub fn construct(&self, sym: SymId, ctor: &Ctor, fields: &[FieldId], values: Vec) -> ts::Expr { + /// A struct's value, from its fields' values in declaration order: a byte slice's held + /// as its parts is made of them, or read from a path or `undefined`. + pub fn construct(&self, sym: SymId, ctor: &Ctor, fields: &[StructField], values: Vec) -> ts::Expr { + let mut props = Vec::with_capacity(values.len()); + for (field, value) in fields.iter().zip(values) { + match *field { + StructField::Whole(id) => props.push((id, value)), + StructField::Slice(ids) => { + let parts = slice_parts(value).expect("a byte slice field's value made of its parts or a path"); + props.extend(ids.into_iter().zip(parts)); + } + } + } let class = Box::new(ts::Expr::Sym(sym)); match ctor { - _ if self.plain(sym) => ts::Expr::Object( - fields - .iter() - .zip(values) - .map(|(f, v)| (ts::Key::Field(*f), v)) - .collect(), - ), + _ if self.plain(sym) => ts::Expr::Object(props.into_iter().map(|(f, v)| (ts::Key::Field(f), v)).collect()), Ctor::Unit => ts::Expr::New(class, Vec::new()), - Ctor::Positional => ts::Expr::New(class, values), + Ctor::Positional => ts::Expr::New(class, props.into_iter().map(|(_, v)| v).collect()), } } + /// The value of field `field` of `base`, a path: a byte slice's held as its parts made + /// of them. + pub fn read_field(base: ts::Expr, field: StructField) -> ts::Expr { + match field { + StructField::Whole(id) => base.field(id), + StructField::Slice(ids) => ts::Expr::New( + Box::new(ts::Expr::Rt("ByteSlice".into())), + ids.map(|id| base.clone().field(id)).to_vec(), + ), + } + } + + /// Whether a struct's `field` is held as a byte slice's parts: a `&[u8]` or `&[u8; N]` + /// that only the crate's own code can name. + pub fn split(&self, field: &ty::FieldDef) -> bool { + let ty = self.tcx.type_of(field.did).instantiate_identity().skip_norm_wip(); + matches!(ty.kind(), ty::Ref(_, inner, rustc_ast::Mutability::Not) + if (matches!(inner.kind(), ty::Slice(e) if matches!(e.kind(), ty::Uint(ty::UintTy::U8))) || byte_array(*inner))) + && !self.tcx.visibility(field.did).is_public() + } + /// Translate every queued function instance, and those they reach. /// /// A census leaves an instance over type parameters to its own function's turn, since @@ -1042,7 +1077,13 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { let mut info = Adt { sym, repr }; if let AdtRepr::Struct { fields, .. } = &mut info.repr { for (i, field) in adt.non_enum_variant().fields.iter().enumerate() { - fields.push(self.program.field(sym, field_name(field.name.as_str(), i))); + let name = field_name(field.name.as_str(), i); + fields.push(match self.split(field) { + true => StructField::Slice( + ["Data", "Start", "Len"].map(|key| self.program.field(sym, format!("{name}{key}"))), + ), + false => StructField::Whole(self.program.field(sym, name)), + }); } } self.adts.insert(did, info.clone()); @@ -1067,17 +1108,36 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { let item = match &info.repr { AdtRepr::Struct { ctor, fields } => { let variant = adt.non_enum_variant(); - let fields = (variant.fields.iter().zip(fields)) - .map(|(field, &id)| { - let span = tcx.def_span(field.did); - ts::Field { + let mut defined = Vec::with_capacity(fields.len()); + for (field, &held) in variant.fields.iter().zip(fields) { + let span = tcx.def_span(field.did); + let doc = self.doc(field.did); + match held { + StructField::Whole(id) => defined.push(ts::Field { id, ty: self.defined_field_ty(field, identity, span), - doc: self.doc(field.did), + doc, init: ts::FieldInit::Ctor, + }), + StructField::Slice(ids) => { + let tys = [ + Type::Builtin("Uint8Array"), + Type::Builtin("number"), + Type::Builtin("number"), + ]; + let docs = [doc, None, None]; + for ((id, ty), doc) in ids.into_iter().zip(tys).zip(docs) { + defined.push(ts::Field { + id, + ty, + doc, + init: ts::FieldInit::Ctor, + }); + } } - }) - .collect(); + } + } + let fields = defined; Item::Struct { sym, params, @@ -1927,6 +1987,42 @@ pub fn byte_array(ty: Ty<'_>) -> bool { matches!(ty.kind(), ty::Array(elem, _) if matches!(elem.kind(), ty::Uint(ty::UintTy::U8))) } +/// The parts of `slice`, an `rt.ByteSlice`, each read once: those it is made of, or those of a +/// path or a slice made from a view of known length, or `undefined`'s, as a value not yet +/// written. `None` for what must be held in a path first. +pub fn slice_parts(slice: ts::Expr) -> Option<[ts::Expr; 3]> { + use ts::Expr; + let mut slice = slice; + while let Expr::Located(_, e) | Expr::As(e, _) = slice { + slice = *e; + } + if let Some(parts) = mir::made_slice(&slice) { + return Some(parts.clone()); + } + match slice { + Expr::Lit(text) if text == "undefined" => Some([(); 3].map(|()| Expr::undefined())), + e if e.is_path() => Some(["data", "start", "length"].map(|key| e.clone().member(key))), + Expr::Call(f, mut args) if matches!(&*f, Expr::Rt(name) if name == "byteSliceFrom") && args.len() == 1 => { + let bytes = args.pop().expect("one argument"); + let length = match &bytes { + e if e.is_path() => e.clone().member("length"), + Expr::Call(of, items) if matches!(&**of, Expr::Member(base, ts::Key::Name(k)) if k == "of" && matches!(&**base, Expr::Ident(n) if n == "Uint8Array")) => { + Expr::lit(items.len().to_string()) + } + Expr::New(class, len) + if matches!(&**class, Expr::Ident(n) if n == "Uint8Array") + && matches!(len.as_slice(), [Expr::Lit(_)]) => + { + len[0].clone() + } + _ => return None, + }; + Some([bytes, Expr::lit("0"), length]) + } + _ => None, + } +} + /// Whether `ty` is a reference to a `[u8; N]`, which is an `rt.ByteSlice` of its bytes, as a /// `&[u8]` is: a split's chunk is then no view. pub fn bytes_ref(ty: Ty<'_>) -> bool { diff --git a/ts/src/lower/unions.rs b/ts/src/lower/unions.rs index a917dbf97..c5dc8fed0 100644 --- a/ts/src/lower/unions.rs +++ b/ts/src/lower/unions.rs @@ -194,7 +194,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { }; let field = &adt.non_enum_variant().fields[FieldIdx::from_usize(first)]; let part_ty = self.field_ty(field.did, args); - let read = self.held_field(value.field(fields[first]), field.did, args, span); + let read = self.held_field(Translator::read_field(value, fields[first]), field.did, args, span); self.part_of(read, part_ty, rest, span) } _ => unreachable!("a union's part is in a tuple or a struct"), From c6013ceffe9ae5acaf31233c28b771aff84d24c9 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 1 Nov 2026 01:04:41 -0800 Subject: [PATCH 3/4] chore(quest): drop the fields quest, leaving the epic its Results of scalars Co-Authored-By: Claude Opus 5.5 --- quest/m2/ts-byte-views/README.md | 14 ++++++++------ quest/m2/ts-byte-views/fields.md | 25 ------------------------- 2 files changed, 8 insertions(+), 31 deletions(-) delete mode 100644 quest/m2/ts-byte-views/fields.md diff --git a/quest/m2/ts-byte-views/README.md b/quest/m2/ts-byte-views/README.md index 68dda5837..f99543d90 100644 --- a/quest/m2/ts-byte-views/README.md +++ b/quest/m2/ts-byte-views/README.md @@ -1,4 +1,4 @@ -# Byte slices without objects +# [M] Byte slices without objects ## Goal @@ -17,17 +17,19 @@ kept them as `(data, start, length)` made Chrome's varint decode 2.3-2.9x and fr Decided with the maintainer on 2026-10-27: scalar-replace values built of byte slices, scalars, tuples, `Option`, `Result`, and `ControlFlow`, materializing an object only where -one escapes, in pieces that each land and measure alone (the children below). +one escapes, in pieces that each land and measure alone. The precedents are `stepped` iterators, `range_nexts`, and `place_opts`, which a general pass could later absorb. +Slices now pass as their parts, a `Result` holding one comes back in a slot +([#160](https://github.com/moq-dev/rust2/pull/160)), and a private slice field is three +fields ([#379](https://github.com/moq-dev/rust2/pull/379)). What remains is a `Result` or +tuple of scalars alone: `Decoder::varint` still returns an `rt.Ok` per read, and the +bench's `frame_header` an `rt.Ok` of a pair per frame. + Left here: iterating a byte slice makes one view, which `rt.iterSlice` and stepping in place read. Measure Chrome, Firefox, Node, and Bun; Chrome decides. Public API: the translator's output for byte slices. - -## Required - -- [Byte slices in fields](/quest/m2/ts-byte-views/fields.md) - a private slice field is three fields diff --git a/quest/m2/ts-byte-views/fields.md b/quest/m2/ts-byte-views/fields.md deleted file mode 100644 index 308e947f8..000000000 --- a/quest/m2/ts-byte-views/fields.md +++ /dev/null @@ -1,25 +0,0 @@ -# [M] Byte slices in fields - -## Goal - -A private byte slice field, such as `Decoder::buf`, is three JS fields, so a cursor that -advances through it makes no object. Chrome's varint and frame decode on `just ts bench` -are faster than with slices passed and returned as parts alone -([#160](https://github.com/moq-dev/rust2/pull/160)), with every differential test passing. - -## Plan - -The [epic's](/quest/m2/ts-byte-views/README.md) hand edit with all three pieces measured -Chrome 2.3-2.9x on varint decode and 2.0-2.2x on frames against [#120](https://github.com/moq-dev/rust2/pull/120), Firefox 2.6-3.5x, -Node and Bun 1.9-3.4x. - -Decided with the maintainer on 2026-10-27: private fields split even on root types such as -`Decoder`, since they are not Rust API. - -- `mem::take(&mut self.buf)` lowers on the three fields. Any other `&mut` to the field - leaves that field an object crate-wide, which needs a pass over every reachable body - before structs are emitted. -- `Debug` and `Clone` rebuild the slice from the fields. - -Public API: the translator's output; a private slice field of an exported class becomes -three `#private` fields. From bb9b3cb399ced548e444c2a8fe5264e2799540b0 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sun, 1 Nov 2026 01:58:02 -0800 Subject: [PATCH 4/4] fix(ts): read a made struct's fields by Rust field past a byte slice's parts Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/lang/expected/fields.ts | 76 +++++++++++++++-------------- ts/fixtures/lang/src/fields.rs | 7 +++ ts/src/lower/mir/mod.rs | 35 ++++++++++--- ts/src/lower/mod.rs | 11 +++-- 4 files changed, 84 insertions(+), 45 deletions(-) diff --git a/ts/fixtures/lang/expected/fields.ts b/ts/fixtures/lang/expected/fields.ts index ee3363cd7..3e70b1343 100644 --- a/ts/fixtures/lang/expected/fields.ts +++ b/ts/fixtures/lang/expected/fields.ts @@ -20,11 +20,15 @@ export const SCANNER: Scanner.Scanner = { bufData: Uint8Array.of(120, 121, 122), export function fields(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { let two!: Glued.Glued; const n: rt.usize = rt.u8Rem(kind, 5) as rt.usize; + const _12: rt.usize = kind as rt.usize; + const _8: rt.ByteSlice = rt.byteSliceRange(data, rt.min(n, data.length)); + rt.byteVecPush(out, _12 & 0xff); + rt.byteVecExtendFromSlice(out, rt.byteSliceRange(new rt.ByteSlice(_8.data, _8.start, _8.length), 0, rt.min(_8.length, 2))); const r: Scanner.Scanner = { bufData: data.data, bufStart: data.start, bufLen: data.length, read: 0 }; while (true) { - const _8: rt.ByteSlice | undefined = Scanner.take(r, rt.max(n, 1)); - if (_8 !== undefined) { - rt.byteVecExtendFromSlice(out, rt.byteSliceRange(_8, 0, rt.min(_8.length, 1))); + const _22: rt.ByteSlice | undefined = Scanner.take(r, rt.max(n, 1)); + if (_22 !== undefined) { + rt.byteVecExtendFromSlice(out, rt.byteSliceRange(_22, 0, rt.min(_22.length, 1))); if (r.read > 12) { break; } @@ -33,11 +37,11 @@ export function fields(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { } } const r2: Scanner.Scanner = { bufData: data.data, bufStart: data.start, bufLen: data.length, read: 0 }; - const _22: rt.ByteSlice | undefined = Scanner.takeOrRewind(r2, rt.usizeAdd(n, 3)); - if (_22 === undefined) { + const _36: rt.ByteSlice | undefined = Scanner.takeOrRewind(r2, rt.usizeAdd(n, 3)); + if (_36 === undefined) { rt.byteVecPush(out, 200); } else { - rt.byteVecPush(out, _22.length & 0xff); + rt.byteVecPush(out, _36.length & 0xff); } rt.byteVecPush(out, r2.bufLen & 0xff); const before: Scanner.Scanner = Scanner.clone$Clone(r2); @@ -47,11 +51,11 @@ export function fields(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecPush(out, Scanner.rest(r2).length & 0xff); rt.byteVecPush(out, r2.bufLen & 0xff); const s: Scanner.Scanner = { bufData: data.data, bufStart: data.start, bufLen: data.length, read: 0 }; - const _56: rt.ByteSlice = rt.byteSliceRange(data, rt.usizeDiv(data.length, 2)); + const _70: rt.ByteSlice = rt.byteSliceRange(data, rt.usizeDiv(data.length, 2)); const old2 = new rt.ByteSlice(s.bufData, s.bufStart, s.bufLen); - s.bufData = _56.data; - s.bufStart = _56.start; - s.bufLen = _56.length; + s.bufData = _70.data; + s.bufStart = _70.start; + s.bufLen = _70.length; rt.byteVecPush(out, rt.usizeSub(old2.length, s.bufLen) & 0xff); const swapped = new rt.ByteSlice(r2.bufData, r2.bufStart, r2.bufLen); r2.bufData = s.bufData; @@ -72,52 +76,52 @@ export function fields(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { r2.bufStart = value2.start; r2.bufLen = value2.length; }), 1); - const _85: rt.ByteSlice = rt.byteSliceRange(new rt.ByteSlice(r2.bufData, r2.bufStart, r2.bufLen), rt.min(new rt.ByteSlice(r2.bufData, r2.bufStart, r2.bufLen).length, 1)); - r2.bufData = _85.data; - r2.bufStart = _85.start; - r2.bufLen = _85.length; + const _99: rt.ByteSlice = rt.byteSliceRange(new rt.ByteSlice(r2.bufData, r2.bufStart, r2.bufLen), rt.min(new rt.ByteSlice(r2.bufData, r2.bufStart, r2.bufLen).length, 1)); + r2.bufData = _99.data; + r2.bufStart = _99.start; + r2.bufLen = _99.length; rt.byteVecPush(out, r2.bufLen & 0xff); - const _92: rt.ByteSlice = rt.byteSliceRange(new rt.ByteSlice(r2.bufData, r2.bufStart, r2.bufLen), rt.usizeDiv(r2.bufLen, 2)); - r2.bufData = _92.data; - r2.bufStart = _92.start; - r2.bufLen = _92.length; + const _106: rt.ByteSlice = rt.byteSliceRange(new rt.ByteSlice(r2.bufData, r2.bufStart, r2.bufLen), rt.usizeDiv(r2.bufLen, 2)); + r2.bufData = _106.data; + r2.bufStart = _106.start; + r2.bufLen = _106.length; rt.byteVecPush(out, r2.bufLen & 0xff); - const _100: rt.ByteSlice | undefined = rt.byteSliceGetRange(data, 0, 2); - if (_100 !== undefined) { - const p: Couple.Couple = { headData: data.data, headStart: data.start, headLen: data.length, wordData: _100.data, wordStart: _100.start, wordLen: _100.length }; + const _114: rt.ByteSlice | undefined = rt.byteSliceGetRange(data, 0, 2); + if (_114 !== undefined) { + const p: Couple.Couple = { headData: data.data, headStart: data.start, headLen: data.length, wordData: _114.data, wordStart: _114.start, wordLen: _114.length }; const q: Couple.Couple = copy$fields_Couple(p); - const _105: rt.ByteSlice = rt.byteSliceRange(new rt.ByteSlice(p.headData, p.headStart, p.headLen), 1); - p.headData = _105.data; - p.headStart = _105.start; - p.headLen = _105.length; + const _119: rt.ByteSlice = rt.byteSliceRange(new rt.ByteSlice(p.headData, p.headStart, p.headLen), 1); + p.headData = _119.data; + p.headStart = _119.start; + p.headLen = _119.length; rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(p.headLen & 0xff, q.headLen & 0xff, Couple.eq$cmp_PartialEq(p, q) ? 1 : 0))); rt.byteVecExtendFromSlice(out, new rt.ByteSlice(q.wordData, q.wordStart, q.wordLen)); const args2: rt.Fmt[] = [(f: rt.Formatter) => Couple.fmt$fmt_Debug(p, f)]; rt.byteVecExtendFromSlice(out, rt.stringAsBytes(rt.fmtFormat([args2[0]]))); } - const _134: rt.ByteSlice = rt.byteSliceRange(data, 0, rt.min(data.length, 3)); - const open: Exposed.Exposed = { buf: _134, at: kind }; + const _148: rt.ByteSlice = rt.byteSliceRange(data, 0, rt.min(data.length, 3)); + const open: Exposed.Exposed = { buf: _148, at: kind }; const args4: rt.Fmt[] = [(f: rt.Formatter) => Exposed.fmt$fmt_Debug(open, f), (f: rt.Formatter) => Scanner.fmt$fmt_Debug(r2, f)]; rt.byteVecExtendFromSlice(out, rt.stringAsBytes(rt.fmtFormat([args4[0], args4[1]]))); - rt.byteVecPush(out, kind ^ (_134.length & 0xff)); + rt.byteVecPush(out, kind ^ (_148.length & 0xff)); if (!(rt.u8Rem(kind, 2) === 0)) { - const _161: Glued.Glued = { _0Data: Uint8Array.of(97, 98), _0Start: 0, _0Len: 2, _1Data: Uint8Array.of(), _1Start: 0, _1Len: 0 }; - two = { _0Data: data.data, _0Start: data.start, _0Len: data.length, _1Data: _161._0Data, _1Start: _161._0Start, _1Len: _161._0Len }; + const _175: Glued.Glued = { _0Data: Uint8Array.of(97, 98), _0Start: 0, _0Len: 2, _1Data: Uint8Array.of(), _1Start: 0, _1Len: 0 }; + two = { _0Data: data.data, _0Start: data.start, _0Len: data.length, _1Data: _175._0Data, _1Start: _175._0Start, _1Len: _175._0Len }; } else { two = { _0Data: Uint8Array.of(97, 98), _0Start: 0, _0Len: 2, _1Data: Uint8Array.of(), _1Start: 0, _1Len: 0 }; } const args6: rt.Fmt[] = [(f: rt.Formatter) => Glued.fmt$fmt_Debug(two, f)]; rt.byteVecExtendFromSlice(out, rt.stringAsBytes(rt.fmtFormat([args6[0]]))); rt.byteVecPush(out, rt.usizeAdd(two._0Len, two._1Len) & 0xff); - const _181: Scanner.Scanner = SCANNER; - rt.byteVecExtendFromSlice(out, new rt.ByteSlice(_181.bufData, _181.bufStart, _181.bufLen)); + const _195: Scanner.Scanner = SCANNER; + rt.byteVecExtendFromSlice(out, new rt.ByteSlice(_195.bufData, _195.bufStart, _195.bufLen)); rt.byteVecPush(out, SCANNER.read & 0xff); rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(rt.u32ToBeBytes(Labeled.sum$u8(new Labeled.Labeled(kind, data.data, data.start, data.length))).reverse())); const g: Sentry.Sentry = new Sentry.Sentry(data.data, data.start, data.length, out); - const _194: rt.ByteSlice = rt.byteSliceRange(new rt.ByteSlice(g.bufData, g.bufStart, g.bufLen), rt.min(g.bufLen, 2)); - g.bufData = _194.data; - g.bufStart = _194.start; - g.bufLen = _194.length; + const _208: rt.ByteSlice = rt.byteSliceRange(new rt.ByteSlice(g.bufData, g.bufStart, g.bufLen), rt.min(g.bufLen, 2)); + g.bufData = _208.data; + g.bufStart = _208.start; + g.bufLen = _208.length; rt.byteVecPush(g.out, option_unwrapOr$0r2bmpr(rt.byteSliceFirst(new rt.ByteSlice(g.bufData, g.bufStart, g.bufLen)), 0)); g[Symbol.dispose](); } diff --git a/ts/fixtures/lang/src/fields.rs b/ts/fixtures/lang/src/fields.rs index b3f76309e..f91777a69 100644 --- a/ts/fixtures/lang/src/fields.rs +++ b/ts/fixtures/lang/src/fields.rs @@ -82,6 +82,13 @@ impl Drop for Sentry<'_, '_> { pub fn fields(kind: u8, data: &[u8], out: &mut Vec) { let n = usize::from(kind % 5); + // Fields read from a struct made once, the one after the slice included. + let made = Scanner { + buf: &data[n.min(data.len())..], + read: usize::from(kind), + }; + out.push(made.read as u8); + out.extend_from_slice(&made.buf[..made.buf.len().min(2)]); // Reads, splits, and a closure that writes the field back. let mut r = Scanner { buf: data, read: 0 }; while let Some(head) = r.take(n.max(1)) { diff --git a/ts/src/lower/mir/mod.rs b/ts/src/lower/mir/mod.rs index 4ad1127b0..3de8956e0 100644 --- a/ts/src/lower/mir/mod.rs +++ b/ts/src/lower/mir/mod.rs @@ -2290,8 +2290,14 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { .fold((effect, memory), |(e, m), p| (e || p.effect, m || p.memory)); let mut folded = self.fold_exprs(Vec::new(), vec![value], true, out); let value = folded.pop().unwrap(); - let parts = - parts.and_then(|rereadable| Some(parts_of(&value)?.into_iter().zip(rereadable).collect())); + let parts = parts.and_then(|rereadable| { + Some( + parts_of(&value, &self.b.t.slices)? + .into_iter() + .zip(rereadable) + .collect(), + ) + }); let mut bare = &value; while let Expr::Located(_, e) = bare { bare = e; @@ -2311,7 +2317,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { kind: StmtKind::Expr(Expr::Assign(_, value)), .. }) = out.last() - && let Some(parts) = parts_of(value) + && let Some(parts) = parts_of(value, &self.b.t.slices) { self.var_parts.insert(local, parts); } @@ -3774,14 +3780,31 @@ fn visit_expr(e: &Expr, f: &mut dyn FnMut(&Expr)) { } /// The operands of an aggregate, from its expression. -fn parts_of(value: &Expr) -> Option> { +fn parts_of(value: &Expr, slices: &FxHashSet) -> Option> { match value { Expr::Array(items) => Some(items.clone()), - Expr::As(inner, _) => parts_of(inner), + Expr::As(inner, _) => parts_of(inner, slices), Expr::Call(f, items) if matches!(&**f, Expr::Member(base, crate::ts::Key::Name(of)) if of == "of" && matches!(&**base, Expr::Ident(u) if u == "Uint8Array")) => { Some(items.clone()) } - Expr::Object(props) => Some(props.iter().map(|(_, v)| v.clone()).collect()), + // A byte slice field held as its parts, `slices` its bytes', is one operand. + Expr::Object(props) => { + let mut parts = Vec::with_capacity(props.len()); + let mut props = props.iter(); + while let Some((key, value)) = props.next() { + parts.push(match key { + crate::ts::Key::Field(id) if slices.contains(id) => { + let (start, length) = (props.next()?.1.clone(), props.next()?.1.clone()); + Expr::New( + Box::new(Expr::Rt("ByteSlice".into())), + vec![value.clone(), start, length], + ) + } + _ => value.clone(), + }); + } + Some(parts) + } Expr::New(f, items) if matches!(&**f, Expr::Rt(name) if name == "Range" || name == "RangeFrom") => { Some(items.clone()) } diff --git a/ts/src/lower/mod.rs b/ts/src/lower/mod.rs index 0549c211a..a849c07e4 100644 --- a/ts/src/lower/mod.rs +++ b/ts/src/lower/mod.rs @@ -266,6 +266,8 @@ pub struct Translator<'a, 'tcx> { slotted: rustc_data_structures::fx::FxHashMap, Option>>, /// The slot of each `Ok` payload type, which every function returning one shares. slots: rustc_data_structures::fx::FxHashMap, SymId>, + /// The field of each struct's byte slice field held as its parts that holds its bytes. + pub slices: rustc_data_structures::fx::FxHashSet, } /// The crates that translate as one program: the package, and every crate it depends on @@ -488,6 +490,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { parting: None, slotted: Default::default(), slots: Default::default(), + slices: Default::default(), }) } @@ -1079,9 +1082,11 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { for (i, field) in adt.non_enum_variant().fields.iter().enumerate() { let name = field_name(field.name.as_str(), i); fields.push(match self.split(field) { - true => StructField::Slice( - ["Data", "Start", "Len"].map(|key| self.program.field(sym, format!("{name}{key}"))), - ), + true => { + let ids = ["Data", "Start", "Len"].map(|key| self.program.field(sym, format!("{name}{key}"))); + self.slices.insert(ids[0]); + StructField::Slice(ids) + } false => StructField::Whole(self.program.field(sym, name)), }); }