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 @@ -28,7 +28,6 @@ The fork's feature quests folded into the [exporter](/quest/m1/ffi-export/README

- [Byte slices without objects](/quest/m2/ts-byte-views/README.md) - reading through a translated cursor allocates no object per read
- [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 moq-net's model to TypeScript](/quest/m2/ts-model/README.md) - the model translates with kio, moq-clock, and moq-pattern, 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
1 change: 0 additions & 1 deletion quest/m2/ts-model/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ Public API: the translator's output for moq-net's model.

## Required

- [Shared ownership](/quest/m2/ts-shared.md) - kio's waiters are `Arc`/`Weak` with observable counts, and its atomics and locks are plain values
- [The rest of std the model reaches](/quest/m2/ts-model/std-rest.md) - `Vec` and `VecDeque` capacity
- [hashbrown as the runtime's map](/quest/m2/ts-model/hashbrown.md) - hashlink's `hashbrown` tables translate as `rt.HashMap`, with std's capacity
- [Async in the TypeScript translator](/quest/m2/ts-async.md) - kio's producers and consumers are futures
44 changes: 0 additions & 44 deletions quest/m2/ts-shared.md

This file was deleted.

1 change: 1 addition & 0 deletions quest/m3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ generic std gap is planned here even when moq never calls it.
- [ptr::replace whose result nothing reads](/quest/m3/ts-ptr-replace-unread.md) - `ptr::replace` with its result discarded type-checks
- [std's sorts call their tests in std's order](/quest/m3/ts-sort-order.md) - `slice::sort` and a B-tree's `collect` call `lt` or `cmp` as std's driftsort does past 16 elements
- [A B-tree's searches call Ord as std's do](/quest/m3/ts-btree-search-order.md) - a B-tree's lookups and inserts call a key's own `Ord` in std's order and number
- [The rest of a guard's maps](/quest/m3/ts-guard-maps.md) - `RefMut::map`, the guards' `filter_map` and `map_split`, and an `RwLock` guard's `map` translate
- [A str's checked and inclusive parts](/quest/m3/ts-str-get.md) - `str::get` and indexing a `str` by `a..=b` or `..=b` translate
- [The rest of a boxed slice's impls](/quest/m3/ts-boxed-slice-rest.md) - `clone_from`, `AsRef`, `AsMut`, `TryFrom` into a boxed array, and `Box<str>` into bytes translate for boxed slices
- [A reversed zip's per-pair cost](/quest/m3/ts-zip-rev-overhead.md) - a reversed by-index zip costs per pair what a forward one does, or the gap is explained
2 changes: 1 addition & 1 deletion quest/m3/ts-bytes-capacity.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ or taken from a `Bytes`, `reserve` throws a `rust2-ts:` error past 512 MiB, coun
the handle read past, where `bytes`' panic, or its growing instead, depends on which
handles dropped. Exact capacity removes that limit.

- Count the live handles of a storage, as [shared ownership](/quest/m2/ts-shared.md) counts
- Count the live handles of a storage, as `rt.Arc` counts
an `Arc`'s, which makes a `Bytes` a value that drops.
- Measure the count's cost on the codec's [TS benchmark](/ts/README.md#bench).

Expand Down
16 changes: 16 additions & 0 deletions quest/m3/ts-guard-maps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# [S] The rest of a guard's maps

## Goal

[rust2-ts](/ts/README.md) translates `RefMut::map`, `Ref::filter_map`, `Ref::map_split`,
`RefMut::filter_map`, `RefMut::map_split`, and the `map` of an `RwLock`'s guards, each
releasing its cell's borrow as the last guard drops, as std's do. Today they are refused.
Proven by differential tests against native Rust.

## Plan

`Ref::map` makes an `rt.RefMap`, which holds the part it lends and counts its borrow in
the cell. A mapped `RefMut` lends a `&mut`, which is an `rt.Place` where JS copies the
part, so writes through it must reach that place rather than a copy in the guard.

Public API: the translator's output.
6 changes: 3 additions & 3 deletions ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ crate translation reaches is refused, and so are two versions of one crate it re
| `static` | a module's value, named for the static, that every use shares, read from the memory rustc evaluated; one that reads another static or a class is an `rt.LazyCell` of it, made on first use, since that may not be made yet when its module is. A `static mut`, and a pointer in one into memory the translation can't name, fail translation |
| a type that drops | a class with `[Symbol.dispose]`: its `Drop` impl, then its fields |
| a type's function | a function of the type's module, taking `self` first: `Decoder.slice(dec, 4)` after `import * as Decoder` |
| `Arc<T>`, `Rc<T>`, `Weak<T>`, `Mutex<T>` | `rt.Arc` for either, with its strong and weak counts, and `new_cyclic`'s `Weak` upgrading only once its value is made, `get_mut` lending its value only while no other `Arc` or `Weak` shares it, `make_mut` cloning it into a new `rt.Arc` while another `Arc` shares it, or moving it there while only `Weak`s do, which then dangle, and `into_inner` giving it only from the last strong `Arc`, after which its `Weak`s no longer upgrade; `rt.Weak`, counted in its `rt.Arc` until it drops, and of no `Arc` once made by `Weak::new`; `rt.Mutex`, its own guard, which a lock gives as `Ok` and the guard's drop unlocks: locking it again while held panics and `try_lock` gives `WouldBlock`, as wasm32's do, and a panic aborts, so nothing poisons it and `thread::panicking` is `false` |
| `Arc<T>`, `Rc<T>`, `Weak<T>`, `Mutex<T>` | `rt.Arc` for either, with its strong and weak counts, and `new_cyclic`'s `Weak` upgrading only once its value is made, `get_mut` lending its value only while no other `Arc` or `Weak` shares it, `make_mut` cloning it into a new `rt.Arc` while another `Arc` shares it, or moving it there while only `Weak`s do, which then dangle, and `into_inner` giving it only from the last strong `Arc`, after which its `Weak`s no longer upgrade; `rt.Weak`, counted in its `rt.Arc` until it drops, and of no `Arc` once made by `Weak::new`; `rt.Mutex`, its own guard, which a lock gives as `Ok` and the guard's drop unlocks: locking it again while held panics and `try_lock` gives `WouldBlock`, as wasm32's do, and a panic aborts, so nothing poisons it and `thread::panicking` is `false`; a `Mutex`'s, a `RefCell`'s, and an `RwLock`'s `into_inner` gives its value and `get_mut` lends it, or an `rt.Place` of it where JS copies it |
| `PoisonError<T>`, `TryLockError<T>` | the `T` it holds; `rt.TryLockError<T>`, tagged as `Bound` is |
| `Cell<T>`, `UnsafeCell<T>`, `RefCell<T>`, `RwLock<T>` | `rt.Cell`, the place holding its value in `v`, for either of the first two: `UnsafeCell::get`'s pointer is the cell where it is an `rt.Place`, and otherwise the object in `v`, refused where the crate may put another cell or object in its place; `rt.RefCell` for either of the others, its own guard, counting its borrows: borrowing it while that conflicts panics as `RefCell`'s does, and locking it so aborts as wasm32's `RwLock` does |
| `Cell<T>`, `UnsafeCell<T>`, `RefCell<T>`, `RwLock<T>` | `rt.Cell`, the place holding its value in `v`, for either of the first two: `UnsafeCell::get`'s pointer is the cell where it is an `rt.Place`, and otherwise the object in `v`, refused where the crate may put another cell or object in its place; `rt.RefCell` for either of the others, its own guard, counting its borrows: borrowing it while that conflicts panics as `RefCell`'s does, and locking it so aborts as wasm32's `RwLock` does; `try_borrow` and `try_borrow_mut` fail with `rt.BorrowError` and `rt.BorrowMutError` instead, a clone borrows the cell, `Ref::clone` counts another borrow of it, and `Ref::map` makes an `rt.RefMap`, the part it lends, which releases the cell's borrow as it drops |
| `thread_local!`, `LocalKey<T>` | an `rt.LocalKey`, a module's value, which makes its value on first use from the `const` or the expression and never drops it, as wasm32's do; `with` calls its function on that value, `try_with` never fails, the `Cell` and `RefCell` helpers lower from std, and a `RefCell` key's `set` and `with_borrow_mut` borrow it as `borrow_mut` does |
| `OnceCell<T>`, `OnceLock<T>` | `rt.OnceCell` and `rt.OnceLock`, holding the `Option<T>` in `v`; a `OnceLock` initialized from within its own initialization panics, as wasm32's does |
| `OnceCell<T>`, `OnceLock<T>` | `rt.OnceCell` and `rt.OnceLock`, holding the `Option<T>` in `v`, which is the place `get_mut` lends where JS copies the value; a `OnceLock` initialized from within its own initialization panics, as wasm32's does, and `get_or_try_init`'s error leaves either empty |
| `Once`, `OnceState` | `rt.Once`, an `rt.OnceLock` that holds `true` once `call_once` or `call_once_force` ran its function, and panics as wasm32's does if run again from within it; `rt.OnceState`, that `Once`, which a panic never poisons, as it aborts |
| `LazyCell<T>`, `LazyLock<T>` in a `static` or `const` | `rt.LazyCell` and `rt.LazyLock`, which run their function on the first `force` or `Deref`, once; forced from within it, either panics as std's does |
| `AtomicU32` and the others of `bool` and integers, `AtomicPtr<T>` | `rt.Atomic<T>`, the `rt.Place` of its value; an `atomic::Ordering` is its discriminant, and one an operation can't take panics as std's does. An `AtomicPtr` compares pointers only to what JS keeps as an object or bytes, as raw pointers do |
Expand Down
30 changes: 28 additions & 2 deletions ts/fixtures/lang/caller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,9 @@ const dyns = [[], [1, 2], [3, 0, 7], [255, 1, 255, 0, 1, 2, 3, 4, 5], [0, 3, 2,
/**
* `cells` scripts of (op, operand) pairs on every kind, so each cell and value type: reads
* and sets, a `None` and a `()` held, initializing from within initialization and
* panicking in it, empty and full, and a cell replaced.
* panicking in it, empty and full, and a cell replaced; clones, `Debug`, and `get_mut`,
* empty and full; and `get_or_try_init` failing, succeeding, set, and set or initialized
* again from within, failing after or not.
*/
const cellsOp = op("cells::cells");
const cellScripts = [
Expand All @@ -361,6 +363,15 @@ const cellScripts = [
[7, 1],
[2, 1, 5, 3, 6, 3, 7, 1],
[8, 4, 0, 0, 8, 5, 0, 0, 1, 3, 8, 6],
[9, 0, 9, 1, 0, 0],
[10, 0, 10, 1, 11, 2, 0, 0],
[11, 1, 2, 1, 11, 4, 0, 0, 4, 0, 11, 5],
[12, 0, 12, 6, 0, 0, 12, 1],
[12, 1, 12, 2],
[12, 2],
[12, 3, 4, 0, 12, 7],
[12, 11],
[12, 7, 12, 4],
[],
];
const cells = Array.from({ length: 256 }, (_, kind) => cellScripts.map((script) => Uint8Array.of(cellsOp, kind, ...script))).flat();
Expand All @@ -380,6 +391,21 @@ const locks = [[0, 1, 5, 6, 2, 7, 3, 8, 3, 0, 1], [4, 0, 4, 1], [2, 9, 2, 10, 4,
(bytes) => Uint8Array.of(op("locks::locks"), 0, ...bytes),
);

/**
* `borrows::guards` scripts: each operation on cells empty and not, cloned while borrowed
* shared and exclusive, and borrowed exclusively while a mapped or cloned guard holds it.
*/
const guards = [
[0x04, 0x05, 0x06, 0x03, 0x07, 0x02, 0x01],
[0x10, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0b, 0x0c, 0x0d],
[0x08, 0xe1, 0xf1],
[0x08, 0xe6],
[0xf6],
].map((bytes) => Uint8Array.of(op("borrows::guards"), 0, ...bytes));

/** `locks::rwlocks` scripts: `get_mut` and `into_inner` among writes and reads. */
const rwlocks = [[6, 7, 0, 6, 2, 14, 15, 1]].map((bytes) => Uint8Array.of(op("locks::rwlocks"), 0, ...bytes));

/**
* `statics::onces` scripts: a `Once` forced, called, and forced again, which drops the
* function it never runs, each static's, each `call_once` and `call_once_force` run again
Expand Down Expand Up @@ -727,4 +753,4 @@ const asciiCases = [
...Array.from({ length: 8 * 3 * 3 }, (_, i) => Uint8Array.of(op("ascii::parts"), 0x10 | (i % 8), Math.floor(i / 8) % 3, Math.floor(i / 24))),
];

export const cases: Uint8Array[] = [...loops, ...durations, ...ints(), ...utf8, ...floats, ...formats, ...parsed, ...singlesCases, ...singlesFormats, ...singlesParsed, ...dyns, ...cells, ...shared, ...locks, ...onces, ...bounds, ...unsafeCells, ...edits, ...subslices, ...consumers, ...closures, ...elements, ...ranges, ...reducers, ...sliceEdits, ...drains, ...iters, ...hinted, ...peekedOthers, ...hints, ...reserves, ...setOps, ...zeros, ...writes, ...floatsMulAdds, ...singlesMulAdds, ...unlikeEqs, ...ownNes, ...rangeFroms, ...compares, ...unlikeCompares, ...rangeMethods, ...wraps(), ...refOrders, ...saturates(), ...methods(), ...separated, ...asciiCases];
export const cases: Uint8Array[] = [...loops, ...durations, ...ints(), ...utf8, ...floats, ...formats, ...parsed, ...singlesCases, ...singlesFormats, ...singlesParsed, ...dyns, ...cells, ...shared, ...locks, ...guards, ...rwlocks, ...onces, ...bounds, ...unsafeCells, ...edits, ...subslices, ...consumers, ...closures, ...elements, ...ranges, ...reducers, ...sliceEdits, ...drains, ...iters, ...hinted, ...peekedOthers, ...hints, ...reserves, ...setOps, ...zeros, ...writes, ...floatsMulAdds, ...singlesMulAdds, ...unlikeEqs, ...ownNes, ...rangeFroms, ...compares, ...unlikeCompares, ...rangeMethods, ...wraps(), ...refOrders, ...saturates(), ...methods(), ...separated, ...asciiCases];
39 changes: 39 additions & 0 deletions ts/fixtures/lang/expected/borrows.Tagged.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Generated by rust2-ts. Do not edit.

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

/**
* Logs its id as it is cloned, with the high bit set, and as it drops; its clone's id
* is one more.
*/
export class Tagged {
id: rt.u8;
n: rt.u32;
log: rt.Arc<rt.Mutex<rt.ByteVec>>;

constructor(id: rt.u8, n: rt.u32, log: rt.Arc<rt.Mutex<rt.ByteVec>>) {
this.id = id;
this.n = n;
this.log = log;
}

[Symbol.dispose](): void {
drop$ops_Drop(this);
this.log[Symbol.dispose]();
}
}

export function clone$Clone(self: Tagged): Tagged {
const _5: rt.Mutex<rt.ByteVec> = rt.mutexLock(self.log.value);
rt.byteVecPush(_5.value, 128 | self.id);
rt.mutexUnlock(_5);
return new Tagged(rt.u8WrappingAdd(self.id, 1), self.n, rt.arcClone(self.log));
}

export function drop$ops_Drop(self: Tagged): void {
const _5: rt.Mutex<rt.ByteVec> = rt.mutexLock(self.log.value);
rt.byteVecPush(_5.value, self.id);
rt.mutexUnlock(_5);
}

//# sourceMappingURL=borrows.Tagged.ts.map
Loading