Skip to content
Merged
3 changes: 2 additions & 1 deletion quest/m3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ generic std gap is planned here even when moq never calls it.
- [ptr::replace whose result nothing reads](/quest/m3/ts-ptr-replace-unread.md) - `ptr::replace` with its result discarded type-checks
- [std's sorts call their tests in std's order](/quest/m3/ts-sort-order.md) - `slice::sort` and a B-tree's `collect` call `lt` or `cmp` as std's driftsort does past 16 elements
- [A B-tree's searches call Ord as std's do](/quest/m3/ts-btree-search-order.md) - a B-tree's lookups and inserts call a key's own `Ord` in std's order and number
- [A &mut str to any String](/quest/m3/ts-str-places.md) - `make_ascii_*` of a `String` a tuple, an array, or a `Vec` holds, and of a part of one, translate
- [A &mut into a Box of a value JS copies](/quest/m3/ts-box-places.md) - a write through `&mut *b` of a `Box<u32>`, a `Box<String>`, or a `Box<str>` reaches the box's place
- [A str's checked and inclusive parts](/quest/m3/ts-str-get.md) - `str::get` and indexing a `str` by `a..=b` or `..=b` translate
- [The rest of a boxed slice's impls](/quest/m3/ts-boxed-slice-rest.md) - `clone_from`, `AsRef`, `AsMut`, `TryFrom` into a boxed array, and `Box<str>` into bytes translate for boxed slices
- [A reversed zip's per-pair cost](/quest/m3/ts-zip-rev-overhead.md) - a reversed by-index zip costs per pair what a forward one does, or the gap is explained
16 changes: 16 additions & 0 deletions quest/m3/ts-box-places.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# [S] A &mut into a Box of a value JS copies

## Goal

[rust2-ts](/ts/README.md) translates a `&mut` to what a `Box` holds, where JS copies it (a
number, a `String`, or a `str`), as a write to the local or field holding the box. Today
`*(&mut *b) += 1` of a `Box<u32>`, `push` on a `Box<String>`, and `make_ascii_uppercase`
on a `Box<str>` translate to `b.v` of a value with no `v`, which `tsc` rejects. Proven by
differential tests against native Rust.

## Plan

Found translating `&mut str`'s places. A `Box<T>` is its `T`, so a `&mut` through it is
the box's own place, as `String::as_mut_str` borrows the `String`'s.

Public API: the translator's output.
17 changes: 17 additions & 0 deletions quest/m3/ts-str-get.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# [S] A str's checked and inclusive parts

## Goal

[rust2-ts](/ts/README.md) translates `str::get`, and indexing a `str` or a `String` by an
inclusive range (`&s[a..=b]`, `&s[..=b]`, and their `&mut` and `get_mut` forms), with
std's `None` and panics. Today each refuses: `get` as an unsupported call, and the
inclusive ranges at `Index::index` or `IndexMut::index_mut`. Proven by differential tests
against native Rust.

## Plan

Found translating `&mut str`'s places, whose `get_mut` and `&mut s[a..b]` take the other
ranges. The runtime's `strSlice` and `strGetMut` check offsets already; an inclusive end
past `usize::MAX` panics as std's does. No consumer calls them yet; planned as part of std.

Public API: the translator's output.
18 changes: 0 additions & 18 deletions quest/m3/ts-str-places.md

This file was deleted.

9 changes: 6 additions & 3 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ the width and sign, which computes on the value's `bigint`.
The stable ASCII methods of `u8` and `char`, and of `[u8]` and `str`, so of a `Vec<u8>`
and a `String`, are std's. A `u8` and a `char` share each runtime function, `rt.isAsciiDigit(c)`.
`make_ascii_uppercase` and `make_ascii_lowercase` of a byte, a char, or a string write
the other case where the `&mut` borrows it; of a part of a `String`, or of a `String`
that a tuple, an array, or a `Vec` holds, they are refused. `escape_ascii`'s iterators read from either
the other case where the `&mut` borrows it, a part of a `String` included. `escape_ascii`'s iterators read from either
end, size themselves as std's do, and write what they have left by `Display`, unpadded.

An `f64` is a JS number, so its operators are JS's, which round as Rust's do. Where
Expand Down Expand Up @@ -287,7 +286,11 @@ returns, from `rfind` or `match_indices`, counts UTF-8 bytes, as Rust's does, an
one `&s[a..b]` takes, which reads the string up to it and panics as std does inside a
char. A JS string can't change in place, so `String`'s edits, `push_str`, `truncate`,
`insert`, `retain`, `replace_range`, and the rest, assign the edited one to the local or
field the `&mut` borrows, taking and checking byte offsets as std does. `extend` and
field the `&mut` borrows, taking and checking byte offsets as std does. A `&mut str` is
the `rt.Place` of its `String` wherever that is, and a part of one, from `&mut s[a..b]`,
`get_mut`, or `split_at_mut`, is `rt.strPart(p, a, b)`, a place of its own that writes the
whole string anew, checking its offsets as `&s[a..b]` does. `as_bytes_mut`,
`get_unchecked_mut`, and `str::from_utf8_mut` are refused. `extend` and
`collect` read their iterator by `for_each`, as std's do, and of chars first reserve its
size hint's lower bound, which panics past `isize::MAX`. A `drain` holds the `&mut`'s
place and writes the string without the drained text as it drops, so one that
Expand Down
4 changes: 4 additions & 0 deletions ts/fixtures/lang/caller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,10 @@ const asciiCases = [
// Text of every char `ascii.rs` tries, each once.
...[0, 1, 2, 3].map((kind) => Uint8Array.of(op("ascii::slices"), kind, ...Array.from({ length: 41 }, (_, i) => i))),
Uint8Array.of(op("ascii::chars"), 0, ...[0xd7ff, 0xd800, 0xdfff, 0xe000, 0x1f600, 0x10ffff, 0x110000].flatMap((n) => [n >>> 24, (n >>> 16) & 0xff, (n >>> 8) & 0xff, n & 0xff])),
// `parts` of each kind at every pair of byte offsets up to two past the end of its
// 14 bytes of text, and of no text.
...Array.from({ length: 8 * 17 * 17 }, (_, i) => Uint8Array.of(op("ascii::parts"), i % 8, Math.floor(i / 8) % 17, Math.floor(i / 8 / 17))),
...Array.from({ length: 8 * 3 * 3 }, (_, i) => Uint8Array.of(op("ascii::parts"), 0x10 | (i % 8), Math.floor(i / 8) % 3, Math.floor(i / 24))),
];

export const cases: Uint8Array[] = [...loops, ...durations, ...ints(), ...utf8, ...floats, ...formats, ...parsed, ...singlesCases, ...singlesFormats, ...singlesParsed, ...dyns, ...cells, ...shared, ...locks, ...onces, ...bounds, ...unsafeCells, ...edits, ...subslices, ...consumers, ...closures, ...elements, ...ranges, ...reducers, ...sliceEdits, ...drains, ...iters, ...hinted, ...peekedOthers, ...hints, ...reserves, ...setOps, ...zeros, ...writes, ...floatsMulAdds, ...singlesMulAdds, ...unlikeEqs, ...ownNes, ...rangeFroms, ...compares, ...unlikeCompares, ...rangeMethods, ...wraps(), ...refOrders, ...saturates(), ...methods(), ...separated, ...asciiCases];
137 changes: 136 additions & 1 deletion ts/fixtures/lang/expected/ascii.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import * as rt from "./rt.ts";
import type * as ascii_Held from "./ascii.Held.ts";
import { isSomeAnd$09tpccf, unwrapOr$0r2bmpr as option_unwrapOr$0r2bmpr, unwrapOr$0l7j3f6 as option_unwrapOr$0l7j3f6, mapOr$02k4ito as option_mapOr$02k4ito, mapOr$0jad0p1 as option_mapOr$0jad0p1 } from "./core/option.ts";
import { isSomeAnd$09tpccf, unwrapOr$1iemzn8 as option_unwrapOr$1iemzn8, unwrapOr$0r2bmpr as option_unwrapOr$0r2bmpr, unwrapOr$0l7j3f6 as option_unwrapOr$0l7j3f6, mapOr$1t0muvr as option_mapOr$1t0muvr, mapOr$02k4ito as option_mapOr$02k4ito, mapOr$0jad0p1 as option_mapOr$0jad0p1, asDeref$0w8at8i as option_asDeref$0w8at8i } from "./core/option.ts";
import { put as fmt_put } from "./fmt.ts";

/** Each test of `b`, a bit apiece. */
Expand Down Expand Up @@ -327,4 +327,139 @@ export function slices(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void {
}
}

/** `s[a..b]`'s case changed through the `&mut str` it is given. */
export function upperPart(s: rt.Place<string>, a: rt.usize, b: rt.usize): void {
const _5: rt.Place<string> = rt.strPart(s, a, b);
_5.v = rt.strToAsciiUppercase(_5.v);
}

/**
* A part `get_mut` or `split_at_mut_checked` may give, upper-cased and put, or 0xfe if
* it gave none.
*/
export function putUpper(out: rt.ByteVec, part: rt.Place<string> | undefined): void {
if (part === undefined) {
rt.byteVecPush(out, 254);
} else {
part.v = rt.strToAsciiUppercase(part.v);
fmt_put(out, part.v);
}
}

/**
* Case changed through a `&mut str` to a `String` where a local, a tuple, an array, a
* `Vec`, or an `Option` holds it, and to parts of one at byte offsets `data` picks up to
* two past the end, the `String` put after each change. A part off a char boundary,
* backwards, or out of bounds panics as std's does; a checked one gives `None`.
*/
export function parts(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void {
let text!: string;
if ((kind & 16) === 0) {
text = "aÉbࠀc😀dZ";
} else {
text = "";
}
const _7: string = text;
const at: (a0: rt.usize) => rt.usize = (i: rt.usize): rt.usize => {
return option_mapOr$1t0muvr(rt.byteSliceGet(data, i), 0, (_2: rt.u8): rt.usize => {
return rt.usizeRem(_2 as rt.usize, rt.usizeAdd(rt.stringLen(_7), 3));
});
};
const _10: [rt.usize, rt.usize] = [at(0), at(1)];
const a: rt.usize = _10[0];
const b: rt.usize = _10[1];
let s: string = text;
const pair: [rt.u8, string] = [7, text];
const array: string[] = ["q", text];
const v: string[] = [text, text];
const _32: rt.u8 = rt.u8Rem(kind, 8);
if (_32 === 0) {
s = rt.strToAsciiUppercase(s);
fmt_put(out, s);
const _41: rt.Place<string> = rt.strPart(rt.place(() => s, (value15) => s = value15), a, b);
_41.v = rt.strToAsciiLowercase(_41.v);
fmt_put(out, s);
} else if (_32 === 1) {
pair[1] = rt.strToAsciiUppercase(pair[1]);
fmt_put(out, pair[1]);
const _54: rt.Place<string> = rt.strPart(rt.place(() => pair[1], (value14) => pair[1] = value14), a);
_54.v = rt.strToAsciiLowercase(_54.v);
fmt_put(out, pair[1]);
rt.byteVecPush(out, pair[0]);
} else if (_32 === 2) {
array[1] = rt.strToAsciiUppercase(array[1]);
fmt_put(out, array[1]);
const _73: rt.Place<string> = rt.strPart(rt.place(() => array[1], (value13) => array[1] = value13), 0, b);
_73.v = rt.strToAsciiLowercase(_73.v);
fmt_put(out, array[1]);
fmt_put(out, array[0]);
} else if (_32 === 3) {
const iter: rt.Iter<rt.Place<string>> = rt.iterSliceMut(v);
while (true) {
const _93: rt.Place<string> | rt.Done = iter.next();
if (_93 === rt.DONE) {
break;
}
_93.v = rt.strToAsciiUppercase(_93.v);
}
const at6 = rt.idx(v, 0);
const _100: rt.Place<string> = rt.strPart(rt.place(() => v[at6], (value11) => v[at6] = value11), 0);
_100.v = rt.strToAsciiLowercase(_100.v);
const at7 = rt.idx(v, 1);
const _105: rt.Place<string> = rt.strPart(rt.place(() => v[at7], (value12) => v[at7] = value12), a, b);
_105.v = rt.strToAsciiLowercase(_105.v);
fmt_put(out, v[rt.idx(v, 0)]);
fmt_put(out, v[rt.idx(v, 1)]);
} else if (_32 === 4) {
const _119: [rt.Place<string>, rt.Place<string>] = rt.strSplitAtMut(rt.place(() => s, (value10) => s = value10), a);
_119[0].v = rt.strToAsciiUppercase(_119[0].v);
_119[1].v = rt.strToAsciiUppercase(_119[1].v);
_119[0].v = rt.strToAsciiLowercase(_119[0].v);
fmt_put(out, _119[0].v);
fmt_put(out, _119[1].v);
const _131: [rt.Place<string>, rt.Place<string>] = rt.strSplitAtMut(_119[1], rt.usizeSaturatingSub(b, a));
_131[1].v = rt.strToAsciiLowercase(_131[1].v);
fmt_put(out, _131[0].v);
fmt_put(out, _131[1].v);
fmt_put(out, s);
} else if (_32 === 5) {
putUpper(out, rt.strGetMut(rt.place(() => s, (value7) => s = value7), a, b));
putUpper(out, rt.strGetMut(rt.place(() => pair[1], (value8) => pair[1] = value8), a));
const at4 = rt.idx(v, 1);
putUpper(out, rt.strGetMut(rt.place(() => v[at4], (value9) => v[at4] = value9), 0, b));
fmt_put(out, s);
fmt_put(out, pair[1]);
fmt_put(out, v[rt.idx(v, 1)]);
} else if (_32 === 6) {
const _168: [rt.Place<string>, rt.Place<string>] | undefined = rt.strSplitAtMutChecked(rt.place(() => array[1], (value5) => array[1] = value5), a);
if (_168 === undefined) {
rt.byteVecPush(out, 253);
} else {
_168[1].v = rt.strToAsciiUppercase(_168[1].v);
putUpper(out, rt.strGetMut(_168[0], rt.min(b, rt.stringLen(_168[0].v))));
}
fmt_put(out, array[1]);
let held: string | undefined = text;
const _191: rt.Place<string> | undefined = held === undefined ? undefined : rt.place(() => held as string, (value6) => held = value6);
if (_191 !== undefined) {
_191.v = rt.strToAsciiUppercase(_191.v);
}
fmt_put(out, option_unwrapOr$1iemzn8(option_asDeref$0w8at8i(held), "-"));
} else {
const at2 = rt.idx(v, 1);
upperPart(rt.strPart(rt.place(() => v[at2], (value) => v[at2] = value), a), 0, rt.usizeSaturatingSub(b, a));
fmt_put(out, v[rt.idx(v, 1)]);
upperPart(rt.place(() => s, (value2) => s = value2), a, b);
fmt_put(out, rt.strPart(rt.place(() => s, (value3) => s = value3), 0, a).v);
fmt_put(out, rt.strPart(rt.place(() => s, (value4) => s = value4), a).v);
const leaked: rt.Place<string> = { v: pair[1] };
const _234: rt.Place<string> = rt.strPart(leaked, rt.min(b, a));
_234.v = rt.strToAsciiUppercase(_234.v);
fmt_put(out, leaked.v);
const empty: rt.Place<string> = { v: "" };
empty.v = rt.strToAsciiUppercase(empty.v);
fmt_put(out, empty.v);
}
}

//# sourceMappingURL=ascii.ts.map
4 changes: 2 additions & 2 deletions ts/fixtures/lang/expected/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import * as rt from "./rt.ts";
import { args } from "./args.ts";
import { fromFns, maps, lent as arrays_lent, asSlices, given, flattened as arrays_flattened } from "./arrays.ts";
import { bytes as ascii_bytes, chars as ascii_chars, slices as ascii_slices } from "./ascii.ts";
import { bytes as ascii_bytes, chars as ascii_chars, slices as ascii_slices, parts as ascii_parts } from "./ascii.ts";
import { atomics } from "./atomics.ts";
import { borrows as borrows_borrows } from "./borrows.ts";
import { bounds as bounds_bounds } from "./bounds.ts";
Expand Down Expand Up @@ -248,7 +248,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec)
reattached(data86, out87);
}] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => undefined], ["pointers::part_written", (_2187: rt.u8, data87: rt.ByteSlice, out88: rt.ByteVec): undefined => {
partWritten(data87, out88);
}] 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]];
}] 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], ["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]];
}

/** Run one case. */
Expand Down
Loading