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/m3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ generic std gap is planned here even when moq never calls it.
- [Placeholders written to a crate's own fmt::Write](/quest/m3/ts-fmt-calls.md) - `write!` with placeholders into a crate's own writer translates, its `write_str` seeing each call std makes
- [Waiting: type-check in process with tsrs](/quest/m3/ts-tsrs.md) - rust2-ts checks bindings through tsrs instead of `tsc` once it is a stable library
- [An inclusive range folded through an adaptor](/quest/m3/ts-range-fold.md) - a `RangeInclusive` folded through `map`, `filter`, or `rev` ends in core's state, and `by_ref` of one type-checks
- [Binding gaps](/quest/m3/ts-bind-gaps.md) - bound types as keys, unit returns checked, renamed dependencies
- [A B-tree's drop order](/quest/m3/ts-btree-drop.md) - dropping a whole `BTreeMap` or `BTreeSet` drops its entries node by node, as std's does
- [Drop order in std's folds and iterators](/quest/m3/ts-drop-order.md) - the values that drop still refused in std's folds, zips, flattens, and collects drop as std's do
- [Set operations](/quest/m3/ts-set-ops.md) - `union`, `intersection`, `difference`, and `symmetric_difference` of hash and B-tree sets translate
Expand Down
14 changes: 0 additions & 14 deletions quest/m3/ts-bind-gaps.md

This file was deleted.

16 changes: 13 additions & 3 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,7 @@ generic bound type's value at each of its instantiations translation reaches.
A type with a representation of its own, std's or a well-known crate's, can't be bound.

Translation can't see into a bound type, so a field, variant, or constructor of one is
refused, as are iterating one, keying a collection by one, and assigning a whole one
through a `&mut`. Its value is never `undefined` or `rt.Pending`, which `None` and
refused, as are iterating one and assigning a whole one through a `&mut`. Its value is never `undefined` or `rt.Pending`, which `None` and
`Poll::Pending` are, and is an object unless the Rust type is `Copy`. A `&mut` to one
passes that object, which the binding's functions change in place. Where the type is
`Copy`, such a `&mut` is refused, since JS shares the value where Rust copies it, and a
Expand All @@ -459,6 +458,15 @@ translation at the Rust item's span, and the output is removed. Only the binding
another only where translation reaches both. A binding inside the output is refused, since
a translation replaces it.

A bound type keys a collection, alone or in a tuple, `Option`, or `Arc`, by the exports of
its Rust impls: `hash$Hash(self, state: rt.Hasher): undefined` and `eq$PartialEq` for a
`HashMap` or `HashSet`, `cmp$Ord` for a `BTreeMap` or `BTreeSet`, and
`partialCmp$PartialOrd` too for a B-tree's `range`. A derived impl's are the same exports.
Each must agree with the Rust impl: `hash` writes what it writes, through `rt.hash8` to
`rt.hash128`, `rt.hashStr`, and `rt.hashBytes`, so values `eq` finds equal hash alike, and
`cmp` orders as it does. A collection finds even a bound value that is a JS primitive by
these, never by `===`. A missing one fails at its impl, as any missing export does.

A bound type with `drop = true` drops where Rust drops it, through its
`[Symbol.dispose]`, which `tsc` checks it has. Without it, dropping one does nothing JS
sees, `tsc` checks it has no `[Symbol.dispose]`, and a type argument that drops is refused.
Expand Down Expand Up @@ -712,7 +720,9 @@ built natively for wasm32 and run beside its translation on the same inputs by
its own included. `caller.ts` names the inputs that reserve at the edges of
`BytesMut`'s 512 MiB limit.
- `bind` binds `buf::Blob`, a renamed dependency's type with `unsafe` inside, and a function and a type of its own that read through raw
pointers, the type dropping an `Arc` where Rust drops it, to the TS in `bind/`. Its
pointers, the type dropping an `Arc` where Rust drops it, to the TS in `bind/`. It keys
hash and B-tree collections by `Blob` and by `key::Nibble`, a JS number equal to another
by its low bits alone, through their bindings' `Hash`, `Eq`, and `Ord`. Its
differential test runs them against native Rust. `wrong.toml` binds TS that doesn't match
the Rust signatures, which `tsc` refuses at each item, with the errors in `wrong.txt`.
`refused.toml` holds bindings the config can't make, with the errors in `refused.txt`.
Expand Down
13 changes: 13 additions & 0 deletions ts/fixtures/bind/bind/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,16 @@ export function deref$Deref(self: Blob): rt.ByteSlice {
export function eq$PartialEq(self: Blob, other: Blob): boolean {
return rt.byteSliceEq(self, other);
}

export function hash$Hash(self: Blob, state: rt.Hasher): undefined {
rt.hashBytes(self, state);
return undefined;
}

export function partialCmp$PartialOrd(self: Blob, other: Blob): rt.Ordering {
return rt.cmpBytes(self, other);
}

export function cmp$Ord(self: Blob, other: Blob): rt.Ordering {
return rt.cmpBytes(self, other);
}
32 changes: 32 additions & 0 deletions ts/fixtures/bind/bind/nibble.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// `key::Nibble`, a byte that compares, hashes, and orders by its low four bits, which the
// `===` of its number would not.

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

export type Nibble = rt.u8;

function make(byte: rt.u8): Nibble {
return byte;
}
export { make as new };

export function get(self: Nibble): rt.u8 {
return self;
}

export function eq$PartialEq(self: Nibble, other: Nibble): boolean {
return (self & 0xf) === (other & 0xf);
}

export function hash$Hash(self: Nibble, state: rt.Hasher): undefined {
rt.hash8(self & 0xf, state);
return undefined;
}

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

export function cmp$Ord(self: Nibble, other: Nibble): rt.Ordering {
return rt.cmp(self & 0xf, other & 0xf);
}
22 changes: 22 additions & 0 deletions ts/fixtures/bind/blob/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Immutable bytes that clones and slices share, read through `unsafe`: a dependency's
//! type that a consumer binds rather than translates.

use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use std::ops::{Bound, Deref, RangeBounds};
use std::sync::Arc;

Expand Down Expand Up @@ -94,6 +96,26 @@ impl PartialEq for Blob {
}
}

impl Eq for Blob {}

impl Hash for Blob {
fn hash<H: Hasher>(&self, state: &mut H) {
(**self).hash(state);
}
}

impl PartialOrd for Blob {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for Blob {
fn cmp(&self, other: &Self) -> Ordering {
(**self).cmp(&**other)
}
}

/// Where bytes come from: a trait, which a binding can't stand in for.
pub trait Source {
/// The next bytes.
Expand Down
13 changes: 13 additions & 0 deletions ts/fixtures/bind/expected/bind/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,16 @@ export function deref$Deref(self: Blob): rt.ByteSlice {
export function eq$PartialEq(self: Blob, other: Blob): boolean {
return rt.byteSliceEq(self, other);
}

export function hash$Hash(self: Blob, state: rt.Hasher): undefined {
rt.hashBytes(self, state);
return undefined;
}

export function partialCmp$PartialOrd(self: Blob, other: Blob): rt.Ordering {
return rt.cmpBytes(self, other);
}

export function cmp$Ord(self: Blob, other: Blob): rt.Ordering {
return rt.cmpBytes(self, other);
}
33 changes: 33 additions & 0 deletions ts/fixtures/bind/expected/bind/nibble.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Generated by rust2-ts. Do not edit.
// `key::Nibble`, a byte that compares, hashes, and orders by its low four bits, which the
// `===` of its number would not.

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

export type Nibble = rt.u8;

function make(byte: rt.u8): Nibble {
return byte;
}
export { make as new };

export function get(self: Nibble): rt.u8 {
return self;
}

export function eq$PartialEq(self: Nibble, other: Nibble): boolean {
return (self & 0xf) === (other & 0xf);
}

export function hash$Hash(self: Nibble, state: rt.Hasher): undefined {
rt.hash8(self & 0xf, state);
return undefined;
}

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

export function cmp$Ord(self: Nibble, other: Nibble): rt.Ordering {
return rt.cmp(self & 0xf, other & 0xf);
}
27 changes: 27 additions & 0 deletions ts/fixtures/bind/expected/core/option.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "../rt.ts";
import type * as Nibble from "../bind/nibble.ts";

export function unwrapOr$0r2bmpr(self: rt.u8 | undefined, default_: rt.u8): rt.u8 {
if (self === undefined) {
return default_;
}
return self;
}

export function unwrapOr$1rbuegs(self: rt.ByteSlice | undefined, default_: rt.ByteSlice): rt.ByteSlice {
if (self === undefined) {
return default_;
}
return self;
}

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

//# sourceMappingURL=option.ts.map
99 changes: 93 additions & 6 deletions ts/fixtures/bind/expected/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
import * as rt from "./rt.ts";
import * as Blob from "./bind/blob.ts";
import * as checksum from "./bind/checksum.ts";
import * as Nibble from "./bind/nibble.ts";
import * as skip from "./bind/skip.ts";
import * as Tally from "./bind/tally.ts";
import { unwrapOr$0r2bmpr, unwrapOr$1rbuegs, mapOr$174crag } from "./core/option.ts";

/**
* Split the input as `Blob`, 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.
* count tallies down as they drop, then key collections by bound types. A first byte of
* 1 slices out of order, which panics.
*/
export function run(input: rt.ByteSlice): rt.ByteVec {
let _125: boolean = false;
let _128: boolean = false;
const out: rt.ByteVec = new rt.ByteVec();
const bytes: Blob.Blob = Blob.copyFromSlice(input);
const mid: rt.usize = rt.usizeDiv(Blob.len(bytes), 2);
Expand Down Expand Up @@ -57,20 +60,104 @@ export function run(input: rt.ByteSlice): rt.ByteVec {
rt.byteVecPush(out, count.strong & 0xff);
const _110: rt.u8 = Tally.get(b) & 0xff;
rt.byteVecPush(out, _110);
_125 = true;
_128 = true;
const held: Tally.Tally | undefined = b;
if (rt.byteSliceFirst(input) === 2) {
_125 = false;
_128 = false;
rt.drop(held);
}
rt.byteVecPush(out, count.strong & 0xff);
keys(input, bytes, out);
const _0: rt.ByteVec = out;
if (_125) {
if (_128) {
rt.drop(held);
}
_125 = false;
_128 = false;
count[Symbol.dispose]();
return _0;
}

/**
* Collections keyed by `Blob`s, equal by their bytes wherever they are sliced from, and by
* `Nibble`s, equal by their low bits: what each finds, keeps, and orders.
*/
export function keys(input: rt.ByteSlice, bytes: Blob.Blob, out: rt.ByteVec): void {
let _12!: rt.usize;
let _143!: rt.u8;
const seen: rt.HashSet<Blob.Blob> = rt.hashNew(rt.hashing(Blob.hash$Hash, Blob.eq$PartialEq, true)) as rt.HashSet<Blob.Blob>;
const counts: rt.HashMap<Blob.Blob, rt.u8> = rt.hashNew(rt.hashing(Blob.hash$Hash, Blob.eq$PartialEq, true)) as rt.HashMap<Blob.Blob, rt.u8>;
const sorted: rt.BTreeSet<Blob.Blob> = rt.btreeNew(Blob.cmp$Ord) as rt.BTreeSet<Blob.Blob>;
const firsts: rt.BTreeMap<Blob.Blob, rt.u8> = rt.btreeNew(Blob.cmp$Ord) as rt.BTreeMap<Blob.Blob, rt.u8>;
const _10: rt.usize = Blob.len(bytes);
let iterStart: rt.usize = 0;
const iterEnd: rt.usize = _10;
while ((_12 = iterStart++) < iterEnd) {
const at: rt.usize = _12;
const part: Blob.Blob = Blob.slice(bytes, new rt.Range(at, rt.min(rt.usizeAdd(at, 2), Blob.len(bytes))));
const _26: Blob.Blob = Blob.clone$Clone(part);
rt.byteVecPush(out, rt.hashAdd(seen, _26) ? 1 : 0);
const _31: Blob.Blob = Blob.clone$Clone(part);
const _28: rt.Place<rt.u8> = rt.entryOrInsertMut(rt.entry(counts, _31), 0);
_28.v = rt.u8Add(_28.v, 1);
const _36: Blob.Blob = Blob.clone$Clone(part);
rt.btreeAdd(sorted, _36);
rt.entryOrInsertMut(rt.entry(firsts, part), at & 0xff);
}
rt.byteVecPush(out, rt.hashLen(seen) & 0xff);
rt.byteVecPush(out, rt.hashLen(counts) & 0xff);
const probe: Blob.Blob = Blob.copyFromSlice(unwrapOr$1rbuegs(rt.byteSliceGetRange(input, 0, 2), input));
rt.byteVecPush(out, rt.hashContains(seen, probe) ? 1 : 0);
rt.byteVecPush(out, unwrapOr$0r2bmpr(rt.hashGet(counts, probe), 0));
rt.byteVecPush(out, rt.btreeContains(sorted, probe) ? 1 : 0);
rt.byteVecPush(out, unwrapOr$0r2bmpr(rt.btreeGet(firsts, probe), 255));
const _81: { start: Blob.Blob } = { start: Blob.clone$Clone(probe) };
rt.byteVecPush(out, rt.iterCount(rt.btreeRangeKeys(sorted, _81, Blob.eq$PartialEq, rt.cmpGt(Blob.partialCmp$PartialOrd))) & 0xff);
const iter2: rt.Iter<[Blob.Blob, rt.u8]> = rt.btreeIter(firsts);
while (true) {
const _87: [Blob.Blob, rt.u8] | rt.Done = iter2.next();
if (_87 === rt.DONE) {
break;
}
rt.byteVecPush(out, Blob.len(_87[0]) & 0xff);
rt.byteVecExtendFromSlice(out, Blob.deref$Deref(_87[0]));
rt.byteVecPush(out, _87[1]);
}
rt.byteVecPush(out, rt.hashDelete(seen, probe) ? 1 : 0);
rt.byteVecPush(out, rt.btreeDelete(sorted, probe) ? 1 : 0);
rt.byteVecPush(out, rt.hashLen(seen) & 0xff);
const nibbles: rt.HashSet<Nibble.Nibble> = rt.hashNew(rt.hashing(Nibble.hash$Hash, Nibble.eq$PartialEq, true)) as rt.HashSet<Nibble.Nibble>;
const lasts: rt.HashMap<Nibble.Nibble, rt.u8> = rt.hashNew(rt.hashing(Nibble.hash$Hash, Nibble.eq$PartialEq, true)) as rt.HashMap<Nibble.Nibble, rt.u8>;
const ordered: rt.BTreeSet<Nibble.Nibble> = rt.btreeNew(Nibble.cmp$Ord) as rt.BTreeSet<Nibble.Nibble>;
const values: rt.BTreeMap<Nibble.Nibble, rt.u8> = rt.btreeNew(Nibble.cmp$Ord) as rt.BTreeMap<Nibble.Nibble, rt.u8>;
for (const byte of rt.byteSliceView(input)) {
const key: Nibble.Nibble = Nibble.new(byte);
rt.byteVecPush(out, rt.hashAdd(nibbles, key) ? 1 : 0);
rt.byteVecPush(out, unwrapOr$0r2bmpr(rt.hashInsert(lasts, key, byte), 255));
rt.btreeAdd(ordered, key);
rt.btreeInsert(values, key, byte);
}
rt.byteVecPush(out, rt.hashLen(nibbles) & 0xff);
let iterStart2: rt.u8 = 0;
const iterEnd2: rt.u8 = 16;
while ((_143 = iterStart2++) < iterEnd2) {
const key3: Nibble.Nibble = Nibble.new(rt.u8Mul(_143, 17));
rt.byteVecPush(out, mapOr$174crag(rt.hashKey(nibbles, key3), 255, (n: Nibble.Nibble): rt.u8 => {
return Nibble.get(n);
}));
rt.byteVecPush(out, unwrapOr$0r2bmpr(rt.hashGet(lasts, key3), 255));
}
const iter5: rt.Iter<[Nibble.Nibble, rt.u8]> = rt.btreeIter(values);
while (true) {
const _164: [Nibble.Nibble, rt.u8] | rt.Done = iter5.next();
if (_164 === rt.DONE) {
break;
}
rt.byteVecPush(out, Nibble.get(_164[0]));
rt.byteVecPush(out, _164[1]);
}
rt.iterExtendBytes(out, rt.iterMap(rt.btreeKeys(ordered), (n2: Nibble.Nibble): rt.u8 => {
return Nibble.get(n2);
}));
}

//# sourceMappingURL=harness.ts.map
8 changes: 4 additions & 4 deletions ts/fixtures/bind/refused.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ binding `raw::Tally::get`: only a type's value drops
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`: the 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
`raw::Held` binds without drop, so it would never drop its `raw::Tally` at fixtures/bind/src/harness.rs:61:13
reading bind/missing.ts: No such file or directory (os error 2) at fixtures/bind/src/harness.rs:28:12
`raw::Held` binds without drop, so it would never drop its `raw::Tally` at fixtures/bind/src/harness.rs:121: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
`raw::Held` binds without drop, so it would never drop its `raw::Tally` at fixtures/bind/src/harness.rs:121: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
an assignment through a reference to `blob::Blob` at fixtures/bind/src/harness.rs:68:2
an assignment through a reference to `blob::Blob` at fixtures/bind/src/harness.rs:128:2
Loading