Normative: Read the imports synchronously in WebAssembly.instantiate(Module).
diff --git a/document/js-api/index.bs b/document/js-api/index.bs index ff66fa5..9a44cb3 100644 --- a/document/js-api/index.bs +++ b/document/js-api/index.bs
@@ -412,9 +412,14 @@ <div algorithm> To <dfn>asynchronously instantiate a WebAssembly module</dfn> from a {{Module}} |moduleObject| and imports |importObject|, perform the following steps: 1. Let |promise| be [=a new promise=]. + 1. Let |module| be |moduleObject|.\[[Module]]. + 1. [=Read the imports=] of |module| with imports |importObject|, and let |imports| be the result. 1. [=Queue a task=] to perform the following steps: - 1. [=instantiate a WebAssembly module|Instantiate the WebAssembly module=] |moduleObject| importing |importObject|, and let |instance| be the result. If this throws an exception, catch it, and [=reject=] |promise| with the exception. - 1. [=Resolve=] |promise| with |instance|. + 1. [=Instantiate the core of a WebAssembly module=] |module| with |imports|, and let |instance| be the result. + If this throws an exception, catch it, [=reject=] |promise| with the exception, and terminate these substeps. + 1. [=Create an instance object=] from |module| and |instance|, and let the result be |instanceObject|. + If this throws an exception, catch it, [=reject=] |promise| with the exception, and terminate these substeps. + 1. [=Resolve=] |promise| with |instanceObject|. 1. Return |promise|. </div>
diff --git a/test/js-api/constructor/instantiate.any.js b/test/js-api/constructor/instantiate.any.js index 9c4f9a6..4949912 100644 --- a/test/js-api/constructor/instantiate.any.js +++ b/test/js-api/constructor/instantiate.any.js
@@ -96,6 +96,64 @@ }, `${name}: Module argument`); } +promise_test(() => { + const builder = new WasmModuleBuilder(); + builder.addImportedGlobal("module", "global", kWasmI32); + const buffer = builder.toBuffer(); + const order = []; + + const imports = { + get module() { + order.push("module getter"); + return { + get global() { + order.push("global getter"); + return 0; + }, + } + }, + }; + + const expected = [ + "module getter", + "global getter", + ]; + const p = WebAssembly.instantiate(buffer, imports); + assert_array_equals(order, []); + return p.then(result => { + assert_WebAssemblyInstantiatedSource(result); + assert_array_equals(order, expected); + }); +}, "Synchronous options handling: Buffer argument"); + +promise_test(() => { + const builder = new WasmModuleBuilder(); + builder.addImportedGlobal("module", "global", kWasmI32); + const buffer = builder.toBuffer(); + const module = new WebAssembly.Module(buffer); + const order = []; + + const imports = { + get module() { + order.push("module getter"); + return { + get global() { + order.push("global getter"); + return 0; + }, + } + }, + }; + + const expected = [ + "module getter", + "global getter", + ]; + const p = WebAssembly.instantiate(module, imports); + assert_array_equals(order, expected); + return p.then(instance => assert_Instance(instance, {})); +}, "Synchronous options handling: Module argument"); + promise_test(t => { const buffer = new Uint8Array(); return promise_rejects(t, new WebAssembly.CompileError(), WebAssembly.instantiate(buffer));