Skip to content

feat(ts): write a lazy cell, a lazy lock, a poll_fn, and a thread-local key whole through a reference - #508

Merged
kixelated merged 6 commits into
mainfrom
quest/m3/ts-lazy-writes
Nov 3, 2026
Merged

kixelated merged 6 commits into
mainfrom
quest/m3/ts-lazy-writes

Conversation

@kixelated

@kixelated kixelated commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A whole write through a &mut to a LazyCell, LazyLock, PollFn, or LocalKey (*r = v, mem::replace, mem::swap, Cell::from_mut(r).set, a mapped RefMut) was refused since #386, because the runtime keeps their state in #private fields that the Object.assign field copy misses.

Approach

  • rt.LazyCell (so rt.LazyLock) and rt.PollFn each own a $move(to?) method that moves their private state into to, or into a new object. ptrWrite, replaceIn, objectCopy (so cellIn and partPlace) use it when an object has it. *r = v on these types lowers to rt.ptrWrite instead of Object.assign.
  • A LocalKey is a handle: every copy of the const names the same storage, so moving its state into another key would change that key for every user. A &mut LocalKey is now an rt.Place, as an Arc's is, so a write changes which key the place holds. A key's const used as a value, not only borrowed, now maps to its module value. Without that, a stock crate can't get an owned key to write.
  • The method is a plain name ($move), not a symbol, because esbuild keeps any class with a computed key and that broke the codec tree-shake test. No Rust field can be named $move.

Tests, each differential against native Rust:

  • lang/statics writes: a LazyCell and a LazyLock whose function owns a value with a Drop impl. Each write way, with neither, either, or both forced first. It checks Debug, the value, how many times the function ran, and drop order.
  • lang/locals keys: a copy of one key written with another by each way. It checks which key the copy names after the write and that neither key's storage changed.
  • task futures::boxed: poll_fns written by each way, plus a Pin<Box<PollFn>> written whole. It checks drop order and which function polls.

I confirmed the statics and task tests fail when the runtime move is disabled. The four reject cases these replace are removed.

Impact

  • Translator output: these writes now translate instead of being refused. A &mut LocalKey is an rt.Place<rt.LocalKey<T>>.
  • Runtime: objectCopy and ptrWrite move $move classes. rt.PollFn's fields are no longer readonly. Nothing is breaking.

Alternatives

  • A symbol key (rt.MOVE): this broke tree-shaking (above).
  • Static dispatch in the translator for every runtime helper: this adds a variant of replaceIn, cellIn, and partPlace per class, for the same result.
  • Moving a LocalKey's state like a lazy cell's: this is wrong, because the key's other copies share that storage.

Follow-ups

None.

Review

Codex gpt-6.1-sol, medium, round 1 on d90460f: no findings. Since then, a conflict-free merge of origin/main into 26361c3.

🤖 Generated with Claude Code

kixelated and others added 6 commits November 3, 2026 01:19
Quest-Claim: 8179264eb8ad9f1d
…al key whole through a reference

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/fixtures/reject/census.txt
#	ts/fixtures/reject/expected.txt
#	ts/runtime/rt.ts
#	ts/runtime/rt/task.ts
@kixelated kixelated changed the title Whole writes into a lazy cell or thread-local key feat(ts): write a lazy cell, a lazy lock, a poll_fn, and a thread-local key whole through a reference Nov 3, 2026
@kixelated
kixelated marked this pull request as ready for review November 3, 2026 09:50
@kixelated
kixelated added this pull request to the merge queue Nov 3, 2026
@coderabbitai

coderabbitai Bot commented Nov 3, 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 38 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: 11635e49-904a-4e8d-a61b-22b8bdfbfa21
Commits

Reviewing files that changed from the base of the PR and between 0bebbec and 26361c3.

Files selected for processing: 26
  • quest/m3/README.md
  • quest/m3/ts-lazy-writes.md
  • ts/README.md
  • ts/fixtures/lang/locals/expected/lib.ts
  • ts/fixtures/lang/locals/expected/locals.ts
  • ts/fixtures/lang/locals/expected/std/thread/local.ts
  • ts/fixtures/lang/locals/src/lib.rs
  • ts/fixtures/lang/locals/src/locals.rs
  • ts/fixtures/lang/statics/caller.ts
  • ts/fixtures/lang/statics/expected/lib.ts
  • ts/fixtures/lang/statics/expected/statics.Dropped.ts
  • ts/fixtures/lang/statics/expected/statics.ts
  • ts/fixtures/lang/statics/expected/std/thread/local.ts
  • ts/fixtures/lang/statics/src/lib.rs
  • ts/fixtures/lang/statics/src/statics.rs
  • ts/fixtures/reject/census.txt
  • ts/fixtures/reject/expected.txt
  • ts/fixtures/reject/src/places.rs
  • ts/fixtures/reject/src/statics.rs
  • ts/fixtures/reject/ts.toml
  • ts/fixtures/task/expected/futures.ts
  • ts/fixtures/task/src/futures.rs
  • ts/runtime/rt.ts
  • ts/runtime/rt/task.ts
  • ts/src/lower/mir/place.rs
  • ts/src/lower/mod.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

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.

Merged via the queue into main with commit a5b0023 Nov 3, 2026
5 checks passed
@kixelated
kixelated deleted the quest/m3/ts-lazy-writes branch November 3, 2026 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant