From 2b2bb4fa95515d80dcbcb250cf8b1c7ee2d7f80e Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 29 Oct 2026 14:33:19 -0700 Subject: [PATCH 1/4] chore(quest): claim quest/m3/ts-arc-mut.md Quest-Claim: 4bf81eb5cc775de9 From 0c480dc252c8d04dc1349f2630b4ca3917354311 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 29 Oct 2026 14:44:27 -0700 Subject: [PATCH 2/4] feat(ts): translate Arc's get_mut, make_mut, and pin, and as_deref of an Option Co-Authored-By: Claude Opus 5.5 --- quest/m3/README.md | 1 - quest/m3/ts-arc-mut.md | 14 -- ts/README.md | 2 +- ts/fixtures/lang/expected/core/option.ts | 40 +++ ts/fixtures/lang/expected/counts.Noisy.ts | 4 + ts/fixtures/lang/expected/counts.ts | 286 ++++++++++++++++++---- ts/fixtures/lang/expected/harness.ts | 122 ++++----- ts/fixtures/lang/src/counts.rs | 104 +++++++- ts/fixtures/lang/src/harness.rs | 2 + ts/runtime/rt/task.ts | 49 ++++ ts/src/lower/bind.rs | 5 +- ts/src/lower/body.rs | 10 +- ts/src/lower/dynamic.rs | 9 +- ts/src/lower/iter.rs | 6 +- ts/src/lower/library.rs | 26 +- ts/src/lower/mir/call.rs | 16 +- ts/src/lower/mod.rs | 16 +- ts/src/lower/pointer.rs | 11 +- ts/src/lower/stock.rs | 10 +- ts/src/lower/task.rs | 15 ++ 20 files changed, 554 insertions(+), 194 deletions(-) delete mode 100644 quest/m3/ts-arc-mut.md diff --git a/quest/m3/README.md b/quest/m3/README.md index 0d20fa142..e7c5b27f2 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -26,7 +26,6 @@ generic std gap is planned here even when moq never calls it. - [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 - [Checks on the differential harnesses](/quest/m3/ts-harness.md) - step lists that drift, literal op bytes, and `refs`'s coverage fail a test or get cases - [Ordering in std](/quest/m3/ts-ordering.md) - `cmp::max`/`min`, `is_sorted`, and `BinaryHeap`, and #173's cost on integers measured -- [Arc's unique-access functions](/quest/m3/ts-arc-mut.md) - `Arc::get_mut`, `make_mut`, `pin`, and `as_deref` of an `Option` translate - [Debug and Display of Arc and ranges](/quest/m3/ts-fmt-wrappers.md) - `Arc` formats as `T`, and ranges as std's `Debug` - [Fold a match a bool decided](/quest/m3/ts-fold-bool.md) - `if matches!` and derived `==` drop a later match's dead arms - [Rename one instance](/quest/m3/ffi-instance-rename.md) - `rename` by an instance's path, once a consumer's clash needs it diff --git a/quest/m3/ts-arc-mut.md b/quest/m3/ts-arc-mut.md deleted file mode 100644 index 95a31a782..000000000 --- a/quest/m3/ts-arc-mut.md +++ /dev/null @@ -1,14 +0,0 @@ -# [S] Arc's unique-access functions - -## Goal - -`Arc::get_mut`, `Arc::make_mut`, `Arc::pin`, and `Option::as_deref` of an `Arc` translate, -proven by differential tests against native Rust, including the strong count `make_mut` -leaves when it clones. - -## Plan - -Found by [#220](https://github.com/kixelated/rust2/pull/220) on 2026-10-29, where each is -refused as an unmapped call. A `&mut Arc` is an `rt.Place` since #220, so `make_mut`'s -clone goes into the place's `v` when the count is above one, and `get_mut` gives the -value only when the strong count is one and no `Weak` is left. diff --git a/ts/README.md b/ts/README.md index 2bd2e4054..23c3c5521 100644 --- a/ts/README.md +++ b/ts/README.md @@ -103,7 +103,7 @@ crate translation reaches is refused, and so are two versions of one crate it re | constant | its value, inlined as Rust inlines it | | 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`, `Rc`, `Weak`, `Mutex` | `rt.Arc` for either, with its strong and weak counts, and `new_cyclic`'s `Weak` upgrading only once its value is made; `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`, `Rc`, `Weak`, `Mutex` | `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, and `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; `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` | | `PoisonError`, `TryLockError` | the `T` it holds; `rt.TryLockError`, tagged as `Bound` is | | `Cell`, `RefCell`, `RwLock` | `rt.Cell`, the place holding its value in `v`; `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 | | `thread_local!`, `LocalKey` | 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 | diff --git a/ts/fixtures/lang/expected/core/option.ts b/ts/fixtures/lang/expected/core/option.ts index 2d4b83127..4087ec82a 100644 --- a/ts/fixtures/lang/expected/core/option.ts +++ b/ts/fixtures/lang/expected/core/option.ts @@ -167,6 +167,20 @@ export function map$Option_Arc_Made_u64_get_closure0(self: rt.Arc | u return f(self); } +export function map$Option_Arc_Noisy_Arc_Noisy_clone(self: rt.Arc | undefined, _: (a0: rt.Arc) => rt.Arc): rt.Arc | undefined { + if (self === undefined) { + return undefined; + } + return rt.arcClone(self); +} + +export function map$Option_Arc_Noisy_Noisy_deref(self: rt.Arc | undefined, _: (a0: rt.Arc) => counts_Noisy.Noisy): counts_Noisy.Noisy | undefined { + if (self === undefined) { + return undefined; + } + return self.value; +} + export function map$Option_Arc_dyn_Shape_u32_shared_closure0(self: rt.Arc | undefined, f: (a0: rt.Arc) => rt.u32): rt.u32 | undefined { if (self === undefined) { return undefined; @@ -174,6 +188,13 @@ export function map$Option_Arc_dyn_Shape_u32_shared_closure0(self: rt.Arc | undefined, _: (a0: rt.Arc) => rt.u8): rt.u8 | undefined { + if (self === undefined) { + return undefined; + } + return self.value; +} + export function map$Option_Box_Named_u16_edits_closure31(self: Named.Named | undefined, f: (a0: Named.Named) => rt.u16): rt.u16 | undefined { if (self === undefined) { return undefined; @@ -377,6 +398,13 @@ export function mapOr$Option_Nibble_u8_sets_closure4(self: Nibble.Nibble | undef return f(self); } +export function mapOr$Option_Noisy_u8_arcs_closure2(self: counts_Noisy.Noisy | undefined, default_: rt.u8, f: (a0: counts_Noisy.Noisy) => rt.u8): rt.u8 { + if (self === undefined) { + return default_; + } + return f(self); +} + export function mapOr$Option_Noisy_u8_borrows_closure0(self: Noisy.Noisy | undefined, default_: rt.u8, f: (a0: Noisy.Noisy) => rt.u8): rt.u8 { if (self === undefined) { return default_; @@ -692,6 +720,18 @@ export function okOrElse$Option_u8_u16_options_closure12(self: rt.u8 | undefined return rt.Ok(self); } +export function asDeref$Option_Arc_Noisy(self: rt.Arc | undefined): counts_Noisy.Noisy | undefined { + return map$Option_Arc_Noisy_Noisy_deref(self, (a0: rt.Arc): counts_Noisy.Noisy => { + return a0.value; + }); +} + +export function asDeref$Option_Arc_u8(self: rt.Arc | undefined): rt.u8 | undefined { + return map$Option_Arc_u8_u8_deref(self, (a0: rt.Arc): rt.u8 => { + return a0.value; + }); +} + export function and$Option_u8_u8(self: rt.u8 | undefined, optb: rt.u8 | undefined): rt.u8 | undefined { if (self === undefined) { return undefined; diff --git a/ts/fixtures/lang/expected/counts.Noisy.ts b/ts/fixtures/lang/expected/counts.Noisy.ts index a9a80af66..bb2095772 100644 --- a/ts/fixtures/lang/expected/counts.Noisy.ts +++ b/ts/fixtures/lang/expected/counts.Noisy.ts @@ -18,6 +18,10 @@ export class Noisy { } } +export function clone$Clone(self: Noisy): Noisy { + return new Noisy(rt.u8WrappingAdd(self.id, 1), rt.arcClone(self.log)); +} + export function drop$Drop(self: Noisy): void { const _5: rt.Mutex = rt.mutexLock(self.log.value); rt.byteVecPush(_5.value, self.id); diff --git a/ts/fixtures/lang/expected/counts.ts b/ts/fixtures/lang/expected/counts.ts index 63ac499ff..9f1f34470 100644 --- a/ts/fixtures/lang/expected/counts.ts +++ b/ts/fixtures/lang/expected/counts.ts @@ -1,7 +1,7 @@ // Generated by rust2-ts. Do not edit. import * as rt from "./rt.ts"; -import { mapOr$Option_Arc_ArcNode_u8_cyclic_arcs_closure4, mapOr$Option_Arc_Noisy_u8_arcs_closure0_closure0, mapOr$Option_Arc_dyn_Id_u8_arcs_closure1, mapOr$Option_Rc_RcNode_u8_cyclic_rcs_closure4 } from "./core/option.ts"; +import { unwrapOr$Option_u8, map$Option_Arc_Noisy_Arc_Noisy_clone, mapOr$Option_Arc_ArcNode_u8_cyclic_arcs_closure4, mapOr$Option_Arc_Noisy_u8_arcs_closure0_closure0, mapOr$Option_Arc_dyn_Id_u8_arcs_closure1, mapOr$Option_Arc_u8_u8_objects_closure1, mapOr$Option_Noisy_u8_arcs_closure2, mapOr$Option_Rc_RcNode_u8_cyclic_rcs_closure4, asDeref$Option_Arc_Noisy, asDeref$Option_Arc_u8 } from "./core/option.ts"; import * as ArcHolder from "./counts.ArcHolder.ts"; import * as ArcNode from "./counts.ArcNode.ts"; import * as Id from "./counts.Id.ts"; @@ -27,7 +27,7 @@ export function arcs(data: rt.ByteSlice, out: rt.ByteVec): void { const weak: rt.Weak[] = []; l1: for (const op of rt.byteSliceView(data)) { const pick: rt.usize = op >> 4 as rt.usize; - const _19: rt.u8 = rt.u8Rem(op, 10); + const _19: rt.u8 = rt.u8Rem(op, 12); if (_19 === 0) { const _22: rt.Arc = new rt.Arc(new Noisy.Noisy(op, rt.arcClone(log))); strong.push(_22); @@ -81,10 +81,10 @@ export function arcs(data: rt.ByteSlice, out: rt.ByteVec): void { } if (_19 === 8) { if (!((strong.length === 0))) { - const at = rt.idx(strong, rt.usizeRem(pick, strong.length)); - const held: rt.Weak | undefined = new rt.Weak(strong[at]); - const kept: ArcHolder.ArcHolder = new ArcHolder.ArcHolder(new rt.Weak(strong[at])); - const captured2 = new rt.Weak(strong[at]); + const at3 = rt.idx(strong, rt.usizeRem(pick, strong.length)); + const held: rt.Weak | undefined = new rt.Weak(strong[at3]); + const kept: ArcHolder.ArcHolder = new ArcHolder.ArcHolder(new rt.Weak(strong[at3])); + const captured2 = new rt.Weak(strong[at3]); const read: () => rt.u8 = rt.closure((): rt.u8 => { return mapOr$Option_Arc_Noisy_u8_arcs_closure0_closure0(rt.weakUpgrade(captured2), 253, (s: rt.Arc): rt.u8 => { const _02: rt.u8 = s.value.id; @@ -94,38 +94,59 @@ export function arcs(data: rt.ByteSlice, out: rt.ByteVec): void { }, (): void => { captured2[Symbol.dispose](); }); - rt.byteVecPush(out, strong[at].weak & 0xff); + rt.byteVecPush(out, strong[at3].weak & 0xff); rt.byteVecPush(out, read()); rt.drop(held); - rt.byteVecPush(out, strong[at].weak & 0xff); + rt.byteVecPush(out, strong[at3].weak & 0xff); kept[Symbol.dispose](); rt.drop(read); + rt.byteVecPush(out, strong[at3].weak & 0xff); + continue; + } + } else if (_19 === 9) { + if (!((strong.length === 0))) { + const _126: Noisy.Noisy | undefined = rt.arcGetMut(strong[rt.idx(strong, rt.usizeRem(pick, strong.length))]); + if (_126 === undefined) { + rt.byteVecPush(out, 252); + continue; + } + _126.id = _126.id ^ 16; + rt.byteVecPush(out, _126.id); + continue; + } + } else if (_19 === 10) { + if (!((strong.length === 0))) { + const at = rt.idx(strong, rt.usizeRem(pick, strong.length)); + const noisy: Noisy.Noisy = rt.arcMakeMut(rt.place(() => strong[at], (value) => strong[at] = value), Noisy.clone$Clone); + noisy.id = noisy.id ^ 32; + rt.byteVecPush(out, noisy.id); + rt.byteVecPush(out, strong[at].strong & 0xff); rt.byteVecPush(out, strong[at].weak & 0xff); continue; } } } - for (const _125 of strong) { - rt.byteVecPush(out, _125.strong & 0xff); - rt.byteVecPush(out, _125.weak & 0xff); + for (const _157 of strong) { + rt.byteVecPush(out, _157.strong & 0xff); + rt.byteVecPush(out, _157.weak & 0xff); } const iterS3: rt.Weak[] = weak; let iterI3: number = 0; while (true) { - const _138: rt.Weak | rt.Done = iterI3 < iterS3.length ? iterS3[iterI3++] : rt.DONE; - if (_138 === rt.DONE) { + const _170: rt.Weak | rt.Done = iterI3 < iterS3.length ? iterS3[iterI3++] : rt.DONE; + if (_170 === rt.DONE) { continue l1; } - rt.byteVecPush(out, _138.arc.strong & 0xff); - rt.byteVecPush(out, rt.weakWeakCount(_138) & 0xff); + rt.byteVecPush(out, _170.arc.strong & 0xff); + rt.byteVecPush(out, rt.weakWeakCount(_170) & 0xff); } } - const _148: rt.Arc | undefined = rt.sliceLast(strong); - if (_148 !== undefined) { - const _154: rt.Arc = rt.arcClone(_148); - const object: rt.Arc = new rt.ArcDyn(_154, new Id.Noisy(_154.value)); + const _180: rt.Arc | undefined = rt.sliceLast(strong); + if (_180 !== undefined) { + const _186: rt.Arc = rt.arcClone(_180); + const object: rt.Arc = new rt.ArcDyn(_186, new Id.Noisy(_186.value)); const weakObject: rt.Weak = new rt.Weak(object); - rt.byteVecPush(out, _148.weak & 0xff); + rt.byteVecPush(out, _180.weak & 0xff); rt.byteVecPush(out, rt.weakWeakCount(weakObject) & 0xff); rt.byteVecPush(out, mapOr$Option_Arc_dyn_Id_u8_arcs_closure1(rt.weakUpgrade(weakObject), 253, (o: rt.Arc): rt.u8 => { const _03: rt.u8 = o.value.id$Id(); @@ -136,20 +157,28 @@ export function arcs(data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecPush(out, weakObject.arc.strong & 0xff); weakObject[Symbol.dispose](); } + new rt.Arc(new Noisy.Noisy(224, rt.arcClone(log)))[Symbol.dispose](); + const _214: rt.Arc | undefined = map$Option_Arc_Noisy_Arc_Noisy_clone(rt.sliceFirst(strong), (a02: rt.Arc): rt.Arc => { + return rt.arcClone(a02); + }); + rt.byteVecPush(out, mapOr$Option_Noisy_u8_arcs_closure2(asDeref$Option_Arc_Noisy(_214), 253, (noisy2: Noisy.Noisy): rt.u8 => { + return noisy2.id; + })); + rt.drop(_214); rt.dropAll(strong); rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(weak), (w: rt.Weak): rt.u8 => { const _53: rt.Arc | undefined = rt.weakUpgrade(w); - const _04: rt.u8 = _53 === undefined ? 1 : 0; + const _05: rt.u8 = _53 === undefined ? 1 : 0; rt.drop(_53); - return _04; + return _05; })); rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(weak), (w2: rt.Weak): rt.u8 => { return rt.weakWeakCount(w2) & 0xff; })); rt.dropAll(weak); - const _191: rt.Mutex = rt.mutexLock(log.value); - rt.byteVecExtendFromSlice(out, rt.byteVecSlice(_191.value)); - rt.mutexUnlock(_191); + const _236: rt.Mutex = rt.mutexLock(log.value); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(_236.value)); + rt.mutexUnlock(_236); log[Symbol.dispose](); } @@ -244,6 +273,71 @@ export function cyclicArcs(data: rt.ByteSlice, out: rt.ByteVec): void { log[Symbol.dispose](); } +/** + * Each byte an operation on pools of strong and weak handles of a byte JS copies, + * the rest of it the handle it picks; each puts what it read and the picked + * handle's counts. + */ +export function uniqueArcs(data: rt.ByteSlice, out: rt.ByteVec): void { + const strong: rt.Arc[] = [new rt.Arc(0)]; + const weak: rt.Weak[] = []; + for (const op of rt.byteSliceView(data)) { + const pick: rt.usize = rt.usizeRem(op >> 3 as rt.usize, strong.length); + const _22: rt.u8 = rt.u8Rem(op, 7); + if (_22 === 0) { + const _25: rt.Arc = new rt.Arc(op); + strong.push(_25); + } else if (_22 === 1) { + const again: rt.Arc = rt.arcClone(strong[rt.idx(strong, pick)]); + strong.push(again); + } else if (_22 === 2) { + const _32: rt.Weak = new rt.Weak(strong[rt.idx(strong, pick)]); + weak.push(_32); + } else if (_22 === 3) { + if (!(strong.length > 1)) { + rt.vecTruncate(weak, 0, rt.drop); + } else { + rt.vecSwapRemove(strong, pick)[Symbol.dispose](); + } + } else if (_22 === 4) { + const _41: rt.Place | undefined = rt.arcGetMutPlace(strong[rt.idx(strong, pick)]); + if (_41 === undefined) { + rt.byteVecPush(out, 252); + } else { + _41.v = rt.u8WrappingAdd(_41.v, op); + rt.byteVecPush(out, _41.v); + } + } else if (_22 === 5) { + const at2 = rt.idx(strong, pick); + const value: rt.Place = rt.arcMakeMutPlace(rt.place(() => strong[at2], (value2) => strong[at2] = value2), undefined); + value.v = value.v ^ op; + rt.byteVecPush(out, value.v); + } else { + const pinned: rt.Arc = new rt.Arc(op); + const held: rt.Arc | undefined = rt.arcClone(strong[rt.idx(strong, pick)]); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(pinned.value, unwrapOr$Option_u8(asDeref$Option_Arc_u8(held), 251)))); + rt.drop(held); + pinned[Symbol.dispose](); + } + const at6 = rt.idx(strong, rt.usizeRem(pick, strong.length)); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(strong[at6].value, strong[at6].strong & 0xff, strong[at6].weak & 0xff))); + } + for (const _90 of weak) { + rt.byteVecPush(out, mapOr$Option_Arc_u8_u8_objects_closure1(rt.weakUpgrade(_90), 253, (s: rt.Arc): rt.u8 => { + const _0: rt.u8 = s.value; + s[Symbol.dispose](); + return _0; + })); + rt.byteVecPush(out, _90.arc.strong & 0xff); + rt.byteVecPush(out, rt.weakWeakCount(_90) & 0xff); + } + rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(strong), (s2: rt.Arc): rt.u8 => { + return s2.value; + })); + rt.dropAll(weak); + rt.dropAll(strong); +} + /** * Each byte a value made by `new_cyclic`, from a closure that moves what it owns, * reads it, reads it as an `Fn`, or owns nothing that drops, holding up to 3 more @@ -345,7 +439,7 @@ export function rcs(data: rt.ByteSlice, out: rt.ByteVec): void { const weak: rt.Weak[] = []; l1: for (const op of rt.byteSliceView(data)) { const pick: rt.usize = op >> 4 as rt.usize; - const _19: rt.u8 = rt.u8Rem(op, 10); + const _19: rt.u8 = rt.u8Rem(op, 12); if (_19 === 0) { const _22: rt.Arc = new rt.Arc(new Noisy.Noisy(op, rt.arcClone(log))); strong.push(_22); @@ -399,10 +493,10 @@ export function rcs(data: rt.ByteSlice, out: rt.ByteVec): void { } if (_19 === 8) { if (!((strong.length === 0))) { - const at = rt.idx(strong, rt.usizeRem(pick, strong.length)); - const held: rt.Weak | undefined = new rt.Weak(strong[at]); - const kept: RcHolder.RcHolder = new RcHolder.RcHolder(new rt.Weak(strong[at])); - const captured2 = new rt.Weak(strong[at]); + const at3 = rt.idx(strong, rt.usizeRem(pick, strong.length)); + const held: rt.Weak | undefined = new rt.Weak(strong[at3]); + const kept: RcHolder.RcHolder = new RcHolder.RcHolder(new rt.Weak(strong[at3])); + const captured2 = new rt.Weak(strong[at3]); const read: () => rt.u8 = rt.closure((): rt.u8 => { return mapOr$Option_Arc_Noisy_u8_arcs_closure0_closure0(rt.weakUpgrade(captured2), 253, (s: rt.Arc): rt.u8 => { const _02: rt.u8 = s.value.id; @@ -412,38 +506,59 @@ export function rcs(data: rt.ByteSlice, out: rt.ByteVec): void { }, (): void => { captured2[Symbol.dispose](); }); - rt.byteVecPush(out, strong[at].weak & 0xff); + rt.byteVecPush(out, strong[at3].weak & 0xff); rt.byteVecPush(out, read()); rt.drop(held); - rt.byteVecPush(out, strong[at].weak & 0xff); + rt.byteVecPush(out, strong[at3].weak & 0xff); kept[Symbol.dispose](); rt.drop(read); + rt.byteVecPush(out, strong[at3].weak & 0xff); + continue; + } + } else if (_19 === 9) { + if (!((strong.length === 0))) { + const _126: Noisy.Noisy | undefined = rt.arcGetMut(strong[rt.idx(strong, rt.usizeRem(pick, strong.length))]); + if (_126 === undefined) { + rt.byteVecPush(out, 252); + continue; + } + _126.id = _126.id ^ 16; + rt.byteVecPush(out, _126.id); + continue; + } + } else if (_19 === 10) { + if (!((strong.length === 0))) { + const at = rt.idx(strong, rt.usizeRem(pick, strong.length)); + const noisy: Noisy.Noisy = rt.arcMakeMut(rt.place(() => strong[at], (value) => strong[at] = value), Noisy.clone$Clone); + noisy.id = noisy.id ^ 32; + rt.byteVecPush(out, noisy.id); + rt.byteVecPush(out, strong[at].strong & 0xff); rt.byteVecPush(out, strong[at].weak & 0xff); continue; } } } - for (const _125 of strong) { - rt.byteVecPush(out, _125.strong & 0xff); - rt.byteVecPush(out, _125.weak & 0xff); + for (const _157 of strong) { + rt.byteVecPush(out, _157.strong & 0xff); + rt.byteVecPush(out, _157.weak & 0xff); } const iterS3: rt.Weak[] = weak; let iterI3: number = 0; while (true) { - const _138: rt.Weak | rt.Done = iterI3 < iterS3.length ? iterS3[iterI3++] : rt.DONE; - if (_138 === rt.DONE) { + const _170: rt.Weak | rt.Done = iterI3 < iterS3.length ? iterS3[iterI3++] : rt.DONE; + if (_170 === rt.DONE) { continue l1; } - rt.byteVecPush(out, _138.arc.strong & 0xff); - rt.byteVecPush(out, rt.weakWeakCount(_138) & 0xff); + rt.byteVecPush(out, _170.arc.strong & 0xff); + rt.byteVecPush(out, rt.weakWeakCount(_170) & 0xff); } } - const _148: rt.Arc | undefined = rt.sliceLast(strong); - if (_148 !== undefined) { - const _154: rt.Arc = rt.arcClone(_148); - const object: rt.Arc = new rt.ArcDyn(_154, new Id.Noisy(_154.value)); + const _180: rt.Arc | undefined = rt.sliceLast(strong); + if (_180 !== undefined) { + const _186: rt.Arc = rt.arcClone(_180); + const object: rt.Arc = new rt.ArcDyn(_186, new Id.Noisy(_186.value)); const weakObject: rt.Weak = new rt.Weak(object); - rt.byteVecPush(out, _148.weak & 0xff); + rt.byteVecPush(out, _180.weak & 0xff); rt.byteVecPush(out, rt.weakWeakCount(weakObject) & 0xff); rt.byteVecPush(out, mapOr$Option_Arc_dyn_Id_u8_arcs_closure1(rt.weakUpgrade(weakObject), 253, (o: rt.Arc): rt.u8 => { const _03: rt.u8 = o.value.id$Id(); @@ -454,21 +569,94 @@ export function rcs(data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecPush(out, weakObject.arc.strong & 0xff); weakObject[Symbol.dispose](); } + new rt.Arc(new Noisy.Noisy(224, rt.arcClone(log)))[Symbol.dispose](); + const _214: rt.Arc | undefined = map$Option_Arc_Noisy_Arc_Noisy_clone(rt.sliceFirst(strong), (a02: rt.Arc): rt.Arc => { + return rt.arcClone(a02); + }); + rt.byteVecPush(out, mapOr$Option_Noisy_u8_arcs_closure2(asDeref$Option_Arc_Noisy(_214), 253, (noisy2: Noisy.Noisy): rt.u8 => { + return noisy2.id; + })); + rt.drop(_214); rt.dropAll(strong); rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(weak), (w: rt.Weak): rt.u8 => { const _53: rt.Arc | undefined = rt.weakUpgrade(w); - const _04: rt.u8 = _53 === undefined ? 1 : 0; + const _05: rt.u8 = _53 === undefined ? 1 : 0; rt.drop(_53); - return _04; + return _05; })); rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(weak), (w2: rt.Weak): rt.u8 => { return rt.weakWeakCount(w2) & 0xff; })); rt.dropAll(weak); - const _191: rt.Mutex = rt.mutexLock(log.value); - rt.byteVecExtendFromSlice(out, rt.byteVecSlice(_191.value)); - rt.mutexUnlock(_191); + const _236: rt.Mutex = rt.mutexLock(log.value); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(_236.value)); + rt.mutexUnlock(_236); log[Symbol.dispose](); } +/** + * Each byte an operation on pools of strong and weak handles of a byte JS copies, + * the rest of it the handle it picks; each puts what it read and the picked + * handle's counts. + */ +export function uniqueRcs(data: rt.ByteSlice, out: rt.ByteVec): void { + const strong: rt.Arc[] = [new rt.Arc(0)]; + const weak: rt.Weak[] = []; + for (const op of rt.byteSliceView(data)) { + const pick: rt.usize = rt.usizeRem(op >> 3 as rt.usize, strong.length); + const _22: rt.u8 = rt.u8Rem(op, 7); + if (_22 === 0) { + const _25: rt.Arc = new rt.Arc(op); + strong.push(_25); + } else if (_22 === 1) { + const again: rt.Arc = rt.arcClone(strong[rt.idx(strong, pick)]); + strong.push(again); + } else if (_22 === 2) { + const _32: rt.Weak = new rt.Weak(strong[rt.idx(strong, pick)]); + weak.push(_32); + } else if (_22 === 3) { + if (!(strong.length > 1)) { + rt.vecTruncate(weak, 0, rt.drop); + } else { + rt.vecSwapRemove(strong, pick)[Symbol.dispose](); + } + } else if (_22 === 4) { + const _41: rt.Place | undefined = rt.arcGetMutPlace(strong[rt.idx(strong, pick)]); + if (_41 === undefined) { + rt.byteVecPush(out, 252); + } else { + _41.v = rt.u8WrappingAdd(_41.v, op); + rt.byteVecPush(out, _41.v); + } + } else if (_22 === 5) { + const at2 = rt.idx(strong, pick); + const value: rt.Place = rt.arcMakeMutPlace(rt.place(() => strong[at2], (value2) => strong[at2] = value2), undefined); + value.v = value.v ^ op; + rt.byteVecPush(out, value.v); + } else { + const pinned: rt.Arc = new rt.Arc(op); + const held: rt.Arc | undefined = rt.arcClone(strong[rt.idx(strong, pick)]); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(pinned.value, unwrapOr$Option_u8(asDeref$Option_Arc_u8(held), 251)))); + rt.drop(held); + pinned[Symbol.dispose](); + } + const at6 = rt.idx(strong, rt.usizeRem(pick, strong.length)); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(strong[at6].value, strong[at6].strong & 0xff, strong[at6].weak & 0xff))); + } + for (const _90 of weak) { + rt.byteVecPush(out, mapOr$Option_Arc_u8_u8_objects_closure1(rt.weakUpgrade(_90), 253, (s: rt.Arc): rt.u8 => { + const _0: rt.u8 = s.value; + s[Symbol.dispose](); + return _0; + })); + rt.byteVecPush(out, _90.arc.strong & 0xff); + rt.byteVecPush(out, rt.weakWeakCount(_90) & 0xff); + } + rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(strong), (s2: rt.Arc): rt.u8 => { + return s2.value; + })); + rt.dropAll(weak); + rt.dropAll(strong); +} + //# sourceMappingURL=counts.ts.map diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index 4965909b1..adabc25ab 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -11,7 +11,7 @@ import { chars } from "./chars.ts"; import { chunks } from "./chunks.ts"; import { hashMaps, sets, btreeMaps, deques, vecs, stores, optionals } from "./collections.ts"; import { combinators } from "./combinators.ts"; -import { arcs, cyclicArcs, cyclicRcs, rcs } from "./counts.ts"; +import { arcs, cyclicArcs, uniqueArcs, cyclicRcs, rcs, uniqueRcs } from "./counts.ts"; import { cows } from "./cows.ts"; import { defaults } from "./defaults.ts"; import { durations } from "./durations.ts"; @@ -41,100 +41,104 @@ import { wrappers } from "./wrappers.ts"; * case added at the end moves no other. */ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void][] { - return copy$str_forabfnu8au8bmutstdvecVecu8s([["ints::u8_op", u8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u16_op", u16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u32_op", u32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u64_op", u64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u128_op", u128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::usize_op", usizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i8_op", i8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i16_op", i16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i32_op", i32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i64_op", i64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i128_op", i128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::isize_op", isizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::neg", neg] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::cast", cast] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::convert", (_273: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void => { + return copy$str_forabfnu8au8bmutstdvecVecu8s([["ints::u8_op", u8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u16_op", u16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u32_op", u32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u64_op", u64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u128_op", u128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::usize_op", usizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i8_op", i8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i16_op", i16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i32_op", i32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i64_op", i64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i128_op", i128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::isize_op", isizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::neg", neg] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::cast", cast] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::convert", (_279: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void => { convert(data, out); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::copies", (_274: rt.u8, data2: rt.ByteSlice, out2: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::copies", (_280: rt.u8, data2: rt.ByteSlice, out2: rt.ByteVec): void => { shapes_copies(data2, out2); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::loops", (_275: rt.u8, data3: rt.ByteSlice, out3: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::loops", (_281: rt.u8, data3: rt.ByteSlice, out3: rt.ByteVec): void => { loops(data3, out3); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::patterns", (_276: rt.u8, data4: rt.ByteSlice, out4: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::patterns", (_282: rt.u8, data4: rt.ByteSlice, out4: rt.ByteVec): void => { patterns(data4, out4); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::closures", (_277: rt.u8, data5: rt.ByteSlice, out5: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::closures", (_283: rt.u8, data5: rt.ByteSlice, out5: rt.ByteVec): void => { closures(data5, out5); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::discriminants", (_278: rt.u8, data6: rt.ByteSlice, out6: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::discriminants", (_284: rt.u8, data6: rt.ByteSlice, out6: rt.ByteVec): void => { discriminants(data6, out6); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::library", (_279: rt.u8, data7: rt.ByteSlice, out7: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::library", (_285: rt.u8, data7: rt.ByteSlice, out7: rt.ByteVec): void => { library(data7, out7); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::ints", ints] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::strings", (_280: rt.u8, data8: rt.ByteSlice, out8: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::ints", ints] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::strings", (_286: rt.u8, data8: rt.ByteSlice, out8: rt.ByteVec): void => { strings(data8, out8); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::derived", (_281: rt.u8, data9: rt.ByteSlice, out9: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::derived", (_287: rt.u8, data9: rt.ByteSlice, out9: rt.ByteVec): void => { derived(data9, out9); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::written", (_282: rt.u8, data10: rt.ByteSlice, out10: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::written", (_288: rt.u8, data10: rt.ByteSlice, out10: rt.ByteVec): void => { written(data10, out10); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::panics", panics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["generics::methods", (_283: rt.u8, data11: rt.ByteSlice, out11: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::panics", panics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["generics::methods", (_289: rt.u8, data11: rt.ByteSlice, out11: rt.ByteVec): void => { methods(data11, out11); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::adaptors", adaptors] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::consumers", consumers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranges", (_284: rt.u8, data12: rt.ByteSlice, out12: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::adaptors", adaptors] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::consumers", consumers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranges", (_290: rt.u8, data12: rt.ByteSlice, out12: rt.ByteVec): void => { ranges(data12, out12); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::options", (_285: rt.u8, data13: rt.ByteSlice, out13: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::options", (_291: rt.u8, data13: rt.ByteSlice, out13: rt.ByteVec): void => { options(data13, out13); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::own", (_286: rt.u8, data14: rt.ByteSlice, out14: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::own", (_292: rt.u8, data14: rt.ByteSlice, out14: rt.ByteVec): void => { own(data14, out14); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::values", (_287: rt.u8, data15: rt.ByteSlice, out15: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::values", (_293: rt.u8, data15: rt.ByteSlice, out15: rt.ByteVec): void => { values(data15, out15); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::order", (_288: rt.u8, data16: rt.ByteSlice, out16: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::order", (_294: rt.u8, data16: rt.ByteSlice, out16: rt.ByteVec): void => { order(data16, out16); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::hash_maps", hashMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::sets", sets] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::btree_maps", btreeMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::deques", (_289: rt.u8, data17: rt.ByteSlice, out17: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::hash_maps", hashMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::sets", sets] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::btree_maps", btreeMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::deques", (_295: rt.u8, data17: rt.ByteSlice, out17: rt.ByteVec): void => { deques(data17, out17); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::flows", (_290: rt.u8, data18: rt.ByteSlice, out18: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::flows", (_296: rt.u8, data18: rt.ByteSlice, out18: rt.ByteVec): void => { flows(data18, out18); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::keys", keys] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chars::chars", chars] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::strs", strs] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::vecs", (_291: rt.u8, data19: rt.ByteSlice, out19: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::keys", keys] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chars::chars", chars] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::strs", strs] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::vecs", (_297: rt.u8, data19: rt.ByteSlice, out19: rt.ByteVec): void => { vecs(data19, out19); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::slices", slices] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::stores", (_292: rt.u8, data20: rt.ByteSlice, out20: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::slices", slices] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::stores", (_298: rt.u8, data20: rt.ByteSlice, out20: rt.ByteVec): void => { stores(data20, out20); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chunks::chunks", chunks] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cows::cows", cows] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["durations::durations", durations] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["combinators::combinators", combinators] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["splits::splits", splits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["nested::nested", nested] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["refs::refs", (_293: rt.u8, data21: rt.ByteSlice, out21: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chunks::chunks", chunks] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cows::cows", cows] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["durations::durations", durations] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["combinators::combinators", combinators] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["splits::splits", splits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["nested::nested", nested] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["refs::refs", (_299: rt.u8, data21: rt.ByteSlice, out21: rt.ByteVec): void => { refs(data21, out21); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::counts", (_294: rt.u8, data22: rt.ByteSlice, out22: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::counts", (_2100: rt.u8, data22: rt.ByteSlice, out22: rt.ByteVec): void => { counts(data22, out22); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_295: rt.u8, _310: rt.ByteSlice, out23: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_2101: rt.u8, _310: rt.ByteSlice, out23: rt.ByteVec): void => { rounding(out23); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::skips", skips] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["wrappers::wrappers", wrappers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::edits", edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["utf8::decodes", decodes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["floats::floats", floats] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["atomics::atomics", atomics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::overrides", overrides] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::dyns", (_296: rt.u8, data23: rt.ByteSlice, out24: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::skips", skips] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["wrappers::wrappers", wrappers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::edits", edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["utf8::decodes", decodes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["floats::floats", floats] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["atomics::atomics", atomics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::overrides", overrides] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::dyns", (_2102: rt.u8, data23: rt.ByteSlice, out24: rt.ByteVec): void => { dyns(data23, out24); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cells::cells", cells] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::shared", (_297: rt.u8, data24: rt.ByteSlice, out25: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cells::cells", cells] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::shared", (_2103: rt.u8, data24: rt.ByteSlice, out25: rt.ByteVec): void => { shared(data24, out25); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranked", ranked] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["captures::captures", captures] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::locks", (_298: rt.u8, data25: rt.ByteSlice, out26: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranked", ranked] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["captures::captures", captures] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::locks", (_2104: rt.u8, data25: rt.ByteSlice, out26: rt.ByteVec): void => { locks(data25, out26); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::relock", (_299: rt.u8, data26: rt.ByteSlice, out27: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::relock", (_2105: rt.u8, data26: rt.ByteSlice, out27: rt.ByteVec): void => { relock(data26, out27); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locals::locals", (_2100: rt.u8, data27: rt.ByteSlice, out28: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locals::locals", (_2106: rt.u8, data27: rt.ByteSlice, out28: rt.ByteVec): void => { locals(data27, out28); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["statics::statics", (_2101: rt.u8, data28: rt.ByteSlice, out29: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["statics::statics", (_2107: rt.u8, data28: rt.ByteSlice, out29: rt.ByteVec): void => { statics(data28, out29); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::rwlocks", (_2102: rt.u8, data29: rt.ByteSlice, out30: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::rwlocks", (_2108: rt.u8, data29: rt.ByteSlice, out30: rt.ByteVec): void => { rwlocks(data29, out30); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["borrows::borrows", (_2103: rt.u8, data30: rt.ByteSlice, out31: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["borrows::borrows", (_2109: rt.u8, data30: rt.ByteSlice, out31: rt.ByteVec): void => { borrows(data30, out31); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["defaults::defaults", (_2104: rt.u8, data31: rt.ByteSlice, out32: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["defaults::defaults", (_2110: rt.u8, data31: rt.ByteSlice, out32: rt.ByteVec): void => { defaults(data31, out32); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::arcs", (_2105: rt.u8, data32: rt.ByteSlice, out33: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::arcs", (_2111: rt.u8, data32: rt.ByteSlice, out33: rt.ByteVec): void => { arcs(data32, out33); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::rcs", (_2106: rt.u8, data33: rt.ByteSlice, out34: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::rcs", (_2112: rt.u8, data33: rt.ByteSlice, out34: rt.ByteVec): void => { rcs(data33, out34); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::cyclic_arcs", (_2107: rt.u8, data34: rt.ByteSlice, out35: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::cyclic_arcs", (_2113: rt.u8, data34: rt.ByteSlice, out35: rt.ByteVec): void => { cyclicArcs(data34, out35); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::cyclic_rcs", (_2108: rt.u8, data35: rt.ByteSlice, out36: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::cyclic_rcs", (_2114: rt.u8, data35: rt.ByteSlice, out36: rt.ByteVec): void => { cyclicRcs(data35, out36); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::anys", (_2109: rt.u8, data36: rt.ByteSlice, out37: rt.ByteVec): void => { - anys(data36, out37); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::fns", (_2110: rt.u8, data37: rt.ByteSlice, out38: rt.ByteVec): void => { - fns(data37, out38); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::copies", (_2111: rt.u8, data38: rt.ByteSlice, out39: rt.ByteVec): void => { - copies(data38, out39); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::lists", (_2112: rt.u8, data39: rt.ByteSlice, out40: rt.ByteVec): void => { - lists(data39, out40); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::nested", (_2113: rt.u8, data40: rt.ByteSlice, out41: rt.ByteVec): void => { - pointers_nested(data40, out41); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::places", (_2114: rt.u8, data41: rt.ByteSlice, out42: rt.ByteVec): void => { - places(data41, out42); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::sentinels", (_2115: rt.u8, data42: rt.ByteSlice, out43: rt.ByteVec): void => { - sentinels(data42, out43); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::shared", (_2116: rt.u8, data43: rt.ByteSlice, out44: rt.ByteVec): void => { - pointers_shared(data43, out44); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::unchecked", (_2117: rt.u8, data44: rt.ByteSlice, out45: rt.ByteVec): void => { - unchecked(data44, out45); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::bytes", bytes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["arrays::from_fns", (_2118: rt.u8, data45: rt.ByteSlice, out46: rt.ByteVec): void => { - fromFns(data45, out46); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::optionals", optionals] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["objects::objects", (_2119: rt.u8, data46: rt.ByteSlice, out47: rt.ByteVec): void => { - objects(data46, out47); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::unique_arcs", (_2115: rt.u8, data36: rt.ByteSlice, out37: rt.ByteVec): void => { + uniqueArcs(data36, out37); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::unique_rcs", (_2116: rt.u8, data37: rt.ByteSlice, out38: rt.ByteVec): void => { + uniqueRcs(data37, out38); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::anys", (_2117: rt.u8, data38: rt.ByteSlice, out39: rt.ByteVec): void => { + anys(data38, out39); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::fns", (_2118: rt.u8, data39: rt.ByteSlice, out40: rt.ByteVec): void => { + fns(data39, out40); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::copies", (_2119: rt.u8, data40: rt.ByteSlice, out41: rt.ByteVec): void => { + copies(data40, out41); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::lists", (_2120: rt.u8, data41: rt.ByteSlice, out42: rt.ByteVec): void => { + lists(data41, out42); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::nested", (_2121: rt.u8, data42: rt.ByteSlice, out43: rt.ByteVec): void => { + pointers_nested(data42, out43); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::places", (_2122: rt.u8, data43: rt.ByteSlice, out44: rt.ByteVec): void => { + places(data43, out44); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::sentinels", (_2123: rt.u8, data44: rt.ByteSlice, out45: rt.ByteVec): void => { + sentinels(data44, out45); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::shared", (_2124: rt.u8, data45: rt.ByteSlice, out46: rt.ByteVec): void => { + pointers_shared(data45, out46); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::unchecked", (_2125: rt.u8, data46: rt.ByteSlice, out47: rt.ByteVec): void => { + unchecked(data46, out47); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::bytes", bytes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["arrays::from_fns", (_2126: rt.u8, data47: rt.ByteSlice, out48: rt.ByteVec): void => { + fromFns(data47, out48); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::optionals", optionals] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["objects::objects", (_2127: rt.u8, data48: rt.ByteSlice, out49: rt.ByteVec): void => { + objects(data48, out49); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]).map(copy$str_forabfnu8au8bmutstdvecVecu8); } diff --git a/ts/fixtures/lang/src/counts.rs b/ts/fixtures/lang/src/counts.rs index 65378236f..243220ab2 100644 --- a/ts/fixtures/lang/src/counts.rs +++ b/ts/fixtures/lang/src/counts.rs @@ -1,7 +1,7 @@ //! Strong and weak counts of `Arc`s and `Rc`s and their `Weak`s, through every way one is //! made and dropped: cloned, downgraded, upgraded, made dangling, made holding handles to //! itself, and held in a `Vec`, an `Option`, a struct, and a closure, of a value and of a -//! trait object. +//! trait object; and the values `get_mut` and `make_mut` write, alone or shared. use std::rc::{self, Rc}; use std::sync::{self, Arc, Mutex}; @@ -12,6 +12,16 @@ struct Noisy { log: Arc>>, } +/// A clone is told apart by its id. +impl Clone for Noisy { + fn clone(&self) -> Self { + Self { + id: self.id.wrapping_add(1), + log: self.log.clone(), + } + } +} + impl Drop for Noisy { fn drop(&mut self) { self.log.lock().unwrap().push(self.id); @@ -37,7 +47,7 @@ impl Id for Noisy { } macro_rules! counts { - ($name:ident, $holder:ident, $cyclic:ident, $node:ident, $Ptr:ident, $weak:ident) => { + ($name:ident, $holder:ident, $cyclic:ident, $node:ident, $unique:ident, $Ptr:ident, $weak:ident) => { /// Holds a handle that doesn't keep its value. struct $holder { weak: $weak::Weak, @@ -140,7 +150,7 @@ macro_rules! counts { let mut weak: Vec<$weak::Weak> = Vec::new(); for &op in data { let pick = usize::from(op >> 4); - match op % 10 { + match op % 12 { 0 => strong.push($Ptr::new(Noisy { id: op, log: log.clone(), @@ -182,6 +192,28 @@ macro_rules! counts { drop(read); out.push($Ptr::weak_count(picked) as u8); } + // The value no other handle shares, which `get_mut` lends. + 9 if !strong.is_empty() => { + let len = strong.len(); + match $Ptr::get_mut(&mut strong[pick % len]) { + Some(noisy) => { + noisy.id ^= 0x10; + out.push(noisy.id); + } + None => out.push(0xfc), + } + } + // A clone while another strong handle shares it, whose share drops, or the + // value moved away from the weak ones, which dangle. + 10 if !strong.is_empty() => { + let len = strong.len(); + let picked = &mut strong[pick % len]; + let noisy = $Ptr::make_mut(picked); + noisy.id ^= 0x20; + out.push(noisy.id); + out.push($Ptr::strong_count(picked) as u8); + out.push($Ptr::weak_count(picked) as u8); + } // Every count, of each strong handle and each weak one. _ => { for s in &strong { @@ -205,14 +237,76 @@ macro_rules! counts { drop(object); out.push($weak::Weak::strong_count(&weak_object) as u8); } + // One pinned, and one read through an `Option`. + drop($Ptr::pin(Noisy { + id: 0xe0, + log: log.clone(), + })); + out.push( + strong + .first() + .map($Ptr::clone) + .as_deref() + .map_or(0xfd, |noisy| noisy.id), + ); drop(strong); out.extend(weak.iter().map(|w| w.upgrade().is_none() as u8)); out.extend(weak.iter().map(|w| $weak::Weak::weak_count(w) as u8)); drop(weak); out.extend_from_slice(&log.lock().unwrap()); } + + /// Each byte an operation on pools of strong and weak handles of a byte JS copies, + /// the rest of it the handle it picks; each puts what it read and the picked + /// handle's counts. + pub fn $unique(data: &[u8], out: &mut Vec) { + let mut strong: Vec<$Ptr> = vec![$Ptr::new(0)]; + let mut weak: Vec<$weak::Weak> = Vec::new(); + for &op in data { + let pick = usize::from(op >> 3) % strong.len(); + match op % 7 { + 0 => strong.push($Ptr::new(op)), + 1 => { + let again = strong[pick].clone(); + strong.push(again); + } + 2 => weak.push($Ptr::downgrade(&strong[pick])), + 3 if strong.len() > 1 => drop(strong.swap_remove(pick)), + 3 => weak.clear(), + 4 => match $Ptr::get_mut(&mut strong[pick]) { + Some(value) => { + *value = value.wrapping_add(op); + out.push(*value); + } + None => out.push(0xfc), + }, + 5 => { + let value = $Ptr::make_mut(&mut strong[pick]); + *value ^= op; + out.push(*value); + } + _ => { + let pinned = $Ptr::pin(op); + let held = Some($Ptr::clone(&strong[pick])); + out.extend_from_slice(&[*pinned, held.as_deref().copied().unwrap_or(0xfb)]); + } + } + let picked = &strong[pick % strong.len()]; + out.extend_from_slice(&[ + **picked, + $Ptr::strong_count(picked) as u8, + $Ptr::weak_count(picked) as u8, + ]); + } + for w in &weak { + out.push(w.upgrade().map_or(0xfd, |s| *s)); + out.push($weak::Weak::strong_count(w) as u8); + out.push($weak::Weak::weak_count(w) as u8); + } + out.extend(strong.iter().map(|s| **s)); + } }; } -counts!(arcs, ArcHolder, cyclic_arcs, ArcNode, Arc, sync); -counts!(rcs, RcHolder, cyclic_rcs, RcNode, Rc, rc); +counts!(arcs, ArcHolder, cyclic_arcs, ArcNode, unique_arcs, Arc, sync); +counts!(rcs, RcHolder, cyclic_rcs, RcNode, unique_rcs, Rc, rc); diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index 8502eb976..1a673f485 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -100,6 +100,8 @@ fn cases() -> Vec<(&'static str, Case)> { ("counts::rcs", |_, data, out| counts::rcs(data, out)), ("counts::cyclic_arcs", |_, data, out| counts::cyclic_arcs(data, out)), ("counts::cyclic_rcs", |_, data, out| counts::cyclic_rcs(data, out)), + ("counts::unique_arcs", |_, data, out| counts::unique_arcs(data, out)), + ("counts::unique_rcs", |_, data, out| counts::unique_rcs(data, out)), ("dyns::anys", |_, data, out| dyns::anys(data, out)), ("dyns::fns", |_, data, out| dyns::fns(data, out)), ("pointers::copies", |_, data, out| pointers::copies(data, out)), diff --git a/ts/runtime/rt/task.ts b/ts/runtime/rt/task.ts index a68ea6355..fb91e2f05 100644 --- a/ts/runtime/rt/task.ts +++ b/ts/runtime/rt/task.ts @@ -137,6 +137,55 @@ export function arcClone(arc: Arc): Arc { return arc; } +/** `Arc::get_mut`: `arc`'s value, if no other `Arc` or `Weak` shares it. */ +export function arcGetMut(arc: Arc): T | undefined { + return arcUnique(arc) ? arc.value : undefined; +} + +/** `Arc::get_mut` of a value JS copies: where it is, if no other `Arc` or `Weak` shares it. */ +export function arcGetMutPlace(arc: Arc): Place | undefined { + return arcUnique(arc) ? arcPlace(arc) : undefined; +} + +/** + * `Arc::make_mut`: the value of the `Arc` at `p`, made its own. Another `Arc` sharing it + * leaves a clone by `clone`, or the value itself if none, in a new `Arc` at `p`, and only + * `Weak`s sharing it leave them dangling as the value moves to a new `Arc`. + */ +export function arcMakeMut(p: Place>, clone?: (value: T) => T): T { + return arcOwn(p, clone).value; +} + +/** `Arc::make_mut` of a value JS copies: where it is. */ +export function arcMakeMutPlace(p: Place>, clone?: (value: T) => T): Place { + return arcPlace(arcOwn(p, clone)); +} + +function arcUnique(arc: Arc): boolean { + return arc.strong === 1 && arc.weak === 0; +} + +function arcOwn(p: Place>, clone: ((value: T) => T) | undefined): Arc { + const arc = p.v; + if (arc.strong !== 1) { + p.v = new Arc(clone ? clone(arc.value) : arc.value); + arc.strong--; + } else if (arc.weak !== 0) { + arc.strong = 0; + p.v = new Arc(arc.value); + } + return p.v; +} + +function arcPlace(arc: Arc): Place { + return place( + () => arc.value, + (value) => { + arc.value = value; + }, + ); +} + /** What a `Weak` that never upgrades points at, as `Weak::new`'s does: no strong count. */ const DANGLING: Arc = /* @__PURE__ */ dangling(); diff --git a/ts/src/lower/bind.rs b/ts/src/lower/bind.rs index 18836406b..76720685e 100644 --- a/ts/src/lower/bind.rs +++ b/ts/src/lower/bind.rs @@ -309,10 +309,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { if crate::lower::iter::is_next(tcx, def_id) { self.err(span, "iterating a bound type, which is not supported yet"); } - let sig = tcx.instantiate_bound_regions_with_erased( - tcx.fn_sig(def_id).instantiate(tcx, instance.args).skip_norm_wip(), - ); - let sig = tcx.normalize_erasing_regions(self.typing_env_pub(), ty::Unnormalized::new_wip(sig)); + let sig = self.sig(def_id, instance.args); // `=> undefined` for `()`, not `fn_ty`'s `=> void`, which accepts any return value. let params = sig.inputs().iter().map(|t| self.ty(*t, span)).collect(); let ty = Type::Func(params, Box::new(self.ty(sig.output(), span))); diff --git a/ts/src/lower/body.rs b/ts/src/lower/body.rs index 4c74a89bb..d43cecd8e 100644 --- a/ts/src/lower/body.rs +++ b/ts/src/lower/body.rs @@ -124,8 +124,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { let call = Expr::Sym(sym).call(values); // The crate's own `next` returns an item or `rt.DONE`, and its caller an `Option`. if super::iter::is_next(tcx, did) { - let sig = tcx.fn_sig(did).instantiate(tcx, instance.args).skip_norm_wip(); - let option = tcx.instantiate_bound_regions_with_erased(sig).output(); + let option = self.t.sig(did, instance.args).output(); self.t.ty(option, span); let ty::Adt(_, option_args) = option.kind() else { unreachable!("`next` returns an `Option`") @@ -138,10 +137,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { if let Some(referent) = library::referent(tcx, def_id, gargs, instance) { // An operand that was a reference to an `rt.Place`, as `&&mut u32` is, passes // what the Place points at. - let inputs = |gargs| { - let sig = tcx.fn_sig(def_id).instantiate(tcx, gargs).skip_norm_wip(); - tcx.instantiate_bound_regions_with_erased(sig).inputs().to_vec() - }; + let inputs = |gargs| self.t.sig(def_id, gargs).inputs().to_vec(); let args = std::iter::zip(args, std::iter::zip(inputs(gargs), inputs(referent))) .map(|(arg, (was, now))| match was.kind() { ty::Ref(_, inner, _) if was != now => self.t.pointee_value(*inner, arg), @@ -238,7 +234,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { pub fn fn_value(&mut self, def_id: rustc_hir::def_id::DefId, gargs: GenericArgsRef<'tcx>, span: Span) -> Expr { let tcx = self.t.tcx; let gargs = self.subst(gargs); - let sig = tcx.instantiate_bound_regions_with_erased(tcx.fn_sig(def_id).instantiate(tcx, gargs).skip_norm_wip()); + let sig = self.t.sig(def_id, gargs); let names: Vec = (0..sig.inputs().len()).map(|i| self.fresh(&format!("a{i}"))).collect(); let values: Vec = names.iter().map(Expr::ident).collect(); // A binding's export, or a function whose body translates, is named directly. diff --git a/ts/src/lower/dynamic.rs b/ts/src/lower/dynamic.rs index c69a95e3c..b4ca756cb 100644 --- a/ts/src/lower/dynamic.rs +++ b/ts/src/lower/dynamic.rs @@ -162,8 +162,7 @@ impl<'tcx> Translator<'_, 'tcx> { let mut members = Vec::new(); for method in &object.methods { let args = method_args(tcx, method.def_id, ty); - let sig = tcx.fn_sig(method.def_id).instantiate(tcx, args).skip_norm_wip(); - let sig = tcx.instantiate_bound_regions_with_erased(sig); + let sig = self.sig(method.def_id, args); let func = self.fn_ty(&sig.inputs()[1..], sig.output(), span); members.push((method.name.clone(), func)); } @@ -247,8 +246,7 @@ impl<'tcx> Translator<'_, 'tcx> { let value = Expr::This.field(v); for method in &object.methods { let args = method_args(tcx, method.def_id, from); - let sig = tcx.fn_sig(method.def_id).instantiate(tcx, args).skip_norm_wip(); - let sig = tcx.instantiate_bound_regions_with_erased(sig); + let sig = self.sig(method.def_id, args); let receiver = match self.receiver(sig.inputs()[0], from, held, value.clone()) { Some(receiver) => receiver, None => { @@ -501,8 +499,7 @@ pub fn call<'tcx>( ) { return Err(args); } - let sig = tcx.fn_sig(call.def_id).instantiate(tcx, call.args).skip_norm_wip(); - let sig = tcx.instantiate_bound_regions_with_erased(sig); + let sig = b.t.sig(call.def_id, call.args); let receiver = sig.inputs()[0]; let object = match receiver.kind() { ty::Adt(_, args) => args.type_at(0), diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index fb266568c..520df1f80 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -494,11 +494,7 @@ pub fn call<'tcx>( "zip" => { let other_ty = targ(1)?; let other = into_iter(b, other_ty, rest.into_iter().next()?, span)?; - let sig = tcx.fn_sig(call.def_id).instantiate(tcx, gargs).skip_norm_wip(); - let zip = b.t.subst( - ty::List::empty(), - tcx.instantiate_bound_regions_with_erased(sig).output(), - ); + let zip = b.t.sig(call.def_id, gargs).output(); let ty::Adt(_, sides) = zip.kind() else { return None; }; diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 9fd70328c..4bfc1eede 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -817,20 +817,10 @@ fn dropping_operand<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>) -> Opti let tcx = b.t.tcx; let generic = tcx.fn_sig(call.def_id).instantiate_identity().skip_norm_wip(); let generic = tcx.instantiate_bound_regions_with_erased(generic).inputs().to_vec(); - let sig = tcx.fn_sig(call.def_id).instantiate(tcx, call.args).skip_norm_wip(); - let env = b.t.typing_env_pub(); - // A census's call over type parameters may leave a projection unnormalized. - let inputs: Vec> = tcx - .instantiate_bound_regions_with_erased(sig) + b.t.sig(call.def_id, call.args) .inputs() .iter() - .map(|&ty| { - tcx.try_normalize_erasing_regions(env, ty::Unnormalized::new_wip(ty)) - .unwrap_or(ty) - }) - .collect(); - inputs - .into_iter() + .copied() .enumerate() .find(|&(i, ty)| { let called = b.t.owned_fn(ty).is_some() && called_by(tcx, call.def_id, generic[i]).is_some(); @@ -1139,8 +1129,7 @@ fn mapping<'tcx>( ("Option", "is_none") => args.pop()?.binary("===", b.t.none(targ(gargs, 0))), ("Option", "as_ref" | "as_mut") => args.pop()?, ("Option", "copied" | "cloned") => { - let sig = tcx.fn_sig(call.def_id).instantiate(tcx, call.args).skip_norm_wip(); - let option = tcx.instantiate_bound_regions_with_erased(sig).output(); + let option = b.t.sig(call.def_id, call.args).output(); let value = args.pop()?; b.t.copied(value, option, span) } @@ -1189,8 +1178,7 @@ fn mapping<'tcx>( // `Bound<&T>::cloned::`: what it holds, cloned. ("Bound", "cloned") => { let value = args.pop()?; - let sig = tcx.fn_sig(call.def_id).instantiate(tcx, gargs).skip_norm_wip(); - let bound = tcx.instantiate_bound_regions_with_erased(sig).output(); + let bound = b.t.sig(call.def_id, gargs).output(); match super::collections::clone_fn(b, targ(gargs, 0), span) { Expr::Lit(l) if l == "undefined" => b.t.copied(value, bound, span), clone => Expr::rt("boundMap", vec![value, clone]), @@ -1200,8 +1188,7 @@ fn mapping<'tcx>( ("RangeBounds", "start_bound" | "end_bound") => { let start = name == "start_bound"; let range = targ(gargs, 0).peel_refs(); - let sig = tcx.fn_sig(call.def_id).instantiate(tcx, gargs).skip_norm_wip(); - let ts = b.t.ty(tcx.instantiate_bound_regions_with_erased(sig).output(), span); + let ts = b.t.ty(b.t.sig(call.def_id, gargs).output(), span); let r = args.pop()?; let (variant, field) = match (range.kind(), b.t.std_of(range)) { // `(Bound, Bound)`, whose bounds are its own. @@ -1502,8 +1489,7 @@ fn mapping<'tcx>( ("Cow", "into_owned") if targ(gargs, 0).is_str() => args.pop()?, ("AsRef", "as_ref") if string(tcx, targ(gargs, 0)) && targ(gargs, 1).is_str() => args.pop()?, ("Cow", "is_borrowed" | "is_owned") => { - let sig = tcx.fn_sig(call.def_id).instantiate(tcx, gargs).skip_norm_wip(); - let cow = tcx.instantiate_bound_regions_with_erased(sig).inputs()[0].peel_refs(); + let cow = b.t.sig(call.def_id, gargs).inputs()[0].peel_refs(); return Some(b.err(span, cow_variant(cow))); } ("ToString", "to_string") | ("ToOwned", "to_owned") if matches!(self_ty()?.peel_refs().kind(), ty::Str) => { diff --git a/ts/src/lower/mir/call.rs b/ts/src/lower/mir/call.rs index ab0f6fb52..542d2961f 100644 --- a/ts/src/lower/mir/call.rs +++ b/ts/src/lower/mir/call.rs @@ -352,12 +352,13 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { } let t = &*self.b.t; // Mappings that lend an element JS copies as its place, as a map's `get_mut` does, the - // place a `&mut dyn Any` wraps, or a `RefCell` key's value. + // place a `&mut dyn Any` wraps, a `RefCell` key's value, or an `Arc`'s. let lends = match library::key(tcx, def_id) { Some((owner, name)) => match (owner.as_str(), name.as_str()) { ("slice", "get_mut" | "first_mut" | "last_mut" | "iter_mut") | ("dyn Any", "downcast_mut") - | ("LocalKey", "with_borrow_mut") => true, + | ("LocalKey", "with_borrow_mut") + | ("Arc" | "Rc", "get_mut" | "make_mut") => true, ("IntoIterator", "into_iter") => gargs.types().next().is_some_and(|ty| { ty.ref_mutability() == Some(ty::Mutability::Mut) && match ty.peel_refs().kind() { @@ -374,14 +375,9 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { }; // Places held in what it is given, not one it is given, or that the iterator it adapts // yields, were made where they came from, as are what it hands a closure of them. - let sig = tcx.fn_sig(def_id).instantiate(tcx, gargs).skip_norm_wip(); - let adapts = tcx - .instantiate_bound_regions_with_erased(sig) - .inputs() - .iter() - .any(|&i| { - t.place_pointee(i).is_none() && t.has_place(i) || t.iter_item(i).is_some_and(|item| t.has_place(item)) - }); + let adapts = t.sig(def_id, gargs).inputs().iter().any(|&i| { + t.place_pointee(i).is_none() && t.has_place(i) || t.iter_item(i).is_some_and(|item| t.has_place(item)) + }); if lends || adapts { return None; } diff --git a/ts/src/lower/mod.rs b/ts/src/lower/mod.rs index 8c1227cf0..914e0164b 100644 --- a/ts/src/lower/mod.rs +++ b/ts/src/lower/mod.rs @@ -869,6 +869,16 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { .instantiate_and_normalize_erasing_regions(args, self.typing_env(), EarlyBinder::bind(self.tcx, value)) } + /// `def_id`'s signature for `args`, with what its projections name, as `Deref::deref`'s + /// `&Self::Target`, where they resolve: a census's call over type parameters may leave one. + pub fn sig(&self, def_id: DefId, args: GenericArgsRef<'tcx>) -> ty::FnSig<'tcx> { + let sig = self.tcx.fn_sig(def_id).instantiate(self.tcx, args).skip_norm_wip(); + let sig = self.tcx.instantiate_bound_regions_with_erased(sig); + self.tcx + .try_normalize_erasing_regions(self.typing_env(), ty::Unnormalized::new_wip(sig)) + .unwrap_or(sig) + } + /// The standard library type `ty` is, if it is one with its own representation. pub fn std_of(&self, ty: Ty<'tcx>) -> Option { match ty.kind() { @@ -1256,7 +1266,11 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { _ => Type::Array(Box::new(self.ty(*elem, span))), }, ty::Param(param) => Type::Param(param.name.to_string()), - ty::FnDef(..) | ty::FnPtr(..) => { + ty::FnDef(def_id, args) => { + let sig = self.sig(*def_id, args.skip_binder()); + self.fn_ty(sig.inputs(), sig.output(), span) + } + ty::FnPtr(..) => { let sig = tcx.instantiate_bound_regions_with_erased(ty.fn_sig(tcx)); self.fn_ty(sig.inputs(), sig.output(), span) } diff --git a/ts/src/lower/pointer.rs b/ts/src/lower/pointer.rs index ed651a5e6..8b96d9344 100644 --- a/ts/src/lower/pointer.rs +++ b/ts/src/lower/pointer.rs @@ -196,11 +196,14 @@ pub fn keeps(tcx: ty::TyCtxt<'_>, def_id: rustc_hir::def_id::DefId) -> bool { } /// Whether std's `def_id` is one a mapping takes the `rt.Place` a `&mut` is for, rather -/// than what it points at: a `MaybeUninit`'s, which it points into, and a well-known -/// crate's, such as a `Bytes` that `Buf::advance` moves past its front. +/// than what it points at: a `MaybeUninit`'s, which it points into, an `Arc`'s that +/// `make_mut` replaces, and a well-known crate's, such as a `Bytes` that `Buf::advance` +/// moves past its front. pub fn takes_place(tcx: ty::TyCtxt<'_>, def_id: rustc_hir::def_id::DefId) -> bool { - matches!(super::library::key(tcx, def_id), Some((owner, _)) if owner == "MaybeUninit") - || super::stock::is_stock(tcx, def_id) + matches!( + super::library::key(tcx, def_id), + Some((owner, name)) if owner == "MaybeUninit" || matches!(owner.as_str(), "Arc" | "Rc") && name == "make_mut" + ) || super::stock::is_stock(tcx, def_id) } /// A call of std's raw pointer, `NonNull`, `MaybeUninit`, or `Box` functions that make or diff --git a/ts/src/lower/stock.rs b/ts/src/lower/stock.rs index b64c547e2..fd2a6ceb8 100644 --- a/ts/src/lower/stock.rs +++ b/ts/src/lower/stock.rs @@ -456,7 +456,7 @@ fn buf<'tcx>( "copy_to_bytes" => rt("SplitTo", vec![this, args.next()?])?, _ => { let (ty, le) = scalar(name.strip_prefix("get_")?); - let out = signature(b.t.tcx, call).output(); + let out = b.t.sig(call.def_id, call.args).output(); let le = Expr::lit(le.to_string()); match ty { "uint" | "int" => { @@ -512,7 +512,7 @@ fn buf_mut<'tcx>( "put_bytes" => rt("PutFill", vec![this, args.next()?, args.next()?]), _ => { let (ty, le) = scalar(name.strip_prefix("put_")?); - let input = *signature(b.t.tcx, call).inputs().get(1)?; + let input = *b.t.sig(call.def_id, call.args).inputs().get(1)?; let value = args.next()?; let le = Expr::lit(le.to_string()); match ty { @@ -601,12 +601,6 @@ fn scalar(name: &str) -> (&str, bool) { } } -/// The signature of the function `call` names, with its arguments. -fn signature<'tcx>(tcx: TyCtxt<'tcx>, call: &Call<'tcx>) -> ty::FnSig<'tcx> { - let sig = tcx.fn_sig(call.def_id).instantiate(tcx, call.args).skip_norm_wip(); - tcx.instantiate_bound_regions_with_erased(sig) -} - /// A function of `rand`: `rng`, or a method of its `Rng` or `RngExt` on a `ThreadRng`. fn rand<'tcx>( b: &mut Body<'_, '_, 'tcx>, diff --git a/ts/src/lower/task.rs b/ts/src/lower/task.rs index 265dbc08c..42da7af18 100644 --- a/ts/src/lower/task.rs +++ b/ts/src/lower/task.rs @@ -132,6 +132,21 @@ pub fn call<'tcx>( let e = match (owner, name) { ("Arc" | "Rc", "new") => Expr::New(Box::new(Expr::Rt("Arc".into())), args), ("Arc" | "Rc", "new_cyclic") => Expr::rt("arcNewCyclic", args), + // JS erases the pin. + ("Arc" | "Rc", "pin") => Expr::New(Box::new(Expr::Rt("Arc".into())), args), + // A `&mut` to the value, which is where it is for one JS copies. `make_mut` takes the + // `Arc`'s place, which it may point at a new `Arc`. + ("Arc" | "Rc", "get_mut" | "make_mut") if call.args.type_at(0).is_sized(tcx, b.t.typing_env_pub()) => { + let value = call.args.type_at(0); + let place = if b.t.needs_place(value) { "Place" } else { "" }; + match name { + "get_mut" => Expr::rt(format!("arcGetMut{place}"), args), + _ => { + let clone = super::collections::clone_fn(b, value, call.span); + Expr::rt(format!("arcMakeMut{place}"), args.into_iter().chain([clone]).collect()) + } + } + } // An `Arc` of a string, which JS shares as it is. ("From", "from") if let [arc, from] = call.args.as_slice() From 34732656c437b6b116627e9805b9a5a7d81c96d6 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 29 Oct 2026 15:01:11 -0700 Subject: [PATCH 3/4] fix(ts): drop the Arc make_mut replaces, and pass a function value's &mut as std's mappings take it Co-Authored-By: Claude Opus 5.5 --- .../lang/expected/counts.ArcReleasing.ts | 41 ++++ .../lang/expected/counts.RcReleasing.ts | 41 ++++ ts/fixtures/lang/expected/counts.ts | 215 +++++++++++++----- ts/fixtures/lang/expected/lib.ts | 10 + ts/fixtures/lang/src/counts.rs | 73 +++++- ts/runtime/rt/task.ts | 21 +- ts/src/lower/body.rs | 12 +- ts/src/lower/task.rs | 7 +- 8 files changed, 350 insertions(+), 70 deletions(-) create mode 100644 ts/fixtures/lang/expected/counts.ArcReleasing.ts create mode 100644 ts/fixtures/lang/expected/counts.RcReleasing.ts diff --git a/ts/fixtures/lang/expected/counts.ArcReleasing.ts b/ts/fixtures/lang/expected/counts.ArcReleasing.ts new file mode 100644 index 000000000..a24880058 --- /dev/null +++ b/ts/fixtures/lang/expected/counts.ArcReleasing.ts @@ -0,0 +1,41 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import { drop$RefCell_Vec_Arc_ArcReleasing } from "./lib.ts"; + +/** + * Releases the other handles to it when cloned, as its id says it was, and logs its + * id when it drops. + */ +export class ArcReleasing { + id: rt.u8; + others: rt.Arc[]>>; + log: rt.Arc>; + + constructor(id: rt.u8, others: rt.Arc[]>>, log: rt.Arc>) { + this.id = id; + this.others = others; + this.log = log; + } + + [Symbol.dispose](): void { + drop$Drop(this); + rt.arcDrop(this.others, drop$RefCell_Vec_Arc_ArcReleasing); + this.log[Symbol.dispose](); + } +} + +export function clone$Clone(self: ArcReleasing): ArcReleasing { + const _5: rt.RefCell[]> = rt.refCellBorrowMut(self.others.value); + rt.vecTruncate(_5.value, 0, rt.drop); + rt.refMutRelease(_5); + return new ArcReleasing(rt.u8WrappingAdd(self.id, 1), rt.arcClone(self.others), rt.arcClone(self.log)); +} + +export function drop$Drop(self: ArcReleasing): void { + const _5: rt.RefCell = rt.refCellBorrowMut(self.log.value); + rt.byteVecPush(_5.value, self.id); + rt.refMutRelease(_5); +} + +//# sourceMappingURL=counts.ArcReleasing.ts.map diff --git a/ts/fixtures/lang/expected/counts.RcReleasing.ts b/ts/fixtures/lang/expected/counts.RcReleasing.ts new file mode 100644 index 000000000..1903a446c --- /dev/null +++ b/ts/fixtures/lang/expected/counts.RcReleasing.ts @@ -0,0 +1,41 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; +import { drop$RefCell_Vec_Rc_RcReleasing } from "./lib.ts"; + +/** + * Releases the other handles to it when cloned, as its id says it was, and logs its + * id when it drops. + */ +export class RcReleasing { + id: rt.u8; + others: rt.Arc[]>>; + log: rt.Arc>; + + constructor(id: rt.u8, others: rt.Arc[]>>, log: rt.Arc>) { + this.id = id; + this.others = others; + this.log = log; + } + + [Symbol.dispose](): void { + drop$Drop(this); + rt.arcDrop(this.others, drop$RefCell_Vec_Rc_RcReleasing); + this.log[Symbol.dispose](); + } +} + +export function clone$Clone(self: RcReleasing): RcReleasing { + const _5: rt.RefCell[]> = rt.refCellBorrowMut(self.others.value); + rt.vecTruncate(_5.value, 0, rt.drop); + rt.refMutRelease(_5); + return new RcReleasing(rt.u8WrappingAdd(self.id, 1), rt.arcClone(self.others), rt.arcClone(self.log)); +} + +export function drop$Drop(self: RcReleasing): void { + const _5: rt.RefCell = rt.refCellBorrowMut(self.log.value); + rt.byteVecPush(_5.value, self.id); + rt.refMutRelease(_5); +} + +//# sourceMappingURL=counts.RcReleasing.ts.map diff --git a/ts/fixtures/lang/expected/counts.ts b/ts/fixtures/lang/expected/counts.ts index 9f1f34470..08d186c5e 100644 --- a/ts/fixtures/lang/expected/counts.ts +++ b/ts/fixtures/lang/expected/counts.ts @@ -1,13 +1,16 @@ // Generated by rust2-ts. Do not edit. import * as rt from "./rt.ts"; -import { unwrapOr$Option_u8, map$Option_Arc_Noisy_Arc_Noisy_clone, mapOr$Option_Arc_ArcNode_u8_cyclic_arcs_closure4, mapOr$Option_Arc_Noisy_u8_arcs_closure0_closure0, mapOr$Option_Arc_dyn_Id_u8_arcs_closure1, mapOr$Option_Arc_u8_u8_objects_closure1, mapOr$Option_Noisy_u8_arcs_closure2, mapOr$Option_Rc_RcNode_u8_cyclic_rcs_closure4, asDeref$Option_Arc_Noisy, asDeref$Option_Arc_u8 } from "./core/option.ts"; +import { drop$RefCell_Vec_Arc_ArcReleasing, drop$RefCell_Vec_Rc_RcReleasing } from "./lib.ts"; +import { unwrapOr$Option_u8, map$Option_Arc_Noisy_Arc_Noisy_clone, mapOr$Option_Arc_ArcNode_u8_cyclic_arcs_closure4, mapOr$Option_Arc_Noisy_u8_arcs_closure0_closure0, mapOr$Option_Arc_dyn_Id_u8_arcs_closure1, mapOr$Option_Arc_u8_u8_objects_closure1, mapOr$Option_Noisy_u8_arcs_closure2, mapOr$Option_Rc_RcNode_u8_cyclic_rcs_closure4, mapOr$Option_u8_u8_btree_maps_closure5, asDeref$Option_Arc_Noisy, asDeref$Option_Arc_u8 } from "./core/option.ts"; import * as ArcHolder from "./counts.ArcHolder.ts"; import * as ArcNode from "./counts.ArcNode.ts"; +import * as ArcReleasing from "./counts.ArcReleasing.ts"; import * as Id from "./counts.Id.ts"; import * as Noisy from "./counts.Noisy.ts"; import * as RcHolder from "./counts.RcHolder.ts"; import * as RcNode from "./counts.RcNode.ts"; +import * as RcReleasing from "./counts.RcReleasing.ts"; /** Puts what dropped since the last flush. */ export function flush(log: rt.Mutex, out: rt.ByteVec): void { @@ -117,7 +120,7 @@ export function arcs(data: rt.ByteSlice, out: rt.ByteVec): void { } else if (_19 === 10) { if (!((strong.length === 0))) { const at = rt.idx(strong, rt.usizeRem(pick, strong.length)); - const noisy: Noisy.Noisy = rt.arcMakeMut(rt.place(() => strong[at], (value) => strong[at] = value), Noisy.clone$Clone); + const noisy: Noisy.Noisy = rt.arcMakeMut(rt.place(() => strong[at], (value) => strong[at] = value), Noisy.clone$Clone, rt.drop); noisy.id = noisy.id ^ 32; rt.byteVecPush(out, noisy.id); rt.byteVecPush(out, strong[at].strong & 0xff); @@ -279,39 +282,65 @@ export function cyclicArcs(data: rt.ByteSlice, out: rt.ByteVec): void { * handle's counts. */ export function uniqueArcs(data: rt.ByteSlice, out: rt.ByteVec): void { + let _43!: rt.Place | undefined; + let value2!: rt.Place; const strong: rt.Arc[] = [new rt.Arc(0)]; const weak: rt.Weak[] = []; - for (const op of rt.byteSliceView(data)) { + const iterS: Uint8Array = rt.byteSliceView(data); + let iterI: number = 0; + while (true) { + const _11: rt.u8 | rt.Done = iterI < iterS.length ? iterS[iterI++] : rt.DONE; + const getMut: (a0: rt.Place>) => rt.Place | undefined = (a0: rt.Place>): rt.Place | undefined => { + return rt.arcGetMutPlace(a0.v); + }; + const makeMut: (a0: rt.Place>) => rt.Place = (a02: rt.Place>): rt.Place => { + return rt.arcMakeMutPlace(a02, undefined, rt.drop); + }; + if (_11 === rt.DONE) { + break; + } + const op: rt.u8 = _11; const pick: rt.usize = rt.usizeRem(op >> 3 as rt.usize, strong.length); - const _22: rt.u8 = rt.u8Rem(op, 7); - if (_22 === 0) { - const _25: rt.Arc = new rt.Arc(op); - strong.push(_25); - } else if (_22 === 1) { + const _24: rt.u8 = rt.u8Rem(op, 7); + if (_24 === 0) { + const _27: rt.Arc = new rt.Arc(op); + strong.push(_27); + } else if (_24 === 1) { const again: rt.Arc = rt.arcClone(strong[rt.idx(strong, pick)]); strong.push(again); - } else if (_22 === 2) { - const _32: rt.Weak = new rt.Weak(strong[rt.idx(strong, pick)]); - weak.push(_32); - } else if (_22 === 3) { + } else if (_24 === 2) { + const _34: rt.Weak = new rt.Weak(strong[rt.idx(strong, pick)]); + weak.push(_34); + } else if (_24 === 3) { if (!(strong.length > 1)) { rt.vecTruncate(weak, 0, rt.drop); } else { rt.vecSwapRemove(strong, pick)[Symbol.dispose](); } - } else if (_22 === 4) { - const _41: rt.Place | undefined = rt.arcGetMutPlace(strong[rt.idx(strong, pick)]); - if (_41 === undefined) { + } else if (_24 === 4) { + if (!((op & 128) === 0)) { + const at5 = rt.idx(strong, pick); + _43 = getMut(rt.place(() => strong[at5], (value5) => strong[at5] = value5)); + } else { + _43 = rt.arcGetMutPlace(strong[rt.idx(strong, pick)]); + } + if (_43 === undefined) { rt.byteVecPush(out, 252); } else { - _41.v = rt.u8WrappingAdd(_41.v, op); - rt.byteVecPush(out, _41.v); + const value: rt.Place = _43; + value.v = rt.u8WrappingAdd(value.v, op); + rt.byteVecPush(out, value.v); } - } else if (_22 === 5) { - const at2 = rt.idx(strong, pick); - const value: rt.Place = rt.arcMakeMutPlace(rt.place(() => strong[at2], (value2) => strong[at2] = value2), undefined); - value.v = value.v ^ op; - rt.byteVecPush(out, value.v); + } else if (_24 === 5) { + if (!((op & 128) === 0)) { + const at3 = rt.idx(strong, pick); + value2 = makeMut(rt.place(() => strong[at3], (value4) => strong[at3] = value4)); + } else { + const at2 = rt.idx(strong, pick); + value2 = rt.arcMakeMutPlace(rt.place(() => strong[at2], (value3) => strong[at2] = value3), undefined, rt.drop); + } + value2.v = value2.v ^ op; + rt.byteVecPush(out, value2.v); } else { const pinned: rt.Arc = new rt.Arc(op); const held: rt.Arc | undefined = rt.arcClone(strong[rt.idx(strong, pick)]); @@ -319,21 +348,46 @@ export function uniqueArcs(data: rt.ByteSlice, out: rt.ByteVec): void { rt.drop(held); pinned[Symbol.dispose](); } - const at6 = rt.idx(strong, rt.usizeRem(pick, strong.length)); - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(strong[at6].value, strong[at6].strong & 0xff, strong[at6].weak & 0xff))); + const at8 = rt.idx(strong, rt.usizeRem(pick, strong.length)); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(strong[at8].value, strong[at8].strong & 0xff, strong[at8].weak & 0xff))); } - for (const _90 of weak) { - rt.byteVecPush(out, mapOr$Option_Arc_u8_u8_objects_closure1(rt.weakUpgrade(_90), 253, (s: rt.Arc): rt.u8 => { + for (const _101 of weak) { + rt.byteVecPush(out, mapOr$Option_Arc_u8_u8_objects_closure1(rt.weakUpgrade(_101), 253, (s: rt.Arc): rt.u8 => { const _0: rt.u8 = s.value; s[Symbol.dispose](); return _0; })); - rt.byteVecPush(out, _90.arc.strong & 0xff); - rt.byteVecPush(out, rt.weakWeakCount(_90) & 0xff); + rt.byteVecPush(out, _101.arc.strong & 0xff); + rt.byteVecPush(out, rt.weakWeakCount(_101) & 0xff); } rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(strong), (s2: rt.Arc): rt.u8 => { return s2.value; })); + const _121: rt.Arc[] = []; + const others: rt.Arc[]>> = new rt.Arc(new rt.RefCell(_121)); + const log: rt.Arc> = new rt.Arc(new rt.RefCell(new rt.ByteVec())); + let first: rt.Arc = new rt.Arc(new ArcReleasing.ArcReleasing(data.length & 0xff, rt.arcClone(others), rt.arcClone(log))); + const shared: rt.u8 = mapOr$Option_u8_u8_btree_maps_closure5(rt.byteSliceFirst(data), 1, (b: rt.u8): rt.u8 => { + return rt.u8Add(rt.u8Rem(b, 3), 1); + }); + const _138: rt.RefCell[]> = rt.refCellBorrowMut(others.value); + const _141: rt.Iter> = rt.iterMap(new rt.Range(0, shared), (_2: rt.u8): rt.Arc => { + return rt.arcClone(first); + }); + rt.iterExtend(_138.value, _141); + rt.refMutRelease(_138); + const _145: ArcReleasing.ArcReleasing = rt.arcMakeMut(rt.place(() => first, (value6) => first = value6), ArcReleasing.clone$Clone, rt.drop); + _145.id = _145.id ^ 64; + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(first.value.id, first.strong & 0xff))); + const _161: rt.RefCell = rt.refCellBorrow(log.value); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(_161.value)); + rt.refRelease(_161); + first[Symbol.dispose](); + const _170: rt.RefCell = rt.refCellBorrow(log.value); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(_170.value)); + rt.refRelease(_170); + log[Symbol.dispose](); + rt.arcDrop(others, drop$RefCell_Vec_Arc_ArcReleasing); rt.dropAll(weak); rt.dropAll(strong); } @@ -529,7 +583,7 @@ export function rcs(data: rt.ByteSlice, out: rt.ByteVec): void { } else if (_19 === 10) { if (!((strong.length === 0))) { const at = rt.idx(strong, rt.usizeRem(pick, strong.length)); - const noisy: Noisy.Noisy = rt.arcMakeMut(rt.place(() => strong[at], (value) => strong[at] = value), Noisy.clone$Clone); + const noisy: Noisy.Noisy = rt.arcMakeMut(rt.place(() => strong[at], (value) => strong[at] = value), Noisy.clone$Clone, rt.drop); noisy.id = noisy.id ^ 32; rt.byteVecPush(out, noisy.id); rt.byteVecPush(out, strong[at].strong & 0xff); @@ -600,39 +654,65 @@ export function rcs(data: rt.ByteSlice, out: rt.ByteVec): void { * handle's counts. */ export function uniqueRcs(data: rt.ByteSlice, out: rt.ByteVec): void { + let _43!: rt.Place | undefined; + let value2!: rt.Place; const strong: rt.Arc[] = [new rt.Arc(0)]; const weak: rt.Weak[] = []; - for (const op of rt.byteSliceView(data)) { + const iterS: Uint8Array = rt.byteSliceView(data); + let iterI: number = 0; + while (true) { + const _11: rt.u8 | rt.Done = iterI < iterS.length ? iterS[iterI++] : rt.DONE; + const getMut: (a0: rt.Place>) => rt.Place | undefined = (a0: rt.Place>): rt.Place | undefined => { + return rt.arcGetMutPlace(a0.v); + }; + const makeMut: (a0: rt.Place>) => rt.Place = (a02: rt.Place>): rt.Place => { + return rt.arcMakeMutPlace(a02, undefined, rt.drop); + }; + if (_11 === rt.DONE) { + break; + } + const op: rt.u8 = _11; const pick: rt.usize = rt.usizeRem(op >> 3 as rt.usize, strong.length); - const _22: rt.u8 = rt.u8Rem(op, 7); - if (_22 === 0) { - const _25: rt.Arc = new rt.Arc(op); - strong.push(_25); - } else if (_22 === 1) { + const _24: rt.u8 = rt.u8Rem(op, 7); + if (_24 === 0) { + const _27: rt.Arc = new rt.Arc(op); + strong.push(_27); + } else if (_24 === 1) { const again: rt.Arc = rt.arcClone(strong[rt.idx(strong, pick)]); strong.push(again); - } else if (_22 === 2) { - const _32: rt.Weak = new rt.Weak(strong[rt.idx(strong, pick)]); - weak.push(_32); - } else if (_22 === 3) { + } else if (_24 === 2) { + const _34: rt.Weak = new rt.Weak(strong[rt.idx(strong, pick)]); + weak.push(_34); + } else if (_24 === 3) { if (!(strong.length > 1)) { rt.vecTruncate(weak, 0, rt.drop); } else { rt.vecSwapRemove(strong, pick)[Symbol.dispose](); } - } else if (_22 === 4) { - const _41: rt.Place | undefined = rt.arcGetMutPlace(strong[rt.idx(strong, pick)]); - if (_41 === undefined) { + } else if (_24 === 4) { + if (!((op & 128) === 0)) { + const at5 = rt.idx(strong, pick); + _43 = getMut(rt.place(() => strong[at5], (value5) => strong[at5] = value5)); + } else { + _43 = rt.arcGetMutPlace(strong[rt.idx(strong, pick)]); + } + if (_43 === undefined) { rt.byteVecPush(out, 252); } else { - _41.v = rt.u8WrappingAdd(_41.v, op); - rt.byteVecPush(out, _41.v); + const value: rt.Place = _43; + value.v = rt.u8WrappingAdd(value.v, op); + rt.byteVecPush(out, value.v); } - } else if (_22 === 5) { - const at2 = rt.idx(strong, pick); - const value: rt.Place = rt.arcMakeMutPlace(rt.place(() => strong[at2], (value2) => strong[at2] = value2), undefined); - value.v = value.v ^ op; - rt.byteVecPush(out, value.v); + } else if (_24 === 5) { + if (!((op & 128) === 0)) { + const at3 = rt.idx(strong, pick); + value2 = makeMut(rt.place(() => strong[at3], (value4) => strong[at3] = value4)); + } else { + const at2 = rt.idx(strong, pick); + value2 = rt.arcMakeMutPlace(rt.place(() => strong[at2], (value3) => strong[at2] = value3), undefined, rt.drop); + } + value2.v = value2.v ^ op; + rt.byteVecPush(out, value2.v); } else { const pinned: rt.Arc = new rt.Arc(op); const held: rt.Arc | undefined = rt.arcClone(strong[rt.idx(strong, pick)]); @@ -640,21 +720,46 @@ export function uniqueRcs(data: rt.ByteSlice, out: rt.ByteVec): void { rt.drop(held); pinned[Symbol.dispose](); } - const at6 = rt.idx(strong, rt.usizeRem(pick, strong.length)); - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(strong[at6].value, strong[at6].strong & 0xff, strong[at6].weak & 0xff))); + const at8 = rt.idx(strong, rt.usizeRem(pick, strong.length)); + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(strong[at8].value, strong[at8].strong & 0xff, strong[at8].weak & 0xff))); } - for (const _90 of weak) { - rt.byteVecPush(out, mapOr$Option_Arc_u8_u8_objects_closure1(rt.weakUpgrade(_90), 253, (s: rt.Arc): rt.u8 => { + for (const _101 of weak) { + rt.byteVecPush(out, mapOr$Option_Arc_u8_u8_objects_closure1(rt.weakUpgrade(_101), 253, (s: rt.Arc): rt.u8 => { const _0: rt.u8 = s.value; s[Symbol.dispose](); return _0; })); - rt.byteVecPush(out, _90.arc.strong & 0xff); - rt.byteVecPush(out, rt.weakWeakCount(_90) & 0xff); + rt.byteVecPush(out, _101.arc.strong & 0xff); + rt.byteVecPush(out, rt.weakWeakCount(_101) & 0xff); } rt.iterExtendBytes(out, rt.iterMap(rt.iterSlice(strong), (s2: rt.Arc): rt.u8 => { return s2.value; })); + const _121: rt.Arc[] = []; + const others: rt.Arc[]>> = new rt.Arc(new rt.RefCell(_121)); + const log: rt.Arc> = new rt.Arc(new rt.RefCell(new rt.ByteVec())); + let first: rt.Arc = new rt.Arc(new RcReleasing.RcReleasing(data.length & 0xff, rt.arcClone(others), rt.arcClone(log))); + const shared: rt.u8 = mapOr$Option_u8_u8_btree_maps_closure5(rt.byteSliceFirst(data), 1, (b: rt.u8): rt.u8 => { + return rt.u8Add(rt.u8Rem(b, 3), 1); + }); + const _138: rt.RefCell[]> = rt.refCellBorrowMut(others.value); + const _141: rt.Iter> = rt.iterMap(new rt.Range(0, shared), (_2: rt.u8): rt.Arc => { + return rt.arcClone(first); + }); + rt.iterExtend(_138.value, _141); + rt.refMutRelease(_138); + const _145: RcReleasing.RcReleasing = rt.arcMakeMut(rt.place(() => first, (value6) => first = value6), RcReleasing.clone$Clone, rt.drop); + _145.id = _145.id ^ 64; + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(first.value.id, first.strong & 0xff))); + const _161: rt.RefCell = rt.refCellBorrow(log.value); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(_161.value)); + rt.refRelease(_161); + first[Symbol.dispose](); + const _170: rt.RefCell = rt.refCellBorrow(log.value); + rt.byteVecExtendFromSlice(out, rt.byteVecSlice(_170.value)); + rt.refRelease(_170); + log[Symbol.dispose](); + rt.arcDrop(others, drop$RefCell_Vec_Rc_RcReleasing); rt.dropAll(weak); rt.dropAll(strong); } diff --git a/ts/fixtures/lang/expected/lib.ts b/ts/fixtures/lang/expected/lib.ts index 426a07176..31700c35b 100644 --- a/ts/fixtures/lang/expected/lib.ts +++ b/ts/fixtures/lang/expected/lib.ts @@ -3,6 +3,8 @@ import * as rt from "./rt.ts"; import type * as Tally from "./collections.Tally.ts"; import type * as Any from "./core/any.Any.ts"; +import type * as ArcReleasing from "./counts.ArcReleasing.ts"; +import type * as RcReleasing from "./counts.RcReleasing.ts"; import type * as Window from "./durations.Window.ts"; import type * as Pair from "./fmt.Pair.ts"; import type * as Either from "./generics.Either.ts"; @@ -321,6 +323,14 @@ export function drop$List_String(_1: List.List): void { List.drop$Drop_String(_1); } +export function drop$RefCell_Vec_Arc_ArcReleasing(value: rt.RefCell[]>): void { + rt.dropAll(value.value); +} + +export function drop$RefCell_Vec_Rc_RcReleasing(value: rt.RefCell[]>): void { + rt.dropAll(value.value); +} + export function drop$Result_MutexGuard_Tally_TryLockError_MutexGuard_Tally(_1: rt.Result, rt.TryLockError>>): void { if (_1.ok) { rt.mutexUnlock(_1.value); diff --git a/ts/fixtures/lang/src/counts.rs b/ts/fixtures/lang/src/counts.rs index 243220ab2..adaf0bbca 100644 --- a/ts/fixtures/lang/src/counts.rs +++ b/ts/fixtures/lang/src/counts.rs @@ -3,6 +3,7 @@ //! itself, and held in a `Vec`, an `Option`, a struct, and a closure, of a value and of a //! trait object; and the values `get_mut` and `make_mut` write, alone or shared. +use std::cell::RefCell; use std::rc::{self, Rc}; use std::sync::{self, Arc, Mutex}; @@ -47,7 +48,7 @@ impl Id for Noisy { } macro_rules! counts { - ($name:ident, $holder:ident, $cyclic:ident, $node:ident, $unique:ident, $Ptr:ident, $weak:ident) => { + ($name:ident, $holder:ident, $cyclic:ident, $node:ident, $unique:ident, $releasing:ident, $Ptr:ident, $weak:ident) => { /// Holds a handle that doesn't keep its value. struct $holder { weak: $weak::Weak, @@ -256,10 +257,37 @@ macro_rules! counts { out.extend_from_slice(&log.lock().unwrap()); } + /// Releases the other handles to it when cloned, as its id says it was, and logs its + /// id when it drops. + struct $releasing { + id: u8, + others: Rc>>>, + log: Rc>>, + } + + impl Clone for $releasing { + fn clone(&self) -> Self { + self.others.borrow_mut().clear(); + Self { + id: self.id.wrapping_add(1), + others: self.others.clone(), + log: self.log.clone(), + } + } + } + + impl Drop for $releasing { + fn drop(&mut self) { + self.log.borrow_mut().push(self.id); + } + } + /// Each byte an operation on pools of strong and weak handles of a byte JS copies, /// the rest of it the handle it picks; each puts what it read and the picked /// handle's counts. pub fn $unique(data: &[u8], out: &mut Vec) { + let get_mut: fn(&mut $Ptr) -> Option<&mut u8> = $Ptr::get_mut; + let make_mut: fn(&mut $Ptr) -> &mut u8 = $Ptr::make_mut; let mut strong: Vec<$Ptr> = vec![$Ptr::new(0)]; let mut weak: Vec<$weak::Weak> = Vec::new(); for &op in data { @@ -273,7 +301,12 @@ macro_rules! counts { 2 => weak.push($Ptr::downgrade(&strong[pick])), 3 if strong.len() > 1 => drop(strong.swap_remove(pick)), 3 => weak.clear(), - 4 => match $Ptr::get_mut(&mut strong[pick]) { + // Called directly, and through a function pointer. + 4 => match if op & 0x80 == 0 { + $Ptr::get_mut(&mut strong[pick]) + } else { + get_mut(&mut strong[pick]) + } { Some(value) => { *value = value.wrapping_add(op); out.push(*value); @@ -281,7 +314,11 @@ macro_rules! counts { None => out.push(0xfc), }, 5 => { - let value = $Ptr::make_mut(&mut strong[pick]); + let value = if op & 0x80 == 0 { + $Ptr::make_mut(&mut strong[pick]) + } else { + make_mut(&mut strong[pick]) + }; *value ^= op; out.push(*value); } @@ -304,9 +341,35 @@ macro_rules! counts { out.push($weak::Weak::weak_count(w) as u8); } out.extend(strong.iter().map(|s| **s)); + // A clone that releases the other handles leaves `make_mut` the last, which drops. + let others = Rc::new(RefCell::new(Vec::new())); + let log = Rc::new(RefCell::new(Vec::new())); + let mut first = $Ptr::new($releasing { + id: data.len() as u8, + others: others.clone(), + log: log.clone(), + }); + let shared = data.first().map_or(1, |b| b % 3 + 1); + others + .borrow_mut() + .extend((0..shared).map(|_| $Ptr::clone(&first))); + $Ptr::make_mut(&mut first).id ^= 0x40; + out.extend_from_slice(&[first.id, $Ptr::strong_count(&first) as u8]); + out.extend_from_slice(&log.borrow()); + drop(first); + out.extend_from_slice(&log.borrow()); } }; } -counts!(arcs, ArcHolder, cyclic_arcs, ArcNode, unique_arcs, Arc, sync); -counts!(rcs, RcHolder, cyclic_rcs, RcNode, unique_rcs, Rc, rc); +counts!( + arcs, + ArcHolder, + cyclic_arcs, + ArcNode, + unique_arcs, + ArcReleasing, + Arc, + sync +); +counts!(rcs, RcHolder, cyclic_rcs, RcNode, unique_rcs, RcReleasing, Rc, rc); diff --git a/ts/runtime/rt/task.ts b/ts/runtime/rt/task.ts index fb91e2f05..fd2d8c046 100644 --- a/ts/runtime/rt/task.ts +++ b/ts/runtime/rt/task.ts @@ -149,27 +149,32 @@ export function arcGetMutPlace(arc: Arc): Place | undefined { /** * `Arc::make_mut`: the value of the `Arc` at `p`, made its own. Another `Arc` sharing it - * leaves a clone by `clone`, or the value itself if none, in a new `Arc` at `p`, and only - * `Weak`s sharing it leave them dangling as the value moves to a new `Arc`. + * leaves a clone by `clone`, or the value itself if none, in a new `Arc` at `p`, and drops + * the old one by `drop`, which is the last if the clone released the others. Only `Weak`s + * sharing it leave them dangling as the value moves to a new `Arc`. */ -export function arcMakeMut(p: Place>, clone?: (value: T) => T): T { - return arcOwn(p, clone).value; +export function arcMakeMut(p: Place>, clone: ((value: T) => T) | undefined, drop: (arc: Arc) => void): T { + return arcOwn(p, clone, drop).value; } /** `Arc::make_mut` of a value JS copies: where it is. */ -export function arcMakeMutPlace(p: Place>, clone?: (value: T) => T): Place { - return arcPlace(arcOwn(p, clone)); +export function arcMakeMutPlace( + p: Place>, + clone: ((value: T) => T) | undefined, + drop: (arc: Arc) => void, +): Place { + return arcPlace(arcOwn(p, clone, drop)); } function arcUnique(arc: Arc): boolean { return arc.strong === 1 && arc.weak === 0; } -function arcOwn(p: Place>, clone: ((value: T) => T) | undefined): Arc { +function arcOwn(p: Place>, clone: ((value: T) => T) | undefined, drop: (arc: Arc) => void): Arc { const arc = p.v; if (arc.strong !== 1) { p.v = new Arc(clone ? clone(arc.value) : arc.value); - arc.strong--; + drop(arc); } else if (arc.weak !== 0) { arc.strong = 0; p.v = new Arc(arc.value); diff --git a/ts/src/lower/body.rs b/ts/src/lower/body.rs index d43cecd8e..908289d8e 100644 --- a/ts/src/lower/body.rs +++ b/ts/src/lower/body.rs @@ -236,7 +236,17 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { let gargs = self.subst(gargs); let sig = self.t.sig(def_id, gargs); let names: Vec = (0..sig.inputs().len()).map(|i| self.fresh(&format!("a{i}"))).collect(); - let values: Vec = names.iter().map(Expr::ident).collect(); + // std's mappings take a `&mut` that is an `rt.Place` as what it points at, as a call's + // operands pass it. + let pointee = self.t.mapped(def_id, gargs) && !super::pointer::takes_place(tcx, def_id); + let values: Vec = std::iter::zip(&names, sig.inputs()) + .map(|(name, &ty)| match ty.kind() { + ty::Ref(_, _, rustc_ast::Mutability::Mut) if pointee && self.t.place_pointee(ty).is_some() => { + Expr::ident(name).member("v") + } + _ => Expr::ident(name), + }) + .collect(); // A binding's export, or a function whose body translates, is named directly. if !matches!(tcx.def_kind(def_id), rustc_hir::def::DefKind::Ctor(..)) && let Ok(Some(instance)) = Instance::try_resolve(tcx, self.t.typing_env_pub(), def_id, gargs) diff --git a/ts/src/lower/task.rs b/ts/src/lower/task.rs index 42da7af18..3cd6b89e4 100644 --- a/ts/src/lower/task.rs +++ b/ts/src/lower/task.rs @@ -143,7 +143,12 @@ pub fn call<'tcx>( "get_mut" => Expr::rt(format!("arcGetMut{place}"), args), _ => { let clone = super::collections::clone_fn(b, value, call.span); - Expr::rt(format!("arcMakeMut{place}"), args.into_iter().chain([clone]).collect()) + let arc = b.t.sig(call.def_id, call.args).inputs()[0].peel_refs(); + let Some(droppers) = b.t.droppers([arc], call.span) else { + return Ok(Some(Expr::undefined())); + }; + let values = args.into_iter().chain([clone]).chain(droppers).collect(); + Expr::rt(format!("arcMakeMut{place}"), values) } } } From 015ec2e4bc55884d1b7170644549af154aaa89e3 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 29 Oct 2026 18:28:02 -0700 Subject: [PATCH 4/4] fix(ts): pass a function value's &mut as its pointee only where std declares one Co-Authored-By: Claude Opus 5.5 --- ts/fixtures/lang/expected/objects.ts | 137 +++++++++++++++------------ ts/fixtures/lang/src/objects.rs | 9 ++ ts/src/lower/body.rs | 22 +++-- ts/src/lower/mir/call.rs | 8 +- ts/src/lower/pointer.rs | 13 +++ 5 files changed, 110 insertions(+), 79 deletions(-) diff --git a/ts/fixtures/lang/expected/objects.ts b/ts/fixtures/lang/expected/objects.ts index 2e8971509..2a79464c7 100644 --- a/ts/fixtures/lang/expected/objects.ts +++ b/ts/fixtures/lang/expected/objects.ts @@ -59,6 +59,13 @@ export function throughMutex$Mutex_u8$2(p: rt.Mutex): rt.u8 { return _0; } +/** A pin of a byte, made through a function pointer, that writes the byte. */ +export function pinVia(p: rt.Place): rt.Place { + return ((a0: rt.Place): rt.Place => { + return a0; + })(p); +} + export function lockedBy(n: rt.u8): rt.Mutex { return new rt.Mutex(rt.u8WrappingAdd(n, 3)); } @@ -179,7 +186,7 @@ export function counts(out: rt.ByteVec, arcs: rt.Arc[], items: rt.Arc = new rt.Arc(by); const two: rt.Arc = new rt.Arc(rt.u8WrappingAdd(by, 1)); @@ -273,87 +280,91 @@ export function objects(data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(throughArc$Arc_u8(rt.place(() => _199, (value5) => _199 = value5)), through$Pin_Arc_u8(rt.place(() => pinned, (value6) => pinned = value6))))); _199[Symbol.dispose](); rt.byteVecPush(out, throughMutex$Mutex_u8(lockedBy(by))); + let pinnedByte: rt.u8 = by; + const _208: rt.Place = pinVia(rt.place(() => pinnedByte, (value7) => pinnedByte = value7)); + _208.v = _208.v ^ 90; + rt.byteVecPush(out, pinnedByte); let owned: rt.Arc = rt.arcClone(held); - const lent2: rt.Place> = rt.place(() => owned, (value7) => owned = value7); + const lent2: rt.Place> = rt.place(() => owned, (value8) => owned = value8); rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(lent2.v.strong & 0xff, lent2.v.value))); const locked: rt.Mutex[]> = new rt.Mutex(v); - const _228: rt.Mutex[]> = rt.mutexLock(locked); - const taken3 = _228.value; - _228.value = []; + const _234: rt.Mutex[]> = rt.mutexLock(locked); + const taken3 = _234.value; + _234.value = []; const taken: rt.Arc[] = taken3; - rt.mutexUnlock(_228); + rt.mutexUnlock(_234); counts(out, [one, two], taken); - const _239: rt.Arc[] = [rt.arcClone(two)]; - const _245: rt.Mutex[]> = rt.mutexLock(locked); - rt.dropAll(_245.value); - _245.value = _239; - rt.mutexUnlock(_245); + const _245: rt.Arc[] = [rt.arcClone(two)]; + const _251: rt.Mutex[]> = rt.mutexLock(locked); + rt.dropAll(_251.value); + _251.value = _245; + rt.mutexUnlock(_251); rt.dropAll(taken); - const _250: rt.Arc[] = [one, two]; - const _258: rt.Mutex[]> = rt.mutexLock(locked); - counts(out, _250, _258.value); - rt.mutexUnlock(_258); + const _256: rt.Arc[] = [one, two]; + const _264: rt.Mutex[]> = rt.mutexLock(locked); + counts(out, _256, _264.value); + rt.mutexUnlock(_264); const cell: rt.RefCell[]> = new rt.RefCell([rt.arcClone(one)]); - const _269: rt.RefCell[]> = rt.refCellBorrowMut(cell); - const taken4 = _269.value; - _269.value = []; + const _275: rt.RefCell[]> = rt.refCellBorrowMut(cell); + const taken4 = _275.value; + _275.value = []; const taken2: rt.Arc[] = taken4; - rt.refMutRelease(_269); - const _271: rt.Arc[] = [rt.arcClone(two), rt.arcClone(two)]; - const _279: rt.RefCell[]> = rt.refCellBorrowMut(cell); - rt.dropAll(_279.value); - _279.value = _271; - rt.refMutRelease(_279); + rt.refMutRelease(_275); + const _277: rt.Arc[] = [rt.arcClone(two), rt.arcClone(two)]; + const _285: rt.RefCell[]> = rt.refCellBorrowMut(cell); + rt.dropAll(_285.value); + _285.value = _277; + rt.refMutRelease(_285); counts(out, [one, two], taken2); - const _290: rt.Arc[] = [one, two]; - const _298: rt.RefCell[]> = rt.refCellBorrow(cell); - counts(out, _290, _298.value); - rt.refRelease(_298); + const _296: rt.Arc[] = [one, two]; + const _304: rt.RefCell[]> = rt.refCellBorrow(cell); + counts(out, _296, _304.value); + rt.refRelease(_304); const sets: rt.HashMap> = rt.hashNew() as rt.HashMap>; - const _304: rt.Iter = rt.iterSlice(rt.byteSliceView(data)); - const _303: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, _304, true); - rt.hashInsert(sets, by, _303); - const _306: rt.BTreeSet | undefined = rt.hashGet(sets, by); - if (_306 !== undefined) { - const _313: Uint8Array = Uint8Array.of(rt.u8WrappingAdd(by, 1)); - rt.byteVecPush(out, rt.btreeLen(rt.replaceIn(_306, rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, rt.iterSlice(_313), true))) & 0xff); + const _310: rt.Iter = rt.iterSlice(rt.byteSliceView(data)); + const _309: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, _310, true); + rt.hashInsert(sets, by, _309); + const _312: rt.BTreeSet | undefined = rt.hashGet(sets, by); + if (_312 !== undefined) { + const _319: Uint8Array = Uint8Array.of(rt.u8WrappingAdd(by, 1)); + rt.byteVecPush(out, rt.btreeLen(rt.replaceIn(_312, rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, rt.iterSlice(_319), true))) & 0xff); } - const _320: Uint8Array = Uint8Array.of(by); - const _319: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, rt.iterSlice(_320), true); - const _324: rt.u8 = rt.u8WrappingAdd(by, 1); - Object.assign(rt.entryOrInsertWith(rt.entry(sets, _324), (): rt.BTreeSet => { + const _326: Uint8Array = Uint8Array.of(by); + const _325: rt.BTreeSet = rt.btreeExtendKeys(rt.btreeNew(rt.cmp) as rt.BTreeSet, rt.iterSlice(_326), true); + const _330: rt.u8 = rt.u8WrappingAdd(by, 1); + Object.assign(rt.entryOrInsertWith(rt.entry(sets, _330), (): rt.BTreeSet => { return rt.btreeNew(rt.cmp) as rt.BTreeSet; - }), _319); + }), _325); const keys: [rt.u8, rt.u8][] = rt.iterToArray(rt.iterMap(rt.hashIter(sets), (_22: [rt.u8, rt.BTreeSet]): [rt.u8, rt.u8] => { return [_22[0], rt.btreeLen(_22[1]) & 0xff] as [rt.u8, rt.u8]; })); rt.sliceSort(keys, rt.cmpLt(rt.cmpTuple([rt.cmp, rt.cmp]))); - for (const _335 of keys) { - rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(_335[0], _335[1]))); + for (const _341 of keys) { + rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(_341[0], _341[1]))); } - const _345: rt.Arc[] = [rt.arcClone(one)]; - const w: Window.Window = new Window.Window(by, _345); + const _351: rt.Arc[] = [rt.arcClone(one)]; + const w: Window.Window = new Window.Window(by, _351); const other: rt.Arc[] = [rt.arcClone(two), rt.arcClone(two)]; const bytes: rt.ByteVec = new rt.ByteVec(); for (const b4 of rt.byteSliceView(data)) { - const _363: rt.u8 = rt.u8Rem(b4, 5); - if (_363 === 0) { + const _369: rt.u8 = rt.u8Rem(b4, 5); + if (_369 === 0) { apply(w, b4); continue; } - if (_363 === 1) { + if (_369 === 1) { rt.byteVecPush(out, park(w, b4)); continue; } - if (_363 === 2) { + if (_369 === 2) { swapAll(w.held, other); continue; } - if (_363 === 3) { + if (_369 === 3) { refill(other, bytes, one, b4); continue; } - if (_363 === 4) { + if (_369 === 4) { if (rt.u8Rem(b4, 2) === 0) { const mine: rt.Arc[] = swapIn(other, two); counts(out, [one, two], mine); @@ -364,8 +375,8 @@ export function objects(data: rt.ByteSlice, out: rt.ByteVec): void { rt.dropAll(takeAll(w.held)); } const pinnedAll: rt.Arc[] = [rt.arcClone(two)]; - for (const _396 of rt.byteSliceView(data)) { - rt.byteVecPush(out, repinAll(pinnedAll, one, _396) & 0xff); + for (const _402 of rt.byteSliceView(data)) { + rt.byteVecPush(out, repinAll(pinnedAll, one, _402) & 0xff); } counts(out, [one, two], pinnedAll); rt.dropAll(pinnedAll); @@ -377,11 +388,11 @@ export function objects(data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecExtendFromSlice(out, rt.byteVecSlice(bytes)); counts(out, [one, two], w.held); counts(out, [one, two], other); - const _456: Uint8Array = Uint8Array.of(by); - const q: rt.VecDeque = rt.dequeExtend(rt.dequeNew() as rt.VecDeque, rt.iterSlice(_456)); + const _462: Uint8Array = Uint8Array.of(by); + const q: rt.VecDeque = rt.dequeExtend(rt.dequeNew() as rt.VecDeque, rt.iterSlice(_462)); const map: rt.HashMap = rt.hashNew() as rt.HashMap; - for (const _460 of rt.byteSliceView(data)) { - rt.byteVecPush(out, requeue(q, map, _460) & 0xff); + for (const _466 of rt.byteSliceView(data)) { + rt.byteVecPush(out, requeue(q, map, _466) & 0xff); } rt.iterExtendBytes(out, rt.dequeIter(q)); rt.iterExtendBytes(out, rt.hashValues(map)); @@ -395,9 +406,9 @@ export function objects(data: rt.ByteSlice, out: rt.ByteVec): void { rt.byteVecPush(out, two.weak & 0xff); const a: Window.Window = Window.withStart(Window.default$Default(), by); const b7: Window.Window = new Window.Window(rt.u8WrappingAdd(by, 2), [rt.arcClone(one)]); - const _496: rt.u16 = by as rt.u16; + const _502: rt.u16 = by as rt.u16; const array: rt.u16[] = Array.from({ length: 2 }, (): rt.u16 => { - return _496; + return _502; }); for (const x of rt.byteSliceView(data)) { if (rt.u8Rem(x, 2) === 0) { @@ -412,13 +423,13 @@ export function objects(data: rt.ByteSlice, out: rt.ByteVec): void { const counting: rt.Waker = new rt.ArcWaker(rt.arcClone(wakes), Wakes.wake$Wake, (arc: rt.Arc) => arc[Symbol.dispose]()); const noop: rt.Waker = rt.NOOP_WAKER.clone(); let w2: rt.Waker = noop.clone(); - for (const _560 of rt.byteSliceView(data)) { - if (!(rt.u8Rem(_560, 2) === 0)) { - _566 = noop; + for (const _566 of rt.byteSliceView(data)) { + if (!(rt.u8Rem(_566, 2) === 0)) { + _572 = noop; } else { - _566 = counting; + _572 = counting; } - const old3: rt.Waker = rewake(rt.place(() => w2, (value8) => w2 = value8), _566); + const old3: rt.Waker = rewake(rt.place(() => w2, (value9) => w2 = value9), _572); old3.wakeByRef(); w2.wakeByRef(); old3[Symbol.dispose](); diff --git a/ts/fixtures/lang/src/objects.rs b/ts/fixtures/lang/src/objects.rs index 7b274d8bb..4adab4fde 100644 --- a/ts/fixtures/lang/src/objects.rs +++ b/ts/fixtures/lang/src/objects.rs @@ -39,6 +39,12 @@ fn through_mutex>>(p: P) -> u8 { *p.lock().unwrap() } +/// A pin of a byte, made through a function pointer, that writes the byte. +fn pin_via<'a>(p: &'a mut u8) -> Pin<&'a mut u8> { + let f: fn(&'a mut u8) -> Pin<&'a mut u8> = Pin::new; + f(p) +} + fn locked_by(n: u8) -> Mutex { Mutex::new(n.wrapping_add(3)) } @@ -210,6 +216,9 @@ pub fn objects(data: &[u8], out: &mut Vec) { // A mutable reference to each, read through the shared `Deref`. out.extend_from_slice(&[through_arc(&mut Arc::clone(&held)), through(&mut pinned)]); out.push(through_mutex(&mut locked_by(by))); + let mut pinned_byte = by; + *Pin::get_mut(pin_via(&mut pinned_byte)) ^= 0x5a; + out.push(pinned_byte); let mut owned = Arc::clone(&held); let lent = Pin::new(&mut owned); out.extend_from_slice(&[Arc::strong_count(&*lent) as u8, **lent]); diff --git a/ts/src/lower/body.rs b/ts/src/lower/body.rs index 908289d8e..2af61a30e 100644 --- a/ts/src/lower/body.rs +++ b/ts/src/lower/body.rs @@ -236,16 +236,20 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { let gargs = self.subst(gargs); let sig = self.t.sig(def_id, gargs); let names: Vec = (0..sig.inputs().len()).map(|i| self.fresh(&format!("a{i}"))).collect(); - // std's mappings take a `&mut` that is an `rt.Place` as what it points at, as a call's - // operands pass it. - let pointee = self.t.mapped(def_id, gargs) && !super::pointer::takes_place(tcx, def_id); + // std's mappings take a `&mut` they declare that is an `rt.Place` as what it points + // at, as a call's operands pass it. + let pointees = match self.t.mapped(def_id, gargs) { + true => super::pointer::pointees(tcx, def_id), + false => Vec::new(), + }; let values: Vec = std::iter::zip(&names, sig.inputs()) - .map(|(name, &ty)| match ty.kind() { - ty::Ref(_, _, rustc_ast::Mutability::Mut) if pointee && self.t.place_pointee(ty).is_some() => { - Expr::ident(name).member("v") - } - _ => Expr::ident(name), - }) + .enumerate() + .map( + |(i, (name, &ty))| match pointees.get(i) == Some(&true) && self.t.place_pointee(ty).is_some() { + true => Expr::ident(name).member("v"), + false => Expr::ident(name), + }, + ) .collect(); // A binding's export, or a function whose body translates, is named directly. if !matches!(tcx.def_kind(def_id), rustc_hir::def::DefKind::Ctor(..)) diff --git a/ts/src/lower/mir/call.rs b/ts/src/lower/mir/call.rs index 542d2961f..dc8de9802 100644 --- a/ts/src/lower/mir/call.rs +++ b/ts/src/lower/mir/call.rs @@ -316,13 +316,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { if let Some(err) = self.std_places(def_id, gargs, result, span) { return err; } - let takes_place = crate::lower::pointer::takes_place(tcx, def_id); - let sig = tcx.fn_sig(def_id).instantiate_identity().skip_norm_wip(); - let inputs = tcx.instantiate_bound_regions_with_erased(sig).inputs().to_vec(); - inputs - .iter() - .map(|t| !takes_place && matches!(t.kind(), ty::Ref(_, _, rustc_ast::Mutability::Mut))) - .collect() + crate::lower::pointer::pointees(tcx, def_id) } }; let values = self.operands(local, &pointees, args, result, span, out); diff --git a/ts/src/lower/pointer.rs b/ts/src/lower/pointer.rs index 8b96d9344..d89c07a57 100644 --- a/ts/src/lower/pointer.rs +++ b/ts/src/lower/pointer.rs @@ -206,6 +206,19 @@ pub fn takes_place(tcx: ty::TyCtxt<'_>, def_id: rustc_hir::def_id::DefId) -> boo ) || super::stock::is_stock(tcx, def_id) } +/// Which of std's `def_id`'s parameters its mapping takes as what the `&mut` it declares +/// points at, rather than the `rt.Place` that `&mut` may be: not a type parameter that +/// one instantiates, as `Pin::new`'s pointer. +pub fn pointees(tcx: ty::TyCtxt<'_>, def_id: rustc_hir::def_id::DefId) -> Vec { + let takes_place = takes_place(tcx, def_id); + let sig = tcx.fn_sig(def_id).instantiate_identity().skip_norm_wip(); + tcx.instantiate_bound_regions_with_erased(sig) + .inputs() + .iter() + .map(|t| !takes_place && matches!(t.kind(), ty::Ref(_, _, rustc_ast::Mutability::Mut))) + .collect() +} + /// A call of std's raw pointer, `NonNull`, `MaybeUninit`, or `Box` functions that make or /// take a raw pointer: its mapping, `None` if it has none, or its arguments back if it is /// no such call.