|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | 3 | // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
|
| 5 | +import 'dart:typed_data'; |
| 6 | + |
5 | 7 | import 'package:_fe_analyzer_shared/src/util/colors.dart' as colors; |
6 | 8 | import 'package:dart2wasm/compile.dart'; |
7 | 9 | import 'package:dart2wasm/compiler_options.dart'; |
| 10 | +import 'package:dart2wasm/io_util.dart'; |
8 | 11 |
|
9 | 12 | import 'filesystem_io.dart' if (dart.library.js_interop) 'filesystem_js.dart'; |
10 | 13 |
|
| 14 | +class _BenchmarkIOManager extends CompilerPhaseInputOutputManager { |
| 15 | + final WasmCompilerFileSystem benchmarkFileSystem; |
| 16 | + Uint8List? moduleBytes; |
| 17 | + |
| 18 | + _BenchmarkIOManager(this.benchmarkFileSystem, WasmCompilerOptions options) |
| 19 | + : super(benchmarkFileSystem, options); |
| 20 | + |
| 21 | + @override |
| 22 | + Future<void> writeWasmModule(Uint8List wasmModule, String moduleName) async { |
| 23 | + moduleBytes = wasmModule; |
| 24 | + } |
| 25 | + |
| 26 | + @override |
| 27 | + Future<void> writeWasmSourceMap(String sourceMap, String moduleName) async {} |
| 28 | + |
| 29 | + @override |
| 30 | + Future<void> writeJsRuntime(String jsRuntime) async {} |
| 31 | + |
| 32 | + @override |
| 33 | + Future<void> writeSupportJs(String supportJs) async {} |
| 34 | + |
| 35 | + void flushWasmModules(String wasmFile) { |
| 36 | + benchmarkFileSystem.writeBytesSync(wasmFile, moduleBytes!); |
| 37 | + } |
| 38 | +} |
| 39 | + |
11 | 40 | Future main(List<String> args) async { |
12 | 41 | final sw = Stopwatch()..start(); |
13 | 42 | final fileSystem = WasmCompilerFileSystem(); |
14 | | - final result = await compileBenchmark( |
15 | | - fileSystem, 'pkg/dart2wasm/benchmark/self_compile_benchmark.dart'); |
| 43 | + final mainFile = 'pkg/dart2wasm/benchmark/self_compile_benchmark.dart'; |
| 44 | + final main = Uri.file('${fileSystem.sdkRoot}/$mainFile'); |
| 45 | + |
| 46 | + final options = WasmCompilerOptions(mainUri: main, outputFile: 'out.wasm'); |
| 47 | + final ioManager = _BenchmarkIOManager(fileSystem, options); |
| 48 | + |
| 49 | + options.librariesSpecPath = |
| 50 | + Uri.file('${fileSystem.sdkRoot}/sdk/lib/libraries.json'); |
| 51 | + |
| 52 | + await compileBenchmark(options, ioManager); |
16 | 53 | print('Dart2WasmSelfCompile(RunTimeRaw): ${sw.elapsed.inMilliseconds} ms.'); |
17 | 54 |
|
18 | 55 | if (args.isNotEmpty) { |
19 | | - final module = result.wasmModules.values.single; |
20 | 56 | final wasmFile = args.single; |
21 | | - fileSystem.writeBytesSync(wasmFile, module.moduleBytes); |
| 57 | + ioManager.flushWasmModules(wasmFile); |
22 | 58 | } |
23 | 59 | } |
24 | 60 |
|
25 | | -Future<CodegenResult> compileBenchmark( |
26 | | - WasmCompilerFileSystem fileSystem, String mainFile) async { |
| 61 | +Future<CodegenResult> compileBenchmark(WasmCompilerOptions options, |
| 62 | + CompilerPhaseInputOutputManager ioManager) async { |
27 | 63 | // Avoid CFE self-detecting whether `stdout`/`stderr` is terminal and supports |
28 | 64 | // colors (as we don't have `dart:io` available when we run dart2wasm in a |
29 | 65 | // wasm runtime). |
30 | 66 | colors.enableColors = false; |
31 | 67 |
|
32 | | - final main = Uri.file('${fileSystem.sdkRoot}/$mainFile'); |
33 | | - |
34 | | - final options = WasmCompilerOptions(mainUri: main, outputFile: 'out.wasm'); |
35 | | - options.librariesSpecPath = |
36 | | - Uri.file('${fileSystem.sdkRoot}/sdk/lib/libraries.json'); |
37 | | - |
38 | | - final result = await compile( |
39 | | - options, fileSystem, (mod) => Uri.parse('$mod.maps'), (diag) { |
| 68 | + final result = await compile(options, ioManager, (diag) { |
40 | 69 | print('Diagnostics: ${diag.severity} ${diag.plainTextFormatted}'); |
41 | 70 | }); |
42 | 71 | if (result is! CompilationSuccess) { |
|
0 commit comments