Files
sdk/pkg/dart2wasm/bin/wasm2wat.dart
Martin Kustermann 4ee6a66270 [dart2wasm] Format pkg/dart2wasm after language version was increased
The change in [0] increased the language version of pkg/dart2wasm. That
in return changes how the package is formatted by the autoformatter.

This CL runs now the formatter to re-format the code. Unfortunately this
makes blame lists worse. But not doing it will make us have to disable
auto-formatting before saving files which is very annoying.

[0] https://dart-review.googlesource.com/c/sdk/+/487944

Change-Id: I6953fe0d6a824b2b79a26bbadb0bb977cec70b7a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/490821
Reviewed-by: Slava Egorov <vegorov@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
2026-03-26 04:32:19 -07:00

123 lines
3.6 KiB
Dart

// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import 'package:args/args.dart';
import 'package:wasm_builder/src/serialize/printer.dart';
import 'package:wasm_builder/wasm_builder.dart';
void main(List<String> args) {
final result = argParser.parse(args);
if (result.flag('help')) {
print('Usage: wasm2wat.dart [...options...] <input.wasm> <...wasm>');
print(argParser.usage);
exit(1);
}
final sort = result.flag('sort');
final writeWat = result.flag('write');
final outputFile = result['output'] as String?;
final wasmFiles = result.rest;
if (outputFile != null && wasmFiles.length != 1) {
print(
'Specified --output=$outputFile but number of wasm files is not 1 '
'(${wasmFiles.join(' ')})',
);
print(argParser.usage);
exit(1);
}
if (wasmFiles.isEmpty) {
print('No wasm files specified');
print(argParser.usage);
exit(1);
}
if (wasmFiles.length > 1 && !writeWat) {
print('More than one wasm file specified without `--write` flag.');
print(argParser.usage);
exit(1);
}
List<RegExp> getFilter(String optionName) {
final filterStrings = result[optionName] as List<String>;
return [for (final f in filterStrings) RegExp(f)];
}
final functionFilters = getFilter('function-name-filter');
final typeFilters = getFilter('type-name-filter');
final globalFilters = getFilter('global-name-filter');
final settings = ModulePrintSettings(
functionFilters: functionFilters,
typeFilters: typeFilters,
globalFilters: globalFilters,
printInSortedOrder: sort,
);
for (final input in wasmFiles) {
final wasmBytes = File(input).readAsBytesSync();
final deserializer = Deserializer(wasmBytes);
final module = Module.deserialize(deserializer);
final wat = module.printAsWat(settings: settings);
if (outputFile != null) {
File(outputFile).writeAsStringSync(wat);
print('-> Written $outputFile');
} else if (writeWat) {
File('$input.wat').writeAsStringSync(wat);
print('-> Written $input.wat');
} else {
print(wat);
}
}
}
final argParser = ArgParser()
..addMultiOption(
'function-name-filter',
abbr: 'f',
help:
'Only print function bodies if the function name matches. '
'The name filter is interpreted as a Dart `RegExp`.',
)
..addMultiOption(
'type-name-filter',
abbr: 't',
help:
'Only print type constituents if the type name matches. '
'The name filter is interpreted as a Dart `RegExp`.',
)
..addMultiOption(
'global-name-filter',
abbr: 'g',
help:
'Only print global initializers if the global name matches. '
'The name filter is interpreted as a Dart `RegExp`.',
)
..addFlag(
'sort',
abbr: 's',
help: 'Print functions/types/... in sorted order (sort by name).',
)
..addFlag(
'write',
abbr: 'w',
help: 'Write the resulting wat code to <input.wasm>.wat.',
)
..addFlag('help', abbr: 'h', help: 'Print the help of this tool.')
..addFlag(
'prefer-multiline',
abbr: 'm',
help:
'Prefer to print global initializers & type definitions as multi line.',
defaultsTo: /* wami equivalent is false */ false,
)
..addOption(
'output',
abbr: 'o',
help:
'The filepath where the output will be written to (default: stdout). '
'The number of input wasm files must be 1 if --output was provided.',
);