{"record":{"id":"857c9e21d9e96f37","repo":"golang/go","slug":"go-run-webassembly-instance-expected","errorCode":null,"errorMessage":"Go.run: WebAssembly.Instance expected","messagePattern":"Go\\.run: WebAssembly\\.Instance expected","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/wasm/wasm_exec.js","lineNumber":481,"sourceCode":"\t\t\t\t\t\t\tthis.mem.setUint8(sp + 48, 0);\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst toCopy = src.subarray(0, dst.length);\n\t\t\t\t\t\tdst.set(toCopy);\n\t\t\t\t\t\tsetInt64(sp + 40, toCopy.length);\n\t\t\t\t\t\tthis.mem.setUint8(sp + 48, 1);\n\t\t\t\t\t},\n\n\t\t\t\t\t\"debug\": (value) => {\n\t\t\t\t\t\tconsole.log(value);\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t};\n\t\t}\n\n\t\tasync run(instance) {\n\t\t\tif (!(instance instanceof WebAssembly.Instance)) {\n\t\t\t\tthrow new Error(\"Go.run: WebAssembly.Instance expected\");\n\t\t\t}\n\t\t\tthis._inst = instance;\n\t\t\tthis.mem = new DataView(this._inst.exports.mem.buffer);\n\t\t\tthis._values = [ // JS values that Go currently has references to, indexed by reference id\n\t\t\t\tNaN,\n\t\t\t\t0,\n\t\t\t\tnull,\n\t\t\t\ttrue,\n\t\t\t\tfalse,\n\t\t\t\tglobalThis,\n\t\t\t\tthis,\n\t\t\t];\n\t\t\tthis._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id\n\t\t\tthis._ids = new Map([ // mapping from JS values to reference ids\n\t\t\t\t[0, 1],\n\t\t\t\t[null, 2],\n\t\t\t\t[true, 3],\n\t\t\t\t[false, 4],","sourceCodeStart":463,"sourceCodeEnd":499,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/lib/wasm/wasm_exec.js#L463-L499","documentation":"Thrown by Go.run(instance) in wasm_exec.js when the argument is not an instance of WebAssembly.Instance. The Go driver expects the already-instantiated wasm module so it can read exports.mem, exports.run, and the syscall shim. Passing the wrong shape (the bytes, the module, the promise) makes every subsequent memory access invalid, so it is rejected up front.","triggerScenarios":"Passing a WebAssembly.Module to go.run instead of the instantiated WebAssembly.Instance; passing the result of WebAssembly.instantiateStreaming's resolved value's .instance incorrectly (e.g., passing the result object instead of result.instance); passing a plain object or null.","commonSituations":"Confusing the two-step instantiate (Module then Instance) pipeline; destructuring WebAssembly.instantiate result incorrectly; copying example code that was written for WebAssembly.instantiateStreaming vs WebAssembly.instantiate.","solutions":["Instantiate first, then pass the .instance property: const { instance } = await WebAssembly.instantiate(bytes, go.importObject); await go.run(instance);","If using instantiateStreaming: const { instance } = await WebAssembly.instantiateStreaming(fetch('main.wasm'), go.importObject); await go.run(instance);","Verify the value before calling run: if (!(instance instanceof WebAssembly.Instance)) throw new TypeError('expected Instance'); to fail with a clearer message."],"exampleFix":"// before\nconst module = await WebAssembly.compile(bytes);\nawait go.run(module); // throws: Go.run: WebAssembly.Instance expected\n\n// after\nconst { instance } = await WebAssembly.instantiate(bytes, go.importObject);\nawait go.run(instance);","handlingStrategy":"type-guard","validationCode":"if (!(instance instanceof WebAssembly.Instance)) {\n  throw new TypeError(`go.run expects WebAssembly.Instance, got ${instance && instance.constructor && instance.constructor.name}`);\n}","typeGuard":"const isWasmInstance = (v) => v instanceof WebAssembly.Instance;","tryCatchPattern":null,"preventionTips":["Always destructure the instantiate result: const { instance } = await WebAssembly.instantiate(bytes, go.importObject).","Distinguish WebAssembly.Module (compiled, not runnable) from WebAssembly.Instance (instantiated, runnable).","Write a thin wrapper that type-checks before calling go.run to give a clearer error."],"tags":["wasm","go","webassembly","api-misuse","type-mismatch"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}