feat: build host-import modules in Rust from shape data (#37) - #44
Merged
Conversation
schplitt
added a commit
that referenced
this pull request
Jul 25, 2026
Closes #37. ## What Host modules no longer reach the runtime as TS-generated ESM source. The client ships the module **shape as plain data** (source modules unchanged; host modules as a tree of function-leaf markers, data leaves as `WireValue`s, nested objects), and the Rust runtime builds the module natively. ### How the module is built (import.meta indirection) The runtime emits only a fixed-shape template — `export const search = import.meta.__iso4[0];` with identifier-validated export names and integer indices. Every value is built natively: - data leaves via the existing `wire_to_v8_value` - function leaves as async trampolines from a fixed factory, with the handle ID passed as a **number**, never printed into source The values array is attached through V8's import-meta callback. Host modules therefore stay ordinary source-text modules, which keeps prefix **snapshots** working with no external-reference bookkeeping (the documented `import { search } from 'tools:x'; globalThis.search = search` prefix pattern is covered by new Rust tests, including a full snapshot roundtrip). Synthetic V8 modules were deliberately not used — their native evaluation-steps pointer would need external references on snapshot create *and* restore, where a miss is a crash. ### Rust owns the handle table Handle IDs are assigned in tree-walk order over the declared bindings and never leave the process. The `__iso4_call` dispatcher stub is now installed by the runtime itself (the client no longer sends a def for it); it resolves the leading ID before the frame is written, so: - `BridgeCall` frames carry `targetKind: import` + resolved `specifier` and leaf path (the previously-dead protocol fields) - bridge-call records carry fully resolved names (`tools:search.query`; shim stubs resolve to their public name Rust-side too) - `bridge-report.ts` (client-side name resolution) is **deleted** ### Rebind validation unified in Rust `PrefixRun` sends only rebind **locations** (`specifier` + leaf path); the runtime validates them against the shape stored with the prefix and rejects undeclared paths, data leaves, and source modules with `ERR_UNDECLARED_BINDING` — the same enforcement point as undeclared globals, no longer skippable by a non-TS client. The client keeps compile-time `RebindImports<M>` plus the client-only checks (non-function rebind values, string overrides). ## Notes - Wire protocol change (`ImportBinding` is now tagged source/host; `PrefixRun` imports become `ImportRebind` entries; `BridgeCallRecord` drops `importHandleId`/`rawName` for a resolved `name`). Protocol version stays at 1 per pre-release policy. - `docs/protocol.md` §5.2/§5.4/§5.6 and `DESIGN.md` (§4.3, §11.4, phase table — Phase 12 superseded) updated; also fixed a stale DESIGN.md claim that `Date` is a supported data leaf. - Behavior nuance: rebinding an *undeclared* specifier with a **non-function** value now reports "can only be rebound with a function" (client-side) instead of "was not declared" — declared-shape knowledge lives in Rust now. ## Tests - Rust: 227 pass (10 new: data/function/nested leaves, resolved-name frames, cross-module handle offsets, invalid-handle catchable rejection, `import.meta` isolation from user code, snapshot roundtrips, walk-order unit test) - TS: 465 + 83 pass; `imports.test.ts` rewritten for the data-tree API; new integration tests for Rust-side rebind enforcement incl. the data-leaf-rebound-with-a-function case and a positive rebind roundtrip - clippy delta vs main: −3 pre-existing errors, +0
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #37.
What
Host modules no longer reach the runtime as TS-generated ESM source. The client ships the module shape as plain data (source modules unchanged; host modules as a tree of function-leaf markers, data leaves as
WireValues, nested objects), and the Rust runtime builds the module natively.How the module is built (import.meta indirection)
The runtime emits only a fixed-shape template —
export const search = import.meta.__iso4[0];with identifier-validated export names and integer indices. Every value is built natively:wire_to_v8_valueThe values array is attached through V8's import-meta callback. Host modules therefore stay ordinary source-text modules, which keeps prefix snapshots working with no external-reference bookkeeping (the documented
import { search } from 'tools:x'; globalThis.search = searchprefix pattern is covered by new Rust tests, including a full snapshot roundtrip). Synthetic V8 modules were deliberately not used — their native evaluation-steps pointer would need external references on snapshot create and restore, where a miss is a crash.Rust owns the handle table
Handle IDs are assigned in tree-walk order over the declared bindings and never leave the process. The
__iso4_calldispatcher stub is now installed by the runtime itself (the client no longer sends a def for it); it resolves the leading ID before the frame is written, so:BridgeCallframes carrytargetKind: import+ resolvedspecifierand leaf path (the previously-dead protocol fields)tools:search.query; shim stubs resolve to their public name Rust-side too)bridge-report.ts(client-side name resolution) is deletedRebind validation unified in Rust
PrefixRunsends only rebind locations (specifier+ leaf path); the runtime validates them against the shape stored with the prefix and rejects undeclared paths, data leaves, and source modules withERR_UNDECLARED_BINDING— the same enforcement point as undeclared globals, no longer skippable by a non-TS client. The client keeps compile-timeRebindImports<M>plus the client-only checks (non-function rebind values, string overrides).Notes
ImportBindingis now tagged source/host;PrefixRunimports becomeImportRebindentries;BridgeCallRecorddropsimportHandleId/rawNamefor a resolvedname). Protocol version stays at 1 per pre-release policy.docs/protocol.md§5.2/§5.4/§5.6 andDESIGN.md(§4.3, §11.4, phase table — Phase 12 superseded) updated; also fixed a stale DESIGN.md claim thatDateis a supported data leaf.Tests
import.metaisolation from user code, snapshot roundtrips, walk-order unit test)imports.test.tsrewritten for the data-tree API; new integration tests for Rust-side rebind enforcement incl. the data-leaf-rebound-with-a-function case and a positive rebind roundtrip