From c98c6e37a2336aace4ed7e0e4b3315faa8a14405 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Wed, 2 Oct 2024 01:50:39 +0200 Subject: [PATCH] Update dart2wasm module runner (#2286) Update dart2wasm module instantiation, compilation, and invoking `main` to support all the ways dart2wasm implemented. --- .../runner/browser/static/run_wasm_chrome.js | 42 +++++++++++++++---- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/pkgs/test/lib/src/runner/browser/static/run_wasm_chrome.js b/pkgs/test/lib/src/runner/browser/static/run_wasm_chrome.js index d8628b9fe..6e3bda02b 100644 --- a/pkgs/test/lib/src/runner/browser/static/run_wasm_chrome.js +++ b/pkgs/test/lib/src/runner/browser/static/run_wasm_chrome.js @@ -7,15 +7,41 @@ // affect chrome. (async () => { // Fetch and compile Wasm binary. - let data = document.getElementById('WasmBootstrapInfo').dataset; - let modulePromise = WebAssembly.compileStreaming(fetch(data.wasmurl)); + let data = document.getElementById("WasmBootstrapInfo").dataset; // Instantiate the Dart module, importing from the global scope. - let dart2wasm = await import('./' + data.jsruntimeurl); - let dartInstance = await dart2wasm.instantiate(modulePromise, {}); + let dart2wasmJsRuntime = await import("./" + data.jsruntimeurl); - // Call `main`. If tasks are placed into the event loop (by scheduling tasks - // explicitly or awaiting Futures), these will automatically keep the script - // alive even after `main` returns. - await dart2wasm.invoke(dartInstance); + // Support three versions of dart2wasm: + // + // (1) Versions before 3.6.0-167.0.dev require the user to compile using the + // browser's `WebAssembly` API, the compiled module needs to be instantiated + // using the JS runtime. + // + // (2) Versions starting with 3.6.0-167.0.dev added helpers for compiling and + // instantiating. + // + // (3) Versions starting with 3.6.0-212.0.dev made compilation functions + // return a new type that comes with instantiation and invoke methods. + + if (dart2wasmJsRuntime.compileStreaming !== undefined) { + // Version (2) or (3). + let compiledModule = await dart2wasmJsRuntime.compileStreaming( + fetch(data.wasmurl), + ); + if (compiledModule.instantiate !== undefined) { + // Version (3). + let instantiatedModule = await compiledModule.instantiate(); + instantiatedModule.invokeMain(); + } else { + // Version (2). + let dartInstance = await dart2wasmJsRuntime.instantiate(compiledModule, {}); + await dart2wasmJsRuntime.invoke(dartInstance); + } + } else { + // Version (1). + let modulePromise = WebAssembly.compileStreaming(fetch(data.wasmurl)); + let dartInstance = await dart2wasmJsRuntime.instantiate(modulePromise, {}); + await dart2wasmJsRuntime.invoke(dartInstance); + } })();