From e6cba62f8ed443662e39f47ffc42d66be07e54ba Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 21:22:22 -0700 Subject: [PATCH 1/3] chore(quest): claim quest/m3/ts-byte-edits.md Quest-Claim: 3c0f810a2cea45eb From 90405464ed835e6b2d46b0131c384c78f1f9b514 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 21:41:33 -0700 Subject: [PATCH 2/3] feat(ts): translate Vec and [u8] edits and sorts Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/lang/expected/edits.ts | 171 ++++++++++++++++++++++++++- ts/fixtures/lang/expected/harness.ts | 66 +++++------ ts/fixtures/lang/src/edits.rs | 139 ++++++++++++++++++++++ ts/fixtures/lang/src/harness.rs | 1 + ts/runtime/rt.ts | 131 ++++++++++++++++---- ts/src/lower/collections.rs | 1 + ts/src/lower/library.rs | 52 +++++--- 7 files changed, 488 insertions(+), 73 deletions(-) diff --git a/ts/fixtures/lang/expected/edits.ts b/ts/fixtures/lang/expected/edits.ts index 2f948f9a9..22af1c43a 100644 --- a/ts/fixtures/lang/expected/edits.ts +++ b/ts/fixtures/lang/expected/edits.ts @@ -188,7 +188,7 @@ export function edits(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { for (const _272 of runs) { putU16s(out, [_272[0], unwrapOr$Option_u16(_272[1], 65535)]); } - rt.sliceReverse(v5); + v5.reverse(); putU16s(out, v5); const named: Named.Named[] = rt.iterToArray(rt.iterMap(rt.iterSlice(words), (_213: rt.u16): Named.Named => { const w6: rt.u16 = _213; @@ -385,4 +385,173 @@ export function edits(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { } } +/** + * [`edits`]'s methods of bytes, which are an `rt.ByteVec` or `rt.ByteSlice` rather than an + * array, on every short prefix of the input and on a slice that starts past its storage's, + * and those that lend a closure another element JS copies, as its place. + */ +export function bytes(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + const at: (a0: rt.usize) => rt.u8 = (i2: rt.usize): rt.u8 => { + return unwrapOr$Option_u8(rt.byteSliceGet(data, i2), 0); + }; + const words: rt.u16[] = rt.iterToArray(rt.iterMap(rt.iterSlice(rt.byteSliceView(data)), (_2: rt.u8): rt.u16 => { + return rt.u16Mul(_2 as rt.u16, 3); + })); + const prefixes: rt.Iter = rt.iterChain(rt.iterMap(new rt.Range(0, rt.min(data.length, 3)), (n3: rt.usize): rt.ByteSlice => { + return rt.byteSliceRange(data, 0, n3); + }), rt.iterSlice([data])); + const _15: rt.u8 = rt.u8Rem(kind, 4); + if (_15 === 0) { + const iter: rt.Iter = prefixes; + while (true) { + const _20: rt.ByteSlice | rt.Done = iter.next(); + if (_20 === rt.DONE) { + break; + } + const v: rt.ByteVec = rt.byteSliceToByteVec(_20); + const seen: rt.ByteVec = new rt.ByteVec(); + rt.byteVecRetain(v, (_22: rt.u8): boolean => { + const b10: rt.u8 = _22; + rt.byteVecPush(seen, b10); + return rt.u8Rem(b10, 3) !== 0; + }); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(seen)); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v)); + const v2: rt.ByteVec = rt.byteSliceToByteVec(_20); + rt.byteVecRetainMut(v2, (b11: rt.Place): boolean => { + b11.v = rt.u8WrappingAdd(b11.v, 1); + return rt.u8Rem(b11.v, 4) !== 0; + }); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v2)); + rt.byteVecPush(out, 254); + } + const halves: rt.u16[] = words.slice(); + rt.vecRetainMut(halves, (x2: rt.Place): boolean => { + x2.v = rt.u16Div(x2.v, 2); + return rt.u16Rem(x2.v, 3) !== 0; + }); + putU16s(out, halves); + } else if (_15 === 1) { + const iter2: rt.Iter = prefixes; + while (true) { + const _53: rt.ByteSlice | rt.Done = iter2.next(); + if (_53 === rt.DONE) { + break; + } + const v3: rt.ByteVec = rt.iterToByteVec(rt.iterMap(rt.iterSlice(rt.byteSliceView(_53)), (b6: rt.u8): rt.u8 => { + return rt.u8Rem(b6, 4); + })); + rt.byteVecDedup(v3); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v3)); + const v4: rt.ByteVec = rt.byteSliceToByteVec(_53); + const seen2: rt.ByteVec = new rt.ByteVec(); + rt.byteVecDedupByMut(v4, (a3: rt.Place, b7: rt.Place): boolean => { + rt.byteVecExtendFromSlice(seen2, rt.byteSliceFrom(Uint8Array.of(a3.v, b7.v))); + b7.v = rt.u8WrappingAdd(b7.v, 1); + return rt.u8Div(a3.v, 32) === rt.u8Div(b7.v, 32); + }); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(seen2)); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v4)); + const v5: rt.ByteVec = rt.byteSliceToByteVec(_53); + rt.byteVecDedupByKeyMut(v5, (b8: rt.Place): rt.u8 => { + b8.v = b8.v ^ 1; + return rt.u8Div(b8.v, 64); + }, rt.same); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v5)); + rt.byteVecPush(out, 254); + } + const v6: rt.u16[] = words.slice(); + rt.vecDedupByMut(v6, (a4: rt.Place, b9: rt.Place): boolean => { + b9.v = rt.u16Add(b9.v, 1); + return rt.u16Div(a4.v, 8) === rt.u16Div(b9.v, 8); + }); + putU16s(out, v6); + const v7: rt.u16[] = words.slice(); + rt.vecDedupByKeyMut(v7, (x: rt.Place): rt.u16 => { + x.v = x.v ^ 1; + return rt.u16Div(x.v, 16); + }, rt.same); + putU16s(out, v7); + } else if (_15 === 2) { + const iter3: rt.Iter = prefixes; + while (true) { + const _101: rt.ByteSlice | rt.Done = iter3.next(); + if (_101 === rt.DONE) { + break; + } + const v8: rt.ByteVec = rt.byteSliceToByteVec(_101); + rt.byteSliceView(rt.byteVecSlice(v8)).sort(); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v8)); + const v9: rt.ByteVec = rt.byteSliceToByteVec(_101); + rt.byteSliceView(rt.byteVecSlice(v9)).sort(); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v9)); + const v10: rt.ByteVec = rt.byteSliceToByteVec(_101); + rt.sliceSortBy(rt.byteSliceView(rt.byteVecSlice(v10)), (a: rt.u8, b2: rt.u8): rt.Ordering => { + return rt.cmp(b2, a); + }); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v10)); + const v11: rt.ByteVec = rt.byteSliceToByteVec(_101); + rt.sliceSortByKey(rt.byteSliceView(rt.byteVecSlice(v11)), (b3: rt.u8): rt.u8 => { + return rt.u8Rem(b3, 5); + }, rt.cmpLt(rt.cmp)); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v11)); + const v12: rt.ByteVec = rt.byteSliceToByteVec(_101); + rt.sliceSortByKey(rt.byteSliceView(rt.byteVecSlice(v12)), (b4: rt.u8): rt.u8 => { + return rt.u8WrappingMul(b4, 13); + }, rt.cmpLt(rt.cmp)); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v12)); + const v13: rt.ByteVec = rt.byteSliceToByteVec(_101); + rt.sliceSortBy(rt.byteSliceView(rt.byteVecSlice(v13)), (a2: rt.u8, b5: rt.u8): rt.Ordering => { + return rt.cmp(b5 ^ 85, a2 ^ 85); + }); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v13)); + const v14: rt.ByteVec = rt.byteSliceToByteVec(_101); + const part3 = rt.min(v14.length, 1); + const tail: rt.ByteSlice = rt.byteSliceRange(rt.byteVecSlice(v14), part3); + rt.byteSliceView(tail).sort(); + rt.byteSliceView(tail).reverse(); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v14)); + rt.byteVecPush(out, unwrapOr$Option_u8(rt.byteSliceLast(rt.byteVecSlice(v14)), 253)); + const part4 = rt.min(v14.length, 1); + rt.byteVecPush(out, unwrapOr$Option_u8(rt.byteSliceLast(rt.byteSliceRange(rt.byteVecSlice(v14), part4)), 253)); + rt.byteSliceView(rt.byteVecSlice(v14)).reverse(); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v14)); + rt.byteVecPush(out, 254); + } + } else { + const _184: [rt.usize, rt.usize] = [rt.usizeRem(at(0) as rt.usize, 8), rt.usizeRem(at(1) as rt.usize, 8)]; + const i: rt.usize = _184[0]; + const j: rt.usize = _184[1]; + const v15: rt.ByteVec = rt.byteSliceToByteVec(data); + rt.byteVecInsert(v15, rt.min(i, v15.length), 170); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v15)); + rt.byteVecPush(out, rt.byteVecSwapRemove(v15, rt.usizeRem(j, v15.length))); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v15)); + if (!((v15.length === 0))) { + rt.byteVecPush(out, rt.byteVecRemove(v15, rt.usizeRem(i, v15.length))); + } + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v15)); + const part = rt.min(v15.length, 1); + const tail2: rt.ByteSlice = rt.byteSliceRange(rt.byteVecSlice(v15), part); + if (!((tail2.length === 0))) { + rt.byteSliceSwap(tail2, rt.usizeRem(i, tail2.length), rt.usizeRem(j, tail2.length)); + } + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v15)); + const _249: rt.u8 = rt.u8Rem(at(2), 5); + if (_249 === 0) { + rt.byteVecInsert(v15, i, 187); + } else if (_249 === 1) { + rt.byteVecPush(out, rt.byteVecRemove(v15, i)); + } else if (_249 === 2) { + rt.byteVecPush(out, rt.byteVecSwapRemove(v15, j)); + } else if (_249 === 3) { + const part2 = rt.usizeRem(i, 3); + rt.byteSliceSwap(rt.byteSliceRange(rt.byteVecSlice(v15), part2), i, j); + } else { + rt.byteSliceSwap(rt.byteVecSlice(v15), i, j); + } + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(v15)); + } +} + //# sourceMappingURL=edits.ts.map diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index 410d375c2..9adb784f6 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -13,7 +13,7 @@ import { cows } from "./cows.ts"; import { defaults } from "./defaults.ts"; import { durations } from "./durations.ts"; import { dyns, shared, anys } from "./dyns.ts"; -import { edits } from "./edits.ts"; +import { edits, bytes } from "./edits.ts"; import { floats } from "./floats.ts"; import { ints, strings, derived, written, counts, rounding, panics } from "./fmt.ts"; import { methods } from "./generics.ts"; @@ -34,69 +34,69 @@ import { wrappers } from "./wrappers.ts"; * case added at the end moves no other. */ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void][] { - return [["ints::u8_op", u8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u16_op", u16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u32_op", u32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u64_op", u64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u128_op", u128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::usize_op", usizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i8_op", i8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i16_op", i16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i32_op", i32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i64_op", i64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i128_op", i128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::isize_op", isizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::neg", neg] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::cast", cast] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::convert", (_219: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void => { + return [["ints::u8_op", u8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u16_op", u16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u32_op", u32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u64_op", u64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u128_op", u128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::usize_op", usizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i8_op", i8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i16_op", i16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i32_op", i32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i64_op", i64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i128_op", i128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::isize_op", isizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::neg", neg] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::cast", cast] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::convert", (_222: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void => { convert(data, out); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::copies", (_220: rt.u8, data2: rt.ByteSlice, out2: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::copies", (_223: rt.u8, data2: rt.ByteSlice, out2: rt.ByteVec): void => { copies(data2, out2); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::loops", (_221: rt.u8, data3: rt.ByteSlice, out3: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::loops", (_224: rt.u8, data3: rt.ByteSlice, out3: rt.ByteVec): void => { loops(data3, out3); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::patterns", (_222: rt.u8, data4: rt.ByteSlice, out4: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::patterns", (_225: rt.u8, data4: rt.ByteSlice, out4: rt.ByteVec): void => { patterns(data4, out4); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::closures", (_223: rt.u8, data5: rt.ByteSlice, out5: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::closures", (_226: rt.u8, data5: rt.ByteSlice, out5: rt.ByteVec): void => { closures(data5, out5); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::discriminants", (_224: rt.u8, data6: rt.ByteSlice, out6: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::discriminants", (_227: rt.u8, data6: rt.ByteSlice, out6: rt.ByteVec): void => { discriminants(data6, out6); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::library", (_225: rt.u8, data7: rt.ByteSlice, out7: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::library", (_228: rt.u8, data7: rt.ByteSlice, out7: rt.ByteVec): void => { library(data7, out7); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::ints", ints] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::strings", (_226: rt.u8, data8: rt.ByteSlice, out8: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::ints", ints] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::strings", (_229: rt.u8, data8: rt.ByteSlice, out8: rt.ByteVec): void => { strings(data8, out8); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::derived", (_227: rt.u8, data9: rt.ByteSlice, out9: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::derived", (_230: rt.u8, data9: rt.ByteSlice, out9: rt.ByteVec): void => { derived(data9, out9); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::written", (_228: rt.u8, data10: rt.ByteSlice, out10: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::written", (_231: rt.u8, data10: rt.ByteSlice, out10: rt.ByteVec): void => { written(data10, out10); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::panics", panics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["generics::methods", (_229: rt.u8, data11: rt.ByteSlice, out11: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::panics", panics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["generics::methods", (_232: rt.u8, data11: rt.ByteSlice, out11: rt.ByteVec): void => { methods(data11, out11); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::adaptors", adaptors] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::consumers", consumers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranges", (_230: rt.u8, data12: rt.ByteSlice, out12: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::adaptors", adaptors] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::consumers", consumers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranges", (_233: rt.u8, data12: rt.ByteSlice, out12: rt.ByteVec): void => { ranges(data12, out12); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::options", (_231: rt.u8, data13: rt.ByteSlice, out13: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::options", (_234: rt.u8, data13: rt.ByteSlice, out13: rt.ByteVec): void => { options(data13, out13); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::own", (_232: rt.u8, data14: rt.ByteSlice, out14: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::own", (_235: rt.u8, data14: rt.ByteSlice, out14: rt.ByteVec): void => { own(data14, out14); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::values", (_233: rt.u8, data15: rt.ByteSlice, out15: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::values", (_236: rt.u8, data15: rt.ByteSlice, out15: rt.ByteVec): void => { values(data15, out15); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::order", (_234: rt.u8, data16: rt.ByteSlice, out16: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::order", (_237: rt.u8, data16: rt.ByteSlice, out16: rt.ByteVec): void => { order(data16, out16); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::hash_maps", hashMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::sets", sets] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::btree_maps", btreeMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::deques", (_235: rt.u8, data17: rt.ByteSlice, out17: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::hash_maps", hashMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::sets", sets] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::btree_maps", btreeMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::deques", (_238: rt.u8, data17: rt.ByteSlice, out17: rt.ByteVec): void => { deques(data17, out17); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::flows", (_236: rt.u8, data18: rt.ByteSlice, out18: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::flows", (_239: rt.u8, data18: rt.ByteSlice, out18: rt.ByteVec): void => { flows(data18, out18); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::keys", keys] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chars::chars", chars] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::strs", strs] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::vecs", (_237: rt.u8, data19: rt.ByteSlice, out19: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::keys", keys] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chars::chars", chars] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::strs", strs] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::vecs", (_240: rt.u8, data19: rt.ByteSlice, out19: rt.ByteVec): void => { vecs(data19, out19); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::slices", slices] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::stores", (_238: rt.u8, data20: rt.ByteSlice, out20: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::slices", slices] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::stores", (_241: rt.u8, data20: rt.ByteSlice, out20: rt.ByteVec): void => { stores(data20, out20); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chunks::chunks", chunks] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cows::cows", cows] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["durations::durations", durations] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["combinators::combinators", combinators] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["splits::splits", splits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["nested::nested", nested] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["refs::refs", (_239: rt.u8, data21: rt.ByteSlice, out21: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chunks::chunks", chunks] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cows::cows", cows] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["durations::durations", durations] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["combinators::combinators", combinators] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["splits::splits", splits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["nested::nested", nested] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["refs::refs", (_242: rt.u8, data21: rt.ByteSlice, out21: rt.ByteVec): void => { refs(data21, out21); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::counts", (_240: rt.u8, data22: rt.ByteSlice, out22: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::counts", (_243: rt.u8, data22: rt.ByteSlice, out22: rt.ByteVec): void => { counts(data22, out22); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_241: rt.u8, _310: rt.ByteSlice, out23: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_244: rt.u8, _310: rt.ByteSlice, out23: rt.ByteVec): void => { rounding(out23); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::skips", skips] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["wrappers::wrappers", wrappers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::edits", edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["utf8::decodes", decodes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["floats::floats", floats] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["atomics::atomics", atomics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::overrides", overrides] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::dyns", (_242: rt.u8, data23: rt.ByteSlice, out24: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::skips", skips] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["wrappers::wrappers", wrappers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::edits", edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["utf8::decodes", decodes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["floats::floats", floats] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["atomics::atomics", atomics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::overrides", overrides] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::dyns", (_245: rt.u8, data23: rt.ByteSlice, out24: rt.ByteVec): void => { dyns(data23, out24); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cells::cells", cells] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::shared", (_243: rt.u8, data24: rt.ByteSlice, out25: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cells::cells", cells] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::shared", (_246: rt.u8, data24: rt.ByteSlice, out25: rt.ByteVec): void => { shared(data24, out25); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranked", ranked] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["captures::captures", captures] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::locks", (_244: rt.u8, data25: rt.ByteSlice, out26: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranked", ranked] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["captures::captures", captures] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::locks", (_247: rt.u8, data25: rt.ByteSlice, out26: rt.ByteVec): void => { locks(data25, out26); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::relock", (_245: rt.u8, data26: rt.ByteSlice, out27: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::relock", (_248: rt.u8, data26: rt.ByteSlice, out27: rt.ByteVec): void => { relock(data26, out27); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["defaults::defaults", (_246: rt.u8, data27: rt.ByteSlice, out28: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["defaults::defaults", (_249: rt.u8, data27: rt.ByteSlice, out28: rt.ByteVec): void => { defaults(data27, out28); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::arcs", (_247: rt.u8, data28: rt.ByteSlice, out29: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::arcs", (_250: rt.u8, data28: rt.ByteSlice, out29: rt.ByteVec): void => { arcs(data28, out29); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::rcs", (_248: rt.u8, data29: rt.ByteSlice, out30: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::rcs", (_251: rt.u8, data29: rt.ByteSlice, out30: rt.ByteVec): void => { rcs(data29, out30); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::anys", (_249: rt.u8, data30: rt.ByteSlice, out31: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::anys", (_252: rt.u8, data30: rt.ByteSlice, out31: rt.ByteVec): void => { anys(data30, out31); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]; + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::bytes", bytes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]; } /** Run one case. */ diff --git a/ts/fixtures/lang/src/edits.rs b/ts/fixtures/lang/src/edits.rs index 9154d5000..e066b15f6 100644 --- a/ts/fixtures/lang/src/edits.rs +++ b/ts/fixtures/lang/src/edits.rs @@ -350,3 +350,142 @@ pub fn edits(kind: u8, data: &[u8], out: &mut Vec) { } } } + +/// [`edits`]'s methods of bytes, which are an `rt.ByteVec` or `rt.ByteSlice` rather than an +/// array, on every short prefix of the input and on a slice that starts past its storage's, +/// and those that lend a closure another element JS copies, as its place. +pub fn bytes(kind: u8, data: &[u8], out: &mut Vec) { + let at = |i: usize| data.get(i).copied().unwrap_or(0); + let words: Vec = data.iter().map(|&b| b as u16 * 3).collect(); + let prefixes = (0..data.len().min(3)).map(|n| &data[..n]).chain([data]); + match kind % 4 { + 0 => { + for data in prefixes { + // `retain` sees each byte once, in order; `retain_mut` keeps what it changed. + let mut v = data.to_vec(); + let mut seen = Vec::new(); + v.retain(|&b| { + seen.push(b); + b % 3 != 0 + }); + out.extend_from_slice(&seen); + out.extend_from_slice(&v); + let mut v = data.to_vec(); + v.retain_mut(|b| { + *b = b.wrapping_add(1); + *b % 4 != 0 + }); + out.extend_from_slice(&v); + out.push(0xfe); + } + // An element JS copies, changed through its place. + let mut halves = words.clone(); + halves.retain_mut(|x| { + *x /= 2; + *x % 3 != 0 + }); + put_u16s(out, &halves); + } + 1 => { + for data in prefixes { + let mut v: Vec = data.iter().map(|b| b % 4).collect(); + v.dedup(); + out.extend_from_slice(&v); + // Current first, then the previous kept, each changed in place. + let mut v = data.to_vec(); + let mut seen = Vec::new(); + v.dedup_by(|a, b| { + seen.extend_from_slice(&[*a, *b]); + *b = b.wrapping_add(1); + *a / 32 == *b / 32 + }); + out.extend_from_slice(&seen); + out.extend_from_slice(&v); + let mut v = data.to_vec(); + v.dedup_by_key(|b| { + *b ^= 1; + *b / 64 + }); + out.extend_from_slice(&v); + out.push(0xfe); + } + // Elements JS copies, changed through their places. + let mut v: Vec = words.clone(); + v.dedup_by(|a, b| { + *b += 1; + *a / 8 == *b / 8 + }); + put_u16s(out, &v); + let mut v: Vec = words.clone(); + v.dedup_by_key(|x| { + *x ^= 1; + *x / 16 + }); + put_u16s(out, &v); + } + 2 => { + for data in prefixes { + let mut v = data.to_vec(); + v.sort(); + out.extend_from_slice(&v); + let mut v = data.to_vec(); + v.sort_unstable(); + out.extend_from_slice(&v); + let mut v = data.to_vec(); + v.sort_by(|a, b| b.cmp(a)); + out.extend_from_slice(&v); + // Stable: bytes of a key keep their order. + let mut v = data.to_vec(); + v.sort_by_key(|b| b % 5); + out.extend_from_slice(&v); + // Unstable, by keys no two bytes share, so std's order is the only one. + let mut v = data.to_vec(); + v.sort_unstable_by_key(|b| b.wrapping_mul(13)); + out.extend_from_slice(&v); + let mut v = data.to_vec(); + v.sort_unstable_by(|a, b| (b ^ 0x55).cmp(&(a ^ 0x55))); + out.extend_from_slice(&v); + // Past the slice's own start in what holds it. + let mut v = data.to_vec(); + let n = v.len(); + let tail = &mut v[n.min(1)..]; + tail.sort(); + tail.reverse(); + out.extend_from_slice(&v); + out.push(v.last().copied().unwrap_or(0xfd)); + out.push(v[v.len().min(1)..].last().copied().unwrap_or(0xfd)); + v.reverse(); + out.extend_from_slice(&v); + out.push(0xfe); + } + } + _ => { + let (i, j) = (at(0) as usize % 8, at(1) as usize % 8); + let mut v = data.to_vec(); + v.insert(i.min(v.len()), 0xaa); + out.extend_from_slice(&v); + out.push(v.swap_remove(j % v.len())); + out.extend_from_slice(&v); + if !v.is_empty() { + out.push(v.remove(i % v.len())); + } + out.extend_from_slice(&v); + // Past the slice's own start in what holds it. + let n = v.len(); + let tail = &mut v[n.min(1)..]; + if !tail.is_empty() { + tail.swap(i % tail.len(), j % tail.len()); + } + out.extend_from_slice(&v); + // Each panics with std's message past the end. + match at(2) % 5 { + 0 => v.insert(i, 0xbb), + 1 => out.push(v.remove(i)), + 2 => out.push(v.swap_remove(j)), + 3 => v[i % 3..].swap(i, j), + _ => v.swap(i, j), + } + out.extend_from_slice(&v); + } + } +} diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index 176ad1f65..458fe9809 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -94,6 +94,7 @@ fn cases() -> Vec<(&'static str, Case)> { ("counts::arcs", |_, data, out| counts::arcs(data, out)), ("counts::rcs", |_, data, out| counts::rcs(data, out)), ("dyns::anys", |_, data, out| dyns::anys(data, out)), + ("edits::bytes", edits::bytes), ] } diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index a64f9f669..30619945f 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -272,11 +272,6 @@ export function nonZero(n: T): T | undefined { return n === 0 ? undefined : n; } -/** `reverse`, in place. */ -export function sliceReverse(s: T[]): void { - s.reverse(); -} - /** `last`, and `last_mut` of an element that is an object: the last element, or `undefined` if empty. */ export function sliceLast(s: ArrayLike): T | undefined { return s.length === 0 ? undefined : s[s.length - 1]; @@ -311,12 +306,12 @@ export function sliceSwap(s: T[], a: number, b: number): void { } /** `sort_by` and `sort_unstable_by`: by `cmp`, whether it orders `Less`, as `sliceSort` sorts. */ -export function sliceSortBy(s: T[], cmp: (a: T, b: T) => Ordering): void { +export function sliceSortBy(s: { [i: number]: T; length: number }, cmp: (a: T, b: T) => Ordering): void { sliceSort(s, (a, b) => cmp(a, b) === -1); } /** `sort_by_key` and `sort_unstable_by_key`: by `lt` of each element's `key`, as `sliceSort` sorts. */ -export function sliceSortByKey(s: T[], key: (item: T) => K, lt: (a: K, b: K) => boolean): void { +export function sliceSortByKey(s: { [i: number]: T; length: number }, key: (item: T) => K, lt: (a: K, b: K) => boolean): void { sliceSort(s, (a, b) => lt(key(a), key(b))); } @@ -327,7 +322,7 @@ export function sliceSortByKey(s: T[], key: (item: T) => K, lt: (a: K, b: * JS's `sort`, which moves `undefined`, a `None`, to the end without comparing it, and * orders its calls differently in each engine. */ -export function sliceSort(s: T[], less: (a: T, b: T) => boolean): void { +export function sliceSort(s: { [i: number]: T; length: number }, less: (a: T, b: T) => boolean): void { const n = s.length; const RUN = 16; for (let lo = 0; lo < n; lo += RUN) { @@ -363,28 +358,50 @@ export function sliceSort(s: T[], less: (a: T, b: T) => boolean): void { } /** - * `Vec::retain` and `retain_mut`: keeps each element `f` returns true for, in order, and - * drops each other one once `f` rejects it. + * Keeps each of the first `n` elements of `a` that `keep` of its index returns true for, + * in order, and drops each other one once `keep` rejects it: how many it kept. */ -export function vecRetain(v: T[], f: (item: T) => boolean, drop?: (item: T) => void): void { +function retainAt(a: { [i: number]: T }, n: number, keep: (i: number) => boolean, drop?: (item: T) => void): number { let write = 0; - for (let read = 0; read < v.length; read++) { - const item = v[read]; - if (f(item)) v[write++] = item; - else drop?.(item); + for (let read = 0; read < n; read++) { + if (keep(read)) a[write++] = a[read]; + else drop?.(a[read]); } - v.length = write; + return write; } -/** `Vec::dedup_by`: drops each element `same(element, previous kept)` is true for, in order. */ -export function vecDedupBy(v: T[], same: (a: T, b: T) => boolean, drop?: (item: T) => void): void { +/** + * Drops each of the first `n` elements of `a` that `same` of its index and the previous + * kept one's is true for, in order: how many it kept. + */ +function dedupAt(a: { [i: number]: T }, n: number, same: (i: number, kept: number) => boolean, drop?: (item: T) => void): number { + if (n === 0) return 0; let write = 1; - for (let read = 1; read < v.length; read++) { - const item = v[read]; - if (same(item, v[write - 1])) drop?.(item); - else v[write++] = item; + for (let read = 1; read < n; read++) { + if (same(read, write - 1)) drop?.(a[read]); + else a[write++] = a[read]; } - if (write < v.length) v.length = write; + return write; +} + +/** `Vec::retain`, and `retain_mut` of an element that is an object. */ +export function vecRetain(v: T[], f: (item: T) => boolean, drop?: (item: T) => void): void { + v.length = retainAt(v, v.length, (i) => f(v[i]), drop); +} + +/** `Vec::retain_mut` of an element JS copies, which `f` takes the place of. */ +export function vecRetainMut(v: T[], f: (item: Place) => boolean, drop?: (item: T) => void): void { + v.length = retainAt(v, v.length, (i) => f(new AtIndex(v, i)), drop); +} + +/** `Vec::dedup_by`: drops each element `same(element, previous kept)` is true for, in order. */ +export function vecDedupBy(v: T[], same: (a: T, b: T) => boolean, drop?: (item: T) => void): void { + v.length = dedupAt(v, v.length, (i, j) => same(v[i], v[j]), drop); +} + +/** `Vec::dedup_by` of an element JS copies, whose places `same` takes. */ +export function vecDedupByMut(v: T[], same: (a: Place, b: Place) => boolean, drop?: (item: T) => void): void { + v.length = dedupAt(v, v.length, (i, j) => same(new AtIndex(v, i), new AtIndex(v, j)), drop); } /** `Vec::dedup_by_key`: as `vecDedupBy`, by `eq` of `key` of each element, then of the previous kept. */ @@ -392,6 +409,11 @@ export function vecDedupByKey(v: T[], key: (item: T) => K, eq: (a: K, b: K vecDedupBy(v, (a, b) => eq(key(a), key(b)), drop); } +/** `Vec::dedup_by_key` of an element JS copies, whose places `key` takes. */ +export function vecDedupByKeyMut(v: T[], key: (item: Place) => K, eq: (a: K, b: K) => boolean, drop?: (item: T) => void): void { + vecDedupByMut(v, (a, b) => eq(key(a), key(b)), drop); +} + /** `Vec::insert`: `item` at `i`, with those from `i` on moved up. */ export function vecInsert(v: T[], i: number, item: T): void { if (i > v.length) panic(`insertion index (is ${i}) should be <= len (is ${v.length})`); @@ -501,6 +523,18 @@ export function byteSliceFirst(s: ByteSlice): number | undefined { return s.length === 0 ? undefined : s.data[s.start]; } +/** `last`: the last byte, or `undefined` if empty. */ +export function byteSliceLast(s: ByteSlice): number | undefined { + return s.length === 0 ? undefined : s.data[s.start + s.length - 1]; +} + +/** `swap`, panicking on either index past the end, `a` first, as std's indexing does. */ +export function byteSliceSwap(s: ByteSlice, a: number, b: number): void { + const x = s.data[s.start + idx(s, a)]; + s.data[s.start + a] = s.data[s.start + idx(s, b)]; + s.data[s.start + b] = x; +} + /** `get`: the byte at `index`, or `undefined` past the end. */ export function byteSliceGet(s: ByteSlice, index: number): number | undefined { return index < s.length ? s.data[s.start + index] : undefined; @@ -651,6 +685,57 @@ export function byteVecClear(v: ByteVec): void { v.length = 0; } +/** `Vec::insert`: `byte` at `i`, with those from `i` on moved up. */ +export function byteVecInsert(v: ByteVec, i: number, byte: number): void { + if (i > v.length) panic(`insertion index (is ${i}) should be <= len (is ${v.length})`); + reserve(v, 1); + v.data.copyWithin(i + 1, i, v.length); + v.data[i] = byte; + v.length++; +} + +/** `Vec::remove`: the byte at `i`, with those after it moved down. */ +export function byteVecRemove(v: ByteVec, i: number): number { + if (i >= v.length) panic(`removal index (is ${i}) should be < len (is ${v.length})`); + const byte = v.data[i]; + v.data.copyWithin(i, i + 1, v.length); + v.length--; + return byte; +} + +/** `Vec::swap_remove`: the byte at `i`, with the last moved into its place. */ +export function byteVecSwapRemove(v: ByteVec, i: number): number { + if (i >= v.length) panic(`swap_remove index (is ${i}) should be < len (is ${v.length})`); + const byte = v.data[i]; + v.data[i] = v.data[--v.length]; + return byte; +} + +/** `Vec::retain`. */ +export function byteVecRetain(v: ByteVec, f: (byte: number) => boolean): void { + v.length = retainAt(v.data, v.length, (i) => f(v.data[i])); +} + +/** `Vec::retain_mut`, which `f` takes each byte's place in. */ +export function byteVecRetainMut(v: ByteVec, f: (byte: Place) => boolean): void { + v.length = retainAt(v.data, v.length, (i) => f(new AtIndex(v.data, i))); +} + +/** `Vec::dedup`. */ +export function byteVecDedup(v: ByteVec): void { + v.length = dedupAt(v.data, v.length, (i, j) => v.data[i] === v.data[j]); +} + +/** `Vec::dedup_by`, whose places `same` takes. */ +export function byteVecDedupByMut(v: ByteVec, same: (a: Place, b: Place) => boolean): void { + v.length = dedupAt(v.data, v.length, (i, j) => same(new AtIndex(v.data, i), new AtIndex(v.data, j))); +} + +/** `Vec::dedup_by_key`, whose places `key` takes. */ +export function byteVecDedupByKeyMut(v: ByteVec, key: (byte: Place) => K, eq: (a: K, b: K) => boolean): void { + byteVecDedupByMut(v, (a, b) => eq(key(a), key(b))); +} + // Strings const encoder = /* @__PURE__ */ new TextEncoder(); diff --git a/ts/src/lower/collections.rs b/ts/src/lower/collections.rs index 78737fd13..e7265a261 100644 --- a/ts/src/lower/collections.rs +++ b/ts/src/lower/collections.rs @@ -287,6 +287,7 @@ pub fn place_call<'tcx>(b: &Body<'_, '_, 'tcx>, call: &Call<'tcx>) -> bool { let name = tcx.item_name(call.def_id); let coll = impl_self_ty(b, call); let (methods, value): (&[&str], usize) = match std { + Std::Vec => (&["retain_mut", "dedup_by", "dedup_by_key"], 0), Std::VecDeque => (&["get_mut", "front_mut", "back_mut", "iter_mut", "retain_mut"], 0), Std::HashMap => (&["get_mut", "iter_mut", "values_mut", "retain"], 1), Std::BTreeMap => (&["get_mut", "iter_mut", "values_mut", "range_mut", "retain"], 1), diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index f9bc922f5..37278469c 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -1189,9 +1189,11 @@ fn mapping<'tcx>( false => return vec_clone(b, slice, elem, span), } } - ("slice", "last") if !is_u8(targ(gargs, 0)) => Expr::rt("sliceLast", args), - ("slice", "reverse") if !is_u8(targ(gargs, 0)) => Expr::rt("sliceReverse", args), - ("slice", "swap") if !is_u8(targ(gargs, 0)) => Expr::rt("sliceSwap", args), + ("slice", "last" | "swap") => match is_u8(targ(gargs, 0)) { + true => rt2(args, "byteSlice", &super::camel(&name)), + false => rt2(args, "slice", &super::camel(&name)), + }, + ("slice", "reverse") => elements(args.pop()?, Ty::new_slice(tcx, targ(gargs, 0))).method("reverse", Vec::new()), ("slice", "contains") => { let elem = targ(gargs, 0); let [s, x] = <[Expr; 2]>::try_from(args).ok()?; @@ -1207,9 +1209,13 @@ fn mapping<'tcx>( ( "slice", "sort" | "sort_unstable" | "sort_by" | "sort_unstable_by" | "sort_by_key" | "sort_unstable_by_key", - ) if !is_u8(targ(gargs, 0)) => { + ) => { + let elem = targ(gargs, 0); let mut values = args; + values[0] = elements(values[0].clone(), Ty::new_slice(tcx, elem)); match name.as_str() { + // A typed array sorts its numbers by value, and equal bytes are alike. + "sort" | "sort_unstable" if is_u8(elem) => values.remove(0).method("sort", Vec::new()), "sort" | "sort_unstable" => { values.push(super::collections::test_fn(b, targ(gargs, 0), "lt", span)); Expr::rt("sliceSort", values) @@ -1302,31 +1308,44 @@ fn mapping<'tcx>( } Expr::rt("vecExtendFromSlice", values) } - ("Vec", "swap_remove") if !is_u8(targ(gargs, 0)) => Expr::rt("vecSwapRemove", args), - ("Vec", "insert" | "remove" | "truncate") if !is_u8(targ(gargs, 0)) => { - Expr::rt_of("vec", &super::camel(&name)).call(args) + ("Vec", "insert" | "remove" | "swap_remove") if is_u8(targ(gargs, 0)) => { + rt2(args, "byteVec", &super::camel(&name)) } + ("Vec", "insert" | "remove" | "swap_remove" | "truncate") => rt2(args, "vec", &super::camel(&name)), ("Vec", "clear") => { let mut values = args; values.push(Expr::lit("0")); Expr::rt("vecTruncate", values) } - // What each takes by value is a closure, which it drops once done. - ("Vec", "retain" | "retain_mut" | "dedup" | "dedup_by" | "dedup_by_key") if !is_u8(targ(gargs, 0)) => { + // What each takes by value is a closure, which it drops once done. An element JS + // copies is lent to the closure as its place where it takes a `&mut`. + ("Vec", "retain" | "retain_mut" | "dedup" | "dedup_by" | "dedup_by_key") => { if let Some(ty) = dropping_operand(b, call) { return Some(b.err( span, format!("passing `{ty}`, which drops, to `{owner}::{name}`, which is not supported yet"), )); } + let elem = targ(gargs, 0); + let of = match is_u8(elem) { + true => "byteVec", + false => "vec", + }; + // Each that takes a `&mut` lends an element JS copies as its place. + let lend = match b.t.needs_place(elem) { + true => "Mut", + false => "", + }; let mut values = args; - match name.as_str() { - "retain" | "retain_mut" => Expr::rt("vecRetain", values), + let f = match name.as_str() { + "retain" => "retain".to_string(), + "retain_mut" => format!("retain{lend}"), + "dedup" if is_u8(elem) => "dedup".to_string(), "dedup" => { - values.push(super::collections::eq_fn(b, targ(gargs, 0), span)); - Expr::rt("vecDedupBy", values) + values.push(super::collections::eq_fn(b, elem, span)); + "dedupBy".to_string() } - "dedup_by" => Expr::rt("vecDedupBy", values), + "dedup_by" => format!("dedupBy{lend}"), _ => { // `Vec::dedup_by_key::`. let key = targ(gargs, 3); @@ -1337,9 +1356,10 @@ fn mapping<'tcx>( )); } values.push(super::collections::eq_fn(b, key, span)); - Expr::rt("vecDedupByKey", values) + format!("dedupByKey{lend}") } - } + }; + rt2(values, of, &f) } ("Vec", "as_slice" | "as_mut_slice") => { let v = args.pop()?; From 87e8bcee3296660c622b8b492067f84103c74259 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Wed, 28 Oct 2026 21:50:34 -0700 Subject: [PATCH 3/3] feat(ts): keep reject's unsupported slice call, migrate sliceReverse, finish the quest Co-Authored-By: Claude Opus 5.5 --- quest/m3/README.md | 1 - quest/m3/ts-byte-edits.md | 21 --------------------- ts/MIGRATE.md | 1 + ts/fixtures/reject/census.md | 4 ++-- ts/fixtures/reject/expected.txt | 2 +- ts/fixtures/reject/src/lib.rs | 4 ++-- ts/fixtures/reject/ts.toml | 2 +- ts/src/lower/library.rs | 1 - 8 files changed, 7 insertions(+), 29 deletions(-) delete mode 100644 quest/m3/ts-byte-edits.md diff --git a/quest/m3/README.md b/quest/m3/README.md index 206557ec4..c101b1d6f 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -23,7 +23,6 @@ generic std gap is planned here even when moq never calls it. - [Binding gaps](/quest/m3/ts-bind-gaps.md) - bound types as keys, unit returns checked, renamed dependencies - [Drop order in std's folds and iterators](/quest/m3/ts-drop-order.md) - the values that drop still refused in std's folds, zips, flattens, and collects drop as std's do - [Checks on the differential harnesses](/quest/m3/ts-harness.md) - step lists that drift, literal op bytes, and `refs`'s coverage fail a test or get cases -- [Edits of byte vectors and slices](/quest/m3/ts-byte-edits.md) - `retain`, `dedup`, `insert`, `remove`, and the rest translate on `Vec` and `[u8]` as on other elements - [Ordering in std](/quest/m3/ts-ordering.md) - `cmp::max`/`min`, `is_sorted`, and `BinaryHeap`, and #173's cost on integers measured - [Debug and Display of Arc and ranges](/quest/m3/ts-fmt-wrappers.md) - `Arc` formats as `T`, and ranges as std's `Debug` - [Fold a match a bool decided](/quest/m3/ts-fold-bool.md) - `if matches!` and derived `==` drop a later match's dead arms diff --git a/quest/m3/ts-byte-edits.md b/quest/m3/ts-byte-edits.md deleted file mode 100644 index 313532472..000000000 --- a/quest/m3/ts-byte-edits.md +++ /dev/null @@ -1,21 +0,0 @@ -# [S] Edits of byte vectors and slices - -## Goal - -rust2-ts translates every `Vec` and slice method on `Vec` and `[u8]` that it translates -for other element types, such as `retain`, `retain_mut`, `dedup`, `insert`, `remove`, -`swap_remove`, and the slice's `last`, `reverse`, `swap`, and sorts, proven by differential -tests against native Rust. - -## Plan - -Found by [#204](https://github.com/kixelated/rust2/pull/204) on 2026-10-28, whose fixture -hit `retain` on a `Vec`: the mappings in `library.rs` skip `u8` elements, which are an -`rt.ByteVec` or `rt.ByteSlice` rather than an array, and nothing maps them instead. rust2 -aims to support all of std, so this has no moq consumer. - -- Find every such skip rather than only the methods named here, and give each the - `rt.ByteVec` or `rt.ByteSlice` counterpart, with the closure's drop and the element's - equality as the generic path has them. - -Public API: the translator's output. diff --git a/ts/MIGRATE.md b/ts/MIGRATE.md index fe5a2f5f9..8db95f8e0 100644 --- a/ts/MIGRATE.md +++ b/ts/MIGRATE.md @@ -80,3 +80,4 @@ `Log.clone$Clone`, `Version.encode` is `Version.encode$Encode_Version`, and `Wrap.fmt$Pair` is `Wrap.fmt$Debug_Pair`. A closure type in a suffix is named for where it is defined, `decode_closure0`. +- `rt.sliceReverse` is gone: call the array's own `reverse`. diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index 07afe5b58..0a5dc19bb 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -188,13 +188,13 @@ The type or module each refused type or path belongs to. | 1 | unsupported call to `_` | `std::any::TypeId::of` | fixtures/reject/src/lib.rs:560:17 | | 1 | unsupported call to `_` | `std::ops::Range::contains` | fixtures/reject/src/lib.rs:180:8 | | 1 | unsupported call to `_` | `std::slice::copy_within` | fixtures/reject/src/lib.rs:336:4 | -| 1 | unsupported call to `_` | `std::slice::sort` | fixtures/reject/src/lib.rs:21:4 | +| 1 | unsupported call to `_` | `std::slice::rotate_left` | fixtures/reject/src/lib.rs:21:4 | ## Spans
unsupported call to `_` (13) -- fixtures/reject/src/lib.rs:21:4: unsupported call to `std::slice::::sort` +- fixtures/reject/src/lib.rs:21:4: unsupported call to `core::slice::::rotate_left` - fixtures/reject/src/lib.rs:74:10: unsupported call to `::write_fmt` - fixtures/reject/src/lib.rs:126:11: unsupported call to ` as std::iter::Iterator>::cycle` - fixtures/reject/src/lib.rs:180:8: unsupported call to `std::ops::Range::::contains::` diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index 0123f025d..79b93574b 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -9,7 +9,7 @@ unsupported type `f32` at fixtures/reject/src/lib.rs:7:25 `dyn std::ops::Fn(u8) -> u8`, a trait object of another crate's trait, which is not supported yet at fixtures/reject/src/lib.rs:11:16 `Option>>` is not supported: its `None` and `Some` would both be `rt.None` at fixtures/reject/src/lib.rs:17:2 `Option>>` is not supported: its `None` and `Some` would both be `rt.None` at fixtures/reject/src/lib.rs:16:15 -unsupported call to `std::slice::::sort` at fixtures/reject/src/lib.rs:21:4 +unsupported call to `core::slice::::rotate_left` at fixtures/reject/src/lib.rs:21:4 `Iterator::find` of `Option>`, whose `None` is `rt.None`, which is not supported yet at fixtures/reject/src/lib.rs:41:42 `Maybe<()>` is not supported: its field `value` is `Option`, typed `T | undefined`, whose `None` would be `rt.None` as `Option<()>` at fixtures/reject/src/lib.rs:31:2 collecting `Option<()>`, whose `None` is `rt.None`, which is not supported yet at fixtures/reject/src/lib.rs:36:43 diff --git a/ts/fixtures/reject/src/lib.rs b/ts/fixtures/reject/src/lib.rs index e77c5c397..1c99224c8 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -17,8 +17,8 @@ pub fn nested(x: Option>>) -> bool { x.is_some() } -pub fn sorted(v: &mut [u8]) { - v.sort(); +pub fn rotated(v: &mut [u8]) { + v.rotate_left(1); } /// A generic type's `Option` is `T | undefined`, which can't hold `rt.None`, the `None` diff --git a/ts/fixtures/reject/ts.toml b/ts/fixtures/reject/ts.toml index 4551be517..caec0747d 100644 --- a/ts/fixtures/reject/ts.toml +++ b/ts/fixtures/reject/ts.toml @@ -3,7 +3,7 @@ roots = [ "float", "dynamic", "nested", - "sorted", + "rotated", "iterators", "generic_unit", "collected", diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 37278469c..79456bc72 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -1331,7 +1331,6 @@ fn mapping<'tcx>( true => "byteVec", false => "vec", }; - // Each that takes a `&mut` lends an element JS copies as its place. let lend = match b.t.needs_place(elem) { true => "Mut", false => "",