Skip to content

feat: build host-import modules in Rust from shape data (#37) - #44

Merged
schplitt merged 1 commit into
mainfrom
feat/host-imports-as-data
Jul 25, 2026
Merged

feat: build host-import modules in Rust from shape data (#37)#44
schplitt merged 1 commit into
mainfrom
feat/host-imports-as-data

Conversation

@schplitt

Copy link
Copy Markdown
Owner

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:

  • 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

@schplitt
schplitt merged commit f2426b6 into main Jul 25, 2026
1 check passed
@schplitt
schplitt deleted the feat/host-imports-as-data branch July 25, 2026 20:04
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
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.

Build host-import modules in Rust from a data description instead of generating JS source in TS

1 participant