Skip to content

Commit 866ab57

Browse files
committed
one commit
1 parent c91b8ae commit 866ab57

File tree

8 files changed

+95
-37
lines changed

8 files changed

+95
-37
lines changed

pkgs/test/lib/src/runner/browser/browser_manager.dart

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ class BrowserManager {
155155
ExecutableSettings settings, Configuration configuration) {
156156
switch (browser.root) {
157157
case Runtime.chrome:
158+
case Runtime.chromeWasm:
158159
return Chrome(url, configuration, settings: settings);
159160
case Runtime.firefox:
160161
return Firefox(url, settings: settings);

pkgs/test/lib/src/runner/browser/dom.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extension WindowExtension on Window {
2121
void postMessage(Object message, String targetOrigin,
2222
[List<MessagePort>? messagePorts]) =>
2323
js_util.callMethod(this, 'postMessage', <Object?>[
24-
message,
24+
js_util.jsify(message),
2525
targetOrigin,
2626
if (messagePorts != null) js_util.jsify(messagePorts)
2727
]);

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ import '../executable_settings.dart';
1010
/// Default settings for starting browser executables with the wasm runtime.
1111
final defaultSettings = UnmodifiableMapView({
1212
Runtime.chromeWasm: ExecutableSettings(
13-
linuxExecutable: 'google-chrome',
13+
linuxExecutable: 'google-chrome-beta',
1414
macOSExecutable:
1515
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
16-
windowsExecutable: r'Google\Chrome\Application\chrome.exe'),
16+
windowsExecutable: r'Google\Chrome\Application\chrome.exe',
17+
arguments:[
18+
'--js-flags=--experimental-wasm-gc --wasm-gc-js-interop '
19+
'--experimental-wasm-stack-switching '
20+
'--experimental-wasm-type-reflection'
21+
]),
1722
});

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

+37-17
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,13 @@ 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)
172+
.replaceFirst('{{wasmUrl}}',
173+
p.basename('$test.browser_test.dart.wasm'))
165174
.replaceAll('{{testName}}', testName);
166175
return shelf.Response.ok(processedContents,
167176
headers: {'Content-Type': 'text/html'});
@@ -235,13 +244,14 @@ class BrowserWasmPlatform extends PlatformPlugin
235244
return _compileFutures.putIfAbsent(dartPath, () async {
236245
var dir = Directory(_compiledDir).createTempSync('test_').path;
237246

238-
// TODO: Update this path to the actual wasm output file path.
239-
var wasmCompiledPath =
240-
p.join(dir, '${p.basename(dartPath)}.browser_test.dart.js');
241-
// TODO: Update this to the actual url we want to serve the compiled WASM
242-
// file(s).
243-
var wasmUrl = '${p.toUri(p.relative(dartPath, from: _root)).path}'
244-
'.browser_test.dart.js';
247+
var baseCompiledPath =
248+
p.join(dir, '${p.basename(dartPath)}.browser_test.dart');
249+
var baseUrl =
250+
'${p.toUri(p.relative(dartPath, from: _root)).path}.browser_test.dart';
251+
var wasmUrl = '$baseUrl.wasm';
252+
var jsRuntimeWrapperUrl = '$baseUrl.js';
253+
var jsRuntimeUrl = p.join(p.dirname(dartPath), 'dart2wasm_runtime.mjs');
254+
var htmlUrl = '$baseUrl.html';
245255

246256
// TODO: This may need to be specialized, or it may just work. Not sure.
247257
var bootstrapContent = '''
@@ -255,21 +265,31 @@ class BrowserWasmPlatform extends PlatformPlugin
255265
}
256266
''';
257267

258-
await _compilers.compile(bootstrapContent, wasmCompiledPath, suiteConfig);
268+
await _compilers.compile(bootstrapContent, baseCompiledPath, suiteConfig);
259269
if (_closed) return;
260270

261-
var bootstrapUrl = '${p.toUri(p.relative(dartPath, from: _root)).path}'
262-
'.browser_test.dart';
263-
_wasmHandler.add(bootstrapUrl, (request) {
264-
return shelf.Response.ok(bootstrapContent,
265-
headers: {'Content-Type': 'application/dart'});
271+
var wasmPath = '$baseCompiledPath.wasm';
272+
_wasmHandler.add(wasmUrl, (request) {
273+
return shelf.Response.ok(File(wasmPath).readAsBytesSync(),
274+
headers: {'Content-Type': 'application/wasm'});
266275
});
267276

268-
_wasmHandler.add(wasmUrl, (request) {
269-
// TODO: Update this with proper headers at a minimum.
270-
return shelf.Response.ok(File(wasmCompiledPath).readAsBytesSync(),
277+
_wasmHandler.add(jsRuntimeWrapperUrl, (request) {
278+
return shelf.Response.ok(File(_jsRuntimeWrapper).readAsBytesSync(),
271279
headers: {'Content-Type': 'application/javascript'});
272280
});
281+
282+
var jsRuntimePath = p.join(dir, 'dart2wasm_runtime.mjs');
283+
_wasmHandler.add(jsRuntimeUrl, (request) {
284+
return shelf.Response.ok(File(jsRuntimePath).readAsBytesSync(),
285+
headers: {'Content-Type': 'application/javascript'});
286+
});
287+
288+
var htmlPath = '$baseCompiledPath.html';
289+
_wasmHandler.add(htmlUrl, (request) {
290+
return shelf.Response.ok(File(htmlPath).readAsBytesSync(),
291+
headers: {'Content-Type': 'text/html'});
292+
});
273293
});
274294
}
275295

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

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<head>
44
<title>{{testName}} Test</title>
55
{{testScript}}
6+
<div id="WasmUrl">{{wasmUrl}}</div>
7+
<div id="JSRuntimeUrl">{{jsRuntimeUrl}}</div>
68
<script src="packages/test/dart.js"></script>
79
</head>
810
</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_api/lib/src/backend/remote_listener.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class RemoteListener {
9494

9595
assert(message['type'] == 'suiteChannel');
9696
suiteChannelManager.connectIn(message['name'] as String,
97-
channel.virtualChannel(message['id'] as int));
97+
channel.virtualChannel((message['id'] as num).toInt()));
9898
});
9999

100100
if ((message['asciiGlyphs'] as bool?) ?? false) glyph.ascii = true;
@@ -218,7 +218,7 @@ class RemoteListener {
218218
testChannel.stream.listen((message) {
219219
assert(message['command'] == 'run');
220220
_runLiveTest(test.load(_suite, groups: groups),
221-
channel.virtualChannel(message['channel'] as int));
221+
channel.virtualChannel((message['channel'] as num).toInt()));
222222
});
223223

224224
return {

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

+21-15
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,35 @@ class WasmCompilerPool extends CompilerPool {
2424
///
2525
/// This wraps the Dart code in the standard browser-testing wrapper.
2626
///
27-
/// The returned [Future] will complete once the `dart2js` process completes
27+
/// The returned [Future] will complete once the `dart2wasm` process completes
2828
/// *and* all its output has been printed to the command line.
2929
@override
3030
Future compileInternal(
3131
String code, String path, SuiteConfiguration suiteConfig) {
3232
return withTempDir((dir) async {
33-
var wrapperPath = p.join(dir, 'runInBrowser.dart');
33+
var wrapperPath = p.join(dir, 'main.dart');
3434
File(wrapperPath).writeAsStringSync(code);
35-
36-
// TODO: Update args to be appropriate, just a starting point.
35+
var outWasmPath = '$path.wasm';
36+
var outJSPath = p.join(p.dirname(path), 'dart2wasm_runtime.mjs');
37+
var dartBinPath = Platform.resolvedExecutable;
38+
var sdkRoot = p.join(p.dirname(dartBinPath), '../');
39+
var platformRoot = p.join(sdkRoot, '../');
40+
var jsRuntimePath = p.join(sdkRoot, 'bin', 'dart2wasm_runtime.mjs');
41+
File(jsRuntimePath).copy(outJSPath);
42+
var platformDill = p.join(platformRoot, 'dart2wasm_platform.dill');
43+
var dartPrecompiledRuntimePath = p.join(platformRoot,
44+
'dart_precompiled_runtime');
45+
var dart2wasmSnapshotPath = p.join(sdkRoot, 'bin/snapshots',
46+
'dart2wasm.snapshot');
3747
var process = await Process.start(
38-
// This is the Dart executable, you may want to change it if dart2wasm
39-
// is spawned a different way.
40-
Platform.resolvedExecutable,
48+
dartPrecompiledRuntimePath,
4149
[
42-
// These are just some likely args you will want, in no particular
43-
// order and also total guesses.
44-
for (var experiment in enabledExperiments)
45-
'--enable-experiment=$experiment',
46-
'--enable-asserts',
47-
'--out=$path',
48-
'--packages=${await packageConfigUri}',
49-
wrapperPath, // Path of the thing to actually compile
50+
dart2wasmSnapshotPath,
51+
'--dart-sdk=$sdkRoot',
52+
'--platform=$platformDill',
53+
'--packages=${(await packageConfigUri).path}',
54+
wrapperPath,
55+
outWasmPath,
5056
]);
5157
if (closed) {
5258
process.kill();

0 commit comments

Comments
 (0)