From 4201b2677eaa9e7bd13f19c2e5f678b33a3ebaeb Mon Sep 17 00:00:00 2001 From: Martin Kustermann Date: Tue, 21 Apr 2026 05:52:14 -0700 Subject: [PATCH] [dart2wasm] Dart format pkg/wasm_builder 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: Ie3cbdc58bbd7f5aba187470554b7c958719fa795 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/496940 Reviewed-by: Slava Egorov Commit-Queue: Martin Kustermann --- pkg/wasm_builder/lib/source_map.dart | 32 +- .../lib/src/builder/data_segment.dart | 12 +- .../lib/src/builder/data_segments.dart | 22 +- .../lib/src/builder/elements.dart | 28 +- .../lib/src/builder/function.dart | 16 +- .../lib/src/builder/functions.dart | 30 +- pkg/wasm_builder/lib/src/builder/global.dart | 20 +- pkg/wasm_builder/lib/src/builder/globals.dart | 21 +- .../lib/src/builder/instructions.dart | 3523 ++++++++++++----- .../lib/src/builder/memories.dart | 25 +- pkg/wasm_builder/lib/src/builder/module.dart | 46 +- pkg/wasm_builder/lib/src/builder/table.dart | 17 +- pkg/wasm_builder/lib/src/builder/tables.dart | 30 +- pkg/wasm_builder/lib/src/builder/tags.dart | 9 +- pkg/wasm_builder/lib/src/builder/types.dart | 46 +- pkg/wasm_builder/lib/src/builder/util.dart | 12 +- pkg/wasm_builder/lib/src/ir/element.dart | 33 +- pkg/wasm_builder/lib/src/ir/function.dart | 41 +- pkg/wasm_builder/lib/src/ir/global.dart | 29 +- pkg/wasm_builder/lib/src/ir/imports.dart | 23 +- pkg/wasm_builder/lib/src/ir/instruction.dart | 155 +- pkg/wasm_builder/lib/src/ir/instructions.dart | 48 +- pkg/wasm_builder/lib/src/ir/memory.dart | 29 +- pkg/wasm_builder/lib/src/ir/module.dart | 186 +- pkg/wasm_builder/lib/src/ir/table.dart | 29 +- pkg/wasm_builder/lib/src/ir/tags.dart | 9 +- pkg/wasm_builder/lib/src/ir/type.dart | 68 +- pkg/wasm_builder/lib/src/ir/types.dart | 2 +- .../lib/src/serialize/printer.dart | 288 +- .../lib/src/serialize/sections.dart | 269 +- 30 files changed, 3637 insertions(+), 1461 deletions(-) diff --git a/pkg/wasm_builder/lib/source_map.dart b/pkg/wasm_builder/lib/source_map.dart index e808412e541..accd01eccf3 100644 --- a/pkg/wasm_builder/lib/source_map.dart +++ b/pkg/wasm_builder/lib/source_map.dart @@ -18,8 +18,12 @@ class SourceMapping { SourceMapping._(this.instructionOffset, this.sourceInfo); SourceMapping( - this.instructionOffset, Uri fileUri, int line, int col, String? name) - : sourceInfo = SourceInfo(fileUri, line, col, name); + this.instructionOffset, + Uri fileUri, + int line, + int col, + String? name, + ) : sourceInfo = SourceInfo(fileUri, line, col, name); SourceMapping.unmapped(this.instructionOffset) : sourceInfo = null; @@ -81,10 +85,9 @@ class SourceMapSerializer { void copyMappings(SourceMapSerializer other, int offset) { for (final mapping in other.mappings) { - mappings.add(SourceMapping._( - mapping.instructionOffset + offset, - mapping.sourceInfo, - )); + mappings.add( + SourceMapping._(mapping.instructionOffset + offset, mapping.sourceInfo), + ); } } @@ -148,16 +151,22 @@ Map _sourceMapToJson(List mappings) { first = false; - lastTargetColumn = - _encodeVLQ(mappingsStr, mapping.instructionOffset, lastTargetColumn); + lastTargetColumn = _encodeVLQ( + mappingsStr, + mapping.instructionOffset, + lastTargetColumn, + ); if (sourceInfo != null) { final sourceIndex = sourceIndices[sourceInfo.fileUri]!; lastSourceIndex = _encodeVLQ(mappingsStr, sourceIndex, lastSourceIndex); lastSourceLine = _encodeVLQ(mappingsStr, sourceInfo.line, lastSourceLine); - lastSourceColumn = - _encodeVLQ(mappingsStr, sourceInfo.col, lastSourceColumn); + lastSourceColumn = _encodeVLQ( + mappingsStr, + sourceInfo.col, + lastSourceColumn, + ); if (sourceInfo.name != null) { final nameIndex = nameIndices[sourceInfo.name!]!; @@ -202,5 +211,6 @@ int _encodeVLQ(StringSink output, int value, int offset) { const int _vlqBaseShift = 5; const int _vlqBaseMask = (1 << 5) - 1; const int _vlqContinuationBit = 1 << 5; -const String _base64Digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn' +const String _base64Digits = + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn' 'opqrstuvwxyz0123456789+/'; diff --git a/pkg/wasm_builder/lib/src/builder/data_segment.dart b/pkg/wasm_builder/lib/src/builder/data_segment.dart index ffa2e648323..b133c8755d0 100644 --- a/pkg/wasm_builder/lib/src/builder/data_segment.dart +++ b/pkg/wasm_builder/lib/src/builder/data_segment.dart @@ -13,8 +13,11 @@ class DataSegmentBuilder extends ir.BaseDataSegment final BytesBuilder content; DataSegmentBuilder( - super.index, Uint8List initialContent, super.memory, super.offset) - : content = BytesBuilder()..add(initialContent); + super.index, + Uint8List initialContent, + super.memory, + super.offset, + ) : content = BytesBuilder()..add(initialContent); bool get isActive => memory != null; bool get isPassive => memory == null; @@ -24,8 +27,9 @@ class DataSegmentBuilder extends ir.BaseDataSegment /// Append content to the data segment. void append(Uint8List data) { content.add(data); - assert(isPassive || - offset! >= 0 && offset! + content.length <= memory!.minSize); + assert( + isPassive || offset! >= 0 && offset! + content.length <= memory!.minSize, + ); } @override diff --git a/pkg/wasm_builder/lib/src/builder/data_segments.dart b/pkg/wasm_builder/lib/src/builder/data_segments.dart index 1a5444dd584..e5cb27b669d 100644 --- a/pkg/wasm_builder/lib/src/builder/data_segments.dart +++ b/pkg/wasm_builder/lib/src/builder/data_segments.dart @@ -21,15 +21,25 @@ class DataSegmentsBuilder with Builder { /// /// If [initialContent] is specified, it defines the initial content of the /// segment. The content can be extended later. - DataSegmentBuilder define( - [Uint8List? initialContent, ir.Memory? memory, int? offset]) { + DataSegmentBuilder define([ + Uint8List? initialContent, + ir.Memory? memory, + int? offset, + ]) { initialContent ??= Uint8List(0); assert((memory != null) == (offset != null)); - assert(memory == null || - offset! >= 0 && - offset + initialContent.length <= memory.minSize * memoryBlockSize); + assert( + memory == null || + offset! >= 0 && + offset + initialContent.length <= + memory.minSize * memoryBlockSize, + ); final builder = DataSegmentBuilder( - _dataSegmentBuilders.length, initialContent, memory, offset); + _dataSegmentBuilders.length, + initialContent, + memory, + offset, + ); _dataSegmentBuilders.add(builder); return builder; } diff --git a/pkg/wasm_builder/lib/src/builder/elements.dart b/pkg/wasm_builder/lib/src/builder/elements.dart index ee108bc18dc..e44996516b4 100644 --- a/pkg/wasm_builder/lib/src/builder/elements.dart +++ b/pkg/wasm_builder/lib/src/builder/elements.dart @@ -13,8 +13,9 @@ class ElementsBuilder with Builder { final _functionTableBuilders = {}; final _expressionTableBuilders = {}; - late final declarativeSegmentBuilder = - DeclarativeSegmentBuilder(_moduleBuilder); + late final declarativeSegmentBuilder = DeclarativeSegmentBuilder( + _moduleBuilder, + ); ElementsBuilder(this._moduleBuilder); @@ -25,14 +26,19 @@ class ElementsBuilder with Builder { assert(table.type.isSubtypeOf(ir.RefType.func(nullable: true))); assert(table.enclosingModule == _moduleBuilder.module); return _functionTableBuilders.putIfAbsent( - table, () => ActiveFunctionSegmentBuilder(table)); + table, + () => ActiveFunctionSegmentBuilder(table), + ); } ActiveExpressionSegmentBuilder activeExpressionSegmentBuilderFor( - ir.Table table) { + ir.Table table, + ) { assert(table.enclosingModule == _moduleBuilder.module); return _expressionTableBuilders.putIfAbsent( - table, () => ActiveExpressionSegmentBuilder(table)); + table, + () => ActiveExpressionSegmentBuilder(table), + ); } @override @@ -77,8 +83,10 @@ class ActiveFunctionSegmentBuilder with Builder> { void setFunctionAt(int index, ir.BaseFunction function) { assert(function.enclosingModule == table.enclosingModule); - assert(table.maxSize == null || index < table.maxSize!, - 'Index $index greater than max table size ${table.maxSize}'); + assert( + table.maxSize == null || index < table.maxSize!, + 'Index $index greater than max table size ${table.maxSize}', + ); _functions[index] = function; table.minSize = math.max(table.minSize, index + 1); } @@ -129,8 +137,10 @@ class ActiveExpressionSegmentBuilder void setExpressionAt(int index, InstructionsBuilder init) { assert(init.moduleBuilder.module == table.enclosingModule); - assert(table.maxSize == null || index < table.maxSize!, - 'Index $index greater than max table size ${table.maxSize}'); + assert( + table.maxSize == null || index < table.maxSize!, + 'Index $index greater than max table size ${table.maxSize}', + ); _expressions[index] = init; table.minSize = math.max(table.minSize, index + 1); } diff --git a/pkg/wasm_builder/lib/src/builder/function.dart b/pkg/wasm_builder/lib/src/builder/function.dart index 827edb91ab3..d3b04366a59 100644 --- a/pkg/wasm_builder/lib/src/builder/function.dart +++ b/pkg/wasm_builder/lib/src/builder/function.dart @@ -17,9 +17,11 @@ class FunctionBuilder extends ir.BaseFunction late InstructionsBuilder _body; FunctionBuilder( - this.moduleBuilder, ir.FinalizableIndex index, ir.FunctionType type, - [String? functionName]) - : super(moduleBuilder.module, index, type, functionName) { + this.moduleBuilder, + ir.FinalizableIndex index, + ir.FunctionType type, [ + String? functionName, + ]) : super(moduleBuilder.module, index, type, functionName) { _body = InstructionsBuilder(moduleBuilder, type.inputs, type.outputs); } @@ -31,8 +33,12 @@ class FunctionBuilder extends ir.BaseFunction @override ir.DefinedFunction forceBuild() => ir.DefinedFunction( - enclosingModule, body.build(), finalizableIndex, type, functionName) - ..isPure = isPure; + enclosingModule, + body.build(), + finalizableIndex, + type, + functionName, + )..isPure = isPure; @override String toString() => functionName ?? "#$finalizableIndex"; diff --git a/pkg/wasm_builder/lib/src/builder/functions.dart b/pkg/wasm_builder/lib/src/builder/functions.dart index 0999e083130..87cba7802bf 100644 --- a/pkg/wasm_builder/lib/src/builder/functions.dart +++ b/pkg/wasm_builder/lib/src/builder/functions.dart @@ -29,17 +29,31 @@ class FunctionsBuilder with Builder { /// The [ir.DefinedFunction.body] must be completed (including the terminating /// `end`) before the module can be serialized. FunctionBuilder define(ir.FunctionType type, [String? name]) { - final function = - FunctionBuilder(_moduleBuilder, ir.FinalizableIndex(), type, name); + final function = FunctionBuilder( + _moduleBuilder, + ir.FinalizableIndex(), + type, + name, + ); _functionBuilders.add(function); return function; } /// Import a function into the module. - ir.ImportedFunction import(String module, String name, ir.FunctionType type, - [String? functionName]) { - final function = ir.ImportedFunction(_moduleBuilder.module, module, name, - ir.FinalizableIndex(), type, functionName); + ir.ImportedFunction import( + String module, + String name, + ir.FunctionType type, [ + String? functionName, + ]) { + final function = ir.ImportedFunction( + _moduleBuilder.module, + module, + name, + ir.FinalizableIndex(), + type, + functionName, + ); _importedFunctions.add(function); return function; } @@ -47,7 +61,9 @@ class FunctionsBuilder with Builder { @override ir.Functions forceBuild() { final built = finalizeImportsAndBuilders( - _importedFunctions, _functionBuilders); + _importedFunctions, + _functionBuilders, + ); return ir.Functions(_importedFunctions, built); } } diff --git a/pkg/wasm_builder/lib/src/builder/global.dart b/pkg/wasm_builder/lib/src/builder/global.dart index eee9653d542..8181db6f992 100644 --- a/pkg/wasm_builder/lib/src/builder/global.dart +++ b/pkg/wasm_builder/lib/src/builder/global.dart @@ -11,13 +11,21 @@ class GlobalBuilder extends ir.Global with IndexableBuilder { final InstructionsBuilder initializer; GlobalBuilder( - this.moduleBuilder, ir.FinalizableIndex index, ir.GlobalType type, - [String? globalName]) - : initializer = InstructionsBuilder(moduleBuilder, [], [type.type], - constantExpression: true), - super(moduleBuilder.module, index, type, globalName); + this.moduleBuilder, + ir.FinalizableIndex index, + ir.GlobalType type, [ + String? globalName, + ]) : initializer = InstructionsBuilder(moduleBuilder, [], [ + type.type, + ], constantExpression: true), + super(moduleBuilder.module, index, type, globalName); @override ir.DefinedGlobal forceBuild() => ir.DefinedGlobal( - enclosingModule, initializer.build(), finalizableIndex, type, globalName); + enclosingModule, + initializer.build(), + finalizableIndex, + type, + globalName, + ); } diff --git a/pkg/wasm_builder/lib/src/builder/globals.dart b/pkg/wasm_builder/lib/src/builder/globals.dart index 203a3bcb3f8..59ef2f738f2 100644 --- a/pkg/wasm_builder/lib/src/builder/globals.dart +++ b/pkg/wasm_builder/lib/src/builder/globals.dart @@ -27,8 +27,12 @@ class GlobalsBuilder with Builder { /// Defines a new global variable in this module. GlobalBuilder define(ir.GlobalType type, [String? name]) { - final global = - GlobalBuilder(_moduleBuilder, ir.FinalizableIndex(), type, name); + final global = GlobalBuilder( + _moduleBuilder, + ir.FinalizableIndex(), + type, + name, + ); _globalBuilders.add(global); return global; } @@ -36,7 +40,12 @@ class GlobalsBuilder with Builder { /// Imports a global variable into this module. ir.ImportedGlobal import(String module, String name, ir.GlobalType type) { final global = ir.ImportedGlobal( - _moduleBuilder.module, module, name, ir.FinalizableIndex(), type); + _moduleBuilder.module, + module, + name, + ir.FinalizableIndex(), + type, + ); _importedGlobals.add(global); return global; } @@ -62,8 +71,10 @@ class GlobalsBuilder with Builder { dfs(g); } - final built = - finalizeImportsAndBuilders(_importedGlobals, order); + final built = finalizeImportsAndBuilders( + _importedGlobals, + order, + ); return ir.Globals(_importedGlobals, built); } } diff --git a/pkg/wasm_builder/lib/src/builder/instructions.dart b/pkg/wasm_builder/lib/src/builder/instructions.dart index caa14a1a208..b87ec920989 100644 --- a/pkg/wasm_builder/lib/src/builder/instructions.dart +++ b/pkg/wasm_builder/lib/src/builder/instructions.dart @@ -132,8 +132,10 @@ class CatchRef extends TryTableCatch { ir.TryTableCatch toIr(int labelIndex) => ir.CatchRef(tag, labelIndex); @override - List caughtValues() => - [...tag.type.inputs, ir.RefType.exn(nullable: false)]; + List caughtValues() => [ + ...tag.type.inputs, + ir.RefType.exn(nullable: false), + ]; } class CatchAll extends TryTableCatch { @@ -153,8 +155,9 @@ class CatchAllRef extends TryTableCatch { ir.TryTableCatch toIr(int labelIndex) => ir.CatchAllRef(labelIndex); @override - List caughtValues() => - [ir.RefType.exn(nullable: false)]; + List caughtValues() => [ + ir.RefType.exn(nullable: false), + ]; } /// A sequence of Wasm instructions. @@ -225,10 +228,12 @@ class InstructionsBuilder with Builder { /// Create a new instruction sequence. InstructionsBuilder( - this.moduleBuilder, List inputs, List outputs, - {this.constantExpression = false}) - : _stackTraces = moduleBuilder.watchPoints.isNotEmpty ? {} : null, - _sourceMappings = moduleBuilder.sourceMapUrl == null ? null : [] { + this.moduleBuilder, + List inputs, + List outputs, { + this.constantExpression = false, + }) : _stackTraces = moduleBuilder.watchPoints.isNotEmpty ? {} : null, + _sourceMappings = moduleBuilder.sourceMapUrl == null ? null : [] { _labelStack.add(Expression(const [], outputs)); for (ir.ValueType paramType in inputs) { _addParameter(paramType); @@ -267,8 +272,14 @@ class InstructionsBuilder with Builder { @override ir.Instructions forceBuild() { if (_patchPoints.isEmpty) { - return ir.Instructions(locals, localNames, _instructions, _stackTraces, - _traceLines, _sourceMappings); + return ir.Instructions( + locals, + localNames, + _instructions, + _stackTraces, + _traceLines, + _sourceMappings, + ); } // We have to fill in the patched instructions & update stack maps. @@ -276,8 +287,9 @@ class InstructionsBuilder with Builder { final instructions = _instructions; final newInstructions = []; final sourceMappings = _sourceMappings; - final newSourceMappings = - _sourceMappings == null ? null : []; + final newSourceMappings = _sourceMappings == null + ? null + : []; // The number of additional patch instructions emitted. int shift = 0; @@ -314,27 +326,42 @@ class InstructionsBuilder with Builder { } } - return ir.Instructions(locals, localNames, newInstructions, _stackTraces, - _traceLines, newSourceMappings); + return ir.Instructions( + locals, + localNames, + newInstructions, + _stackTraces, + _traceLines, + newSourceMappings, + ); } /// Marks a region in the instruction stream (defined by instructions emitted /// by `fun`) which will be updated in the link phase via the `linkFun`. InstructionsBuilder? createPatchableRegion( - List inputs, List outputs) { + List inputs, + List outputs, + ) { assert(_verifyTypes(inputs, outputs, trace: [''])); if (!_reachable) return null; - final patchBuilder = InstructionsBuilder(moduleBuilder, inputs, outputs, - constantExpression: constantExpression); - _patchPoints - .add(_PatchableRegion._PatchPoint(_instructions.length, patchBuilder)); + final patchBuilder = InstructionsBuilder( + moduleBuilder, + inputs, + outputs, + constantExpression: constantExpression, + ); + _patchPoints.add( + _PatchableRegion._PatchPoint(_instructions.length, patchBuilder), + ); return patchBuilder; } void _add(ir.Instruction i) { - assert(!constantExpression || i.isConstant, - "Non-constant instruction $i added to constant expression"); + assert( + !constantExpression || i.isConstant, + "Non-constant instruction $i added to constant expression", + ); if (!_reachable) return; _instructions.add(i); if (moduleBuilder.watchPoints.isNotEmpty) { @@ -379,10 +406,12 @@ class InstructionsBuilder with Builder { } } - bool _debugTrace(List? trace, - {required bool reachableAfter, - int indentBefore = 0, - int indentAfter = 0}) { + bool _debugTrace( + List? trace, { + required bool reachableAfter, + int indentBefore = 0, + int indentAfter = 0, + }) { if (traceEnabled && trace != null) { _indent += indentBefore; String instr = "${" " * _indent} ${trace.join(" ")}"; @@ -392,12 +421,14 @@ class InstructionsBuilder with Builder { final int stackHeight = _stackTypes.length; final String stack = reachableAfter ? stackHeight <= maxStackShown - ? _stackTypes.join(', ') - : [ - ..._stackTypes.sublist(0, maxStackShown ~/ 2), - "... ${stackHeight - maxStackShown} omitted ...", - ..._stackTypes.sublist(stackHeight - (maxStackShown + 1) ~/ 2) - ].join(', ') + ? _stackTypes.join(', ') + : [ + ..._stackTypes.sublist(0, maxStackShown ~/ 2), + "... ${stackHeight - maxStackShown} omitted ...", + ..._stackTypes.sublist( + stackHeight - (maxStackShown + 1) ~/ 2, + ), + ].join(', ') : "-"; final String line = "$instr$stack\n"; _indent += indentAfter; @@ -437,8 +468,10 @@ class InstructionsBuilder with Builder { List get stack => _stackTypes; - List _checkStackTypes(List inputs, - [List? stack]) { + List _checkStackTypes( + List inputs, [ + List? stack, + ]) { stack ??= _stack(inputs.length); bool typesMatch = true; for (int i = 0; i < inputs.length; i++) { @@ -455,15 +488,26 @@ class InstructionsBuilder with Builder { return stack; } - bool _verifyTypes(List inputs, List outputs, - {List? trace, bool reachableAfter = true}) { - return _verifyTypesFun(inputs, (_) => outputs, - trace: trace, reachableAfter: reachableAfter); + bool _verifyTypes( + List inputs, + List outputs, { + List? trace, + bool reachableAfter = true, + }) { + return _verifyTypesFun( + inputs, + (_) => outputs, + trace: trace, + reachableAfter: reachableAfter, + ); } - bool _verifyTypesFun(List inputs, - List Function(List) outputsFun, - {List? trace, bool reachableAfter = true}) { + bool _verifyTypesFun( + List inputs, + List Function(List) outputsFun, { + List? trace, + bool reachableAfter = true, + }) { if (!_reachable) { return _debugTrace(trace, reachableAfter: false); } @@ -472,8 +516,9 @@ class InstructionsBuilder with Builder { final String expected = inputs.join(', '); final String got = _stackTypes.sublist(baseStackHeight).join(', '); _reportError( - "Underflowing base stack of innermost block: expected [$expected], " - "but stack contained [$got]"); + "Underflowing base stack of innermost block: expected [$expected], " + "but stack contained [$got]", + ); } final List stack = _checkStackTypes(inputs); _stackTypes.length -= inputs.length; @@ -481,8 +526,11 @@ class InstructionsBuilder with Builder { return _debugTrace(trace, reachableAfter: reachableAfter); } - bool _verifyBranchTypes(Label label, - [int popped = 0, List pushed = const []]) { + bool _verifyBranchTypes( + Label label, [ + int popped = 0, + List pushed = const [], + ]) { if (!_reachable) { return true; } @@ -495,9 +543,10 @@ class InstructionsBuilder with Builder { ? pushed.sublist(pushed.length - inputs.length) : [ ..._stackTypes.sublist( - _stackTypes.length - popped + pushed.length - inputs.length, - _stackTypes.length - popped), - ...pushed + _stackTypes.length - popped + pushed.length - inputs.length, + _stackTypes.length - popped, + ), + ...pushed, ]; _checkStackTypes(inputs, stack); return true; @@ -505,20 +554,26 @@ class InstructionsBuilder with Builder { bool _verifyStartOfBlock(Label label, {required List trace}) { return _debugTrace( - ["$label:", ...trace, ir.FunctionType(label.inputs, label.outputs)], - reachableAfter: _reachable, indentAfter: 1); + ["$label:", ...trace, ir.FunctionType(label.inputs, label.outputs)], + reachableAfter: _reachable, + indentAfter: 1, + ); } - bool _verifyEndOfBlock(List outputs, - {required List trace, - required bool reachableAfter, - required bool reindent}) { + bool _verifyEndOfBlock( + List outputs, { + required List trace, + required bool reachableAfter, + required bool reindent, + }) { final Label label = _topOfLabelStack; if (_reachable) { final int expectedHeight = label.baseStackHeight + label.outputs.length; if (_stackTypes.length != expectedHeight) { - _reportError("Incorrect stack height at end of block" - " (expected $expectedHeight, actual ${_stackTypes.length})"); + _reportError( + "Incorrect stack height at end of block" + " (expected $expectedHeight, actual ${_stackTypes.length})", + ); } _checkStackTypes(label.outputs); } @@ -528,10 +583,12 @@ class InstructionsBuilder with Builder { _stackTypes.addAll(outputs); } _resetLocalInitialization(label); - return _debugTrace([if (label.hasOrdinal) "$label:", ...trace], - reachableAfter: reachableAfter, - indentBefore: -1, - indentAfter: reindent ? 1 : 0); + return _debugTrace( + [if (label.hasOrdinal) "$label:", ...trace], + reachableAfter: reachableAfter, + indentBefore: -1, + indentAfter: reindent ? 1 : 0, + ); } // Source maps @@ -542,7 +599,8 @@ class InstructionsBuilder with Builder { /// This assumes [recordSourceMaps] is `true`. void startSourceMapping(Uri fileUri, int line, int col, String? name) { _addSourceMapping( - SourceMapping(_instructions.length, fileUri, line, col, name)); + SourceMapping(_instructions.length, fileUri, line, col, name), + ); } /// Stop mapping added instructions to the last source location given in @@ -594,8 +652,14 @@ class InstructionsBuilder with Builder { /// Emit an `unreachable` instruction. void unreachable() { - assert(_verifyTypes(const [], const [], - trace: const ['unreachable'], reachableAfter: false)); + assert( + _verifyTypes( + const [], + const [], + trace: const ['unreachable'], + reachableAfter: false, + ), + ); _add(const ir.Unreachable()); _reachable = false; } @@ -619,88 +683,111 @@ class InstructionsBuilder with Builder { } Label _beginBlock( - Label label, - ir.Instruction Function() noEffect, - ir.Instruction Function(ir.ValueType type) oneOutput, - ir.Instruction Function(ir.FunctionType type) function) { + Label label, + ir.Instruction Function() noEffect, + ir.Instruction Function(ir.ValueType type) oneOutput, + ir.Instruction Function(ir.FunctionType type) function, + ) { if (label.inputs.isEmpty && label.outputs.isEmpty) { _add(noEffect()); } else if (label.inputs.isEmpty && label.outputs.length == 1) { _add(oneOutput(label.outputs.single)); } else { - _add(function( - moduleBuilder.types.defineFunction(label.inputs, label.outputs))); + _add( + function( + moduleBuilder.types.defineFunction(label.inputs, label.outputs), + ), + ); } return label; } /// Emit a `block` instruction. /// Branching to the returned label will branch to the matching `end`. - Label block( - [List inputs = const [], - List outputs = const []]) => - _beginBlock( - _pushLabel(Block(inputs, outputs), trace: const ['block']), - ir.BeginNoEffectBlock.new, - ir.BeginOneOutputBlock.new, - ir.BeginFunctionBlock.new); + Label block([ + List inputs = const [], + List outputs = const [], + ]) => _beginBlock( + _pushLabel(Block(inputs, outputs), trace: const ['block']), + ir.BeginNoEffectBlock.new, + ir.BeginOneOutputBlock.new, + ir.BeginFunctionBlock.new, + ); /// Emit a `loop` instruction. /// Branching to the returned label will branch to the `loop`. - Label loop( - [List inputs = const [], - List outputs = const []]) => - _beginBlock( - _pushLabel(Loop(inputs, outputs), trace: const ['loop']), - ir.BeginNoEffectLoop.new, - ir.BeginOneOutputLoop.new, - ir.BeginFunctionLoop.new); + Label loop([ + List inputs = const [], + List outputs = const [], + ]) => _beginBlock( + _pushLabel(Loop(inputs, outputs), trace: const ['loop']), + ir.BeginNoEffectLoop.new, + ir.BeginOneOutputLoop.new, + ir.BeginFunctionLoop.new, + ); /// Emit an `if` instruction. /// Branching to the returned label will branch to the matching `end`. - Label if_( - [List inputs = const [], - List outputs = const []]) { + Label if_([ + List inputs = const [], + List outputs = const [], + ]) { assert(_verifyTypes(const [ir.NumType.i32], const [])); return _beginBlock( - _pushLabel(If(inputs, outputs), trace: const ['if']), - ir.BeginNoEffectIf.new, - ir.BeginOneOutputIf.new, - ir.BeginFunctionIf.new); + _pushLabel(If(inputs, outputs), trace: const ['if']), + ir.BeginNoEffectIf.new, + ir.BeginOneOutputIf.new, + ir.BeginFunctionIf.new, + ); } /// Emit an `else` instruction. void else_() { - assert(_topOfLabelStack is If || - _reportError("Unexpected 'else' (not in 'if' block)")); + assert( + _topOfLabelStack is If || + _reportError("Unexpected 'else' (not in 'if' block)"), + ); final If label = _topOfLabelStack as If; assert(!label.hasElse || _reportError("Duplicate 'else' in 'if' block")); - assert(_verifyEndOfBlock(label.inputs, + assert( + _verifyEndOfBlock( + label.inputs, trace: const ['else'], reachableAfter: _topOfLabelStack.reachable, - reindent: true)); + reindent: true, + ), + ); label.hasElse = true; _reachable = _topOfLabelStack.reachable; _add(const ir.Else()); } /// Emit a legacy `try` instruction. - Label try_legacy( - [List inputs = const [], - List outputs = const []]) => - _beginBlock( - _pushLabel(Try(inputs, outputs), trace: const ['try']), - ir.BeginNoEffectTry.new, - ir.BeginOneOutputTry.new, - ir.BeginFunctionTry.new); + Label try_legacy([ + List inputs = const [], + List outputs = const [], + ]) => _beginBlock( + _pushLabel(Try(inputs, outputs), trace: const ['try']), + ir.BeginNoEffectTry.new, + ir.BeginOneOutputTry.new, + ir.BeginFunctionTry.new, + ); /// Emit a legacy `catch` instruction. void catch_legacy(ir.Tag tag) { - assert(_topOfLabelStack is Try || - _reportError("Unexpected 'catch' (not in 'try' block)")); + assert( + _topOfLabelStack is Try || + _reportError("Unexpected 'catch' (not in 'try' block)"), + ); final Try try_ = _topOfLabelStack as Try; - assert(_verifyEndOfBlock(tag.type.inputs, - trace: ['catch', tag], reachableAfter: try_.reachable, reindent: true)); + assert( + _verifyEndOfBlock( + tag.type.inputs, + trace: ['catch', tag], + reachableAfter: try_.reachable, + reindent: true, + ), + ); assert(tag.enclosingModule == module); try_.hasCatch = true; _reachable = try_.reachable; @@ -731,10 +818,14 @@ class InstructionsBuilder with Builder { /// Emit an `end` instruction. void end() { - assert(_verifyEndOfBlock(_topOfLabelStack.outputs, + assert( + _verifyEndOfBlock( + _topOfLabelStack.outputs, trace: const ['end'], reachableAfter: _topOfLabelStack.reachable, - reindent: false)); + reindent: false, + ), + ); _reachable = _topOfLabelStack.reachable; _labelStack.removeLast(); _add(const ir.End()); @@ -748,8 +839,14 @@ class InstructionsBuilder with Builder { /// Emit a `br` instruction. void br(Label label) { - assert(_verifyTypes(const [], const [], - trace: ['br', label], reachableAfter: false)); + assert( + _verifyTypes( + const [], + const [], + trace: ['br', label], + reachableAfter: false, + ), + ); assert(_verifyBranchTypes(label)); _add(ir.Br(_labelIndex(label))); _reachable = false; @@ -757,82 +854,119 @@ class InstructionsBuilder with Builder { /// Emit a `br_if` instruction. void br_if(Label label) { - assert(_verifyTypes(const [ir.NumType.i32], const [], - trace: ['br_if', label])); + assert( + _verifyTypes(const [ir.NumType.i32], const [], trace: ['br_if', label]), + ); assert(_verifyBranchTypes(label)); _add(ir.BrIf(_labelIndex(label))); } /// Emit a `br_table` instruction. void br_table(List