Skip to content

fix(compiler): skip slot boundaries for hook calls in module-scope IIFEs - #1574

Merged
trueadm merged 6 commits into
mainfrom
fix/module-init-iife-hooks
Oct 2, 2026
Merged

trueadm merged 6 commits into
mainfrom
fix/module-init-iife-hooks

Conversation

@trueadm

@trueadm trueadm commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Follows #1572 (merged), which skips the boundary for hook calls evaluated at module init. This PR extends that to functions the module invokes in place.

  • A plain .ts/.js module that calls a hook-named method or custom hook inside a function it invokes in place now evaluates, for example export const value = (() => store.useValue())(). Before, it threw ReferenceError: Cannot access '_h$0' before initialization on load.
  • A hook call that may run during a render keeps its own withSlot boundary. This covers generator bodies and IIFEs inside a component or hook.

Why

The plain hook pass appends its _h$N slot constants after the module body. #1572 skips the boundary for calls evaluated while the module initializes, using withinFunction(scope) in hook-deps.js. #1546's custom-hook calls already go through the same check. A module-scope IIFE also runs during module initialization, but its body is a function scope, so its hook calls still got a boundary and hit the TDZ.

Changes

  • immediatelyInvokedFunction(call) returns the arrow or function expression a call runs in place: directly, through TypeScript or paren wrappers (via unwrapValue), or via .call/.apply. buildScopes marks that function's scope invokedInPlace before the walk reaches its body. This follows the IIFE detection used by compile.js when it rejects hooks in plain JS loops.
  • withinFunction walks through an invokedInPlace scope, because its body runs with the code around it. A module-scope IIFE therefore counts as module init, and so do its nested IIFEs and parameter defaults. An IIFE inside a component or hook still has that function above it and keeps its boundary, so it needs no separate "is the IIFE itself within a function" check.
  • Generator and async-generator callees are excluded, because their bodies run on .next(), which a render may call. Async functions count: the body before the first await runs in place, and the rest runs in a microtask outside every render.
  • withinFunction is the only reader of the flag, and only the plain pass consults it, so full-compiler output is unchanged.

Validation

  • pnpm exec vitest run packages/octane/tests/transitive-hook-ownership.test.ts (octane and octane-prod projects)
    • evaluates hook methods in functions the module invokes in place ({client, server} × {surgical, inlineHookMemo: true}). It covers arrow, function expression, unparenthesized .call, .apply, an as-wrapped callee, an optional call, a nested IIFE, a parameter default, imported/local/aliased custom hooks, and async before and after await. With fix(compiler): leave hook methods called during module init unslotted #1572's hook-deps.js, all 4 configurations fail with the TDZ ReferenceError.
    • keeps a boundary for hook methods in a {generator, nested} IIFE body that runs during render (both emitters, SSR + client mount + update). Each fails with 'firstfirst' when its guard is removed: the generator exclusion, or walking through an in-place scope rather than stopping at it.
  • pnpm typecheck:files and pnpm format:files:check on the changed files, pnpm sync clean
  • pnpm test / pnpm typecheck / pnpm format:check repo-wide: left to CI. Local full runs were load-bound: load average about 60 on 18 cores, with timeouts in unrelated files that pass in isolation.

Performance: compile-time only. The pass adds one shape check per CallExpression and one boolean per scope record. Emitted code changes only for module-init IIFEs, which previously crashed on load. No benchmark can measure this.

Risk / follow-ups

  • These still keep a boundary and so still throw at module init: callbacks passed to synchronous array methods at module scope (items.map((x) => store.useLabel(x))), .bind(...)(), and new (function () { … })(). The receiver of .map is not provably an array, and treating a deferred callback as module init would silently share slots, which is worse than a loud load error.
  • A function invoked in place that also escapes and is later called during a render loses its boundary there. This matches the compile.js IIFE precedent.
  • Found while writing the fixture: any plain module containing a template literal skips the inline memo tier. canPrintProgram wants a visitor for TemplateElement, but esrap prints those inside TemplateLiteral. This is being handled in a separate session, and the fixture avoids template literals.

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Note

Medium Risk
Compiler-only scope analysis change affects when plain modules wrap hook calls; incorrect IIFE detection could mis-slot hooks at load time or during render, but behavior is covered by new SSR/client tests.

Overview
Fixes module-load crashes in the plain .ts/.js hook pass when hooks run inside immediately invoked arrow or function expressions at module scope (e.g. export const x = (() => store.useValue())()), which previously threw a TDZ ReferenceError on _h$N because slot constants are emitted after the module body.

hook-deps.js now detects in-place invocations (direct (), .call, .apply, with TS/paren unwrapping) via immediatelyInvokedFunction, marks those function scopes as invokedInPlace, and treats them like module initialization in withinFunction so custom hooks and hook-named methods skip withSlot boundaries there. Generators are excluded so bodies that run on .next() during render still get boundaries; nested IIFEs inside components/hooks are unchanged.

Adds regression tests for many module-init IIFE shapes (async, nested, custom hooks) and for render-time generator/nested-return cases that must keep boundaries.

Reviewed by Cursor Bugbot for commit a0d44dc. Bugbot is set up for automated code reviews on this repo. Configure here.

trueadm and others added 5 commits October 2, 2026 15:15
The plain .ts/.js hook pass wrapped every `obj.useX()` call in
`withSlot(_h$N, () => obj.useX())`, but declares its slot constants after
the module body to keep authored line numbers. A hook method evaluated
while the module initializes, e.g. `export const initial =
store.useValue()`, therefore threw `ReferenceError: Cannot access '_h$0'
before initialization`.

Such a call runs outside every render and needs no boundary. The scope
analysis now stamps `_octaneModuleInitCall` on a hook-method call with no
enclosing function (plain pass only; the full compiler declares its slots
before the body) and `hookMethodName` skips it, so the surgical and inline
emitters and optional-chain lowering all leave it authored. Static fields,
static blocks, computed keys and decorators of a module-level class run at
module init too. An instance field initializer runs with each
construction, possibly during render, so it gets an `initializer` scope
that keeps its boundary, as a constructor body does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…methods

# Conflicts:
#	packages/octane/src/compiler/hook-deps.js
The merge with #1546 shares the instance-field `initializer` scope with
its withinFunction, so a module-declared custom hook called from an
instance field also keeps its own slot.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ule init

A module-scope IIFE runs during module initialization, but its body is a
function scope, so its hook-method and custom-hook calls still got a
withSlot(_h$N, ...) boundary and the module threw a TDZ ReferenceError on
load.

A call whose callee is an arrow or function expression (directly, through
TypeScript or paren wrappers, or via .call/.apply) now marks that
function's scope invokedInPlace, and withinFunction walks through it. An
IIFE inside a component or hook still has that function above it and keeps
its boundary. Generator bodies run on .next(), possibly during a render,
so they do not count.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@trueadm
trueadm marked this pull request as ready for review October 2, 2026 15:14
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Oct 2, 2026
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

🟢 No size increases and no timing regressions outside noise.

Compares fdf54feac with 1871e1811 on the same runner. Timing verdicts require a change beyond the combined margin of error (at least 5%) and one 0.1ms timer tick, with the fastest sample also moving beyond that margin. Workflow run

js-framework: 1k/10k row operations (ms, lower is better)

All 20 timed operations (20 within noise)
target operation base head Δ noise
octane-tsrx run 4.08 4.79 +17.4% ±67% ⚪ within noise
octane-tsrx replace 7.84 8.55 +9.1% ±41% ⚪ within noise
octane-tsrx add 3.83 3.76 −1.8% ±19% ⚪ within noise
octane-tsrx update 0.43 0.56 +30.2% ±72% ⚪ within noise
octane-tsrx select 0.15 0.15 −0.0% ±98% ⚪ within noise
octane-tsrx swap 0.35 0.40 +14.3% ±35% ⚪ within noise
octane-tsrx remove 0.34 0.35 +2.9% ±46% ⚪ within noise
octane-tsrx runlots 35.61 38.32 +7.6% ±14% ⚪ within noise
octane-tsrx select_lots 0.13 0.22 +69.2% ±92% ⚪ within noise
octane-tsrx clear 34.92 36.27 +3.9% ±26% ⚪ within noise
octane-jsx run 5.12 5.53 +8.0% ±100% ⚪ within noise
octane-jsx replace 9.53 10.01 +5.0% ±69% ⚪ within noise
octane-jsx add 3.53 3.63 +2.8% ±12% ⚪ within noise
octane-jsx update 0.57 0.66 +15.8% ±43% ⚪ within noise
octane-jsx select 0.37 0.47 +27.0% ±49% ⚪ within noise
octane-jsx swap 0.82 0.78 −4.9% ±64% ⚪ within noise
octane-jsx remove 0.62 0.64 +3.2% ±58% ⚪ within noise
octane-jsx runlots 33.46 32.67 −2.4% ±16% ⚪ within noise
octane-jsx select_lots 2.91 3.45 +18.6% ±45% ⚪ within noise
octane-jsx clear 34.85 40.00 +14.8% ±17% ⚪ within noise

Application bundle size (bytes)

No changes across 45 measured values.

Public-import reachability bundles (bytes)

No changes across 105 measured values.

CI job durations

  • 🟡 6 job(s) slower than every recent main run: test shard (Node 24, 1/4), test shard (Node 24, 3/4), validate publishable packages, heavy integration (package builds, eval corpus, runtime loaders, Astro), Lynx compatibility, example apps (2/3)

Compares the CI run for a0d44dc18 (run) with the last 5 successful main push runs. A job is only slower or faster outside the whole main range, by at least 10% and 15s. Job time excludes runner setup.

job main median main range this PR Δ
wall clock (first job start to last job end) 13m 00s 11m 55s–14m 59s 13m 49s +49s (+6%) ⚪ within noise
total job time 140m 58s 134m 18s–157m 45s 152m 47s +11m 49s (+8%) ⚪ within noise
test shard (Node 24, 1/4) 11m 40s 11m 19s–12m 22s 13m 04s +1m 24s (+12%) 🟡 slower
test shard (Node 24, 3/4) 9m 33s 9m 29s–10m 46s 10m 54s +1m 21s (+14%) 🟡 slower
validate publishable packages 3m 58s 3m 51s–4m 54s 5m 07s +1m 09s (+29%) 🟡 slower
heavy integration (package builds, eval corpus, runtime loaders, Astro) 5m 23s 5m 21s–5m 53s 6m 13s +50s (+15%) 🟡 slower
Lynx compatibility 1m 32s 1m 29s–1m 36s 2m 21s +49s (+53%) 🟡 slower
example apps (2/3) 3m 44s 3m 42s–3m 55s 4m 07s +23s (+10%) 🟡 slower
All 26 timed jobs
job main median main range this PR Δ
heavy integration (browser - chromium) 11m 40s 11m 03s–14m 20s 13m 11s +1m 31s (+13%) ⚪ within noise
React parity shard (4/4) 10m 53s 10m 08s–13m 14s 12m 23s +1m 30s (+14%) ⚪ within noise
test shard (Node 24, 1/4) 11m 40s 11m 19s–12m 22s 13m 04s +1m 24s (+12%) 🟡 slower
test shard (Node 24, 3/4) 9m 33s 9m 29s–10m 46s 10m 54s +1m 21s (+14%) 🟡 slower
validate publishable packages 3m 58s 3m 51s–4m 54s 5m 07s +1m 09s (+29%) 🟡 slower
lint checks 8m 51s 7m 43s–12m 03s 9m 43s +52s (+10%) ⚪ within noise
test shard (Node 24, 2/4) 11m 55s 10m 38s–13m 38s 12m 47s +52s (+7%) ⚪ within noise
heavy integration (package builds, eval corpus, runtime loaders, Astro) 5m 23s 5m 21s–5m 53s 6m 13s +50s (+15%) 🟡 slower
Lynx compatibility 1m 32s 1m 29s–1m 36s 2m 21s +49s (+53%) 🟡 slower
React parity shard (1/4) 11m 18s 10m 23s–11m 27s 11m 49s +31s (+5%) ⚪ within noise
React parity shard (3/4) 10m 25s 10m 07s–11m 31s 10m 48s +23s (+4%) ⚪ within noise
example apps (2/3) 3m 44s 3m 42s–3m 55s 4m 07s +23s (+10%) 🟡 slower
React parity shard (2/4) 10m 08s 10m 04s–11m 23s 10m 30s +22s (+4%) ⚪ within noise
typecheck checks 2m 13s 1m 56s–2m 40s 2m 32s +19s (+14%) ⚪ within noise
example apps (1/3) 3m 13s 3m 04s–3m 31s 3m 31s +18s (+9%) ⚪ within noise
Three compatibility 2m 43s 2m 42s–3m 16s 2m 54s +11s (+7%) ⚪ within noise
website integration suite 6m 06s 5m 43s–6m 49s 6m 16s +10s (+3%) ⚪ within noise
example apps (3/3) 3m 30s 3m 22s–3m 49s 3m 38s +8s (+4%) ⚪ within noise
classify changeset release 13s 12s–15s 14s +1s (+8%) ⚪ within noise
typecheck 3s 3s–4s 3s ±0s (±0%) ⚪ within noise
examples 3s 3s–4s 3s ±0s (±0%) ⚪ within noise
React parity checks 12s 10s–14s 12s ±0s (±0%) ⚪ within noise
test (24) 3s 2s–5s 3s ±0s (±0%) ⚪ within noise
CI provenance 3s 3s–4s 3s ±0s (±0%) ⚪ within noise
lint 4s 3s–4s 3s −1s (−25%) ⚪ within noise
test shard (Node 24, 4/4) 10m 26s 10m 21s–11m 11s 10m 18s −8s (−1%) ⚪ within noise

…hooks

# Conflicts:
#	packages/octane/src/compiler/hook-deps.js
#	packages/octane/tests/transitive-hook-ownership.test.ts
@trueadm
trueadm merged commit ce97a96 into main Oct 2, 2026
31 checks passed
@trueadm
trueadm deleted the fix/module-init-iife-hooks branch October 2, 2026 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant