From da75d07632927fa952f3c748bd2cfcfbc5d1fe1d Mon Sep 17 00:00:00 2001 From: Pablo Rivas <5464238+elkhantar@users.noreply.github.com> Date: Thu, 2 Jul 2026 10:54:51 +0200 Subject: [PATCH] Compile the QuickJS module on the instantiating runtime Runtime.CompileModule bakes the compiling runtime's store-assigned function type IDs into the CompiledModule. Since wazero v1.12, import resolution compares those numeric IDs (required for the GC proposal's concrete ref types) rather than structural signatures, so a module compiled on the global throwaway runtime fails to instantiate on the per-New runtime with 'signature mismatch' on identical-looking signatures (wazero <= v1.11 compared structurally and tolerated this). Compiling on the instantiating runtime assigns consistent type IDs. The machine code is shared through the CompilationCache held in cachedRuntimeConfig, so the per-runtime compile is a cache hit. --- runtime.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/runtime.go b/runtime.go index f099bc3..6de84a6 100644 --- a/runtime.go +++ b/runtime.go @@ -132,12 +132,30 @@ func New(options ...Option) (runtime *Runtime, err error) { return nil, fmt.Errorf("failed to setup host module: %w", err) } + // Compile on this runtime rather than reusing the globally compiled + // module: CompileModule bakes the compiling Runtime's store-assigned + // function type IDs into the CompiledModule, and since wazero v1.12 + // import resolution compares those numeric IDs (needed for the GC + // proposal's concrete ref types) instead of structural signatures. + // Instantiating a module compiled on another Runtime therefore fails + // with "signature mismatch" on identical-looking signatures. The + // machine code is shared through the CompilationCache held in + // cachedRuntimeConfig, so this per-runtime compile is a cache hit. + qjsBytes := wasmBytes + if len(option.QuickJSWasmBytes) > 0 { + qjsBytes = option.QuickJSWasmBytes + } + localCompiled, err := runtime.wrt.CompileModule(option.Context, qjsBytes) + if err != nil { + return nil, fmt.Errorf("failed to compile qjs module: %w", err) + } + fsConfig := wazero. NewFSConfig(). WithDirMount(runtime.option.CWD, "/") if runtime.module, err = runtime.wrt.InstantiateModule( option.Context, - compiledQJSModule, + localCompiled, wazero.NewModuleConfig(). WithStartFunctions(option.StartFunctionName). WithSysWalltime().