Skip to content

Commit af91845

Browse files
committed
tidy
1 parent 5760e6d commit af91845

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
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 {
@@ -159,9 +164,11 @@ class BrowserWasmPlatform extends PlatformPlugin
159164
var testName = htmlEscape.convert(test);
160165
var template = _config.customHtmlTemplatePath ?? _defaultTemplatePath;
161166
var contents = File(template).readAsStringSync();
167+
var jsRuntime = 'dart2wasm_runtime.mjs';
162168
var processedContents = contents
163169
// Checked during loading phase that there is only one {{testScript}} placeholder.
164170
.replaceFirst('{{testScript}}', link)
171+
.replaceFirst('{{jsRuntimeUrl}}', jsRuntime)
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 = p.join(p.dirname(dartPath), 'dart2wasm_runtime.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 = p.join(dir, 'dart2wasm_runtime.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, {});
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

+4-5
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,20 @@ 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 = p.join(p.dirname(path), 'dart2wasm_runtime.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,
4444
'dart_precompiled_runtime');
45-
var dart2wasmSnapshotPath = p.join(platformRoot,
46-
'dart2wasm_asserts.snapshot');
45+
var dart2wasmSnapshotPath = p.join(sdkRoot, 'bin/snapshots',
46+
'dart2wasm.snapshot');
4747
var process = await Process.start(
4848
dartPrecompiledRuntimePath,
4949
[
50-
'--enable_asserts',
5150
dart2wasmSnapshotPath,
5251
'--dart-sdk=$sdkRoot',
5352
'--platform=$platformDill',

0 commit comments

Comments
 (0)