-
-
Notifications
You must be signed in to change notification settings - Fork 163
feat(runtime): perry/tui handles are ordinary objects (#10821 row 3) #10915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| `perry/tui` now hands TypeScript real objects instead of small registry | ||
| integers. Every value the module returned — a widget from `Text` / `Box` / | ||
| `Table` / `Tabs` / …, a `state(initial)` container, a `useRef` box, and the | ||
| `useApp()` / `useStdout()` / `useFocusManager()` singletons — used to be an id | ||
| NaN-boxed with `POINTER_TAG`, a number pretending to be a pointer. Three | ||
| registries minted those ids and three more were plain constants, so SIX id | ||
| spaces shared one encoding and they collided: | ||
|
|
||
| useApp() -> 1 Text("hi") -> 1 useRef(x), first -> 1 | ||
| useStdout() -> 2 Box() -> 2 useRef(y), second -> 2 | ||
| useFocusManager() -> 3 Spacer() -> 3 | ||
| state(0), first -> 0 <- POINTER_TAG | 0, a null pointer wearing | ||
| the pointer tag | ||
|
|
||
| `useApp() === Text("hi")` was therefore `true`, a `Map` or `Set` keyed on two | ||
| different handles kept one entry, a `WeakMap` entry stored under a widget was | ||
| readable through the App handle, and the first `state(0)` of a program was a | ||
| tagged null. None of it was reachable through a type error: the values are | ||
| indistinguishable at run time, because the encoding carries no provenance. | ||
|
|
||
| Each kind is now a `GC_TYPE_OBJECT` with its own class id, a real ShapeId and | ||
| ZERO own keys, so `typeof` is `"object"`, `Object.keys` is `[]`, | ||
| `JSON.stringify` is `{}` (it was `null`), spread and `Object.assign` copy | ||
| nothing, and two handles are two values. `useApp()`, `useStdout()` and | ||
| `useFocusManager()` still answer the SAME object on every call — ink's do, and | ||
| perry's did too while they were constants — so they are per-realm singletons in | ||
| rooted slots rather than re-minted per call. `useRef` is likewise stable across | ||
| renders: the hook slot owns its handle object. | ||
|
|
||
| `state.get()` / `.set(v)`, `ref.get()` / `.set(v)`, `app.exit()` / | ||
| `.waitUntilExit()`, `stdout.write()` / `.columns()` / `.rows()` and | ||
| `focusManager.focusNext()` / `.focusPrevious()` / `.focus(id)` are now real | ||
| methods on a per-kind prototype as well as the statically lowered | ||
| `class_filter` rows they already were. Before this they existed ONLY as static | ||
| lowerings, so a handle reached through an untyped value (`const s: any = state(0)`) | ||
| answered `undefined` for every one of them. | ||
|
|
||
| The registry ids are unchanged and stay the module's internal currency: the | ||
| widget tree, the Taffy layout pass, the paint pass and the hook slots all still | ||
| speak ids, and only the value that crosses the FFI boundary changed. A handle | ||
| of one kind can no longer address another kind's registry entry, which the | ||
| overlapping id spaces previously allowed — `tui::is_known_handle` and the three | ||
| `contains_handle` probes it unioned are deleted, because a class-id load answers | ||
| the same question without asking three mutexes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| //! The class ids of runtime classes whose instances are ORDINARY objects | ||
| //! (#340/#341) — one place that says which id belongs to which family. | ||
| //! | ||
| //! Honest tags gives every native-backed value a `GC_TYPE_OBJECT` carrying a | ||
| //! family class id, and those ids were being minted family by family as bare | ||
| //! constants in the family's own module. There is no central allocator for | ||
| //! class ids in the tree, per-module class-id collisions are a known open | ||
| //! problem (#10824: a class id must also never alias a live `ShapeId`), and | ||
| //! the worker-transfer guard had grown into a range spelled | ||
| //! `TEXT_ENCODER_CLASS_ID..=IMMEDIATE_CLASS_ID` **inside `text.rs`** — a check | ||
| //! about every family, living in one family's file, widened by hand each time | ||
| //! a family landed. | ||
| //! | ||
| //! This module owns the `0xFFFF_24xx` web-builtin block instead. A family | ||
| //! declares its own alias from here (so its call sites are unchanged), the | ||
| //! range is closed by construction, and the const assertions below fail the | ||
| //! build rather than a test if two families ever name one id or if an id | ||
| //! wanders into the `ShapeId` window. | ||
| //! | ||
| //! **Allocating an id: take the next value, add it to `ALL` and to the | ||
| //! `is_native_backed_class_id` range if the family owns native state.** Do not | ||
| //! reuse a retired id: a stale compiled artifact naming it would brand the | ||
| //! wrong family. | ||
|
|
||
| /// Web-builtin block start. `ShapeId`s live in `[0x8000_0000, 0xC000_0000)` | ||
| /// (`object/shapes.rs`), so this block is far above them; the assertion at the | ||
| /// bottom of the file pins that rather than trusting the comment (#10824). | ||
| pub(crate) const WEB_BUILTIN_BLOCK_START: u32 = 0xFFFF_2401; | ||
|
|
||
| // --- Pre-existing, declared elsewhere and aliased here so the block is one | ||
| // --- list. These six are ordinary objects already, but their state is own | ||
| // --- fields rather than prototype accessors (#10823-shaped), so they are not | ||
| // --- yet `is_native_backed_class_id`. | ||
| pub(crate) const ABORT_CONTROLLER: u32 = 0xFFFF_2401; | ||
| pub(crate) const ABORT_SIGNAL: u32 = 0xFFFF_2402; | ||
| pub(crate) const EVENT: u32 = 0xFFFF_2403; | ||
| pub(crate) const CUSTOM_EVENT: u32 = 0xFFFF_2404; | ||
| pub(crate) const DOM_EXCEPTION: u32 = 0xFFFF_2405; | ||
| pub(crate) const EVENT_TARGET: u32 = 0xFFFF_2406; | ||
|
|
||
| // --- Migrated families. Each carries native state in `ObjectMeta.native_state` | ||
| // --- and is therefore refused by the worker-transfer guard. | ||
| pub(crate) const TEXT_ENCODER: u32 = 0xFFFF_2407; | ||
| pub(crate) const TEXT_DECODER: u32 = 0xFFFF_2408; | ||
| pub(crate) const TIMEOUT: u32 = 0xFFFF_2409; | ||
| pub(crate) const IMMEDIATE: u32 = 0xFFFF_240A; | ||
| pub(crate) const TUI_WIDGET: u32 = 0xFFFF_240B; | ||
| pub(crate) const TUI_STATE: u32 = 0xFFFF_240C; | ||
| pub(crate) const TUI_REF_BOX: u32 = 0xFFFF_240D; | ||
| pub(crate) const TUI_APP: u32 = 0xFFFF_240E; | ||
| pub(crate) const TUI_STDOUT: u32 = 0xFFFF_240F; | ||
| pub(crate) const TUI_FOCUS_MANAGER: u32 = 0xFFFF_2410; | ||
|
|
||
| /// The first id in the native-state range and the last one, inclusive. Every | ||
| /// family between them carries a `native_state` word the far side of a | ||
| /// `postMessage` could not reconstruct. | ||
| const NATIVE_BACKED_FIRST: u32 = TEXT_ENCODER; | ||
| const NATIVE_BACKED_LAST: u32 = TUI_FOCUS_MANAGER; | ||
|
|
||
| /// Class ids whose instances are ordinary objects carrying native state that | ||
| /// cannot cross a thread boundary (#340/#341). | ||
| /// | ||
| /// A contiguous range, so a family joins it by taking the next id rather than | ||
| /// by editing the transfer path. `thread.rs` calls this in its | ||
| /// `GC_TYPE_OBJECT` arm: an ordinary object no longer hits the "kinds 13-16 | ||
| /// are native handles" rejection, so without this a `postMessage`d handle | ||
| /// would arrive as a plain `{}` with no native state instead of the named | ||
| /// `TypeError` #6185 made these surface. | ||
| pub(crate) fn is_native_backed_class_id(class_id: u32) -> bool { | ||
| (NATIVE_BACKED_FIRST..=NATIVE_BACKED_LAST).contains(&class_id) | ||
| } | ||
|
|
||
| /// Every id this module hands out, newest last. Used by the assertions below | ||
| /// and by the uniqueness test. | ||
| const ALL: &[u32] = &[ | ||
| ABORT_CONTROLLER, | ||
| ABORT_SIGNAL, | ||
| EVENT, | ||
| CUSTOM_EVENT, | ||
| DOM_EXCEPTION, | ||
| EVENT_TARGET, | ||
| TEXT_ENCODER, | ||
| TEXT_DECODER, | ||
| TIMEOUT, | ||
| IMMEDIATE, | ||
| TUI_WIDGET, | ||
| TUI_STATE, | ||
| TUI_REF_BOX, | ||
| TUI_APP, | ||
| TUI_STDOUT, | ||
| TUI_FOCUS_MANAGER, | ||
| ]; | ||
|
|
||
| /// Strictly ascending ⟹ no two families share an id, and the block stays | ||
| /// dense so `is_native_backed_class_id` can remain one range compare. A | ||
| /// `const fn` loop rather than a test: a duplicated id must not be able to | ||
| /// reach a build at all, and a `debug_assert` would enforce nothing in release | ||
| /// (#10824 was a SHIPPED aliasing bug). | ||
| const fn strictly_ascending(ids: &[u32]) -> bool { | ||
| let mut i = 1; | ||
| while i < ids.len() { | ||
| if ids[i - 1] >= ids[i] { | ||
| return false; | ||
| } | ||
| i += 1; | ||
| } | ||
| true | ||
| } | ||
|
|
||
| const _: () = assert!(strictly_ascending(ALL), "two families claim one class id"); | ||
| const _: () = assert!(ALL[0] == WEB_BUILTIN_BLOCK_START); | ||
| // A class id must never alias a live ShapeId: the shape store mints into | ||
| // `[0x8000_0000, 0xC000_0000)` and a collision would make a shape compare | ||
| // answer for a family brand. | ||
| const _: () = assert!(NATIVE_BACKED_FIRST >= 0xC000_0000); | ||
| const _: () = assert!(NATIVE_BACKED_LAST == ALL[ALL.len() - 1]); | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| /// Every family that owns a `native_state` word is inside the transfer | ||
| /// guard's range, and the classes that do NOT own one are outside it. The | ||
| /// second half is what a new family gets wrong: taking the next id makes | ||
| /// it non-transferable automatically, which is right, but an id added | ||
| /// BELOW the range would silently ship a family that deep-copies into a | ||
| /// worker as an empty object. | ||
| #[test] | ||
| fn the_transfer_guard_covers_exactly_the_migrated_families() { | ||
| for id in [ | ||
| TEXT_ENCODER, | ||
| TEXT_DECODER, | ||
| TIMEOUT, | ||
| IMMEDIATE, | ||
| TUI_WIDGET, | ||
| TUI_STATE, | ||
| TUI_REF_BOX, | ||
| TUI_APP, | ||
| TUI_STDOUT, | ||
| TUI_FOCUS_MANAGER, | ||
| ] { | ||
| assert!( | ||
| is_native_backed_class_id(id), | ||
| "{id:#x} carries native state but crosses a thread boundary" | ||
| ); | ||
| } | ||
| for id in [ | ||
| ABORT_CONTROLLER, | ||
| ABORT_SIGNAL, | ||
| EVENT, | ||
| CUSTOM_EVENT, | ||
| DOM_EXCEPTION, | ||
| EVENT_TARGET, | ||
| 0, | ||
| 1, | ||
| 0x8000_0000, | ||
| ] { | ||
| assert!(!is_native_backed_class_id(id), "{id:#x} is not migrated"); | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Correct the comment:
tui::is_known_handleis deleted, not surviving.The comment states that
tui::is_known_handlesurvives for the ledger's own question.crates/perry-runtime/src/tui/mod.rslines 48-55 and the changelog both record that this PR deletes the function together with the threecontains_handleprobes. A reader who follows this comment searches for a symbol that no longer exists.📝 Proposed comment fix
📝 Committable suggestion
🤖 Prompt for AI Agents