Skip to content

Commit 4cd6096

Browse files
osa1Commit Queue
authored and
Commit Queue
committed
[dart2wasm] Move Wasm compilation functions into generated runtime blob
To be able to control how dart2wasm-generated modules are compiled, add a `WebAssembly.compile` and `WebAssmebly.compileStreaming` wrapper to the generated runtime blob. These wrappers do js-string built-in detection and enable the built-in when it's available. Once the SDK is rolled into engine we will update the engine dart2wasm module loader to use `compileStreaming` from the runtime blob, in flutter/engine#51488. This will allow enabling the built-in without duplicating the feature detection and compilation code in the engine. Change-Id: Icb939f16a2b0ae1d348ec5a814371e9ffde4fd68 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/381581 Reviewed-by: Martin Kustermann <[email protected]> Commit-Queue: Ömer Ağacan <[email protected]>
1 parent 3217fb1 commit 4cd6096

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

pkg/dart2wasm/bin/run_wasm.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -370,18 +370,7 @@ if (argsSplit != -1) {
370370
const main = async () => {
371371
const dart2wasm = await import(args[jsRuntimeArg]);
372372

373-
/// Returns whether the `js-string` built-in is supported.
374-
function detectImportedStrings() {
375-
let bytes = [
376-
0, 97, 115, 109, 1, 0, 0, 0, 1, 4, 1, 96, 0,
377-
0, 2, 23, 1, 14, 119, 97, 115, 109, 58, 106, 115, 45,
378-
115, 116, 114, 105, 110, 103, 4, 99, 97, 115, 116, 0, 0
379-
];
380-
return !WebAssembly.validate(
381-
new Uint8Array(bytes), {builtins: ['js-string']});
382-
}
383-
384-
function compile(filename, withJsStringBuiltins) {
373+
function compile(filename) {
385374
// Create a Wasm module from the binary Wasm file.
386375
var bytes;
387376
if (isJSC) {
@@ -391,10 +380,7 @@ const main = async () => {
391380
} else {
392381
bytes = readRelativeToScript(filename, "binary");
393382
}
394-
return WebAssembly.compile(
395-
bytes,
396-
withJsStringBuiltins ? {builtins: ['js-string']} : {}
397-
);
383+
return dart2wasm.compile(bytes);
398384
}
399385

400386
globalThis.window ??= globalThis;
@@ -411,7 +397,7 @@ const main = async () => {
411397

412398
// Instantiate the Dart module, importing from the global scope.
413399
var dartInstance = await dart2wasm.instantiate(
414-
compile(args[wasmArg], detectImportedStrings()),
400+
compile(args[wasmArg]),
415401
Promise.resolve(importObject),
416402
);
417403

pkg/dart2wasm/lib/js/runtime_blob.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,42 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
const jsRuntimeBlobPart1 = r'''
6+
// Returns whether the `js-string` built-in is supported.
7+
function detectJsStringBuiltins() {
8+
let bytes = [
9+
0, 97, 115, 109, 1, 0, 0, 0, 1, 4, 1, 96, 0,
10+
0, 2, 23, 1, 14, 119, 97, 115, 109, 58, 106, 115, 45,
11+
115, 116, 114, 105, 110, 103, 4, 99, 97, 115, 116, 0, 0
12+
];
13+
return WebAssembly.validate(
14+
new Uint8Array(bytes), {builtins: ['js-string']});
15+
}
16+
17+
// Compile a dart2wasm-generated Wasm module using
18+
// `WebAssembly.compileStreaming`, with the flags needed by dart2wasm. `source`
19+
// needs to have a type expected by `WebAssembly.compileStreaming`.
20+
//
21+
// Pass the output of this to `instantiate` below to instantiate the compiled
22+
// module.
23+
export const compileStreaming = (source) => {
24+
return WebAssembly.compileStreaming(
25+
source,
26+
detectJsStringBuiltins() ? {builtins: ['js-string']} : {}
27+
);
28+
}
29+
30+
// Compile a dart2wasm-generated Wasm module using `WebAssembly.compile`, with
31+
// the flags needed by dart2wasm. `source` needs to have a type expected by
32+
// `WebAssembly.compileStreaming`.
33+
//
34+
// Pass the output of this to `instantiate` below to instantiate the compiled
35+
// module.
36+
export const compile = (bytes) => {
37+
return WebAssembly.compile(
38+
bytes,
39+
detectJsStringBuiltins() ? {builtins: ['js-string']} : {}
40+
);
41+
}
642
743
// `modulePromise` is a promise to the `WebAssembly.module` object to be
844
// instantiated.

pkg/test_runner/lib/src/browser.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,11 @@ String dart2wasmHtml(String title, String wasmPath, String mjsPath) {
328328
src="/root_dart/pkg/test_runner/lib/src/test_controller.js">
329329
</script>
330330
<script type="module">
331-
const dartModulePromise = WebAssembly.compileStreaming(fetch('$wasmPath'));
332-
const imports = {};
333331
let dart2wasm_runtime = await import('$mjsPath');
332+
const dartModulePromise =
333+
dart2wasm_runtime.compileStreaming(fetch('$wasmPath'));
334334
let moduleInstance =
335-
await dart2wasm_runtime.instantiate(dartModulePromise, imports);
335+
await dart2wasm_runtime.instantiate(dartModulePromise, {});
336336
337337
dartMainRunner(() => {
338338
dart2wasm_runtime.invoke(moduleInstance);

0 commit comments

Comments
 (0)