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 @@ -32,7 +32,6 @@ The fork's feature quests folded into the [exporter](/quest/m1/ffi-export.md);
- [Shared ownership](/quest/m2/ts-shared.md) - `Arc`/`Rc`/`Weak` keep exact counts, and locks and atomics become plain values
- [Matches TypeScript has already narrowed](/quest/m2/ts-narrow-test.md) - an inner match on a value an outer match decided passes `tsc`
- [Small std gaps](/quest/m2/ts-std-gaps.md) - `bool::then`, `iter::successors`, `usize::from(bool)`, and `None.ok_or` narrowing
- [Copy-on-write strings](/quest/m2/ts-cow-str.md) - `Cow<str>` translates, finishing `ietf/namespace.rs`
- [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
19 changes: 0 additions & 19 deletions quest/m2/ts-cow-str.md

This file was deleted.

20 changes: 13 additions & 7 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ in `rt/str.ts`, collections in `rt/collections.ts`, and ownership and async in

| Rust | TypeScript |
| --- | --- |
| `bool`, `str`, `String` | `boolean`, `string` |
| `bool`, `str`, `String`, `Cow<str>` | `boolean`, `string` |
| `char` | `rt.char`: a `number` branded so, its Unicode scalar value |
| `u8`-`u32`, `i8`-`i32`, `usize`, `isize` | `rt.u8` and so on: a `number` branded with its type, checked: overflow panics |
| `u64`, `i64`, `u128`, `i128` | `rt.u64` and so on: a branded `number` while a safe integer, 32-bit limbs past 2^53, checked the same way |
Expand Down Expand Up @@ -134,7 +134,9 @@ closure, which it calls on each char it reads, in the order std reads them. An o
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.
local or field the `&mut` borrows. A `Cow<str>` is its text, borrowed or owned, so
`to_mut` borrows the `Cow`'s own place, and code that tells its variants apart, a `match`
on them or `Cow::is_borrowed`, is refused.

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 @@ -272,6 +274,9 @@ they need.
function of it is one instance's, typed by its `self`. A type per instance
(`Wrap$Pair`) would rename the type for callers, and TS generics with trait
dictionaries would undo monomorphization.
- A `Cow<str>` is a JS string, as `str` and `String` are, rather than an object tagged by
its variant: no `Cow<str>` in moq-net or kio tells its variants apart, and a tag would
wrap every value and unwrap it at every read.
- `Result` is a plain `{ ok, value } | { ok, error }` object rather than an exception, so
`?` and `match` keep Rust's control flow exactly. Making no `Ok` objects at all measured
no faster on `just ts bench`, in V8 or JavaScriptCore.
Expand Down Expand Up @@ -323,8 +328,8 @@ bind to hand-written TS through config ([model](/quest/m2/ts-model.md)).

These fail translation: floats, statics, async blocks that borrow a number
mutably and async closures, values that drop in a closure dropped without a call that
takes it or in an iterator ([Drop](/quest/m2/ts-drop.md)), a collection holding a type that can be
`undefined`, as `()` and `Option` can, a collection's `capacity`, looking up a key by
takes it or in an iterator ([Drop](/quest/m2/ts-drop.md)), a `Cow` of other than `str`,
a collection holding a type that can be `undefined`, as `()` and `Option` can, a collection's `capacity`, looking up a key by
another type it borrows as through the crate's own `Borrow`, a `&mut` to a value a map
holds that JS copies kept other than to read and write it, `Hasher::write` of bytes, the adaptors
without a mapping in `src/lower/iter.rs` (such as `cycle`, `unzip`, and `partition`), an
Expand Down Expand Up @@ -357,9 +362,10 @@ built natively for wasm32 and run beside its translation on the same inputs by
Bun bundles a caller of `Decoder.u8` and `Decoder.new` through `import * as Decoder`,
which must keep only those and what they call.
- `lang` covers every integer width's arithmetic and casts, `char`s next to each
encoding's edges and the surrogates, control flow, patterns, `Copy` aggregates, those
a container stores included and those written into only in another module, behind a
type parameter, or through a box, formatting, methods on generic types, iterators, with
encoding's edges and the surrogates, `Cow<str>` borrowed and owned, control flow,
patterns, `Copy` aggregates, those a container stores included and those written into
only in another module, behind a type parameter, or through a box, formatting, methods
on generic types, iterators, with
closures that log what each
adaptor reads, and std's collections, over keys whose own `Hash`, `Eq`, and `Ord`
disagree with JS's, and `vec![x; n]` of types whose `Clone` counts its clones. Its differential test runs through `caller.ts`, a
Expand Down
76 changes: 76 additions & 0 deletions ts/fixtures/lang/expected/cows.Named.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";

export function clone$Clone(self: Named): Named {
return { name: self.name, id: self.id };
}

export function fmt$Debug(self: Named, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
const _3: string = "Named";
const _4: string = "name";
const _5: rt.Fmt = (f: rt.Formatter) => rt.fmtStr(self.name, f);
const _7: string = "id";
const _8: rt.Fmt = (f: rt.Formatter) => rt.u8Display(self.id, f);
return rt.debugStructFinish(rt.debugStructField(rt.debugStructField(rt.fmtDebugStruct(f, _3), _4, _5), _7, _8));
}

export function default$Default(): Named {
return { name: "", id: 0 };
}

export function eq$PartialEq(self: Named, other: Named): boolean {
if (!(self.id === other.id)) {
return false;
}
return self.name === other.name;
}

export function hash$Hash_DefaultHasher(self: Named, state: rt.Hasher): void {
rt.hashStr(self.name, state);
rt.hashInt(self.id, state);
}

export function partialCmp$PartialOrd(self: Named, other: Named): rt.Ordering | undefined {
return cmp$Ord(self, other);
}

export function cmp$Ord(self: Named, other: Named): rt.Ordering {
const cmp: rt.Ordering = rt.cmpStr(self.name, other.name);
if (cmp === 0) {
return rt.cmp(self.id, other.id);
}
return cmp;
}

/** A `Cow` in a field, with each derived impl reading it. */
export interface Named {
name: string;
id: rt.u8;
}

/** Borrowed unless it has to make the text, as moq-net's `Relative::new` is. */
function new_(s: string, id: rt.u8): Named {
let name!: string;
const trimmed: string = rt.strTrimEndMatches(s, "/");
if (!((trimmed.length === 0))) {
name = trimmed;
} else {
const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.u8Display(id, f)];
name = rt.fmtFormat(["#", args2[0]]);
}
return { name, id };
}

/** Through `to_mut` of the field. */
export function tag(self: Named, c: rt.char): void {
self.name = self.name + String.fromCodePoint(c);
}

export function intoOwned(self: Named): Named {
return { name: self.name, id: self.id };
}

export { new_ as new };

//# sourceMappingURL=cows.Named.ts.map
164 changes: 164 additions & 0 deletions ts/fixtures/lang/expected/cows.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";
import * as Named from "./cows.Named.ts";
import { put } from "./fmt.ts";
import { text } from "./strs.ts";

/** `to_mut` of a `Result`'s payload, which the store rebuilds the `Result` around. */
export function exclaimed(r: rt.Result<string, rt.u8>): rt.Result<string, rt.u8> {
if (r.ok) {
return rt.Ok(r.value + "!");
}
return r;
}

export function cow(s: string, owned: boolean): string {
if (!owned) {
return s;
}
return s;
}

export function cows(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void {
let shared!: rt.Arc<string>;
let r!: rt.Result<string, rt.u8>;
let unwind: number = 0;
try {
const s: string = text(data);
const owned: boolean = (kind & 128) !== 0;
const first: string = rt.optionUnwrapOr(rt.iterToOption(rt.strSplit(s, "/").next()), "");
const _14: rt.u8 = rt.u8Rem(kind, 6);
if (_14 === 0) {
const c: string = cow(s, owned);
rt.byteVecPush(out, rt.stringLen(c) & 0xff);
rt.byteVecPush(out, c.length === 0 ? 1 : 0);
rt.byteVecPush(out, rt.iterCount(rt.strChars(c)) & 0xff);
put(out, c);
put(out, c);
put(out, c);
put(out, c);
put(out, c);
put(out, "");
put(out, "fallback");
put(out, s);
shared = new rt.Arc(s);
unwind = 267;
put(out, shared.value);
const part: string = rt.optionUnwrapOr(rt.optionMap(rt.iterNth(rt.strSplit(s, "\\"), 1), (a03: string): string => {
return a03;
}), "fallback");
unwind = 266;
put(out, part);
const into: string = cow(first, owned);
unwind = 265;
put(out, into);
const fromStr: string = first;
unwind = 264;
put(out, fromStr);
unwind = 0;
rt.drop(shared);
} else if (_14 === 1) {
let c2: string = cow(s, owned);
c2 = c2 + "!";
c2 = c2 + "?";
c2 = c2 + "𐀀";
put(out, c2);
put(out, s);
const v: string[] = [cow(first, owned), cow(s, !owned)];
const at = rt.idx(v, 1);
v[at] = v[at] + "v";
for (const _126 of v) {
put(out, _126);
}
put(out, c2);
if ((kind & 64) === 0) {
r = rt.Ok(cow(first, owned));
} else {
r = rt.Err(kind);
}
const _140: rt.Result<string, rt.u8> = exclaimed(r);
if (_140.ok) {
put(out, _140.value);
} else {
rt.byteVecPush(out, _140.error);
}
} else if (_14 === 2) {
const a: string = cow(s, owned);
const b: string = cow(first, !owned);
const iter2: rt.Iter<boolean> = rt.iterSlice([a === first, a === first, a === s, first === a, s === a, a === b, a !== b, rt.cmpStr(a, b) < 0, rt.cmpStr(a, b) >= 0, rt.cmpStr(a, b) === 1]);
while (true) {
const _189: boolean | rt.Done = iter2.next();
if (_189 === rt.DONE) {
break;
}
rt.byteVecPush(out, _189 ? 1 : 0);
}
rt.byteVecPush(out, rt.ordMax(a, b, rt.cmpStr) === b ? 1 : 0);
} else if (_14 === 3) {
const n: Named.Named = Named.new(first, kind);
const m2: Named.Named = Named.clone$Clone(n);
Named.tag(n, 120);
const args2: rt.Fmt[] = [(f: rt.Formatter) => Named.fmt$Debug(n, f), (f: rt.Formatter) => Named.fmt$Debug(m2, f)];
put(out, rt.fmtFormat([args2[0], " ", args2[1]]));
rt.byteVecPush(out, Named.eq$PartialEq(n, m2) ? 1 : 0);
rt.byteVecPush(out, Named.cmp$Ord(n, m2) > 0 ? 1 : 0);
rt.byteVecPush(out, Named.partialCmp$PartialOrd(n, m2) === -1 ? 1 : 0);
put(out, Named.intoOwned(n).name);
const _255: Named.Named = Named.default$Default();
const _257: Named.Named = Named.new("", kind);
const args4: rt.Fmt[] = [(f: rt.Formatter) => Named.fmt$Debug(_255, f), (f: rt.Formatter) => Named.fmt$Debug(_257, f)];
put(out, rt.fmtFormat([args4[0], " ", args4[1]]));
} else if (_14 === 4) {
const counts: rt.BTreeMap<string, rt.u8> = rt.btreeNew(rt.cmpStr) as rt.BTreeMap<string, rt.u8>;
const iter3: rt.Iter<[rt.usize, string]> = rt.iterEnumerate(rt.strSplit(s, "/"));
while (true) {
const _271: [rt.usize, string] | rt.Done = iter3.next();
if (_271 === rt.DONE) {
break;
}
const _279: string = cow(_271[1], rt.usizeRem(_271[0], 2) === 1 === owned);
const place = rt.entryOrInsertMut(rt.entry(counts, _279), 0);
place.v = rt.u8Add(place.v, 1);
}
const iter4: rt.Iter<[string, rt.u8]> = rt.btreeIter(counts);
while (true) {
const _288: [string, rt.u8] | rt.Done = iter4.next();
if (_288 === rt.DONE) {
break;
}
put(out, _288[0]);
rt.byteVecPush(out, _288[1]);
}
rt.byteVecPush(out, rt.optionUnwrapOr(rt.btreeGet(counts, first), 255));
const names: rt.HashSet<Named.Named> = rt.hashNew(rt.hashing(Named.hash$Hash_DefaultHasher, Named.eq$PartialEq)) as rt.HashSet<Named.Named>;
const iter5: rt.Iter<string> = rt.strSplit(s, "/");
while (true) {
const _308: string | rt.Done = iter5.next();
if (_308 === rt.DONE) {
break;
}
const _314: Named.Named = Named.new(_308, 0);
rt.hashAdd(names, _314);
}
rt.byteVecPush(out, rt.hashLen(names) & 0xff);
const _324: Named.Named = Named.new(first, 0);
rt.byteVecPush(out, rt.hashContains(names, _324) ? 1 : 0);
} else {
const c4: string = cow(s, owned);
const args6: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtPad(f, c4), (f: rt.Formatter) => rt.fmtStr(c4, f)];
put(out, rt.fmtFormat(["[", args6[0], "|", rt.fmtSpec({ align: ">", width: 6 }, args6[0]), "|", rt.fmtSpec({ align: "<", width: 5 }, args6[0]), "|", rt.fmtSpec({ align: "^", width: 7 }, args6[0]), "|", rt.fmtSpec({ fill: "*", align: "^", width: 3 }, args6[0]), "|", args6[1], "]"]));
const part2 = cow(first, owned);
const args8: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtOption(part2, f, rt.fmtStr)];
put(out, rt.fmtFormat([args8[0]]));
}
} catch (e2) {
if (unwind === 0) {
throw e2;
}
rt.drop(shared);
throw e2;
}
}

//# sourceMappingURL=cows.ts.map
7 changes: 5 additions & 2 deletions ts/fixtures/lang/expected/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as rt from "./rt.ts";
import { chars } from "./chars.ts";
import { chunks } from "./chunks.ts";
import { hashMaps, sets, btreeMaps, deques, vecs, stores } from "./collections.ts";
import { cows } from "./cows.ts";
import { ints, strings, derived, written, panics } from "./fmt.ts";
import { methods } from "./generics.ts";
import { u8Op, u16Op, u32Op, u64Op, u128Op, usizeOp, i8Op, i16Op, i32Op, i64Op, i128Op, isizeOp, i8Neg, i16Neg, i32Neg, i64Neg, i128Neg, isizeNeg, cast, convert } from "./ints.ts";
Expand All @@ -21,7 +22,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, 46);
const _14: rt.u8 = rt.u8Rem(op, 47);
if (_14 === 0) {
u8Op(kind, rt.byteSliceRange(input, 2), out);
} else if (_14 === 1) {
Expand Down Expand Up @@ -130,8 +131,10 @@ export function run(input: rt.ByteSlice): rt.ByteVec {
slices(kind, rt.byteSliceRange(input, 2), out);
} else if (_14 === 44) {
stores(rt.byteSliceRange(input, 2), out);
} else {
} else if (_14 === 45) {
chunks(kind, rt.byteSliceRange(input, 2), out);
} else {
cows(kind, rt.byteSliceRange(input, 2), out);
}
return out;
}
Expand Down
Loading