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);
- [Drop in the TypeScript translator](/quest/m2/ts-drop.md) - every value that drops in moq-net and kio drops where Rust drops it
- [Async in the TypeScript translator](/quest/m2/ts-async.md) - moq-net's and kio's async code translates with Rust's lazy futures
- [Shared ownership](/quest/m2/ts-shared.md) - `Arc`/`Rc`/`Weak` keep exact counts, and locks and atomics become plain values
- [Translate char and vec![x]](/quest/m2/ts-char-vec.md) - `char` values and `vec![x]` translate, unblocking moq-net's refused sites
- [Firefox in the TS benchmark](/quest/m2/ts-bench-firefox.md) - SpiderMonkey joins Chrome, Bun, and Node in `just ts bench`
- [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
- [Symlinked paths](/quest/m2/uniffi-symlink-path.md) - the fork builds when a consumer reaches it through a symlink, as on macOS's `/var`
Expand Down
21 changes: 0 additions & 21 deletions quest/m2/ts-char-vec.md

This file was deleted.

18 changes: 10 additions & 8 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ runtime: what std does that JS does not, with iterators in `rt/iter.ts`, collect
| Rust | TypeScript |
| --- | --- |
| `bool`, `str`, `String` | `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 |
| `struct` | a plain object, typed by an interface; a class if it may drop |
Expand Down Expand Up @@ -105,9 +106,9 @@ runtime ports `core::fmt`, so the text matches native Rust's byte for byte. A pl
becomes a function writing its value, and `fmt::Arguments` an array of literal pieces
and such functions: `rt.fmtWriteFmt(f, ["code=", (f) => rt.u16Display(code, f)])`. A `&dyn
Debug` or `&dyn Display` is such a function too. Format specs take fill, alignment,
width, `#`, `0`, `x`, `X`, and `?`. `Debug` of a string escapes by the JS engine's
Unicode tables, so a character newer than its Unicode version is escaped where Rust
prints it. A caller prints a `printable` type as `to_string()` and `format!("{err:?}")`
width, `#`, `0`, `x`, `X`, and `?`. `Debug` of a string or `char` escapes by the JS
engine's Unicode tables, so a character newer than its Unicode version is escaped where
Rust prints it. A caller prints a `printable` type as `to_string()` and `format!("{err:?}")`
would: `rt.fmtToString((f) => DecodeError.fmt$Display(err, f))` and
`rt.fmtFormat([(f) => DecodeError.fmt$Debug(err, f)])`, after
`import * as DecodeError from "./coding/decode.DecodeError.ts"`.
Expand Down Expand Up @@ -283,7 +284,7 @@ bind to hand-written TS through config ([model](/quest/m2/ts-model.md)).

## Not yet

These fail translation: floats, `char`, statics, async blocks that borrow a number
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, in an iterator, in an enum or generic `Drop` type, passed to a std function
that may discard them (`unwrap_or`), borrowed in an `Arc` or `Mutex`, or in a
Expand Down Expand Up @@ -319,10 +320,11 @@ built natively for wasm32 and run beside its translation on the same inputs by
`bench::run` and then with it as the first root, which must keep every line in order.
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, control flow, patterns,
`Copy` aggregates, 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. Its differential test runs through `caller.ts`, a
- `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,
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
hand-written caller that also prints a `printable` error nothing translated prints,
and adds every 64- and 128-bit case over each pair of values next to a power of two,
where the runtime switches between numbers and limbs, and its limbs carry and borrow,
Expand Down
165 changes: 165 additions & 0 deletions ts/fixtures/lang/expected/chars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";
import { put } from "./fmt.ts";

/** The scalars to try: the edges, then each big-endian `u32` in `data`, zero-padded. */
export function scalars(data: Uint8Array): rt.u32[] {
const all: rt.u32[] = [0, 127, 128, 255, 256, 2047, 2048, 55295, 55296, 56319, 56320, 57343, 57344, 65533, 65535, 65536, 1114109, 1114111, 1114112, 4294967295].slice();
let rest: Uint8Array = data;
while (true) {
if (!((rest.length === 0))) {
const buf: Uint8Array = rt.bytes(4);
const n: rt.usize = rt.min(rest.length, 4);
const _13: Uint8Array = rt.sliceRange(buf, 0, n);
const _17: Uint8Array = rest;
rt.sliceCopyFromSlice(_13, rt.sliceRange(_17, 0, n));
all.push(rt.u32FromBeBytes(buf));
const _24: Uint8Array = rest;
rest = rt.sliceRange(_24, n);
} else {
return all;
}
}
}

/** A char's class, by a switch on its value and ranges of it. */
export function class_(c: rt.char): rt.u8 {
b1: {
if (c === 0) {
return 0;
}
if (c !== 95) {
if (c === 47 || c === 92) {
return 4;
}
if (97 <= c) {
if (c <= 122) {
return 1;
}
}
if (65 <= c) {
if (c <= 90) {
break b1;
}
}
if (48 <= c) {
if (c <= 57) {
return 3;
}
}
if (128 <= c) {
if (c <= 2047) {
return 5;
}
}
if (57344 <= c) {
if (c <= 63743) {
return 6;
}
}
if (!(65536 <= c)) {
return 8;
}
return 7;
}
}
return 2;
}

export function chars(kind: rt.u8, data: Uint8Array, out: rt.ByteVec): void {
const _4: rt.u8 = rt.u8Rem(kind, 4);
if (_4 === 0) {
const iterS: rt.u32[] = scalars(data);
let iterI: number = 0;
while (true) {
const _10: rt.u32 | rt.Done = iterI < iterS.length ? iterS[iterI++] : rt.DONE;
if (_10 === rt.DONE) {
break;
}
const _14: rt.char | undefined = rt.charFromU32(_10);
if (_14 === undefined) {
rt.byteVecPush(out, 255);
} else {
const c: rt.char = _14;
rt.byteVecPush(out, class_(c));
rt.byteVecExtendFromSlice(out, rt.u32ToBeBytes(c as rt.u32));
}
}
} else if (_4 === 1) {
const iterS2: rt.u32[] = scalars(data);
let iterI2: number = 0;
while (true) {
const _27: rt.u32 | rt.Done = iterI2 < iterS2.length ? iterS2[iterI2++] : rt.DONE;
if (_27 === rt.DONE) {
break;
}
const n2: rt.u32 = _27;
const c2: rt.char = rt.optionUnwrapOr(rt.charFromU32(n2), (n2 & 0xff) as rt.char);
rt.byteVecPush(out, c2 & 0xff);
rt.byteVecExtendFromSlice(out, rt.u16ToBeBytes(c2 & 0xffff));
rt.byteVecPush(out, ((c2 << 24) >> 24) & 0xff);
rt.byteVecExtendFromSlice(out, rt.i16ToBeBytes((c2 << 16) >> 16));
rt.byteVecExtendFromSlice(out, rt.i32ToBeBytes(c2 as rt.i32));
rt.byteVecExtendFromSlice(out, rt.u64ToBeBytes(c2 as rt.u64));
rt.byteVecExtendFromSlice(out, rt.i128ToBeBytes(c2 as rt.i128));
rt.byteVecPush(out, ((n2 & 0xff) as rt.char) & 0xff);
rt.byteVecExtendFromSlice(out, rt.u16ToBeBytes(((c2 as rt.u32) & 0xff) as rt.u16));
}
} else if (_4 === 2) {
const cs: rt.char[] = rt.iterToArray(rt.iterFilterMap(rt.iterSlice(scalars(data)), (a0: rt.u32): rt.char | undefined => {
return rt.charFromU32(a0);
}));
const sorted: rt.BTreeSet<rt.char> = rt.btreeNew(rt.cmp) as rt.BTreeSet<rt.char>;
const seen: rt.HashSet<rt.char> = rt.hashNew() as rt.HashSet<rt.char>;
const iter3: rt.Iter<[rt.usize, rt.char]> = rt.iterEnumerate(rt.iterSlice(cs));
while (true) {
const _88: [rt.usize, rt.char] | rt.Done = iter3.next();
if (_88 === rt.DONE) {
break;
}
const i: rt.usize = _88[0];
const c3: rt.char = _88[1];
const _96: rt.usize = rt.usizeRem(rt.usizeAdd(rt.usizeMul(i, 7), 3), cs.length);
const at = rt.idx(cs, _96);
const d: rt.char = cs[at];
rt.byteVecPush(out, (c3 < d ? 1 : 0) | (((c3 === d ? 1 : 0) << 1) & 0xff) | (((c3 >= 122 ? 1 : 0) << 2) & 0xff));
rt.byteVecPush(out, rt.hashAdd(seen, c3) ? 1 : 0);
rt.btreeAdd(sorted, c3);
}
const iter4: rt.Iter<rt.char> = rt.btreeKeys(sorted);
while (true) {
const _128: rt.char | rt.Done = iter4.next();
if (_128 === rt.DONE) {
break;
}
rt.byteVecExtendFromSlice(out, rt.u32ToBeBytes(_128 as rt.u32));
}
} else {
const iterS3: rt.u32[] = scalars(data);
let iterI3: number = 0;
while (true) {
const _140: rt.u32 | rt.Done = iterI3 < iterS3.length ? iterS3[iterI3++] : rt.DONE;
if (_140 === rt.DONE) {
break;
}
const _144: rt.char | undefined = rt.charFromU32(_140);
if (_144 !== undefined) {
const arg = ([_144] as [rt.char])[0];
const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtCharDisplay(arg, f)];
put(out, rt.fmtFormat([args2[0], "|", rt.fmtSpec({ align: ">", width: 3 }, args2[0]), "|", rt.fmtSpec({ fill: "*", align: "^", width: 4 }, args2[0]), "|", rt.fmtSpec({ align: "<", width: 2 }, args2[0])]));
}
}
const iter6: rt.Iter<rt.char> = rt.iterSlice([97, 39, 34, 92, 10, 0, 127, 233, 769, 173, 57344, 128512, 65438, 65439, 4447, 4448, 12644, 65440]);
while (true) {
const _161: rt.char | rt.Done = iter6.next();
if (_161 === rt.DONE) {
break;
}
const args4: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtCharDebug(_161, f), (f: rt.Formatter) => rt.fmtCharDisplay(_161, f)];
put(out, rt.fmtFormat([args4[0], "|", rt.fmtSpec({ align: ">", width: 6 }, args4[0]), "|", args4[1]]));
}
}
}

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

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

/**
* A value that counts how far it is cloned from the one `vec!` was given, which
* `vec![x; n]` clones for every element but the last, which is `x` itself.
*/
export interface Cloned {
depth: rt.u8;
name: string;
}

export function clone$Clone(self: Cloned): Cloned {
return { depth: rt.u8Add(self.depth, 1), name: self.name };
}

//# sourceMappingURL=collections.Cloned.ts.map
88 changes: 87 additions & 1 deletion ts/fixtures/lang/expected/collections.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";
import { copy$Clash, copy$Rank, copy$Tally, copy$u8_Tally } from "./lib.ts";
import { copy$Bumped, copy$Clash, copy$Rank, copy$Tally, copy$u8_Tally, copy$u8_u16, copy$u8_u8 } from "./lib.ts";
import * as Clash from "./collections.Clash.ts";
import * as Cloned from "./collections.Cloned.ts";
import * as Dir from "./collections.Dir.ts";
import * as Fussy from "./collections.Fussy.ts";
import * as Nibble from "./collections.Nibble.ts";
import * as Pair from "./collections.Pair.ts";
import * as Rank from "./collections.Rank.ts";
import * as Tally from "./collections.Tally.ts";
import * as Bumped from "./iters.Bumped.ts";

/** A 64-bit key: small, or past 2^53, where it is no longer a JS number. */
export function wide(b: rt.u8): rt.u64 {
Expand Down Expand Up @@ -1015,4 +1017,88 @@ export function deques(data: Uint8Array, out: rt.ByteVec): void {
rt.byteVecPush(out, rt.dequeIndex(q2, b2));
}

/** `vec![a, b]` and `vec![x; n]`, of bytes, numbers, `Copy` tuples, and types that clone. */
export function vecs(data: Uint8Array, out: rt.ByteVec): void {
let name!: string;
const a: rt.u8 = rt.optionUnwrapOr(rt.sliceFirst(data), 0);
const b: rt.u8 = rt.optionUnwrapOr(rt.sliceGet(data, 1), 0);
const n: rt.usize = rt.usizeRem(rt.optionUnwrapOr(rt.sliceGet(data, 2), 0) as rt.usize, 5);
const _16: rt.Result<string, rt.FromUtf8Error> = rt.stringFromUtf8(rt.sliceToByteVec(data));
if (_16.ok) {
const s: string = _16.value;
name = s;
} else {
name = "?";
}
rt.byteVecExtendFromSlice(out, rt.byteVecView(rt.byteVecFrom(Uint8Array.of(a, b, 66))));
const pairs: [rt.u8, rt.u16][] = [[a, b as rt.u16] as [rt.u8, rt.u16]].map(copy$u8_u16);
pairs.push([b, a as rt.u16] as [rt.u8, rt.u16]);
const at = rt.idx(pairs, 0);
pairs[at][0] = pairs[at][0] ^ 255;
const iterS: [rt.u8, rt.u16][] = pairs;
let iterI: number = 0;
while (true) {
const _39: [rt.u8, rt.u16] | rt.Done = iterI < iterS.length ? iterS[iterI++] : rt.DONE;
if (_39 === rt.DONE) {
break;
}
rt.byteVecPush(out, _39[0] ^ (_39[1] & 0xff));
}
rt.byteVecPush(out, [name, "x", name].length & 0xff);
const one: Cloned.Cloned[] = [{ depth: a, name: name }];
const at2 = rt.idx(one, 0);
rt.byteVecPush(out, one[at2].depth);
rt.byteVecExtendFromSlice(out, rt.byteVecView(rt.byteVecOf(b, n)));
rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(rt.vecFromElem(rt.u16Mul(a as rt.u16, 257), n, undefined)), (w: rt.u16): rt.u8 => {
return rt.u16Shr(w, 4) & 0xff;
}));
const tuples: [rt.u8, rt.u8][] = rt.vecFromElem(copy$u8_u8([a, b] as [rt.u8, rt.u8]), n, copy$u8_u8);
if (n > 0) {
const at3 = rt.idx(tuples, 0);
tuples[at3][0] = 238;
}
rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(tuples), (t: [rt.u8, rt.u8]): rt.u8 => {
return t[0] ^ t[1];
}));
rt.byteVecPush(out, rt.vecFromElem(name, n, undefined).length & 0xff);
const cloned: Cloned.Cloned[] = rt.vecFromElem({ depth: 0, name }, n, Cloned.clone$Clone);
rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(cloned), (c: Cloned.Cloned): rt.u8 => {
return c.depth;
}));
const _113: rt.Iter<Cloned.Cloned> = rt.iterSlice(cloned);
rt.byteVecPush(out, rt.iterAll(_113, (c2: Cloned.Cloned): boolean => {
return rt.stringLen(c2.name) === data.length;
}) ? 1 : 0);
rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(rt.vecFromElem(copy$Bumped({ _0: b }), n, Bumped.clone$Clone)), (o: Bumped.Bumped): rt.u8 => {
return o._0;
}));
const pair: [rt.u8, rt.u8] = [a, b];
const held: Bumped.Bumped = { _0: a };
const listed: [rt.u8, rt.u8][] = [pair, pair].map(copy$u8_u8);
const at4 = rt.idx(listed, 0);
listed[at4][0] = listed[at4][0] ^ 255;
const repeated: [rt.u8, rt.u8][] = rt.vecFromElem(copy$u8_u8(pair), n, copy$u8_u8);
const again: [rt.u8, rt.u8][] = rt.vecFromElem(copy$u8_u8(pair), n, copy$u8_u8);
const bumps: Bumped.Bumped[] = rt.vecFromElem(copy$Bumped(held), n, Bumped.clone$Clone);
const kept: Bumped.Bumped[] = rt.vecFromElem(copy$Bumped(held), n, Bumped.clone$Clone);
if (n > 0) {
const _137: rt.usize = rt.usizeSub(n, 1);
const at5 = rt.idx(repeated, _137);
repeated[at5][1] = repeated[at5][1] ^ 255;
const _141: rt.usize = rt.usizeSub(n, 1);
const at6 = rt.idx(bumps, _141);
bumps[at6]._0 = bumps[at6]._0 ^ 255;
}
const _147: rt.u8 = a;
const _148: rt.u8 = b;
const at7 = rt.idx(listed, 1);
rt.byteVecExtendFromSlice(out, Uint8Array.of(_147, _148, listed[at7][0], a));
rt.iterExtendBytes(out, rt.iterMap(rt.iterChain(rt.iterSlice(repeated), rt.iterSlice(again)), (t2: [rt.u8, rt.u8]): rt.u8 => {
return t2[1];
}));
rt.iterExtendBytes(out, rt.iterMap(rt.iterChain(rt.iterSlice(bumps), rt.iterSlice(kept)), (o2: Bumped.Bumped): rt.u8 => {
return o2._0;
}));
}

//# sourceMappingURL=collections.ts.map
Loading