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/ts-model/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,4 @@ Public API: the translator's output for moq-net's model.
- [Translate safe dependencies with the package](/quest/m2/ts-model/crates.md) - kio, moq-clock, moq-pattern, and rangemap translate as one program with moq-net
- [Std gaps the model reaches](/quest/m2/ts-model/std-more.md) - ranges, wrappers, and `Vec`'s editing methods
- [Options of values that can be undefined](/quest/m2/ts-model/nested-option.md) - `Option<Option<T>>` and `Option<()>` keep `None` apart from `Some(None)`
- [Mutable references to values JS copies](/quest/m2/ts-model/mut-place.md) - a `&mut` to a number or an `Option` passed on or kept
- [Async in the TypeScript translator](/quest/m2/ts-async.md) - kio's producers and consumers are futures
18 changes: 0 additions & 18 deletions quest/m2/ts-model/mut-place.md

This file was deleted.

26 changes: 19 additions & 7 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ in `rt/str.ts`, collections in `rt/collections.ts`, ownership and async in
| `Result<T, E>` | `rt.Result<T, E>`: `{ ok: true, value }` or `{ ok: false, error }`; `rt.OK` or `rt.ERR` where the payload is `undefined`, as in `Ok(())` |
| `ControlFlow<B, C>` | `rt.Result<C, B>`, `Ok` to continue; what `?` branches on is the value it was applied to |
| tuple, `[T; N]`, `&[T]`, `Vec<T>` | an array; for bytes, `Uint8Array` for `[u8; N]`, `rt.ByteSlice` for `&[u8]` and `&[u8; N]`, and `rt.ByteVec` for `Vec<u8>` |
| `&T`, `&mut T`, `Box<T>` | the value: a `&mut` must point at an object JS can change in place, or at a map's value through an `rt.Place` |
| `&T`, `&mut T`, `Box<T>` | the value; a `&mut` to a value JS copies, such as a number, an `Option`, or a `String`, is an `rt.Place<T>` |
| an iterator | `rt.Iter<Item>`, whose adaptors are runtime functions: `rt.iterMap(rt.iterSlice(v), f)` |
| `a..b`, `a..=b` | `rt.Range<T>`, `rt.RangeInclusive<T>`, which are iterators too |
| `HashMap`, `HashSet`, `BTreeMap`, `BTreeSet`, `VecDeque` | `rt.HashMap<K, V>` and so on, each a plain object whose functions are the runtime's: `rt.hashGet(map, key)` |
Expand Down Expand Up @@ -172,6 +172,16 @@ a std function's parameters take, where the crate calls it or takes it as a valu
one kept other than in a local, counts as a write into what it points at. A write over a type parameter counts for every type its bounds allow. A caller must
not change in place what it passes to or gets from the translation either.

A `&mut` to a value JS copies, which no JS value can point at, such as a number, an
`Option`, an enum, a tuple, a `Box`, or a `String`, is an `rt.Place` whose `v` reads and
writes where it points. Where it is only read and written through, in the function that
borrows it, it is the place it borrows, so a body that passes none on makes none. Passed
on, returned, kept in a field, or captured by a value, it is
`rt.place(() => x, (value) => x = value)` over that place, the objects and index it goes
through held as they were when it was borrowed; a reborrow of one is the same
`rt.Place`. A caller passes one as `{ v }`, or as any object whose `v` it reads and writes.
std's mappings take a `&mut` parameter as what it points at.

A collection keeps Rust's key semantics. A hash map or set keeps a key that is a JS
number, string, or boolean in a JS `Map`, which compares those as Rust's `Eq` does, and
finds any other key, a 64-bit one past 2^53 included, through its translated `Hash` and
Expand Down Expand Up @@ -402,16 +412,16 @@ to hand-written TS ([bindings](#bindings)).

## Not yet

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
These fail translation: floats, statics, 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 `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
another type it borrows as through the crate's own `Borrow`, a `&mut` to a value JS
copies that std makes, other than a map's, or hands a closure, as `iter_mut` and
`and_modify` do, one into a `Result`'s payload passed on or kept, `Hasher::write` of bytes, the adaptors
without a mapping in `src/lower/iter.rs` (such as `cycle`, `unzip`, and `partition`), an
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
`String` with `write!`, a `str` pattern other than a
`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
Expand Down Expand Up @@ -447,7 +457,9 @@ built natively for wasm32 and run beside its translation on the same inputs by
closures that log what each
adaptor reads, `Option`'s and `Result`'s combinators, with closures that log each call,
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
disagree with JS's, `vec![x; n]` of types whose `Clone` counts its clones, and `&mut`
to values JS copies, of locals, fields, elements, and a map's values, passed on,
returned, kept in a struct, and captured. 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
7 changes: 7 additions & 0 deletions ts/fixtures/bind/bind/skip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// `raw::skip`, which reads through a raw pointer.

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

export function skip(bytes: rt.ByteSlice, at: rt.Place<rt.usize>): void {
while (at.v < bytes.length && bytes.data[bytes.start + at.v] === 0) at.v++;
}
8 changes: 8 additions & 0 deletions ts/fixtures/bind/expected/bind/skip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Generated by rust2-ts. Do not edit.
// `raw::skip`, which reads through a raw pointer.

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

export function skip(bytes: rt.ByteSlice, at: rt.Place<rt.usize>): void {
while (at.v < bytes.length && bytes.data[bytes.start + at.v] === 0) at.v++;
}
74 changes: 39 additions & 35 deletions ts/fixtures/bind/expected/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@
import * as rt from "./rt.ts";
import * as Bytes from "./bind/bytes.ts";
import * as checksum from "./bind/checksum.ts";
import * as skip from "./bind/skip.ts";
import * as Tally from "./bind/tally.ts";

/**
* Split the input as `Bytes`, compare and hash the parts, and count tallies down as they
* drop. A first byte of 1 slices out of order, which panics.
* Split the input as `Bytes`, compare and hash the parts, skip its leading zeros, and
* count tallies down as they drop. A first byte of 1 slices out of order, which panics.
*/
export function run(input: rt.ByteSlice): rt.ByteVec {
let count!: rt.Arc<rt.u32>;
let a!: Tally.Tally;
let b!: Tally.Tally;
let held!: Tally.Tally | undefined;
let _120!: boolean;
let _121!: boolean;
let _122!: boolean;
let _127!: boolean;
let _128!: boolean;
let _129!: boolean;
let unwind: number = 0;
try {
_122 = false;
_121 = false;
_120 = false;
_129 = false;
_128 = false;
_127 = false;
const out: rt.ByteVec = new rt.ByteVec();
const bytes: Bytes.Bytes = Bytes.copyFromSlice(input);
const mid: rt.usize = rt.usizeDiv(Bytes.len(bytes), 2);
Expand Down Expand Up @@ -52,43 +53,46 @@ export function run(input: rt.ByteSlice): rt.ByteVec {
}
rt.byteVecPush(out, rt.u32Shr(sum, _65) & 0xff);
}
let at: rt.usize = 0;
skip.skip(input, rt.place(() => at, (value) => at = value));
rt.byteVecPush(out, at & 0xff);
if (rt.byteSliceFirst(input) === 1) {
const _81: rt.Range<rt.usize> = new rt.Range(mid, rt.usizeSaturatingSub(mid, 1));
Bytes.slice(bytes, _81);
const _88: rt.Range<rt.usize> = new rt.Range(mid, rt.usizeSaturatingSub(mid, 1));
Bytes.slice(bytes, _88);
}
count = new rt.Arc(input.length as rt.u32);
unwind = 67;
unwind = 69;
a = Tally.new(count);
unwind = 82;
_122 = true;
unwind = 84;
_129 = true;
b = Tally.new(count);
unwind = 80;
_121 = true;
unwind = 82;
_128 = true;
rt.byteVecPush(out, count.strong & 0xff);
_122 = false;
_129 = false;
rt.drop(a);
rt.byteVecPush(out, count.strong & 0xff);
const _104: rt.u8 = Tally.get(b) & 0xff;
rt.byteVecPush(out, _104);
unwind = 78;
_121 = false;
const _108: Tally.Tally = b;
_120 = true;
held = _108;
const _111: rt.u8 = Tally.get(b) & 0xff;
rt.byteVecPush(out, _111);
unwind = 80;
_128 = false;
const _115: Tally.Tally = b;
_127 = true;
held = _115;
if (rt.byteSliceFirst(input) === 2) {
_120 = false;
_127 = false;
rt.drop(held);
}
rt.byteVecPush(out, count.strong & 0xff);
const _0: rt.ByteVec = out;
if (_120) {
unwind = 80;
if (_127) {
unwind = 82;
rt.drop(held);
}
unwind = 0;
_120 = false;
_121 = false;
_122 = false;
_127 = false;
_128 = false;
_129 = false;
rt.drop(count);
return _0;
} catch (e) {
Expand All @@ -97,21 +101,21 @@ export function run(input: rt.ByteSlice): rt.ByteVec {
if (unwind === 0) {
throw e;
}
if (unwind === 67) {
if (unwind === 69) {
break b5;
}
if (unwind === 78) {
if (_120) {
if (unwind === 80) {
if (_127) {
rt.drop(held);
}
} else if (unwind !== 80) {
} else if (unwind !== 82) {
break b6;
}
if (_121) {
if (_128) {
rt.drop(b);
}
}
if (_122) {
if (_129) {
rt.drop(a);
}
}
Expand Down
1 change: 1 addition & 0 deletions ts/fixtures/bind/refused.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ roots = ["harness::run", "harness::refused"]
"raw::Tally" = "bind/bytes.ts"
"checksum" = "bind/missing.ts"
"raw::checksum" = "bind/checksum.ts"
"raw::skip" = "bind/skip.ts"
"raw::Held" = "bind/tally.ts"
"raw::zero" = "bind/checksum.ts"
"raw::value" = "bind/checksum.ts"
18 changes: 9 additions & 9 deletions ts/fixtures/bind/refused.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ binding `raw::Tally::new`: rt/tally.ts would replace the runtime, rt.ts and rt/
binding `raw::checksum`: `checksum` binds the same item
binding `std::vec::Vec`: std's type has a representation of its own
reading bind/missing.ts: No such file or directory (os error 2) at fixtures/bind/src/harness.rs:25:12
unsupported type `*const u32` at fixtures/bind/src/raw.rs:27:2
`raw::Held` binds without drop, so it would never drop its `raw::Tally` at fixtures/bind/src/harness.rs:58:13
a binding of `raw::value`, whose `N` one export for every instance can't recover from its arguments at fixtures/bind/src/raw.rs:56:1
a binding of `raw::zero`, whose `T` one export for every instance can't recover from its arguments at fixtures/bind/src/raw.rs:51:1
`raw::Held` binds without drop, so it would never drop its `raw::Tally` at fixtures/bind/src/harness.rs:58:6
unsupported call to `std::sync::Arc::<u32>::from_raw` at fixtures/bind/src/raw.rs:46:17
unsupported call to `std::sync::Arc::<u32>::into_raw` at fixtures/bind/src/raw.rs:33:11
a raw pointer dereferenced at fixtures/bind/src/raw.rs:39:12
an assignment through a reference to `bytes::Bytes` at fixtures/bind/src/harness.rs:65:2
unsupported type `*const u32` at fixtures/bind/src/raw.rs:36:2
`raw::Held` binds without drop, so it would never drop its `raw::Tally` at fixtures/bind/src/harness.rs:61:13
a binding of `raw::value`, whose `N` one export for every instance can't recover from its arguments at fixtures/bind/src/raw.rs:65:1
a binding of `raw::zero`, whose `T` one export for every instance can't recover from its arguments at fixtures/bind/src/raw.rs:60:1
`raw::Held` binds without drop, so it would never drop its `raw::Tally` at fixtures/bind/src/harness.rs:61:6
unsupported call to `std::sync::Arc::<u32>::from_raw` at fixtures/bind/src/raw.rs:55:17
unsupported call to `std::sync::Arc::<u32>::into_raw` at fixtures/bind/src/raw.rs:42:11
a raw pointer dereferenced at fixtures/bind/src/raw.rs:48:12
an assignment through a reference to `bytes::Bytes` at fixtures/bind/src/harness.rs:68:2
9 changes: 6 additions & 3 deletions ts/fixtures/bind/src/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use std::sync::Arc;

use bytes::Bytes;

use crate::raw::{Tally, checksum};
use crate::raw::{Tally, checksum, skip};

/// Split the input as `Bytes`, compare and hash the parts, and count tallies down as they
/// drop. A first byte of 1 slices out of order, which panics.
/// Split the input as `Bytes`, compare and hash the parts, skip its leading zeros, and
/// count tallies down as they drop. A first byte of 1 slices out of order, which panics.
pub fn run(input: &[u8]) -> Vec<u8> {
let mut out = Vec::new();
let bytes = Bytes::copy_from_slice(input);
Expand All @@ -26,6 +26,9 @@ pub fn run(input: &[u8]) -> Vec<u8> {
for shift in [0, 8, 16, 24] {
out.push((sum >> shift) as u8);
}
let mut at = 0;
skip(input, &mut at);
out.push(at as u8);
if input.first() == Some(&1) {
let _ = bytes.slice(mid..mid.saturating_sub(1));
}
Expand Down
9 changes: 9 additions & 0 deletions ts/fixtures/bind/src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ pub fn checksum(bytes: &[u8]) -> u32 {
sum
}

/// Moves `at` past the zeros it points at, read through a raw pointer.
pub fn skip(bytes: &[u8], at: &mut usize) {
let ptr = bytes.as_ptr();
// SAFETY: `*at` is in bounds while it is less than the length.
while *at < bytes.len() && unsafe { *ptr.add(*at) } == 0 {
*at += 1;
}
}

/// A count held by a raw pointer to its `Arc`, which drops it where Rust drops the tally.
pub struct Tally {
count: *const u32,
Expand Down
1 change: 1 addition & 0 deletions ts/fixtures/bind/ts.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ roots = ["harness::run"]
[bind]
"bytes::Bytes" = "bind/bytes.ts"
"raw::checksum" = "bind/checksum.ts"
"raw::skip" = "bind/skip.ts"
"raw::Tally" = { file = "bind/tally.ts", drop = true }
1 change: 1 addition & 0 deletions ts/fixtures/bind/wrong.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ roots = ["harness::run", "harness::held"]
[bind]
"bytes::Bytes" = "wrong/bytes.ts"
"raw::checksum" = "bind/checksum.ts"
"raw::skip" = "bind/skip.ts"
"raw::Tally" = { file = "wrong/tally.ts", drop = true }
"raw::Held" = "wrong/held.ts"
4 changes: 2 additions & 2 deletions ts/fixtures/bind/wrong.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ the binding of `bytes::Bytes::slice::<std::ops::RangeTo<usize>>` in wrong/bytes.
the binding of `<bytes::Bytes as std::cmp::PartialEq>::eq` in wrong/bytes.ts doesn't type-check: error TS2322: Type '(self: ByteSlice, other: string) => boolean' is not assignable to type '(a0: ByteSlice, a1: ByteSlice) => boolean'.
Types of parameters 'other' and 'a1' are incompatible.
Type 'ByteSlice' is not assignable to type 'string'. at bytes-1.12.1/src/bytes.rs:769:5
the binding of `raw::Tally` in wrong/tally.ts doesn't type-check: error TS2322: Type 'Tally' is not assignable to type '"a bound value that drops must have a [Symbol.dispose]"'. at fixtures/bind/src/raw.rs:26:1
the binding of `raw::Tally` in wrong/tally.ts doesn't type-check: error TS2322: Type 'Tally' is not assignable to type '"a bound value that drops must have a [Symbol.dispose]"'. at fixtures/bind/src/raw.rs:35:1
the binding of `raw::Held<std::option::Option<u8>>` in wrong/held.ts doesn't type-check: error TS2322: Type 'u8 | undefined' is not assignable to type '"a bound value can't be undefined, which None is"'.
Type 'undefined' is not assignable to type '"a bound value can't be undefined, which None is"'. at fixtures/bind/src/raw.rs:61:1
Type 'undefined' is not assignable to type '"a bound value can't be undefined, which None is"'. at fixtures/bind/src/raw.rs:70:1
4 changes: 2 additions & 2 deletions ts/fixtures/lang/expected/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1102,8 +1102,8 @@ export function stores(data: rt.ByteSlice, out: rt.ByteVec): void {
if (_88 !== undefined) {
_88.v[0] = _88.v[0] ^ 1;
}
const place = rt.entryOrInsertMut(rt.entry(hash, 2), copy$u8_u8(pair));
place.v[1] = place.v[1] ^ 2;
const _93: rt.Place<[rt.u8, rt.u8]> = rt.entryOrInsertMut(rt.entry(hash, 2), copy$u8_u8(pair));
_93.v[1] = _93.v[1] ^ 2;
rt.iterExtendBytes(out, rt.iterFlatMap(new rt.Range(0, 3), (k: rt.u8): Uint8Array => {
return mapOr$Option_u8_u8_u8s_stores_closure2_closure0(rt.hashGet(hash, k), rt.bytes(2).fill(255), (p2: [rt.u8, rt.u8]): Uint8Array => {
return Uint8Array.of(p2[0], p2[1]);
Expand Down
7 changes: 7 additions & 0 deletions ts/fixtures/lang/expected/core/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,13 @@ export function mapOr$Option_u16_u8_hash_maps_closure0(self: rt.u16 | undefined,
return f(self);
}

export function mapOr$Option_u8_bool_take_largest_closure0(self: rt.u8 | undefined, default_: boolean, f: (a0: rt.u8) => boolean): boolean {
if (self === undefined) {
return default_;
}
return f(self);
}

export function mapOr$Option_u8_u8_options_closure5(self: rt.u8 | undefined, default_: rt.u8, f: (a0: rt.u8) => rt.u8): rt.u8 {
if (self === undefined) {
return default_;
Expand Down
4 changes: 2 additions & 2 deletions ts/fixtures/lang/expected/cows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ export function cows(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void {
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 _276: rt.Place<rt.u8> = rt.entryOrInsertMut(rt.entry(counts, _279), 0);
_276.v = rt.u8Add(_276.v, 1);
}
const iter4: rt.Iter<[string, rt.u8]> = rt.btreeIter(counts);
while (true) {
Expand Down
7 changes: 5 additions & 2 deletions ts/fixtures/lang/expected/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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";
import { adaptors, consumers, ranges, options, own, values } from "./iters.ts";
import { keys } from "./keys.ts";
import { refs } from "./refs.ts";
import { copies, order, discriminants, loops, patterns, closures, library, flows } from "./shapes.ts";
import { strs, slices } from "./strs.ts";

Expand All @@ -24,7 +25,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, 49);
const _14: rt.u8 = rt.u8Rem(op, 50);
if (_14 === 0) {
u8Op(kind, rt.byteSliceRange(input, 2), out);
} else if (_14 === 1) {
Expand Down Expand Up @@ -139,8 +140,10 @@ export function run(input: rt.ByteSlice): rt.ByteVec {
cows(kind, rt.byteSliceRange(input, 2), out);
} else if (_14 === 47) {
durations(kind, rt.byteSliceRange(input, 2), out);
} else {
} else if (_14 === 48) {
combinators(kind, rt.byteSliceRange(input, 2), out);
} else {
refs(rt.byteSliceRange(input, 2), out);
}
return out;
}
Expand Down
Loading