|
| 1 | +// Copyright (c) 2025, 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 | +import 'dart:io'; |
| 6 | +import 'dart:io' as io; |
| 7 | +import 'dart:typed_data'; |
| 8 | + |
| 9 | +import 'package:args/args.dart'; |
| 10 | +import 'package:path/path.dart' as path; |
| 11 | + |
| 12 | +import 'package:wasm_builder/src/ir/ir.dart'; |
| 13 | +import 'package:wasm_builder/src/serialize/deserializer.dart'; |
| 14 | +import 'package:wasm_builder/src/serialize/printer.dart'; |
| 15 | + |
| 16 | +import 'self_compile_test.dart' show withTempDir; |
| 17 | + |
| 18 | +void main(List<String> args) async { |
| 19 | + final result = argParser.parse(args); |
| 20 | + final help = result.flag('help'); |
| 21 | + final write = result.flag('write'); |
| 22 | + final runFromSource = result.flag('src'); |
| 23 | + |
| 24 | + if (help) { |
| 25 | + print('Usage:\n${argParser.usage}'); |
| 26 | + io.exit(0); |
| 27 | + } |
| 28 | + if (result.rest.isNotEmpty) { |
| 29 | + print('Unknown arguments: ${result.rest.join(' ')}'); |
| 30 | + print('Usage:\n${argParser.usage}'); |
| 31 | + io.exit(1); |
| 32 | + } |
| 33 | + |
| 34 | + await withTempDir((String tempDir) async { |
| 35 | + for (final dartFilename in listIrTests()) { |
| 36 | + void failTest() { |
| 37 | + print('-> test "$dartFilename" failed\n'); |
| 38 | + io.exitCode = 254; |
| 39 | + } |
| 40 | + |
| 41 | + final dartCode = File(dartFilename).readAsStringSync(); |
| 42 | + final watFile = File(path.setExtension(dartFilename, '.wat')); |
| 43 | + final wasmFile = File(path.join( |
| 44 | + tempDir, path.setExtension(path.basename(dartFilename), '.wasm'))); |
| 45 | + |
| 46 | + print('Testing $dartFilename'); |
| 47 | + |
| 48 | + final result = await Process.run('/usr/bin/env', [ |
| 49 | + 'bash', |
| 50 | + 'pkg/dart2wasm/tool/compile_benchmark', |
| 51 | + if (runFromSource) '--src', |
| 52 | + '--no-strip-wasm', |
| 53 | + '-o', |
| 54 | + wasmFile.path, |
| 55 | + dartFilename |
| 56 | + ]); |
| 57 | + if (result.exitCode != 0) { |
| 58 | + print('Compilation failed:'); |
| 59 | + print('stdout:\n${result.stdout}'); |
| 60 | + print('stderr:\n${result.stderr}\n'); |
| 61 | + failTest(); |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + print('Compiled to ${wasmFile.path}'); |
| 66 | + |
| 67 | + final wasmBytes = wasmFile.readAsBytesSync(); |
| 68 | + final wat = |
| 69 | + moduleToString(parseModule(wasmBytes), parseNameFilters(dartCode)); |
| 70 | + if (write) { |
| 71 | + watFile.writeAsStringSync(wat); |
| 72 | + continue; |
| 73 | + } |
| 74 | + if (!watFile.existsSync()) { |
| 75 | + print('Expected "${watFile.path}" to exist.'); |
| 76 | + failTest(); |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + final oldWat = watFile.readAsStringSync(); |
| 81 | + if (oldWat != wat) { |
| 82 | + print( |
| 83 | + '-> Expectation mismatch. Run with `-w` to update expectation file.'); |
| 84 | + failTest(); |
| 85 | + continue; |
| 86 | + } |
| 87 | + } |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +final argParser = ArgParser() |
| 92 | + ..addFlag('help', |
| 93 | + abbr: 'h', defaultsTo: false, help: 'Prints available options.') |
| 94 | + ..addFlag('src', defaultsTo: false, help: 'Runs the compiler from source.') |
| 95 | + ..addFlag('write', |
| 96 | + abbr: 'w', defaultsTo: false, help: 'Writes new expectation files.'); |
| 97 | + |
| 98 | +Iterable<String> listIrTests() { |
| 99 | + return Directory('pkg/dart2wasm/test/ir_tests') |
| 100 | + .listSync(recursive: true) |
| 101 | + .whereType<File>() |
| 102 | + .map((file) => file.path) |
| 103 | + .where((path) => path.endsWith('.dart')); |
| 104 | +} |
| 105 | + |
| 106 | +Module parseModule(Uint8List wasmBytes) { |
| 107 | + final deserializer = Deserializer(wasmBytes); |
| 108 | + return Module.deserialize(deserializer); |
| 109 | +} |
| 110 | + |
| 111 | +String moduleToString(Module module, List<RegExp> functionNameFilters) { |
| 112 | + bool printFunctionBody(BaseFunction function) { |
| 113 | + final name = function.functionName; |
| 114 | + if (name == null) return false; |
| 115 | + return functionNameFilters.any((pattern) => name.contains(pattern)); |
| 116 | + } |
| 117 | + |
| 118 | + final mp = ModulePrinter(module, printFunctionBody: printFunctionBody); |
| 119 | + for (final function in module.functions.defined) { |
| 120 | + if (printFunctionBody(function)) { |
| 121 | + mp.enqueueFunction(function); |
| 122 | + } |
| 123 | + } |
| 124 | + return mp.print(); |
| 125 | +} |
| 126 | + |
| 127 | +List<RegExp> parseNameFilters(String dartCode) { |
| 128 | + const functionFilter = '// functionFilter='; |
| 129 | + final filters = <RegExp>[]; |
| 130 | + for (final line in dartCode.split('\n')) { |
| 131 | + if (line.startsWith(functionFilter)) { |
| 132 | + final filter = line.substring(functionFilter.length).trim(); |
| 133 | + if (filter.isNotEmpty) { |
| 134 | + filters.add(RegExp(filter)); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + return filters; |
| 139 | +} |
0 commit comments