Skip to content

Commit

Permalink
Update dart2wasm module runner (#2286)
Browse files Browse the repository at this point in the history
Update dart2wasm module instantiation, compilation, and invoking `main`
to support all the ways dart2wasm implemented.
  • Loading branch information
osa1 authored Oct 1, 2024
1 parent 22835e2 commit c98c6e3
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions pkgs/test/lib/src/runner/browser/static/run_wasm_chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
})();

0 comments on commit c98c6e3

Please sign in to comment.