diff --git a/quest/m2/ts-shared.md b/quest/m2/ts-shared.md index 6f3f4fb34..8cf310653 100644 --- a/quest/m2/ts-shared.md +++ b/quest/m2/ts-shared.md @@ -30,15 +30,11 @@ is its own guard and counts its borrows: a conflicting borrow panics, and a conf lock aborts, each with wasm32's message. Their `get_mut`, `into_inner`, `try_borrow`, `Ref::map`, and `Ref::clone` are still refused. A `thread_local!` key is an `rt.LocalKey`, a module's value that makes its value on -first use, as wasm32's `LazyStorage` does, and never drops it. A `RefCell` key's `set` -and `with_borrow_mut` are still refused: std's bodies write through a `&mut`, which a -body of std's can't yet. -Left: `static` atomics, below, which a module's value, as a thread-local's, can hold. +first use, as wasm32's `LazyStorage` does, and never drops it. A `static` atomic is a +module's value too, an `rt.Atomic` of the value the static's memory starts with. +Left: the counters' cost, below. - `Send` and `Sync` bounds have no meaning in JS; accept them. -- A `static` atomic (`static NEXT: AtomicU64`, 2 in moq-net and 1 in kio) is still - refused as a constant of type `&Atomic`: a `static` with interior mutability needs one - module-level value that every use shares. - Measure the counter overhead on the [TS benchmark](/ts/README.md#bench). Public API: the translator's output for shared and interior-mutable types. diff --git a/ts/README.md b/ts/README.md index 08da8e82f..2bd2e4054 100644 --- a/ts/README.md +++ b/ts/README.md @@ -106,9 +106,9 @@ crate translation reaches is refused, and so are two versions of one crate it re | `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` | | `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, and the `Cell` and `RefCell` helpers lower from std, except a `RefCell` key's `set` and `with_borrow_mut` | +| `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 | | `OnceCell`, `OnceLock` | `rt.OnceCell` and `rt.OnceLock`, holding the `Option` in `v`; a `OnceLock` initialized from within its own initialization panics, as wasm32's does | -| `AtomicU32` and the others of `bool` and integers | `rt.Atomic`, the `rt.Place` of its value; an `atomic::Ordering` is its discriminant, and one an operation can't take panics as std's does | +| `AtomicU32` and the others of `bool` and integers | `rt.Atomic`, the `rt.Place` of its value; an `atomic::Ordering` is its discriminant, and one an operation can't take panics as std's does. A `static` one is a module's value, named for the static, that every use shares | | `async fn` | a function returning `rt.Future`: a class of its own, its coroutine's state machine | | `Poll`, `Waker`, `Context` | `T \| typeof rt.Pending`; `T \| rt.OuterPending` where `T` can already be pending, as `Poll>` can, so `Ready(Pending)` keeps apart from `Pending`; `rt.Waker`, which is also the context | | `pin!(x)`, `poll_fn(f)`, `future::pending()` | `x`; `rt.PollFn`, which drops `f` with itself; `rt.NEVER` | @@ -556,7 +556,7 @@ fields overlap in different types ([unsafe](/quest/m3/ts-unsafe.md)), and trait behind `&`, `&mut`, `Box`, or `Arc` ([trait objects](/quest/m3/ts-dyn.md)), fail translation. Until they translate, an item that needs one, such as a `dyn Error`, binds to hand-written TS ([bindings](#bindings)). -These fail translation: formatting and parsing an `f64`, and `f64::clamp`, whose panic formats them ([float formatting](/quest/m2/ts-model/floats.md)), `f32` ([`f32`](/quest/m3/ts-f32.md)), statics, async closures, a `Poll` of a `Poll` of a `Poll`, a future whose output can be pending, an instance of a generic type whose field is a `Poll` of a type parameter that can be pending, a closure that owns a value +These fail translation: formatting and parsing an `f64`, and `f64::clamp`, whose panic formats them ([float formatting](/quest/m2/ts-model/floats.md)), `f32` ([`f32`](/quest/m3/ts-f32.md)), statics other than atomics and the bytes of an immutable one, async closures, a `Poll` of a `Poll` of a `Poll`, a future whose output can be pending, an instance of a generic type whose field is a `Poll` of a type parameter that can be pending, a closure that owns a value that drops given to a std function that drops it beside another value that drops, as `for_each` of items that drop does, or keeps it other than in an iterator adaptor, and values that drop in some iterators ([drop order](/quest/m3/ts-drop-order.md)), a `Cow` of other than `str`, diff --git a/ts/fixtures/lang/expected/core/option.ts b/ts/fixtures/lang/expected/core/option.ts index 599583730..2d4b83127 100644 --- a/ts/fixtures/lang/expected/core/option.ts +++ b/ts/fixtures/lang/expected/core/option.ts @@ -384,7 +384,7 @@ export function mapOr$Option_Noisy_u8_borrows_closure0(self: Noisy.Noisy | undef return f(self); } -export function mapOr$Option_Noisy_u8_locals_closure12(self: locals_Noisy.Noisy | undefined, default_: rt.u8, f: (a0: locals_Noisy.Noisy) => rt.u8): rt.u8 { +export function mapOr$Option_Noisy_u8_locals_closure18(self: locals_Noisy.Noisy | undefined, default_: rt.u8, f: (a0: locals_Noisy.Noisy) => rt.u8): rt.u8 { if (self === undefined) { return default_; } @@ -419,7 +419,7 @@ export function mapOr$Option_Pair_u8_copies_closure0(self: Pair.Pair | undefined return f(self); } -export function mapOr$Option_PhantomData_u8_u8_locals_closure9(self: undefined | rt.None, default_: rt.u8, f: (a0: undefined) => rt.u8): rt.u8 { +export function mapOr$Option_PhantomData_u8_u8_locals_closure14(self: undefined | rt.None, default_: rt.u8, f: (a0: undefined) => rt.u8): rt.u8 { if (self === rt.None) { return default_; } diff --git a/ts/fixtures/lang/expected/core/result.ts b/ts/fixtures/lang/expected/core/result.ts index 71dc67fca..3773d001a 100644 --- a/ts/fixtures/lang/expected/core/result.ts +++ b/ts/fixtures/lang/expected/core/result.ts @@ -88,7 +88,7 @@ export function mapOr$Result_u8_u16_u8_results_closure7(self: rt.Result, default_: rt.u8, f: (a0: rt.usize) => rt.u8): rt.u8 { +export function mapOr$Result_usize_AccessError_u8_locals_closure8(self: rt.Result, default_: rt.u8, f: (a0: rt.usize) => rt.u8): rt.u8 { if (self.ok) { return f(self.value); } diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index 40db61d85..4965909b1 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -31,6 +31,7 @@ import { lists, shared as pointers_shared, places, unchecked, nested as pointers import { refs } from "./refs.ts"; import { copies as shapes_copies, order, discriminants, loops, patterns, closures, library, flows } from "./shapes.ts"; import { splits } from "./splits.ts"; +import { statics } from "./statics.ts"; import { strs, slices } from "./strs.ts"; import { decodes } from "./utf8.ts"; import { wrappers } from "./wrappers.ts"; @@ -40,98 +41,100 @@ 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", (_270: 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", (_273: 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", (_271: rt.u8, data2: rt.ByteSlice, out2: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::copies", (_274: 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", (_272: rt.u8, data3: rt.ByteSlice, out3: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::loops", (_275: 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", (_273: rt.u8, data4: rt.ByteSlice, out4: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::patterns", (_276: 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", (_274: rt.u8, data5: rt.ByteSlice, out5: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::closures", (_277: 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", (_275: rt.u8, data6: rt.ByteSlice, out6: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::discriminants", (_278: 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", (_276: rt.u8, data7: rt.ByteSlice, out7: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::library", (_279: 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", (_277: 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", (_280: 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", (_278: rt.u8, data9: rt.ByteSlice, out9: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::derived", (_281: 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", (_279: rt.u8, data10: rt.ByteSlice, out10: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::written", (_282: 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", (_280: 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", (_283: 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", (_281: 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", (_284: 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", (_282: rt.u8, data13: rt.ByteSlice, out13: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::options", (_285: 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", (_283: rt.u8, data14: rt.ByteSlice, out14: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::own", (_286: 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", (_284: rt.u8, data15: rt.ByteSlice, out15: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::values", (_287: 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", (_285: rt.u8, data16: rt.ByteSlice, out16: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::order", (_288: 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", (_286: 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", (_289: 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", (_287: rt.u8, data18: rt.ByteSlice, out18: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::flows", (_290: 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", (_288: 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", (_291: 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", (_289: 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", (_292: 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", (_290: 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", (_293: 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", (_291: rt.u8, data22: rt.ByteSlice, out22: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::counts", (_294: 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", (_292: rt.u8, _310: rt.ByteSlice, out23: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_295: 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", (_293: 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", (_296: 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", (_294: 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", (_297: 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", (_295: 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", (_298: 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", (_296: rt.u8, data26: rt.ByteSlice, out27: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::relock", (_299: 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", (_297: rt.u8, data27: rt.ByteSlice, out28: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locals::locals", (_2100: rt.u8, data27: rt.ByteSlice, out28: rt.ByteVec): void => { locals(data27, out28); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["locks::rwlocks", (_298: rt.u8, data28: rt.ByteSlice, out29: rt.ByteVec): void => { - rwlocks(data28, out29); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["borrows::borrows", (_299: rt.u8, data29: rt.ByteSlice, out30: rt.ByteVec): void => { - borrows(data29, out30); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["defaults::defaults", (_2100: rt.u8, data30: rt.ByteSlice, out31: rt.ByteVec): void => { - defaults(data30, out31); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::arcs", (_2101: rt.u8, data31: rt.ByteSlice, out32: rt.ByteVec): void => { - arcs(data31, out32); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::rcs", (_2102: rt.u8, data32: rt.ByteSlice, out33: rt.ByteVec): void => { - rcs(data32, out33); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::cyclic_arcs", (_2103: rt.u8, data33: rt.ByteSlice, out34: rt.ByteVec): void => { - cyclicArcs(data33, out34); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["counts::cyclic_rcs", (_2104: rt.u8, data34: rt.ByteSlice, out35: rt.ByteVec): void => { - cyclicRcs(data34, out35); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::anys", (_2105: rt.u8, data35: rt.ByteSlice, out36: rt.ByteVec): void => { - anys(data35, out36); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::fns", (_2106: rt.u8, data36: rt.ByteSlice, out37: rt.ByteVec): void => { - fns(data36, out37); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::copies", (_2107: rt.u8, data37: rt.ByteSlice, out38: rt.ByteVec): void => { - copies(data37, out38); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::lists", (_2108: rt.u8, data38: rt.ByteSlice, out39: rt.ByteVec): void => { - lists(data38, out39); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::nested", (_2109: rt.u8, data39: rt.ByteSlice, out40: rt.ByteVec): void => { - pointers_nested(data39, out40); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::places", (_2110: rt.u8, data40: rt.ByteSlice, out41: rt.ByteVec): void => { - places(data40, out41); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::sentinels", (_2111: rt.u8, data41: rt.ByteSlice, out42: rt.ByteVec): void => { - sentinels(data41, out42); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::shared", (_2112: rt.u8, data42: rt.ByteSlice, out43: rt.ByteVec): void => { - pointers_shared(data42, out43); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::unchecked", (_2113: rt.u8, data43: rt.ByteSlice, out44: rt.ByteVec): void => { - unchecked(data43, out44); - }] 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", (_2114: rt.u8, data44: rt.ByteSlice, out45: rt.ByteVec): void => { - fromFns(data44, out45); - }] 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", (_2115: rt.u8, data45: rt.ByteSlice, out46: rt.ByteVec): void => { - objects(data45, out46); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["statics::statics", (_2101: 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 => { + 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 => { + 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 => { + 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 => { + 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 => { + 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 => { + 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 => { + 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]]).map(copy$str_forabfnu8au8bmutstdvecVecu8); } diff --git a/ts/fixtures/lang/expected/locals.ts b/ts/fixtures/lang/expected/locals.ts index fa66f9de2..7b413429d 100644 --- a/ts/fixtures/lang/expected/locals.ts +++ b/ts/fixtures/lang/expected/locals.ts @@ -1,14 +1,14 @@ // Generated by rust2-ts. Do not edit. import * as rt from "./rt.ts"; -import { copy$Twin, copy$u8_u8 } from "./lib.ts"; +import { copy$Option_u64_u64, copy$Twin, copy$u8_u8 } from "./lib.ts"; import { thenSome$bool_PhantomData_u8 } from "./core/bool.ts"; -import { mapOr$Option_Noisy_u8_locals_closure12, mapOr$Option_PhantomData_u8_u8_locals_closure9, mapOr$Option_u64_u64_u8_borrows_closure1 } from "./core/option.ts"; -import { mapOr$Result_usize_AccessError_u8_locals_closure3 } from "./core/result.ts"; +import { mapOr$Option_Noisy_u8_locals_closure18, mapOr$Option_PhantomData_u8_u8_locals_closure14, mapOr$Option_u64_u64_u8_borrows_closure1 } from "./core/option.ts"; +import { mapOr$Result_usize_AccessError_u8_locals_closure8 } from "./core/result.ts"; import * as Noisy from "./locals.Noisy.ts"; import type * as Polling from "./locals.Polling.ts"; import type * as Twin from "./locals.Twin.ts"; -import { set$LocalKey_Cell_u32, get$LocalKey_Cell_Twin_Twin, get$LocalKey_Cell_u32, take$LocalKey_Cell_u32, replace$LocalKey_Cell_u32, withBorrow$LocalKey_RefCell_Vec_u8_locals_closure10_u8, take$LocalKey_RefCell_Option_Noisy, take$LocalKey_RefCell_Vec_u8, replace$LocalKey_RefCell_Option_Noisy, replace$LocalKey_RefCell_Vec_u8 } from "./std/thread/local.ts"; +import { set$LocalKey_Cell_u32, get$LocalKey_Cell_Twin_Twin, get$LocalKey_Cell_u32, take$LocalKey_Cell_u32, replace$LocalKey_Cell_u32, withBorrow$LocalKey_RefCell_Option_Noisy_locals_closure3_u8, withBorrow$LocalKey_RefCell_Vec_u8_locals_closure0_Vec_u8, withBorrow$LocalKey_RefCell_Vec_u8_locals_closure16_u8, withBorrow$LocalKey_RefCell_u32_locals_closure21_u8, withBorrow$LocalKey_RefCell_u32_locals_closure22_u32, take$LocalKey_RefCell_Option_Noisy, take$LocalKey_RefCell_Vec_u8, replace$LocalKey_RefCell_Option_Noisy, replace$LocalKey_RefCell_Vec_u8 } from "./std/thread/local.ts"; export const COUNT: rt.LocalKey> = /* @__PURE__ */ new rt.LocalKey(COUNT$init, false); @@ -45,139 +45,197 @@ export function POLLING$init(): Polling.Polling { return { current: new rt.Cell(undefined as [rt.u64, rt.u64] | undefined), held: new rt.RefCell(new rt.ByteVec()) }; } -/** Each byte an operation on the thread-locals; each puts what it read and what dropped. */ +export const TALLY: rt.LocalKey> = /* @__PURE__ */ new rt.LocalKey((): rt.RefCell => { + return TALLY$init(undefined); +}, true); + +export function TALLY$init(_1: undefined): rt.RefCell { + return new rt.RefCell(0); +} + +/** + * Each byte an operation on the thread-locals; each puts what it read and what dropped. + * First, what each holds, which only the value it starts with can be. + */ export function locals(data: rt.ByteSlice, out: rt.ByteVec): void { + rt.byteVecPush(out, ((a0: rt.Cell): rt.u32 => { + return a0.v; + })(COUNT.get()) & 0xff); + rt.iterExtendBytes(out, rt.iterSlice(rt.byteVecView(withBorrow$LocalKey_RefCell_Vec_u8_locals_closure0_Vec_u8(LOG, (log2: rt.ByteVec): rt.ByteVec => { + return rt.byteVecFrom(rt.byteVecView(log2)); + })))); + rt.byteVecPush(out, ((p: Polling.Polling): rt.u8 => { + return mapOr$Option_u64_u64_u8_borrows_closure1(copy$Option_u64_u64(p.current.v), 253, (_2: [rt.u64, rt.u64]): rt.u8 => { + return rt.u64Low(_2[0]) & 0xff; + }); + })(POLLING.get())); + rt.byteVecPush(out, ((p2: Polling.Polling): rt.u8 => { + const _64: rt.RefCell = rt.refCellBorrow(p2.held); + const _05: rt.u8 = _64.value.length & 0xff; + rt.refRelease(_64); + return _05; + })(POLLING.get())); + rt.byteVecPush(out, withBorrow$LocalKey_RefCell_Option_Noisy_locals_closure3_u8(HELD, (held: Noisy.Noisy | undefined): rt.u8 => { + return mapOr$Option_Noisy_u8_locals_closure18(held, 253, (n: Noisy.Noisy): rt.u8 => { + return n.id; + }); + })); + rt.byteVecPush(out, withBorrow$LocalKey_RefCell_u32_locals_closure21_u8(TALLY, (tally: rt.u32): rt.u8 => { + return tally & 0xff; + })); + const _26: [Twin.Twin, Twin.Twin] = get$LocalKey_Cell_Twin_Twin(PAIRS); + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(_26[0]._0 & 0xff, _26[0]._1 & 0xff, _26[1]._0 & 0xff, _26[1]._1 & 0xff))); + rt.byteVecPush(out, serial(false)); const log: rt.Arc> = new rt.Arc(new rt.Mutex(new rt.ByteVec())); for (const op of rt.byteSliceView(data)) { - const _13: rt.u8 = rt.u8Rem(op, 9); - if (_13 === 0) { - ((c3: rt.Cell): void => { - rt.cellSet(c3, rt.u32WrappingAdd(c3.v, op as rt.u32)); + const _46: rt.u8 = rt.u8Rem(op, 9); + if (_46 === 0) { + ((c4: rt.Cell): void => { + rt.cellSet(c4, rt.u32WrappingAdd(c4.v, op as rt.u32)); })(COUNT.get()); - rt.byteVecPush(out, ((a0: rt.Cell): rt.u32 => { - return a0.v; + rt.byteVecPush(out, ((a02: rt.Cell): rt.u32 => { + return a02.v; })(COUNT.get()) & 0xff); - } else if (_13 === 1) { - ((log6: rt.RefCell): void => { - const _513: rt.RefCell = rt.refCellBorrowMut(log6); + } else if (_46 === 1) { + ((log7: rt.RefCell): void => { + const _513: rt.RefCell = rt.refCellBorrowMut(log7); rt.byteVecPush(_513.value, op); rt.refMutRelease(_513); })(LOG.get()); - } else if (_13 === 2) { - rt.byteVecPush(out, mapOr$Result_usize_AccessError_u8_locals_closure3(rt.Ok(((log5: rt.RefCell): rt.usize => { - const _512: rt.RefCell = rt.refCellBorrow(log5); - const _010: rt.usize = _512.value.length; + } else if (_46 === 2) { + rt.byteVecPush(out, mapOr$Result_usize_AccessError_u8_locals_closure8(rt.Ok(((log6: rt.RefCell): rt.usize => { + const _512: rt.RefCell = rt.refCellBorrow(log6); + const _020: rt.usize = _512.value.length; rt.refRelease(_512); - return _010; - })(LOG.get())), 253, (n3: rt.usize): rt.u8 => { - return n3 & 0xff; + return _020; + })(LOG.get())), 253, (n4: rt.usize): rt.u8 => { + return n4 & 0xff; })); - } else if (_13 === 3) { - rt.byteVecPush(out, ((c2: rt.Cell): rt.u8 => { - return ((log4: rt.RefCell): rt.u8 => { - const _39: rt.u8 = c2.v & 0xff; - const _95: rt.RefCell = rt.refCellBorrow(log4); - const _78: rt.ByteVec = _95.value; - const _09: rt.u8 = _39 ^ _78.data[rt.idx(_78, 0)]; - rt.refRelease(_95); - return _09; + } else if (_46 === 3) { + rt.byteVecPush(out, ((c3: rt.Cell): rt.u8 => { + return ((log5: rt.RefCell): rt.u8 => { + const _318: rt.u8 = c3.v & 0xff; + const _910: rt.RefCell = rt.refCellBorrow(log5); + const _78: rt.ByteVec = _910.value; + const _019: rt.u8 = _318 ^ _78.data[rt.idx(_78, 0)]; + rt.refRelease(_910); + return _019; })(LOG.get()); })(COUNT.get())); - } else if (_13 === 4) { - const outer: [rt.u64, rt.u64] | undefined = ((p: Polling.Polling): [rt.u64, rt.u64] | undefined => { - const _46: [rt.u64, rt.u64] | undefined = [op as rt.u64, 1]; - return rt.cellReplace(p.current, _46); + } else if (_46 === 4) { + const outer: [rt.u64, rt.u64] | undefined = ((p3: Polling.Polling): [rt.u64, rt.u64] | undefined => { + const _45: [rt.u64, rt.u64] | undefined = [op as rt.u64, 1]; + return rt.cellReplace(p3.current, _45); })(POLLING.get()); - const len: rt.usize = ((p2: Polling.Polling): rt.usize => { - const _69: rt.RefCell = rt.refCellBorrowMut(p2.held); - rt.byteVecPush(_69.value, op); - rt.refMutRelease(_69); - const _11: rt.RefCell = rt.refCellBorrow(p2.held); - const _06: rt.usize = _11.value.length; - rt.refRelease(_11); - return _06; + const len: rt.usize = ((p4: Polling.Polling): rt.usize => { + const _611: rt.RefCell = rt.refCellBorrowMut(p4.held); + rt.byteVecPush(_611.value, op); + rt.refMutRelease(_611); + const _1110: rt.RefCell = rt.refCellBorrow(p4.held); + const _016: rt.usize = _1110.value.length; + rt.refRelease(_1110); + return _016; })(POLLING.get()); - rt.byteVecPush(out, mapOr$Option_u64_u64_u8_borrows_closure1(outer, 253, (_25: [rt.u64, rt.u64]): rt.u8 => { - return rt.u64Low(_25[0]) & 0xff; + rt.byteVecPush(out, mapOr$Option_u64_u64_u8_borrows_closure1(outer, 253, (_23: [rt.u64, rt.u64]): rt.u8 => { + return rt.u64Low(_23[0]) & 0xff; })); rt.byteVecPush(out, len & 0xff); - } else if (_13 === 5) { - ((held: rt.RefCell): void => { - const _35: Noisy.Noisy | undefined = new Noisy.Noisy(op, rt.arcClone(log)); - const _9: rt.RefCell = rt.refCellBorrowMut(held); - rt.drop(_9.value); - _9.value = _35; - rt.refMutRelease(_9); + } else if (_46 === 5) { + ((held3: rt.RefCell): void => { + const _316: Noisy.Noisy | undefined = new Noisy.Noisy(op, rt.arcClone(log)); + const _94: rt.RefCell = rt.refCellBorrowMut(held3); + rt.drop(_94.value); + _94.value = _316; + rt.refMutRelease(_94); })(HELD.get()); - } else if (_13 === 6) { + } else if (_46 === 6) { set$LocalKey_Cell_u32(COUNT, rt.u32WrappingMul(get$LocalKey_Cell_u32(COUNT), 3)); rt.byteVecPush(out, replace$LocalKey_Cell_u32(COUNT, op as rt.u32) & 0xff); rt.byteVecPush(out, take$LocalKey_Cell_u32(COUNT) & 0xff); set$LocalKey_Cell_u32(COUNT, rt.u32Add(op as rt.u32, 1)); - const _70: [Twin.Twin, Twin.Twin] = get$LocalKey_Cell_Twin_Twin(PAIRS); - rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(_70[0]._0 & 0xff, _70[0]._1 & 0xff, _70[1]._0 & 0xff, _70[1]._1 & 0xff))); - rt.byteVecPush(out, mapOr$Option_PhantomData_u8_u8_locals_closure9(thenSome$bool_PhantomData_u8(rt.u8Rem(op, 2) === 0, undefined), 253, (_2: undefined): rt.u8 => { + const _103: [Twin.Twin, Twin.Twin] = get$LocalKey_Cell_Twin_Twin(PAIRS); + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(_103[0]._0 & 0xff, _103[0]._1 & 0xff, _103[1]._0 & 0xff, _103[1]._1 & 0xff))); + rt.byteVecPush(out, mapOr$Option_PhantomData_u8_u8_locals_closure14(thenSome$bool_PhantomData_u8(rt.u8Rem(op, 2) === 0, undefined), 253, (_22: undefined): rt.u8 => { return 1; })); - } else if (_13 === 7) { - rt.byteVecPush(out, withBorrow$LocalKey_RefCell_Vec_u8_locals_closure10_u8(LOG, (log2: rt.ByteVec): rt.u8 => { - return log2.length & 0xff; + } else if (_46 === 7) { + rt.localKeyWithBorrowMut(LOG, (log3: rt.ByteVec): void => { + rt.byteVecPush(log3, op); + }); + rt.byteVecPush(out, withBorrow$LocalKey_RefCell_Vec_u8_locals_closure16_u8(LOG, (log4: rt.ByteVec): rt.u8 => { + return log4.length & 0xff; })); rt.iterExtendBytes(out, rt.iterSlice(rt.byteVecView(replace$LocalKey_RefCell_Vec_u8(LOG, rt.byteVecFrom(Uint8Array.of(op)))))); + rt.localKeyRefCellSet(LOG, rt.byteVecFrom(Uint8Array.of(op, op))); rt.iterExtendBytes(out, rt.iterSlice(rt.byteVecView(take$LocalKey_RefCell_Vec_u8(LOG)))); - ((log3: rt.RefCell): void => { - const _52: rt.RefCell = rt.refCellBorrowMut(log3); - rt.iterExtendBytes(_52.value, rt.iterSlice(Uint8Array.of(1, 2, 3))); - rt.refMutRelease(_52); - })(LOG.get()); - const _98: rt.LocalKey> = HELD; - const _101: rt.Arc> = rt.arcClone(log); - const old: Noisy.Noisy | undefined = replace$LocalKey_RefCell_Option_Noisy(_98, new Noisy.Noisy(op, _101)); - rt.byteVecPush(out, mapOr$Option_Noisy_u8_locals_closure12(old, 253, (n: Noisy.Noisy): rt.u8 => { - return n.id; + rt.localKeyRefCellSet(LOG, rt.byteVecFrom(Uint8Array.of(1, 2, 3))); + const _141: rt.LocalKey> = HELD; + const _144: rt.Arc> = rt.arcClone(log); + rt.localKeyRefCellSet(_141, new Noisy.Noisy(op, _144), rt.drop); + rt.localKeyWithBorrowMutPlace(HELD, (held2: rt.Place): void => { + rt.drop(held2.v); + held2.v = undefined; + }); + const old: Noisy.Noisy | undefined = replace$LocalKey_RefCell_Option_Noisy(HELD, new Noisy.Noisy(rt.u8WrappingAdd(op, 1), rt.arcClone(log))); + rt.byteVecPush(out, mapOr$Option_Noisy_u8_locals_closure18(old, 253, (n2: Noisy.Noisy): rt.u8 => { + return n2.id; })); rt.drop(old); - rt.byteVecPush(out, mapOr$Option_Noisy_u8_locals_closure12(take$LocalKey_RefCell_Option_Noisy(HELD), 253, (n2: Noisy.Noisy): rt.u8 => { - const _04: rt.u8 = n2.id; - n2[Symbol.dispose](); - return _04; + rt.byteVecPush(out, mapOr$Option_Noisy_u8_locals_closure18(take$LocalKey_RefCell_Option_Noisy(HELD), 253, (n3: Noisy.Noisy): rt.u8 => { + const _012: rt.u8 = n3.id; + n3[Symbol.dispose](); + return _012; + })); + rt.localKeyWithBorrowMutPlace(TALLY, (tally2: rt.Place): void => { + tally2.v = rt.u32WrappingAdd(rt.u32WrappingMul(tally2.v, 3), op as rt.u32); + }); + rt.byteVecPush(out, withBorrow$LocalKey_RefCell_u32_locals_closure21_u8(TALLY, (tally3: rt.u32): rt.u8 => { + return tally3 & 0xff; + })); + rt.localKeyRefCellSet(TALLY, withBorrow$LocalKey_RefCell_u32_locals_closure22_u32(TALLY, (tally4: rt.u32): rt.u32 => { + return rt.u32Div(tally4, 2); })); } else { rt.byteVecPush(out, serial(false)); } - const _118: rt.Mutex = rt.mutexLock(log.value); - rt.iterExtendBytes(out, rt.iterSlice(rt.byteSliceView(rt.byteVecSlice(_118.value)))); - rt.mutexUnlock(_118); - const _125: rt.Mutex = rt.mutexLock(log.value); - rt.byteVecClear(_125.value); - rt.mutexUnlock(_125); + const _181: rt.Mutex = rt.mutexLock(log.value); + rt.iterExtendBytes(out, rt.iterSlice(rt.byteSliceView(rt.byteVecSlice(_181.value)))); + rt.mutexUnlock(_181); + const _188: rt.Mutex = rt.mutexLock(log.value); + rt.byteVecClear(_188.value); + rt.mutexUnlock(_188); } - ((c4: rt.Cell): void => { - rt.cellSet(c4, 7); + ((c5: rt.Cell): void => { + rt.cellSet(c5, 7); })(COUNT.get()); - ((log7: rt.RefCell): void => { - const _515: rt.RefCell = rt.refCellBorrowMut(log7); - rt.byteVecTruncate(_515.value, 3); + ((tally5: rt.RefCell): void => { + const _515: rt.RefCell = rt.refCellBorrowMut(tally5); + _515.value = 0; rt.refMutRelease(_515); + })(TALLY.get()); + ((log8: rt.RefCell): void => { + const _516: rt.RefCell = rt.refCellBorrowMut(log8); + rt.byteVecTruncate(_516.value, 3); + rt.refMutRelease(_516); })(LOG.get()); - ((p3: Polling.Polling): void => { - rt.cellSet(p3.current, undefined as [rt.u64, rt.u64] | undefined); - const _97: rt.RefCell = rt.refCellBorrowMut(p3.held); - rt.byteVecClear(_97.value); - rt.refMutRelease(_97); + ((p5: Polling.Polling): void => { + rt.cellSet(p5.current, undefined as [rt.u64, rt.u64] | undefined); + const _911: rt.RefCell = rt.refCellBorrowMut(p5.held); + rt.byteVecClear(_911.value); + rt.refMutRelease(_911); })(POLLING.get()); - rt.drop(((held2: rt.RefCell): Noisy.Noisy | undefined => { - const _517: rt.RefCell = rt.refCellBorrowMut(held2); - const taken = _517.value; - _517.value = undefined; - const _012: Noisy.Noisy | undefined = taken; - rt.refMutRelease(_517); - return _012; + rt.drop(((held4: rt.RefCell): Noisy.Noisy | undefined => { + const _518: rt.RefCell = rt.refCellBorrowMut(held4); + const taken = _518.value; + _518.value = undefined; + const _022: Noisy.Noisy | undefined = taken; + rt.refMutRelease(_518); + return _022; })(HELD.get())); serial(true); - const _143: rt.Mutex = rt.mutexLock(log.value); - rt.iterExtendBytes(out, rt.iterSlice(rt.byteSliceView(rt.byteVecSlice(_143.value)))); - rt.mutexUnlock(_143); + const _208: rt.Mutex = rt.mutexLock(log.value); + rt.iterExtendBytes(out, rt.iterSlice(rt.byteSliceView(rt.byteVecSlice(_208.value)))); + rt.mutexUnlock(_208); log[Symbol.dispose](); } diff --git a/ts/fixtures/lang/expected/statics.ts b/ts/fixtures/lang/expected/statics.ts new file mode 100644 index 000000000..77e78c194 --- /dev/null +++ b/ts/fixtures/lang/expected/statics.ts @@ -0,0 +1,93 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; + +export const COUNT: rt.Atomic = /* @__PURE__ */ new rt.Atomic({ hi: 0xffffffff, lo: 0xfffffffd } as rt.u64); + +export const SIGNED: rt.Atomic = /* @__PURE__ */ new rt.Atomic(-128); + +export const FLAG: rt.Atomic = /* @__PURE__ */ new rt.Atomic(true); + +export const WIDE: rt.Atomic = /* @__PURE__ */ new rt.Atomic({ hi: -0x80000000, lo: 0x1 } as rt.i64); + +export const SIZE: rt.Atomic = /* @__PURE__ */ new rt.Atomic(7); + +export const TABLE: rt.ByteSlice = /* @__PURE__ */ rt.byteSliceFrom(Uint8Array.of(9, 8, 7, 6)); + +/** + * Each byte an operation on the statics; each puts what it read. First, what each holds, + * which only the value it starts with can be. + */ +export function statics(data: rt.ByteSlice, out: rt.ByteVec): void { + let _68!: rt.u8; + rt.iterExtendBytes(out, rt.iterSlice(rt.u64ToBeBytes(rt.atomicLoad(COUNT, 0)).reverse())); + rt.iterExtendBytes(out, rt.iterSlice(rt.i64ToBeBytes(rt.atomicLoad(WIDE, 0)).reverse())); + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(rt.atomicLoad(SIGNED, 0) & 0xff, rt.atomicLoad(FLAG, 0) ? 1 : 0, rt.atomicLoad(SIZE, 0) & 0xff))); + rt.byteVecPush(out, next(false)); + for (const op of rt.byteSliceView(data)) { + const n: rt.u8 = rt.u8Div(op, 6); + const _38: rt.u8 = rt.u8Rem(op, 6); + if (_38 === 0) { + rt.iterExtendBytes(out, rt.iterSlice(rt.u64ToBeBytes(rt.atomicFetch(COUNT, rt.u64WrappingAdd, n as rt.u64, 3)).reverse())); + continue; + } + if (_38 === 1) { + rt.byteVecPush(out, rt.atomicFetch(SIGNED, rt.i8WrappingSub, (n << 24) >> 24, 4) & 0xff); + continue; + } + if (_38 === 2) { + rt.byteVecPush(out, rt.atomicFetch(FLAG, (old, value) => old !== value, rt.u8Rem(n, 2) === 0, 0) ? 1 : 0); + continue; + } + if (_38 === 3) { + rt.iterExtendBytes(out, rt.iterSlice(rt.i64ToBeBytes(rt.atomicSwap(WIDE, rt.i64Neg(n as rt.i64), 2)).reverse())); + continue; + } + if (_38 === 4) { + const seen: rt.usize = rt.atomicLoad(SIZE, 0); + const _69: rt.Result = rt.atomicCompareExchange(SIZE, seen, rt.usizeWrappingMul(seen, n as rt.usize), 4, 0); + if (_69.ok) { + _68 = _69.value & 0xff; + } else { + _68 = 253; + } + rt.byteVecPush(out, _68); + continue; + } + if (_38 === 5) { + if (rt.u8Rem(n, 2) === 0) { + rt.byteVecPush(out, next(false)); + continue; + } + } + const _83: rt.ByteSlice = table(); + const _84: rt.usize = rt.usizeRem(n as rt.usize, 4); + if (!(_84 < 4)) { + rt.panic("index out of bounds: the len is " + 4 + " but the index is " + _84); + } + rt.iterExtendBytes(out, rt.iterSlice(Uint8Array.of(_83.data[_83.start + _84], table() === table() ? 1 : 0))); + } + rt.atomicStore(COUNT, { hi: 0xffffffff, lo: 0xfffffffd } as rt.u64, 0); + rt.atomicStore(SIGNED, -128, 0); + rt.atomicStore(FLAG, true, 0); + rt.atomicStore(WIDE, { hi: -0x80000000, lo: 0x1 } as rt.i64, 0); + rt.atomicStore(SIZE, 7, 0); + next(true); +} + +export function table(): rt.ByteSlice { + return TABLE; +} + +/** The next id from a static in a function, as kio's `waiter` takes one; starts over if `reset`. */ +export function next(reset: boolean): rt.u8 { + if (!reset) { + return rt.u64Low(rt.atomicFetch(NEXT, rt.u64WrappingAdd, 1, 0)) & 0xff; + } + rt.atomicStore(NEXT, 1, 0); + return 0; +} + +export const NEXT: rt.Atomic = /* @__PURE__ */ new rt.Atomic(1); + +//# sourceMappingURL=statics.ts.map diff --git a/ts/fixtures/lang/expected/std/thread/local.ts b/ts/fixtures/lang/expected/std/thread/local.ts index 078aaa559..c5e24024e 100644 --- a/ts/fixtures/lang/expected/std/thread/local.ts +++ b/ts/fixtures/lang/expected/std/thread/local.ts @@ -38,7 +38,29 @@ export function replace$LocalKey_Cell_u32(self: rt.LocalKey>, va })(self.get()); } -export function withBorrow$LocalKey_RefCell_Vec_u8_locals_closure10_u8(self: rt.LocalKey>, f: (a0: rt.ByteVec) => rt.u8): rt.u8 { +export function withBorrow$LocalKey_RefCell_Option_Noisy_locals_closure3_u8(self: rt.LocalKey>, f: (a0: Noisy.Noisy | undefined) => rt.u8): rt.u8 { + const captured = f; + return ((cell: rt.RefCell): rt.u8 => { + const _32: (a0: Noisy.Noisy | undefined) => rt.u8 = captured; + const _7: rt.RefCell = rt.refCellBorrow(cell); + const _02: rt.u8 = _32(_7.value); + rt.refRelease(_7); + return _02; + })(self.get()); +} + +export function withBorrow$LocalKey_RefCell_Vec_u8_locals_closure0_Vec_u8(self: rt.LocalKey>, f: (a0: rt.ByteVec) => rt.ByteVec): rt.ByteVec { + const captured = f; + return ((cell: rt.RefCell): rt.ByteVec => { + const _32: (a0: rt.ByteVec) => rt.ByteVec = captured; + const _7: rt.RefCell = rt.refCellBorrow(cell); + const _02: rt.ByteVec = _32(_7.value); + rt.refRelease(_7); + return _02; + })(self.get()); +} + +export function withBorrow$LocalKey_RefCell_Vec_u8_locals_closure16_u8(self: rt.LocalKey>, f: (a0: rt.ByteVec) => rt.u8): rt.u8 { const captured = f; return ((cell: rt.RefCell): rt.u8 => { const _32: (a0: rt.ByteVec) => rt.u8 = captured; @@ -49,6 +71,28 @@ export function withBorrow$LocalKey_RefCell_Vec_u8_locals_closure10_u8(self: rt. })(self.get()); } +export function withBorrow$LocalKey_RefCell_u32_locals_closure21_u8(self: rt.LocalKey>, f: (a0: rt.u32) => rt.u8): rt.u8 { + const captured = f; + return ((cell: rt.RefCell): rt.u8 => { + const _32: (a0: rt.u32) => rt.u8 = captured; + const _7: rt.RefCell = rt.refCellBorrow(cell); + const _02: rt.u8 = _32(_7.value); + rt.refRelease(_7); + return _02; + })(self.get()); +} + +export function withBorrow$LocalKey_RefCell_u32_locals_closure22_u32(self: rt.LocalKey>, f: (a0: rt.u32) => rt.u32): rt.u32 { + const captured = f; + return ((cell: rt.RefCell): rt.u32 => { + const _32: (a0: rt.u32) => rt.u32 = captured; + const _7: rt.RefCell = rt.refCellBorrow(cell); + const _02: rt.u32 = _32(_7.value); + rt.refRelease(_7); + return _02; + })(self.get()); +} + export function take$LocalKey_RefCell_Option_Noisy(self: rt.LocalKey>): Noisy.Noisy | undefined { return ((a0: rt.RefCell): Noisy.Noisy | undefined => { return rt.refCellReplace(a0, undefined); diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index 4491c4916..8502eb976 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -4,7 +4,7 @@ use crate::{ arrays, atomics, borrows, captures, cells, chars, chunks, collections, combinators, counts, cows, defaults, durations, dyns, edits, floats, fmt, generics, ints, iters, keys, locals, locks, nested, objects, pointers, refs, - shapes, splits, strs, utf8, wrappers, + shapes, splits, statics, strs, utf8, wrappers, }; /// The native side's entry: [`run`], except that a case starting with 0xff prints a @@ -92,6 +92,7 @@ fn cases() -> Vec<(&'static str, Case)> { ("locks::locks", |_, data, out| locks::locks(data, out)), ("locks::relock", |_, data, out| locks::relock(data, out)), ("locals::locals", |_, data, out| locals::locals(data, out)), + ("statics::statics", |_, data, out| statics::statics(data, out)), ("locks::rwlocks", |_, data, out| locks::rwlocks(data, out)), ("borrows::borrows", |_, data, out| borrows::borrows(data, out)), ("defaults::defaults", |_, data, out| defaults::defaults(data, out)), diff --git a/ts/fixtures/lang/src/lib.rs b/ts/fixtures/lang/src/lib.rs index 73905488f..29920f2eb 100644 --- a/ts/fixtures/lang/src/lib.rs +++ b/ts/fixtures/lang/src/lib.rs @@ -5,8 +5,8 @@ //! types, formatting, collections, UTF-8 decoding, `&mut` to values JS copies, whole //! writes through a `&mut` to an object, the standard library calls it maps, `Option`'s //! and `Result`'s combinators from core's MIR, `Option`s of values that can already be -//! `undefined`, atomics, trait objects, one-time cells, mutexes, `Cell`s, `RefCell`s, and -//! read-write locks, the defaults of std types, the counts of shared pointers, arrays +//! `undefined`, atomics, `static` ones included, trait objects, one-time cells, mutexes, +//! `Cell`s, `RefCell`s, read-write locks, and thread-locals, the defaults of std types, the counts of shared pointers, arrays //! `array::from_fn` makes, and maps of //! `Option`s. //! @@ -43,6 +43,7 @@ mod pointers; mod refs; mod shapes; mod splits; +mod statics; mod strs; mod utf8; #[cfg(target_arch = "wasm32")] diff --git a/ts/fixtures/lang/src/locals.rs b/ts/fixtures/lang/src/locals.rs index 4eaec597d..26047aa16 100644 --- a/ts/fixtures/lang/src/locals.rs +++ b/ts/fixtures/lang/src/locals.rs @@ -41,6 +41,7 @@ thread_local! { } }; static HELD: RefCell> = const { RefCell::new(None) }; + static TALLY: RefCell = RefCell::new(0); // A copy the `const` makes, then changes the original of, of a type nothing else writes. static PAIRS: Cell<(Twin, Twin)> = const { let mut x = Twin(1, 2); @@ -51,7 +52,17 @@ thread_local! { } /// Each byte an operation on the thread-locals; each puts what it read and what dropped. +/// First, what each holds, which only the value it starts with can be. pub fn locals(data: &[u8], out: &mut Vec) { + out.push(COUNT.with(Cell::get) as u8); + out.extend(LOG.with_borrow(|log| log.clone())); + out.push(POLLING.with(|p| p.current.get().map_or(0xfd, |(a, _)| a as u8))); + out.push(POLLING.with(|p| p.held.borrow().len() as u8)); + out.push(HELD.with_borrow(|held| held.as_ref().map_or(0xfd, |n| n.id))); + out.push(TALLY.with_borrow(|tally| *tally as u8)); + let (Twin(a, b), Twin(c, d)) = PAIRS.get(); + out.extend([a as u8, b as u8, c as u8, d as u8]); + out.push(serial(false)); let log = Arc::new(Mutex::new(Vec::new())); for &op in data { match op % 9 { @@ -92,17 +103,29 @@ pub fn locals(data: &[u8], out: &mut Vec) { } // The old value each helper replaces drops, or is put. 7 => { + LOG.with_borrow_mut(|log| log.push(op)); out.push(LOG.with_borrow(|log| log.len() as u8)); out.extend(LOG.replace(vec![op])); + LOG.set(vec![op, op]); out.extend(LOG.take()); - LOG.with(|log| log.borrow_mut().extend([1, 2, 3])); - let old = HELD.replace(Some(Noisy { + LOG.set(vec![1, 2, 3]); + HELD.set(Some(Noisy { id: op, log: log.clone(), })); + // Drops what it held. + HELD.with_borrow_mut(|held| *held = None); + let old = HELD.replace(Some(Noisy { + id: op.wrapping_add(1), + log: log.clone(), + })); out.push(old.as_ref().map_or(0xfd, |n| n.id)); drop(old); out.push(HELD.take().map_or(0xfd, |n| n.id)); + // A value JS copies, written through its place. + TALLY.with_borrow_mut(|tally| *tally = tally.wrapping_mul(3).wrapping_add(u32::from(op))); + out.push(TALLY.with_borrow(|tally| *tally as u8)); + TALLY.set(TALLY.with_borrow(|tally| tally / 2)); } _ => out.push(serial(false)), } @@ -110,6 +133,7 @@ pub fn locals(data: &[u8], out: &mut Vec) { log.lock().unwrap().clear(); } COUNT.with(|c| c.set(7)); + TALLY.with(|tally| *tally.borrow_mut() = 0); LOG.with(|log| log.borrow_mut().truncate(3)); POLLING.with(|p| { p.current.set(None); diff --git a/ts/fixtures/lang/src/statics.rs b/ts/fixtures/lang/src/statics.rs new file mode 100644 index 000000000..ab0066965 --- /dev/null +++ b/ts/fixtures/lang/src/statics.rs @@ -0,0 +1,72 @@ +//! `static` atomics, each one value every use shares: of every kind, at module level and +//! in a function, as kio's `waiter` keeps its ids, starting from a value of the type's +//! extremes. Each case leaves every static as it found it, so a fresh instance and a +//! reused one agree. + +use std::sync::atomic::Ordering::{AcqRel, Acquire, Relaxed, SeqCst}; +use std::sync::atomic::{AtomicBool, AtomicI8, AtomicI64, AtomicU64, AtomicUsize}; + +static COUNT: AtomicU64 = AtomicU64::new(u64::MAX - 2); +static SIGNED: AtomicI8 = AtomicI8::new(-128); +static FLAG: AtomicBool = AtomicBool::new(true); +static WIDE: AtomicI64 = AtomicI64::new(i64::MIN + 1); +static SIZE: AtomicUsize = AtomicUsize::new(7); +/// Read, never written: the static's memory. +static TABLE: [u8; 4] = [9, 8, 7, 6]; + +/// Each byte an operation on the statics; each puts what it read. First, what each holds, +/// which only the value it starts with can be. +pub fn statics(data: &[u8], out: &mut Vec) { + out.extend(COUNT.load(Relaxed).to_le_bytes()); + out.extend(WIDE.load(Relaxed).to_le_bytes()); + out.extend([ + SIGNED.load(Relaxed) as u8, + u8::from(FLAG.load(Relaxed)), + SIZE.load(Relaxed) as u8, + ]); + out.push(next(false)); + for &op in data { + let n = op / 6; + match op % 6 { + // Wraps past the type's end, as an atomic's arithmetic does. + 0 => out.extend(COUNT.fetch_add(u64::from(n), AcqRel).to_le_bytes()), + 1 => out.push(SIGNED.fetch_sub(n as i8, SeqCst) as u8), + 2 => out.push(u8::from(FLAG.fetch_xor(n % 2 == 0, Relaxed))), + 3 => out.extend(WIDE.swap(-i64::from(n), Acquire).to_le_bytes()), + 4 => { + let seen = SIZE.load(Relaxed); + out.push( + match SIZE.compare_exchange(seen, seen.wrapping_mul(usize::from(n)), SeqCst, Relaxed) { + Ok(old) => old as u8, + Err(_) => 0xfd, + }, + ); + } + 5 if n % 2 == 0 => out.push(next(false)), + // One table, wherever it is borrowed. + _ => out.extend([table()[usize::from(n) % 4], u8::from(std::ptr::eq(table(), table()))]), + } + } + COUNT.store(u64::MAX - 2, Relaxed); + SIGNED.store(-128, Relaxed); + FLAG.store(true, Relaxed); + WIDE.store(i64::MIN + 1, Relaxed); + SIZE.store(7, Relaxed); + next(true); +} + +fn table() -> &'static [u8; 4] { + &TABLE +} + +/// The next id from a static in a function, as kio's `waiter` takes one; starts over if `reset`. +fn next(reset: bool) -> u8 { + static NEXT: AtomicU64 = AtomicU64::new(1); + match reset { + true => { + NEXT.store(1, Relaxed); + 0 + } + false => NEXT.fetch_add(1, Relaxed) as u8, + } +} diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index b2412625f..fc1213895 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -44,7 +44,6 @@ | 1 | a cast from `_` to `_` | fixtures/reject/src/lib.rs:87:2 | | 1 | a cast from a pointer to `_` to one to `_`, which reads the bytes of one type as another | fixtures/reject/src/lib.rs:616:27 | | 1 | a coercion to `_` | fixtures/reject/src/lib.rs:344:2 | -| 1 | a constant of type `_` | fixtures/reject/src/lib.rs:80:2 | | 1 | a dangling pointer to `_`, a value of no bytes made of more than structs and tuples | fixtures/reject/src/lib.rs:701:11 | | 1 | a future of `_`, which can be `rt.Pending`, as the future's own `Pending` is | fixtures/reject/src/lib.rs:96:34 | | 1 | a mutable borrow of a subslice of `_`, which JS would copy, having no views | fixtures/reject/src/lib.rs:236:13 | @@ -56,6 +55,7 @@ | 1 | a raw pointer to `_` in a variable assigned again while it may point there | fixtures/reject/src/lib.rs:677:10 | | 1 | a raw pointer to `_` in an element, a payload, or a union field, which is not supported yet | fixtures/reject/src/lib.rs:685:10 | | 1 | a scalar constant of type `_` | fixtures/reject/src/lib.rs:8:2 | +| 1 | a static of type `_` | fixtures/reject/src/lib.rs:80:2 | | 1 | a trait object of `_`, which is not supported yet | fixtures/reject/src/lib.rs:535:26 | | 1 | a zero-sized constant of type `_` | fixtures/reject/src/lib.rs:62:2 | | 1 | an upcast from `_` to `_`, which is not supported yet | fixtures/reject/src/lib.rs:593:32 | @@ -175,7 +175,6 @@ The type or module each refused type or path belongs to. | 1 | a coercion to `_` | `std::sync::Arc` | fixtures/reject/src/lib.rs:344:2 | | 1 | a coercion to `_`, which is not supported yet | `&Tail` | fixtures/reject/src/lib.rs:545:30 | | 1 | a coercion to `_`, which is not supported yet | `std::sync::Weak` | fixtures/reject/src/lib.rs:509:40 | -| 1 | a constant of type `_` | `&u8` | fixtures/reject/src/lib.rs:80:2 | | 1 | a dangling pointer to `_`, a value of no bytes made of more than structs and tuples | `Only` | fixtures/reject/src/lib.rs:701:11 | | 1 | a future of `_`, which can be `rt.Pending`, as the future's own `Pending` is | `std::task::Poll` | fixtures/reject/src/lib.rs:96:34 | | 1 | a mutable borrow of a subslice of `_`, which JS would copy, having no views | `u32` | fixtures/reject/src/lib.rs:236:13 | @@ -184,6 +183,7 @@ The type or module each refused type or path belongs to. | 1 | a raw pointer to `_` in a variable assigned again while it may point there | `(u32, u32)` | fixtures/reject/src/lib.rs:677:10 | | 1 | a raw pointer to `_` in an element, a payload, or a union field, which is not supported yet | `(u32, u32)` | fixtures/reject/src/lib.rs:685:10 | | 1 | a scalar constant of type `_` | `f32` | fixtures/reject/src/lib.rs:8:2 | +| 1 | a static of type `_` | `u8` | fixtures/reject/src/lib.rs:80:2 | | 1 | a trait object of `_`, which is not supported yet | `[u8; 4]` | fixtures/reject/src/lib.rs:535:26 | | 1 | a zero-sized constant of type `_` | `{async closure}` | fixtures/reject/src/lib.rs:62:2 | | 1 | an assignment through a reference to `_` | `std::sync::MutexGuard` | fixtures/reject/src/lib.rs:102:2 | @@ -474,12 +474,6 @@ The type or module each refused type or path belongs to. -
a constant of type `_` (1) - -- fixtures/reject/src/lib.rs:80:2: a constant of type `&u8` - -
-
a dangling pointer to `_`, a value of no bytes made of more than structs and tuples (1) - fixtures/reject/src/lib.rs:701:11: a dangling pointer to `Only`, a value of no bytes made of more than structs and tuples @@ -546,6 +540,12 @@ The type or module each refused type or path belongs to.
+
a static of type `_` (1) + +- fixtures/reject/src/lib.rs:80:2: a static of type `u8` + +
+
a trait object of `_`, which is not supported yet (1) - fixtures/reject/src/lib.rs:535:26: a trait object of `[u8; 4]`, which is not supported yet diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index 99f6ba8be..e4c73c741 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -17,7 +17,7 @@ collecting `Option<()>`, whose `None` is `rt.None`, which is not supported yet a a zero-sized constant of type `{async closure@fixtures/reject/src/lib.rs:61:10: 61:18}` at fixtures/reject/src/lib.rs:62:2 `Pointer` formatting, as in `{:p}`, is not supported at fixtures/reject/src/lib.rs:68:11 unsupported call to `::write_fmt` at fixtures/reject/src/lib.rs:74:10 -a constant of type `&u8` at fixtures/reject/src/lib.rs:80:2 +a static of type `u8` at fixtures/reject/src/lib.rs:80:2 `Poll>>>` is not supported: its `Pending` and `Ready` would both be `rt.OuterPending` at fixtures/reject/src/lib.rs:91:20 a future of `std::task::Poll`, which can be `rt.Pending`, as the future's own `Pending` is at fixtures/reject/src/lib.rs:96:34 an assignment through a reference to `std::sync::MutexGuard<'_, u8>` at fixtures/reject/src/lib.rs:102:2 diff --git a/ts/runtime/rt/task.ts b/ts/runtime/rt/task.ts index e93364320..a68ea6355 100644 --- a/ts/runtime/rt/task.ts +++ b/ts/runtime/rt/task.ts @@ -4,7 +4,7 @@ // A value whose drop JS can observe has `[Symbol.dispose]`, which runs its `Drop` impl // and drops its fields. `None` is `undefined`, which drops as nothing. -import { Err, type None, OK, Ok, Panic, panic, type Place, type Result } from "../rt.ts"; +import { Err, type None, OK, Ok, Panic, panic, type Place, place, type Result } from "../rt.ts"; // Drops @@ -399,6 +399,41 @@ export function localKeyInitializeWith( return f(key.install(init) ? none : init, key.get()); } +/** + * `LocalKey::set` of a `RefCell` key: `value` becomes a lazy key's first value, or else + * replaces its value as an exclusive borrow would, after `dropOld` drops the old one. + */ +export function localKeyRefCellSet(key: LocalKey>, value: T, dropOld?: (old: T) => void): void { + if (key.install(new RefCell(value))) return; + const cell = refCellBorrowMut(key.get()); + dropOld?.(cell.value); + cell.value = value; + refMutRelease(cell); +} + +/** `LocalKey::with_borrow_mut`: `f` of its value, exclusively borrowed while `f` runs. */ +export function localKeyWithBorrowMut(key: LocalKey>, f: (value: T) => R): R { + const cell = refCellBorrowMut(key.get()); + const out = f(cell.value); + refMutRelease(cell); + return out; +} + +/** `localKeyWithBorrowMut` of a value JS copies, which `f` gets the place of. */ +export function localKeyWithBorrowMutPlace(key: LocalKey>, f: (value: Place) => R): R { + const cell = refCellBorrowMut(key.get()); + const out = f( + place( + () => cell.value, + (v) => { + cell.value = v; + }, + ), + ); + refMutRelease(cell); + return out; +} + // Atomics, which one thread never races: each operation reads and writes as it would // alone, and an ordering it can't take panics as std's does. diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index d929a7898..9fd70328c 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -545,6 +545,13 @@ pub fn from_mir<'tcx>(t: &Translator<'_, 'tcx>, instance: Instance<'tcx>) -> boo && t.self_ty(instance) .is_none_or(|ty| !(t.is_copy(ty) && clone_copies(t, ty))); } + // A `RefCell` key's `set` maps, as its body writes the cell in place. + let refcell_set = std == Some(Std::LocalKey) + && name.as_str() == "set" + && t.self_ty(instance).is_some_and(|key| { + matches!(key.kind(), ty::Adt(_, args) + if matches!(args.type_at(0).kind(), ty::Adt(adt, _) if Std::of(tcx, adt.did()) == Some(Std::RefCell))) + }); // A lock's `unwrap_or_else` maps, to unwrap its guard in place. let lock = std == Some(Std::Result) && name.as_str() == "unwrap_or_else" @@ -557,8 +564,12 @@ pub fn from_mir<'tcx>(t: &Translator<'_, 'tcx>, instance: Instance<'tcx>) -> boo ) | ( Some(Std::Result), "unwrap" | "expect" | "unwrap_err" | "is_ok" | "is_err" - ) | (Some(Std::LocalKey), "with" | "try_with" | "initialize_with") + ) | ( + Some(Std::LocalKey), + "with" | "try_with" | "initialize_with" | "with_borrow_mut" + ) ) && !lock + && !refcell_set } /// The key `def_id` makes the value of, if it is what `thread_local!` declares in a key's @@ -606,7 +617,7 @@ fn discards(owner: &str, name: &str) -> Option<&'static [usize]> { // `get_or_init::` drops its function uncalled if the cell is set. ("OnceCell" | "OnceLock", "get_or_init") => &[1], ("Arc" | "Rc", "new_cyclic") => &[1], - ("LocalKey", "with" | "try_with" | "initialize_with") => &[1], + ("LocalKey", "with" | "try_with" | "initialize_with" | "with_borrow_mut") => &[1], _ => return None, }) } diff --git a/ts/src/lower/mir/call.rs b/ts/src/lower/mir/call.rs index d8c73b132..ab0f6fb52 100644 --- a/ts/src/lower/mir/call.rs +++ b/ts/src/lower/mir/call.rs @@ -351,11 +351,13 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { return None; } let t = &*self.b.t; - // Mappings that lend an element JS copies as its place, as a map's `get_mut` does, or - // the place a `&mut dyn Any` wraps. + // 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. 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") => true, + ("slice", "get_mut" | "first_mut" | "last_mut" | "iter_mut") + | ("dyn Any", "downcast_mut") + | ("LocalKey", "with_borrow_mut") => true, ("IntoIterator", "into_iter") => gargs.types().next().is_some_and(|ty| { ty.ref_mutability() == Some(ty::Mutability::Mut) && match ty.peel_refs().kind() { diff --git a/ts/src/lower/mir/place.rs b/ts/src/lower/mir/place.rs index b594bc672..509f0cddf 100644 --- a/ts/src/lower/mir/place.rs +++ b/ts/src/lower/mir/place.rs @@ -4,11 +4,11 @@ //! struct is a member of the same object. A reference assigned once from a place that //! stays put reads as that place, which is how a `&mut` to a number changes the number. -use rustc_abi::{FieldIdx, VariantIdx}; +use rustc_abi::{FieldIdx, Size, VariantIdx}; use rustc_data_structures::fx::FxHashSet; use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, DefIndex}; -use rustc_middle::mir::interpret::{GlobalId, Scalar}; +use rustc_middle::mir::interpret::{GlobalAlloc, GlobalId, Scalar, alloc_range}; use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor}; use rustc_middle::mir::{self, ConstValue, Local, Operand, Place, PlaceTy, ProjectionElem}; use rustc_middle::ty::{self, Instance, Ty, TyCtxt, TypeVisitableExt}; @@ -19,7 +19,7 @@ use crate::lower::library::{Flow, Std}; use crate::lower::num::Int; use crate::lower::{AdtRepr, ObjectKind, byte_array, bytes_ref}; use crate::program::{SymKind, Symbol}; -use crate::ts::{Expr, Item, Stmt, StmtKind}; +use crate::ts::{Expr, Item, Stmt, StmtKind, SymId}; impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { /// A local read whole. @@ -1145,11 +1145,35 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { }) } + /// The symbol of the module value that stands for `item`, named for it, and whether + /// this made it, so that the caller adds its item. + fn module_value(&mut self, item: DefId) -> (SymId, bool) { + if let Some(&sym) = self.b.t.values.get(&item) { + return (sym, false); + } + let tcx = self.tcx; + let sym = self.b.t.program.sym(Symbol { + module: self.b.t.module_of(item), + base: tcx.item_name(item).to_string(), + suffix: String::new(), + owner: None, + kind: SymKind::Value, + order: self.b.t.order(tcx.def_span(item)), + instance: String::new(), + name: String::new(), + alias: None, + }); + self.b.t.values.insert(item, sym); + (sym, true) + } + /// The module value of the thread-local `key`, made the first time: an `rt.LocalKey` /// over what initializes it. fn local_key(&mut self, key: DefId, span: Span) -> Expr { let tcx = self.tcx; - if let Some(&sym) = self.b.t.local_keys.get(&key) { + // Before its initializer, which may read it. + let (sym, new) = self.module_value(key); + if !new { return Expr::Sym(sym); } let key_ty = tcx.type_of(key).instantiate_identity().skip_norm_wip(); @@ -1157,19 +1181,6 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { unreachable!("a thread-local's key is a LocalKey") }; let value_ty = args.type_at(0); - let sym = self.b.t.program.sym(Symbol { - module: self.b.t.module_of(key), - base: tcx.item_name(key).to_string(), - suffix: String::new(), - owner: None, - kind: SymKind::Value, - order: self.b.t.order(tcx.def_span(key)), - instance: String::new(), - name: String::new(), - alias: None, - }); - // Before its initializer, which may read it. - self.b.t.local_keys.insert(key, sym); let (init, lazy) = self.local_init(key, span); let mut ctor = vec![init, Expr::lit(if lazy { "true" } else { "false" })]; // What drops the value an initializer made from within itself, which only an @@ -1233,6 +1244,59 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { } } + /// The module value of the static `item`, which `ty` borrows whole, made the first time + /// from the memory it starts with, which a static has even when it is another crate's: + /// an atomic's `rt.Atomic`, or an immutable one's bytes, one object every use shares. + fn static_value(&mut self, item: DefId, ty: Ty<'tcx>, span: Span) -> Expr { + let tcx = self.tcx; + let static_ty = tcx.type_of(item).instantiate_identity().skip_norm_wip(); + if tcx.is_mutable_static(item) || ty.builtin_deref(true) != Some(static_ty) { + return self.err(span, format!("a constant of type `{ty}` into a static")); + } + let Ok(alloc) = tcx.eval_static_initializer(item) else { + return self.err(span, "a static that failed to evaluate"); + }; + let value = match static_ty.kind() { + // An `AtomicPtr`'s pointer has no `rt.Atomic`. + ty::Adt(adt, args) + if Std::of(tcx, adt.did()) == Some(Std::Atomic) + && (args.type_at(0).is_bool() || args.type_at(0).is_integral()) => + { + let int = args.type_at(0); + let env = self.b.t.typing_env_pub(); + let Ok(layout) = tcx.layout_of(env.as_query_input(int)) else { + return self.err(span, format!("a static of type `{static_ty}`")); + }; + let Ok(scalar) = alloc + .inner() + .read_scalar(&tcx, alloc_range(Size::ZERO, layout.size), false) + else { + return self.err(span, format!("a static of type `{static_ty}` that holds a pointer")); + }; + let int = self.const_value(ConstValue::Scalar(scalar), int, span); + Expr::New(Box::new(Expr::Rt("Atomic".into())), vec![int]) + } + ty::Array(elem, len) + if matches!(elem.kind(), ty::Uint(ty::UintTy::U8)) && len.try_to_target_usize(tcx).is_some() => + { + let len = len.try_to_target_usize(tcx).unwrap() as usize; + bytes_lit(alloc.inner().inspect_with_uninit_and_ptr_outside_interpreter(0..len)) + } + _ => return self.err(span, format!("a static of type `{static_ty}`")), + }; + let (sym, new) = self.module_value(item); + if new { + let ty = self.b.t.ty(ty, span); + self.b.t.program.items.push(Item::Value { + sym, + ty, + value, + doc: None, + }); + } + Expr::Sym(sym) + } + fn const_value(&mut self, val: ConstValue, ty: Ty<'tcx>, span: Span) -> Expr { let tcx = self.tcx; // An aggregate, field by field. @@ -1294,6 +1358,17 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { }, }, ConstValue::Scalar(Scalar::Ptr(ptr, _)) => { + let (prov, offset) = ptr.prov_and_relative_offset(); + let alloc = match tcx.global_alloc(prov.alloc_id()) { + GlobalAlloc::Static(item) => { + return match offset.bytes() { + 0 => self.static_value(item, ty, span), + _ => self.err(span, format!("a constant of type `{ty}` into a static")), + }; + } + GlobalAlloc::Memory(alloc) => alloc, + _ => return self.err(span, format!("a constant of type `{ty}`")), + }; // `&[u8; N]`, such as a byte string. let ty::Ref(_, inner, _) = ty.kind() else { return self.err(span, format!("a pointer constant of type `{ty}`")); @@ -1304,16 +1379,11 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { let (Some(len), ty::Uint(ty::UintTy::U8)) = (len.try_to_target_usize(tcx), elem.kind()) else { return self.err(span, format!("a constant of type `{ty}`")); }; - let (prov, offset) = ptr.prov_and_relative_offset(); - let alloc = tcx.global_alloc(prov.alloc_id()).unwrap_memory(); let start = offset.bytes() as usize; - let bytes = alloc - .inner() - .inspect_with_uninit_and_ptr_outside_interpreter(start..start + len as usize); - let items = bytes.iter().map(|b| Expr::lit(b.to_string())).collect(); - Expr::rt( - "byteSliceFrom", - vec![Expr::ident("Uint8Array").member("of").call(items)], + bytes_lit( + alloc + .inner() + .inspect_with_uninit_and_ptr_outside_interpreter(start..start + len as usize), ) } ConstValue::Slice { .. } => { @@ -1325,13 +1395,7 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { Ok(s) => Expr::Str(s.to_string()), Err(_) => self.err(span, "a string constant that is not UTF-8"), }, - ty::Slice(elem) if matches!(elem.kind(), ty::Uint(ty::UintTy::U8)) => { - let items = bytes.iter().map(|b| Expr::lit(b.to_string())).collect(); - Expr::rt( - "byteSliceFrom", - vec![Expr::ident("Uint8Array").member("of").call(items)], - ) - } + ty::Slice(elem) if matches!(elem.kind(), ty::Uint(ty::UintTy::U8)) => bytes_lit(bytes), _ => self.err(span, format!("a slice constant of type `{ty}`")), } } @@ -1688,3 +1752,12 @@ impl<'tcx> Fx<'_, '_, '_, 'tcx> { false } } + +/// An `rt.ByteSlice` of `bytes`. +fn bytes_lit(bytes: &[u8]) -> Expr { + let items = bytes.iter().map(|b| Expr::lit(b.to_string())).collect(); + Expr::rt( + "byteSliceFrom", + vec![Expr::ident("Uint8Array").member("of").call(items)], + ) +} diff --git a/ts/src/lower/mod.rs b/ts/src/lower/mod.rs index 630e48fc5..8c1227cf0 100644 --- a/ts/src/lower/mod.rs +++ b/ts/src/lower/mod.rs @@ -202,8 +202,8 @@ pub struct Translator<'a, 'tcx> { pub drop_shims: rustc_data_structures::fx::FxHashMap, SymId>, /// The function copying each `Copy` aggregate type. copy_shims: rustc_data_structures::fx::FxHashMap, SymId>, - /// Each thread-local's module value, by its key's `const` item. - pub local_keys: rustc_data_structures::fx::FxHashMap, + /// Each module value, by the item it stands for: a thread-local's key or a static. + pub values: rustc_data_structures::fx::FxHashMap, /// The types the crate writes into in place, which alone are copied. writes: copy::Writes<'tcx>, /// The bodies of other crates whose writes count: those the run before reached. @@ -392,7 +392,7 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { object_classes: Default::default(), drop_shims: Default::default(), copy_shims: Default::default(), - local_keys: Default::default(), + values: Default::default(), // Once the bindings are known. writes: Default::default(), read, diff --git a/ts/src/lower/task.rs b/ts/src/lower/task.rs index 61f98fc12..265dbc08c 100644 --- a/ts/src/lower/task.rs +++ b/ts/src/lower/task.rs @@ -240,6 +240,18 @@ pub fn call<'tcx>( let [key, f] = two(args); Expr::result(true, f.call(vec![key.member("get").call(Vec::new())])) } + // With what drops the value it replaces. + ("LocalKey", "set") => { + let Some(droppers) = b.t.droppers([call.args.type_at(0)], call.span) else { + return Ok(Some(Expr::undefined())); + }; + Expr::rt("localKeyRefCellSet", args.into_iter().chain(droppers).collect()) + } + // With the value's place where JS would copy it. + ("LocalKey", "with_borrow_mut") => match b.t.needs_place(call.args.type_at(0)) { + true => Expr::rt("localKeyWithBorrowMutPlace", args), + false => Expr::rt("localKeyWithBorrowMut", args), + }, // With the `Option`'s `None` where it is `rt.None`. ("LocalKey", "initialize_with") => { let none = b.t.none_arg(call.args.type_at(0));