fix(wasm-host): auto-provision wasm-host in all compile paths - #8337
Conversation
A program referencing WebAssembly.* did not reliably get the perry-runtime wasm-host cargo feature enabled, causing link failures with _Undefined symbols for architecture arm64: _js_webassembly_module_new_. Three gaps are closed: 1. --enable-wasm-runtime did not enable the feature. The flag only linked libperry_wasm_host.a (run_pipeline.rs) but never enabled perry-runtime/wasm-host in the auto-optimize cargo invocation (freshness.rs only checked ctx.needs_wasm_runtime, not args.enable_wasm_runtime). Fix: fold args.enable_wasm_runtime into ctx.needs_wasm_runtime early in run_pipeline so the flag is an explicit override that triggers the same provisioning as auto-detection. 2. PERRY_NO_AUTO_OPTIMIZE=1 never enabled wasm-host. The no-auto path (no_auto.rs) only resolved prebuilt well-known ext archives; the prebuilt libperry_runtime.a is built WITHOUT wasm-host (deliberately kept out of default to avoid wasmi bloat). Fix: when needs_wasm_runtime is set, build perry-runtime-static with default features + perry-runtime/wasm-host into a dedicated target dir so the prebuilt is not clobbered. perry-wasm-host has no tokio dep, so no PerryTS#507 concern. 3. Dynamic WebAssembly access was not detected. The static lowering (module_static.rs) sets hir_module.uses_webassembly for direct WebAssembly.Module/instantiate/etc. call sites, but a minified bundle can reach WebAssembly via dynamic property access that lowers to an ordinary PropertyGet without hitting any set-site. Fix: add a fallback grep for WebAssembly in the HIR debug string, mirroring the fetch/crypto fallbacks (zero false negatives). Regression tests assert wasm-usage -> wasm-host cross-feature provisioned, non-wasm -> not provisioned, and wasm usage changes the cache key.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (4)
Included review availability: Your plan includes up to 8 reviews per rolling hour; 4 remain after this review. 📝 WalkthroughWalkthroughThe compiler now detects WebAssembly references through lowered HIR fallback scanning and explicit flags. It propagates one runtime state to optimized-library selection, rebuilds the wasm-host runtime when needed, and separates wasm and non-wasm cache entries. ChangesWebAssembly runtime support
Estimated code review effort: 4 (Complex) | ~45 minutes Merge Risk: ⚪ Minimal · up to The change improves wasm-host provisioning across compile paths, and no actionable merge-blocking risk remains at the current head after normal checks and review. Sequence Diagram(s)sequenceDiagram
participant CompilePipeline
participant CompileContext
participant ResolveNoAutoOptimizedLibs
participant BuildWasmHostRuntime
participant Cargo
CompilePipeline->>CompileContext: normalize WebAssembly runtime state
CompileContext->>ResolveNoAutoOptimizedLibs: provide needs_wasm_runtime
ResolveNoAutoOptimizedLibs->>BuildWasmHostRuntime: request wasm-host runtime
BuildWasmHostRuntime->>Cargo: build perry-runtime with wasm-host
Cargo-->>BuildWasmHostRuntime: return platform-specific archive
BuildWasmHostRuntime-->>ResolveNoAutoOptimizedLibs: return runtime or fallback
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
|
Merging — everything passes except Since this is a fork branch I can't push the fix here, so I'm landing the Validated otherwise: One note for later, not a blocker: the new |
#8337 landed from a fork branch with cargo fmt --check failing on no_auto.rs and tests.rs. Verified this is the branch's own formatting and not the #8338 merge: clean main was fmt-clean and #8337 alone still failed, including no_auto.rs which #8338 never touched. Co-authored-by: Ralph Küpper <ralph3@skelpo.com>
Problem
A program referencing
WebAssembly.*did NOT reliably get thewasm-hostcargo feature enabled onperry-runtime, causing link failures:This blocks sdxgen (whose acorn.wasm path needs it) and is the concrete instance of the
--enable-wasm-runtime/ wasm-host auto-detect gap.Root cause — three broken paths
--enable-wasm-runtimedid not enable the feature. The flag only linkedlibperry_wasm_host.a(run_pipeline.rs:5288checkedargs.enable_wasm_runtime) but never enabledperry-runtime/wasm-hostin the auto-optimize cargo invocation (freshness.rs:192only checkedctx.needs_wasm_runtime, NOTargs.enable_wasm_runtime). So the runtime archive lackedjs_webassembly_*symbol definitions while the host library was linked — the symbol stayed undefined.PERRY_NO_AUTO_OPTIMIZE=1never enabledwasm-host. The no-auto path (no_auto.rs) only resolved prebuilt well-known ext archives. The prebuiltlibperry_runtime.ais built WITHOUTwasm-host(deliberately kept out ofdefaultto avoid wasmi bloat on non-wasm programs). So even thoughfeature_detect.rssetctx.needs_wasm_runtime = true, the no-auto path never rebuilt the runtime with the feature.Dynamic
WebAssemblyaccess was not detected. The static lowering (module_static.rs) setshir_module.uses_webassemblyfor directWebAssembly.Module/instantiate/etc. call sites, but a minified bundle can reachWebAssemblyvia dynamic property access (const WA = WebAssembly; WA.Module(bytes),globalThis.WebAssembly) that lowers to an ordinaryPropertyGetwithout hitting any set-site. The codegen still emitsjs_webassembly_*FFI calls for those paths, so the link died.Fix
--enable-wasm-runtimeoverride (run_pipeline.rs): foldargs.enable_wasm_runtimeintoctx.needs_wasm_runtimebefore any feature/library provisioning so the flag triggers the same provisioning as auto-detection — thewasm-hostcargo feature, the runtime rebuild, the library link, and the symbol-stub scan all see it.No-auto runtime rebuild (
no_auto.rs): whenctx.needs_wasm_runtimeis set, buildperry-runtime-staticwith default features +perry-runtime/wasm-hostinto a dedicated target dir (target/perry-wasm-host-runtime) so the prebuiltlibperry_runtime.ais not clobbered. This is the same on-demand build patternbuild_missing_prebuilt_ext_libuses for CPU-only ext wrappers.perry-wasm-hosthas no tokio dep, so there is no perry-ext-net: outbound TCP panics — LTO dead-strips tokio CONTEXT statics #507 shared-tokio concern.Dynamic access fallback (
feature_detect.rs): add a fallback grep for"WebAssembly"in the HIR debug string, mirroring the fetch/crypto/EventEmitter fallbacks. Over-matching only over-links the wasm host (a size cost); the rule is zero false negatives.Verification
cargo test -p perry --bin perry -- compile::optimized_libs— 34 passed (31 existing + 3 new regression tests).cargo test -p perry-hir --lib— 315 passed.wasm-hostcross-feature provisioned; non-wasm → not provisioned; wasm usage changes the cache key.Backward compat
--enable-wasm-runtimestill works (now actually enables the feature, not just the library).wasm-hoststays out ofdefault, the detection only fires onWebAssembly.*usage, and the no-auto rebuild only runs whenneeds_wasm_runtimeis set.Summary by CodeRabbit
New Features
Bug Fixes