Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion quest/m2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ The fork's feature quests folded into the [exporter](/quest/m1/ffi-export.md);
- [Output tsc accepts for writes through Result and Poll compares](/quest/m2/ts-tsc-shapes.md) - two shapes whose output fails `tsc` type-check
- [Default comparison methods](/quest/m2/ts-cmp-defaults.md) - inherited `ne`, `lt`, `le`, `gt`, `ge` and `[u8]` equality translate
- [Copy-on-write strings](/quest/m2/ts-cow-str.md) - `Cow<str>` translates, finishing `ietf/namespace.rs`
- [str slicing and &str patterns](/quest/m2/ts-str-slice.md) - `&s[a..b]` and `&str` patterns translate, so `rfind`'s offsets are usable
- [Translate moq-net's model to TypeScript](/quest/m2/ts-model.md) - the driver grows from moq-net's codec to its model, for moq to compare against WASM
- [Waiting: Swift 6.4 in nixpkgs-unstable](/quest/m2/uniffi-swift-arm.md) - the nightly UniFFI fixtures move back to arm
- [Waiting: ubrn reads UniFFI 0.32](/quest/m2/ubrn-release.md) - the binding benchmark's browser runner pins a ubrn release, not an open PR
20 changes: 0 additions & 20 deletions quest/m2/ts-str-slice.md

This file was deleted.

21 changes: 11 additions & 10 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,12 @@ std's adaptors through `rt.iterFrom`. A caller calls `next()` for one item, or i
an `rt.Iter` with `for ... of`.

A `str` is a JS string, which is UTF-16: `chars` steps by code point, and a search by a
`char` or a predicate, as in `rt.strSplit(s, "/")`, takes the char's text or the closure,
which it calls on each char it reads, in the order std reads them. An offset it returns,
from `rfind` or `match_indices`, counts UTF-8 bytes, as Rust's does. A JS string can't
change in place, so `String::push` assigns a longer one to the local or field the `&mut`
borrows.
`char`, a `&str`, or a predicate, as in `rt.strSplit(s, "/")`, takes the text or the
closure, which it calls on each char it reads, in the order std reads them. An offset it
returns, from `rfind` or `match_indices`, counts UTF-8 bytes, as Rust's does, and so does
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::push` assigns a longer one to the
local or field the `&mut` borrows.

Functions and members are camelCase. Functions are monomorphized: each instance is its
own function, suffixed after a `$` by its trait, then by its type's type arguments and
Expand Down Expand Up @@ -323,11 +324,11 @@ without a mapping in `src/lower/iter.rs` (such as `cycle`, `unzip`, and `partiti
own `impl Iterator` with `next_back`, `len`, or another provided method of its own, other
format specs (a precision, `+`, `{:b}`, `{:x?}`, and widths from arguments), writing to a
`String` with `write!` or pushing to one through a parameter, a `str` pattern other than a
`char` or a predicate, a copy of a closure that changes what it captured, `mem::swap`,
`replace`, or `take` of a reference's whole pointee, assigning an `Arc`, a waker, a
`MutexGuard`, a pinned reference, or a future through a reference, a switch on or a
field of another crate's type without a mapping, and std functions without a mapping in
`src/lower/library.rs`.
`char`, a `&str`, or a predicate, a mutable `str` slice, a copy of a closure that changes
what it captured, `mem::swap`, `replace`, or `take` of a reference's whole pointee,
assigning an `Arc`, a waker, a `MutexGuard`, a pinned reference, or a future through a
reference, a switch on or a field of another crate's type without a mapping, and std
functions without a mapping in `src/lower/library.rs`.

## Tests

Expand Down
6 changes: 4 additions & 2 deletions ts/fixtures/lang/expected/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { u8Op, u16Op, u32Op, u64Op, u128Op, usizeOp, i8Op, i16Op, i32Op, i64Op,
import { adaptors, consumers, ranges, options, own, values } from "./iters.ts";
import { keys } from "./keys.ts";
import { copies, order, discriminants, loops, patterns, closures, library, flows } from "./shapes.ts";
import { strs } from "./strs.ts";
import { strs, slices } from "./strs.ts";

/** Run one case. */
export function run(input: rt.ByteSlice): rt.ByteVec {
Expand All @@ -20,7 +20,7 @@ export function run(input: rt.ByteSlice): rt.ByteVec {
const _10: [rt.u8, rt.u8] = [input.data[input.start], input.data[input.start + 1]];
const op: rt.u8 = _10[0];
const kind: rt.u8 = _10[1];
const _14: rt.u8 = rt.u8Rem(op, 44);
const _14: rt.u8 = rt.u8Rem(op, 45);
if (_14 === 0) {
u8Op(kind, rt.byteSliceRange(input, 2), out);
} else if (_14 === 1) {
Expand Down Expand Up @@ -125,6 +125,8 @@ export function run(input: rt.ByteSlice): rt.ByteVec {
strs(kind, rt.byteSliceRange(input, 2), out);
} else if (_14 === 42) {
vecs(rt.byteSliceRange(input, 2), out);
} else if (_14 === 43) {
slices(kind, rt.byteSliceRange(input, 2), out);
} else {
stores(rt.byteSliceRange(input, 2), out);
}
Expand Down
136 changes: 136 additions & 0 deletions ts/fixtures/lang/expected/strs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,142 @@ export function strs(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void {
}
}

/**
* Up to three chars of `s`, from the char the kind's high bits pick: empty a quarter of
* the time, and otherwise found at least once unless the text is shorter.
*/
export function pattern(kind: rt.u8, s: string): string {
let p: string = "";
const iter: rt.Iter<rt.char> = rt.iterTake(rt.iterSkip(rt.strChars(s), rt.u8Div(kind, 32) as rt.usize), rt.u8Rem(rt.u8Div(kind, 8), 4) as rt.usize);
while (true) {
const _17: rt.char | rt.Done = iter.next();
if (_17 === rt.DONE) {
return p;
}
p = p + String.fromCodePoint(_17);
}
}

/**
* `&s[a..b]` by byte offsets, and the `str` methods by a `&str` pattern, which an empty
* one matches before each char and after the last.
*/
export function slices(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void {
let _211!: [rt.usize, rt.usize];
let sliced!: string;
const s: string = text(data);
const _6: string = s;
const p: string = pattern(kind, _6);
b0: {
const _8: rt.u8 = rt.u8Rem(kind, 8);
if (_8 === 0) {
let n: rt.i32 = 0;
const iter: rt.Iter<string> = rt.strSplit(s, p);
while (true) {
const _18: string | rt.Done = iter.next();
if (_18 === rt.DONE) {
break;
}
put(out, _18);
n = rt.i32Add(n, 1);
}
rt.byteVecPush(out, n & 0xff);
put(out, rt.optionUnwrapOr(rt.iterLast(rt.strSplit(s, p)), "none"));
} else {
if (_8 === 1) {
const iter2: rt.Iter<[rt.usize, string]> = rt.strMatchIndices(s, p);
while (true) {
const _42: [rt.usize, string] | rt.Done = iter2.next();
if (_42 === rt.DONE) {
break b0;
}
const at: rt.usize = _42[0];
putOffset(out, at);
put(out, _42[1]);
put(out, rt.strSlice(s, 0, at));
const part6 = rt.usizeAdd(at, rt.stringLen(_42[1]));
put(out, rt.strSlice(s, part6));
const part7 = rt.usizeAdd(at, rt.stringLen(_42[1]));
put(out, rt.strSlice(s, at, part7));
}
}
if (_8 === 2) {
put(out, rt.strTrimStartMatches(s, p));
put(out, rt.strTrimEndMatches(s, p));
put(out, rt.strTrimEndMatches(rt.strTrimStartMatches(s, p), p));
rt.byteVecPush(out, rt.strStartsWith(s, p) ? 1 : 0);
rt.byteVecPush(out, rt.strContains(s, p) ? 1 : 0);
rt.byteVecPush(out, rt.strContains(s, p) ? 1 : 0);
rt.byteVecPush(out, rt.strContains(s, "/") ? 1 : 0);
rt.byteVecPush(out, rt.strContains(s, (c: rt.char): boolean => {
return rt.charIsAsciiDigit(c);
}) ? 1 : 0);
putOffset(out, rt.strRfind(s, p));
} else if (_8 === 3) {
let path: string = s;
while (true) {
const _125: rt.usize | undefined = rt.strRfind(path, "/");
if (_125 !== undefined) {
const _130: string = path;
path = rt.strSlice(_130, 0, _125);
put(out, path);
} else {
break;
}
}
put(out, path);
} else if (_8 === 4) {
const keep: rt.usize = rt.usizeAdd(rt.u8Rem(rt.u8Div(kind, 8), 4) as rt.usize, 1);
const _146: rt.Iter<[rt.usize, string]> = rt.strMatchIndices(s, "/");
const _149: rt.usize = rt.usizeSub(keep, 1);
put(out, rt.optionUnwrapOr(rt.optionMap(rt.iterNth(_146, _149), (_2: [rt.usize, string]): string => {
return rt.strSlice(s, 0, _2[0]);
}), "none"));
const _159: rt.Iter<[rt.usize, string]> = rt.strMatchIndices(s, "/");
const _162: rt.usize = rt.usizeSub(keep, 1);
const part5 = rt.optionMapOr(rt.iterNth(_159, _162), rt.stringLen(s), (_22: [rt.usize, string]): rt.usize => {
return rt.usizeAdd(_22[0], 1);
});
put(out, rt.strSlice(s, part5));
} else if (_8 === 5) {
const parts: string[] = rt.iterToArray(rt.strSplit(s, "/"));
put(out, parts.join(p));
put(out, rt.iterToArray(rt.iterMap(rt.iterSlice(parts), (part3: string): string => {
return part3;
})).join(p));
put(out, ["a", "", s].join("/"));
} else {
const at2: (a0: rt.u8) => rt.usize = (b2: rt.u8): rt.usize => {
if ((kind & 32) === 0) {
return rt.usizeRem(b2 as rt.usize, rt.usizeAdd(rt.stringLen(s), 2));
}
return rt.usizeSaturatingSub(rt.usizeAdd(rt.stringLen(s), 1), rt.usizeRem(b2 as rt.usize, 4));
};
if (!(data.length >= 2)) {
_211 = [0, 0] as [rt.usize, rt.usize];
} else {
_211 = [at2(data.data[data.start]), at2(data.data[data.start + 1])] as [rt.usize, rt.usize];
}
const a: rt.usize = _211[0];
const b: rt.usize = _211[1];
const _225: rt.u8 = rt.u8Rem(rt.u8Div(kind, 8), 4);
if (_225 === 0) {
sliced = rt.strSlice(s, a, b);
} else if (_225 === 1) {
sliced = rt.strSlice(s, a);
} else if (_225 === 2) {
const _236: string = s;
sliced = rt.strSlice(_236, 0, b);
} else {
const _240: string = s;
sliced = rt.strSlice(_240, 0);
}
put(out, sliced);
}
}
}
}

/** A predicate that is a function, not a closure. */
export function digit(c: rt.char): boolean {
return rt.charIsAsciiDigit(c);
Expand Down
3 changes: 2 additions & 1 deletion ts/fixtures/lang/src/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn run(input: &[u8]) -> Vec<u8> {
return out;
};
let (op, kind) = (*op, *kind);
match op % 44 {
match op % 45 {
0 => ints::u8_op(kind, data, &mut out),
1 => ints::u16_op(kind, data, &mut out),
2 => ints::u32_op(kind, data, &mut out),
Expand Down Expand Up @@ -73,6 +73,7 @@ pub fn run(input: &[u8]) -> Vec<u8> {
40 => chars::chars(kind, data, &mut out),
41 => strs::strs(kind, data, &mut out),
42 => collections::vecs(data, &mut out),
43 => strs::slices(kind, data, &mut out),
_ => collections::stores(data, &mut out),
}
out
Expand Down
106 changes: 102 additions & 4 deletions ts/fixtures/lang/src/strs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! `str` and `String` over `char`: iterated from both ends, searched by a `char` or a
//! predicate, trimmed, split, and built a char at a time. Text is made of chars of every
//! UTF-8 width and those next to UTF-16's surrogates, so byte offsets and code units part.
//! Predicates log each char they are called with, so where a search stops is output too.
//! `str` and `String` over `char`: iterated from both ends, searched by a `char`, a
//! predicate, or a `&str`, trimmed, split, sliced by byte offsets, and built a char at a
//! time. Text is made of chars of every UTF-8 width and those next to UTF-16's surrogates,
//! so byte offsets and code units part. Predicates log each char they are called with, so
//! where a search stops is output too.

use crate::fmt::put;

Expand Down Expand Up @@ -225,6 +226,103 @@ pub fn strs(kind: u8, data: &[u8], out: &mut Vec<u8>) {
}
}

/// Up to three chars of `s`, from the char the kind's high bits pick: empty a quarter of
/// the time, and otherwise found at least once unless the text is shorter.
fn pattern(kind: u8, s: &str) -> String {
let mut p = String::new();
for c in s.chars().skip((kind / 32) as usize).take((kind / 8 % 4) as usize) {
p.push(c);
}
p
}

/// `&s[a..b]` by byte offsets, and the `str` methods by a `&str` pattern, which an empty
/// one matches before each char and after the last.
pub fn slices(kind: u8, data: &[u8], out: &mut Vec<u8>) {
let s = text(data);
let p = pattern(kind, &s);
match kind % 8 {
0 => {
// `split`, by a `&str` and by a `&String`.
let mut n = 0;
for part in s.split(p.as_str()) {
put(out, part);
n += 1;
}
out.push(n as u8);
put(out, s.split(&p).last().unwrap_or("none"));
}
1 => {
// `match_indices`: byte offsets, then the text before and after each match.
for (at, matched) in s.match_indices(p.as_str()) {
put_offset(out, Some(at));
put(out, matched);
put(out, &s[..at]);
put(out, &s[at + matched.len()..]);
put(out, &s[at..at + matched.len()]);
}
}
2 => {
// Trimmed, tested, and found.
put(out, s.trim_start_matches(p.as_str()));
put(out, s.trim_end_matches(p.as_str()));
put(out, s.trim_start_matches(&p).trim_end_matches(&p));
out.push(s.starts_with(p.as_str()) as u8);
out.push(s.contains(p.as_str()) as u8);
out.push(s.contains(&p) as u8);
out.push(s.contains('/') as u8);
out.push(s.contains(|c: char| c.is_ascii_digit()) as u8);
put_offset(out, s.rfind(p.as_str()));
}
3 => {
// Each parent of a path, as moq-net's discovery walks them.
let mut path = s.as_str();
while let Some(slash) = path.rfind('/') {
path = &path[..slash];
put(out, path);
}
put(out, path);
}
4 => {
// A path cut after its `keep`th part, and the rest, as moq-net's compression
// cuts one.
let keep = (kind / 8 % 4) as usize + 1;
let head = s.match_indices('/').nth(keep - 1).map(|(end, _)| &s[..end]);
put(out, head.unwrap_or("none"));
let start = s.match_indices('/').nth(keep - 1).map_or(s.len(), |(i, _)| i + 1);
put(out, &s[start..]);
}
5 => {
// Parts joined again, by the pattern.
let parts: Vec<&str> = s.split('/').collect();
put(out, &parts.join(p.as_str()));
let owned: Vec<String> = parts.iter().map(|part| part.to_string()).collect();
put(out, &owned.join(p.as_str()));
put(out, &["a", "", &s].join("/"));
}
_ => {
// A slice at any offsets, or at ones about the end by the kind's bit 5, which
// panics as std does past the end, backwards, or inside a char, whichever comes
// first.
let at = |b: u8| match kind & 0x20 {
0 => b as usize % (s.len() + 2),
_ => (s.len() + 1).saturating_sub(b as usize % 4),
};
let (a, b) = match data {
[a, b, ..] => (at(*a), at(*b)),
_ => (0, 0),
};
let sliced = match kind / 8 % 4 {
0 => &s[a..b],
1 => &s[a..],
2 => &s.as_str()[..b],
_ => &s.as_str()[..],
};
put(out, sliced);
}
}
}

/// A predicate that is a function, not a closure.
fn digit(c: char) -> bool {
c.is_ascii_digit()
Expand Down
6 changes: 3 additions & 3 deletions ts/fixtures/reject/census.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ The type or module each refused type or path belongs to.
| 1 | `std::ops::RangeTo` | fixtures/reject/src/lib.rs:289:7 |
| 1 | `std::ops::Range` | fixtures/reject/src/lib.rs:207:8 |
| 1 | `std::slice::Iter` | fixtures/reject/src/lib.rs:153:11 |
| 1 | `std::str` | fixtures/reject/src/lib.rs:382:4 |
| 1 | `std::str` | fixtures/reject/src/lib.rs:383:4 |
| 1 | `std::sync::MutexGuard` | fixtures/reject/src/lib.rs:148:2 |
| 1 | `u64` | fixtures/reject/src/lib.rs:118:2 |
| 1 | `{async closure}` | fixtures/reject/src/lib.rs:74:2 |
Expand Down Expand Up @@ -138,7 +138,7 @@ The type or module each refused type or path belongs to.
| 1 | unsupported call to `_` | `std::ops::Range::contains` | fixtures/reject/src/lib.rs:207:8 |
| 1 | unsupported call to `_` | `std::slice::copy_within` | fixtures/reject/src/lib.rs:367:4 |
| 1 | unsupported call to `_` | `std::slice::sort` | fixtures/reject/src/lib.rs:32:4 |
| 1 | unsupported call to `_` | `std::str::split` | fixtures/reject/src/lib.rs:382:4 |
| 1 | unsupported call to `_` | `std::str::split` | fixtures/reject/src/lib.rs:383:4 |
| 1 | unsupported type `_` | `dyn std::ops::Fn(u8) -> u8` | fixtures/reject/src/lib.rs:10:16 |

## Spans
Expand All @@ -163,7 +163,7 @@ The type or module each refused type or path belongs to.
- fixtures/reject/src/lib.rs:153:11: unsupported call to `<std::slice::Iter<'_, u8> as std::iter::Iterator>::cycle`
- fixtures/reject/src/lib.rs:207:8: unsupported call to `std::ops::Range::<Key>::contains::<Key>`
- fixtures/reject/src/lib.rs:367:4: unsupported call to `core::slice::<impl [(u8, u8)]>::copy_within::<std::ops::Range<usize>>`
- fixtures/reject/src/lib.rs:382:4: unsupported call to `core::str::<impl str>::split::<&str>`
- fixtures/reject/src/lib.rs:383:4: unsupported call to `core::str::<impl str>::split::<[char; 2]>`

</details>

Expand Down
Loading