Skip to content

feat(ts): translate static atomics, and a RefCell thread-local's set and with_borrow_mut - #232

Merged
kixelated merged 6 commits into
mainfrom
quest/m2/ts-shared
Oct 29, 2026
Merged

kixelated merged 6 commits into
mainfrom
quest/m2/ts-shared

Conversation

@kixelated

@kixelated kixelated commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Claims quest/m2/ts-shared.md.

Problem

A static atomic, as kio's waiter and moq-net's listing take their ids from (static NEXT: AtomicU64), was refused as "a constant of type &Atomic". A RefCell thread-local's set and with_borrow_mut were refused, because std's bodies write the cell in place. And a reference to an immutable static's bytes (&TABLE for static TABLE: [u8; 4]) crashed the translator: expected memory, got Static.

Approach

  • A &STATIC reaches the translator as a pointer constant into a GlobalAlloc::Static, and each static becomes one module value. A static atomic is export const NEXT: rt.Atomic<rt.u64> = /* @__PURE__ */ new rt.Atomic(1), the same Item::Value a thread-local's key is. Its initial integer is read from the static's evaluated allocation, which works when the static is another crate's, as kio's is to moq-net. Thread-local keys and statics now share one map of module values.
  • AtomicPtr statics, static mut, and a borrow of part of a static are refused with a message naming them. An immutable [u8; N] static is its bytes, export const TABLE: rt.ByteSlice, one object every borrow returns, so ptr::eq holds; it crashed before. Any other static is refused as a static of its type.
  • LocalKey::<RefCell<T>>::set maps to rt.localKeyRefCellSet: on a lazy key's first use it installs the value without running the initializer, and otherwise it borrows exclusively, drops the old value, and writes. with_borrow_mut maps to rt.localKeyWithBorrowMut, or …Place with an rt.Place over the cell's value when JS copies T. It is listed among the mappings that lend a place.

Tests:

  • The new statics lang fixture covers every atomic kind at module level and in a function, with initial values at the types' extremes and the immutable byte table and its identity, all differential against native wasm32.
  • locals adds a RefCell<u32> key (place path), with_borrow_mut that drops what it held, and set on RefCell keys.
  • Both fixtures now output every value at the start of each case. Before, the end-of-case reset stored the true initial values, so a wrong initial value was never seen: a mutation starting every static at 0 survived until this change, and is now caught.
  • Mutations caught: a wrong static initial value (1 difference, the first case), localKeyRefCellSet not dropping the old value (276), and the place's setter writing nothing (406).

Census (refusals, main → this branch): kio 154 → 153, model 541 → 537, moq-net 1614 → 1607. The remaining static refusals are AtomicPtr statics in parking_lot and wasmtimer.

No hot path changes (module values and a few runtime helpers), so no bench.

Impact

  • Output: a static atomic is an exported module value named for the static.
  • Runtime: new rt.localKeyRefCellSet, rt.localKeyWithBorrowMut, and rt.localKeyWithBorrowMutPlace. All additive, so no MIGRATE.md entry.

Alternatives

  • Translate the static's initializer MIR, as a thread-local's const is: another crate's static has no such MIR in metadata.
  • Let std's with_borrow_mut and set bodies lower: they write in place, which the crate's copy analysis of its own bodies can't see.

Follow-ups

  • The quest keeps its last item, measuring the counters' cost on the TS bench.
  • AtomicPtr statics, other interior-mutable statics such as OnceLock and LazyLock, and a const of an atomic (lock_api's RawMutex::INIT) are still refused.

Review

Local review: Codex (quest review), gpt-6.1-sol, medium effort, on head 4c4c8e4d062a06411b7f9c01794b44198245dd0e. Round 2: no findings. The head since, ee1afffb815c25824356442ac8a2574100670576, only adds a conflict-free merge of main (#236), with snapshots unchanged.

Round 1 (head 4dad3461), one finding:

  • Fixed: an immutable byte static (&TABLE) built a fresh Uint8Array at each use, so ptr::eq(table(), table()) was false. Such a static is now one module value every use returns. The statics fixture checks ptr::eq, which reports 440 differences against the old per-use copy.

🤖 Generated with Claude Code

(written by Claude Opus 5.5)

kixelated and others added 5 commits October 29, 2026 13:07
Quest-Claim: c5cfab38ff419e2b
…nd with_borrow_mut

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tomics

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title Shared ownership in the TypeScript translator feat(ts): translate static atomics, and a RefCell thread-local's set and with_borrow_mut Oct 29, 2026
@kixelated
kixelated marked this pull request as ready for review October 29, 2026 20:59
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-29T21:17:30.829308Z ee1afff New commits
🔒 Security Review ✅ Completed 2026-10-29T21:18:17.876307Z ee1afff New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: 4c4c8e4

No actionable findings. Reusing one module value per static preserves identity and atomic state; reading evaluated initialization avoids relying on another crate's initializer MIR. Unsupported static forms remain explicit refusals. The RefCell helpers retain the exclusive borrow through mutation/drop and use Place for values that require write-back, while Cell helpers keep their existing lowering.

The fixtures check initial values before reset, shared byte-table identity, integer extremes, mutation and observable drops. A useful nonblocking addition would exercise RefCell::set before the first get, with an initializer side effect, to directly check the lazy-install path.

Verification: static diff, surrounding lowering/runtime logic and generated fixtures, with an independent pass. No translator, TypeScript compilation, differential tests, mutation checks or census runs performed; author-reported results were not independently reproduced.

(Written by OpenAI)

@kixelated
kixelated merged commit 5318470 into main Oct 29, 2026
1 check passed
@kixelated
kixelated deleted the quest/m2/ts-shared branch October 29, 2026 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant