Skip to content

Commit f3e9600

Browse files
committed
tidy
1 parent ac7e726 commit f3e9600

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

pkgs/test/lib/src/runner/wasm/platform.dart

+18-5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class BrowserWasmPlatform extends PlatformPlugin
5252
Uri.parse('package:test/src/runner/browser/static/favicon.ico'))),
5353
p.fromUri(packageConfig.resolve(
5454
Uri.parse('package:test/src/runner/wasm/static/default.html.tpl'))),
55+
p.fromUri(packageConfig.resolve(
56+
Uri.parse('package:test/src/runner/wasm/static/run_wasm_chrome.js'))),
5557
root: root);
5658
}
5759

@@ -117,8 +119,11 @@ class BrowserWasmPlatform extends PlatformPlugin
117119
/// The default template for html tests.
118120
final String _defaultTemplatePath;
119121

122+
/// The `package:test` side wrapper for the Dart2Wasm runtime.
123+
final String _jsRuntimeWrapper;
124+
120125
BrowserWasmPlatform._(this._server, Configuration config, String faviconPath,
121-
this._defaultTemplatePath,
126+
this._defaultTemplatePath, this._jsRuntimeWrapper,
122127
{String? root})
123128
: _config = config,
124129
_root = root ?? p.current {
@@ -162,6 +167,8 @@ class BrowserWasmPlatform extends PlatformPlugin
162167
var processedContents = contents
163168
// Checked during loading phase that there is only one {{testScript}} placeholder.
164169
.replaceFirst('{{testScript}}', link)
170+
.replaceFirst('{{jsRuntimeUrl}}',
171+
p.basename('$test.browser_test.dart.mjs'))
165172
.replaceFirst('{{wasmUrl}}',
166173
p.basename('$test.browser_test.dart.wasm'))
167174
.replaceAll('{{testName}}', testName);
@@ -242,7 +249,8 @@ class BrowserWasmPlatform extends PlatformPlugin
242249
var baseUrl =
243250
'${p.toUri(p.relative(dartPath, from: _root)).path}.browser_test.dart';
244251
var wasmUrl = '$baseUrl.wasm';
245-
var jsUrl = '$baseUrl.js';
252+
var jsRuntimeWrapperUrl = '$baseUrl.js';
253+
var jsRuntimeUrl = '$baseUrl.mjs';
246254
var htmlUrl = '$baseUrl.html';
247255

248256
// TODO: This may need to be specialized, or it may just work. Not sure.
@@ -266,9 +274,14 @@ class BrowserWasmPlatform extends PlatformPlugin
266274
headers: {'Content-Type': 'application/wasm'});
267275
});
268276

269-
var jsPath = '$baseCompiledPath.js';
270-
_wasmHandler.add(jsUrl, (request) {
271-
return shelf.Response.ok(File(jsPath).readAsBytesSync(),
277+
_wasmHandler.add(jsRuntimeWrapperUrl, (request) {
278+
return shelf.Response.ok(File(_jsRuntimeWrapper).readAsBytesSync(),
279+
headers: {'Content-Type': 'application/javascript'});
280+
});
281+
282+
var jsRuntimePath = '$baseCompiledPath.mjs';
283+
_wasmHandler.add(jsRuntimeUrl, (request) {
284+
return shelf.Response.ok(File(jsRuntimePath).readAsBytesSync(),
272285
headers: {'Content-Type': 'application/javascript'});
273286
});
274287

pkgs/test/lib/src/runner/wasm/static/default.html.tpl

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<title>{{testName}} Test</title>
55
{{testScript}}
66
<div id="WasmUrl">{{wasmUrl}}</div>
7+
<div id="JSRuntimeUrl">{{jsRuntimeUrl}}</div>
78
<script src="packages/test/dart.js"></script>
89
</head>
910
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// TODO(joshualitt): Investigate making this a module. Currently, Dart2Wasm is
6+
// borken in D8 with modules because of an issue with async. This may or may not
7+
// affect chrome.
8+
const main = async () => {
9+
// Fetch and compile Wasm binary.
10+
let wasmUrl = document.getElementById('WasmUrl').textContent;
11+
let modulePromise = WebAssembly.compileStreaming(fetch(wasmUrl));
12+
13+
// Instantiate the Dart module, importing from the global scope.
14+
let jsRuntimeUrl = document.getElementById('JSRuntimeUrl').textContent;
15+
let dart2wasm = await import(jsRuntimeUrl);
16+
let dartInstance = await dart2wasm.instantiate(modulePromise, Promise.resolve(importObject));
17+
18+
// Call `main`. If tasks are placed into the event loop (by scheduling tasks
19+
// explicitly or awaiting Futures), these will automatically keep the script
20+
// alive even after `main` returns.
21+
await dart2wasm.invoke(dartInstance);
22+
};
23+
main();
24+

pkgs/test_core/lib/src/runner/wasm_compiler_pool.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ class WasmCompilerPool extends CompilerPool {
3333
var wrapperPath = p.join(dir, 'main.dart');
3434
File(wrapperPath).writeAsStringSync(code);
3535
var outWasmPath = '$path.wasm';
36-
var outJSPath = '$path.js';
36+
var outJSPath = '$path.mjs';
3737
var dartBinPath = Platform.resolvedExecutable;
3838
var sdkRoot = p.join(p.dirname(dartBinPath), '../');
3939
var platformRoot = p.join(sdkRoot, '../');
40-
var jsRuntimePath = p.join(platformRoot, 'run_wasm_chrome.js');
40+
var jsRuntimePath = p.join(sdkRoot, 'bin', 'dart2wasm_runtime.mjs');
4141
File(jsRuntimePath).copy(outJSPath);
4242
var platformDill = p.join(platformRoot, 'dart2wasm_platform.dill');
4343
var dartPrecompiledRuntimePath = p.join(platformRoot,

0 commit comments

Comments
 (0)