Skip to content

feat(ts): translate std's Once over the runtime's OnceLock - #262

Merged
kixelated merged 5 commits into
mainfrom
quest/m2/ts-model/once
Oct 30, 2026
Merged

kixelated merged 5 commits into
mainfrom
quest/m2/ts-model/once

Conversation

@kixelated

@kixelated kixelated commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

crossbeam-epoch keeps its collector in its own OnceLock, a std Once beside an UnsafeCell<MaybeUninit<T>>. std's Once had no mapping, so the model's census refused it 8 times: the type, the static's memory, is_completed, and call_once. Quest: quest/m2/ts-model/once.md.

Approach

Once reuses the runtime's OnceLock rather than adding a cell: rt.Once is an rt.OnceLock<true>.

  • Once::new(), and a Once read from a static's or const's memory in its Incomplete state, are new rt.OnceLock(undefined). Any other state is refused, as for OnceLock.
  • call_once and call_once_force are rt.onceCall. It runs f through onceGetOrInit, or drops f uncalled once the Once has run (discards).
  • Calling either again from inside its own function panics with wasm32 no_threads' message, one-time initialization may not be performed recursively.
  • is_completed is v === true.
  • OnceState is the Once itself. is_poisoned is rt.onceIsPoisoned, which always returns false: a panic aborts, so poisoning is never observable without unwinding. poison is pub(crate) in std, so no crate can poison one either.
  • wait, wait_force, and Debug stay refused.

Differential cases in statics::onces, run against native wasm32:

  • a crossbeam-shaped Slot in a static, read from memory
  • a static Once
  • local Onces: run once, call_once_force's state, a function dropped uncalled (seen through an Rc count)
  • reentrant call_once and call_once_force, comparing the panic message with wasm32's

Named scripts in caller.ts make sure these run. I checked that the test catches regressions: a mutated panic message failed 669 cases, and skipping the drop failed 23.

Census, from just ts census at the pinned moq revision:

Census Before After
model 497 489
moq-net 1572 1564
kio 153 145

Every Once refusal is gone and no new refusal appears. crossbeam-epoch's OnceLock<Collector> is now refused only at its UnsafeCell: the field at sync/once_lock.rs:11, and get at lines 64 and 77.

Impact

  • Translator output: std::sync::Once and OnceState now translate.
  • New runtime exports: rt.Once, rt.OnceState, rt.onceCall, rt.onceIsPoisoned.
  • Nothing breaks, so there's no MIGRATE.md entry.

Alternatives

  • A separate rt.Once class with its own state. Dropped: OnceLock already tracks incomplete, running, and complete, including the reentrancy panic.
  • Representing Once as OnceLock<()> with rt.None. Clumsier output, and it needs a sentinel None at every call.
  • Mapping is_poisoned to a bare false. This leaves the closure's state parameter unread, which tsc rejects.

Follow-ups

The quest's Goal also needs std's UnsafeCell, which nothing translates yet (10 refusals in the model's census). This PR rewrites the quest to that remaining work as quest/m2/ts-model/unsafe-cell.md, at the same rank in the epic, rather than deleting it.

Choices made without asking:

  • Kept that work in m2, as the original quest was.
  • Sized it [M].

Review

  • Reviewer: quest review, Codex gpt-6.1-sol, medium effort.
  • Round 1, head 780e1e8: no findings.
  • Round 2, head 49828c5: no findings. This round covers the merge of origin/main with hand-resolved conflicts in harness.rs, caller.ts, and the regenerated expected/harness.ts.
  • Local checks: just ci passed on 49828c5.

Merge of main at 23b09571c0aa65fe273f47bfc2d077073ee28bf9: the lang harness case list conflicted with #259's cases; resolved by keeping main's cases then statics::onces, and the regenerated expected/harness.ts only adds that case and renumbers one placeholder. just ci passed on this exact tree. No translator or runtime code changed since 49828c55, so this is a recorded skip, too trivial to review, on top of round 2's clean review.

(written by Claude Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 3 commits October 30, 2026 01:19
Quest-Claim: f1bf91be7dd6a091
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/fixtures/lang/caller.ts
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
@kixelated kixelated changed the title A std Once in a static feat(ts): translate std's Once over the runtime's OnceLock Oct 30, 2026
@kixelated
kixelated marked this pull request as ready for review October 30, 2026 08:55
@coderabbitai

coderabbitai Bot commented Oct 30, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 59 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c167a4dc-38a5-4635-965b-51621498b6cd
Commits

Reviewing files that changed from the base of the PR and between 5b6adaa and 49828c5.

Files selected for processing: 15
  • quest/m2/ts-model/README.md
  • quest/m2/ts-model/once.md
  • quest/m2/ts-model/unsafe-cell.md
  • ts/README.md
  • ts/fixtures/lang/caller.ts
  • ts/fixtures/lang/expected/harness.ts
  • ts/fixtures/lang/expected/statics.Slot.ts
  • ts/fixtures/lang/expected/statics.ts
  • ts/fixtures/lang/src/harness.rs
  • ts/fixtures/lang/src/statics.rs
  • ts/runtime/rt/task.ts
  • ts/src/lower/library.rs
  • ts/src/lower/mir/memory.rs
  • ts/src/lower/mod.rs
  • ts/src/lower/task.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

kixelated and others added 2 commits October 30, 2026 01:56
# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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: 49828c55f9a88cbf74cf9d18c6042d30b4d5545f.

No actionable findings. The shared OnceLock state preserves recursive-initialization checks and publishes completion only after the callback returns. Already-completed calls drop the uncalled closure, and constant-memory lowering accepts only the incomplete state. The non-poisoned OnceState matches the documented single-threaded wasm32 abort model.

Direction: reusing OnceLock is simpler than maintaining a second state machine. Keeping the remaining UnsafeCell work explicit avoids claiming that the full crossbeam collector now translates. The differential fixtures cover local/static instances, completion observed during initialization, forced calls, owned captures and recursion. A useful small extension is an owned capture dropped by an already-completed ordinary call_once, beside the forced-call case.

Verification limits: static GitHub review with an independent runtime/drop pass. I did not execute the translator, differential tests, mutation tests, census or just ci; reported results are not independently verified.

(Written by OpenAI)

@kixelated
kixelated merged commit c1f1bc0 into main Oct 30, 2026
1 check passed
@kixelated
kixelated deleted the quest/m2/ts-model/once branch October 30, 2026 09:08
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