Files
sdk/pkg/dart2wasm/lib/static_dispatch_table.dart
T
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

65 lines
2.3 KiB
Dart

// Copyright (c) 2024, 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 'package:wasm_builder/wasm_builder.dart' as w;
import 'translator.dart';
/// Builds a table of functions that can be used across modules.
class CrossModuleFunctionTable {
static const w.HeapType _tableHeapType = w.HeapType.func;
final Translator translator;
/// Contents of wasm table.
final Map<w.BaseFunction, int> _table = {};
late final w.TableBuilder _definedWasmTable = translator.mainModule.tables
.define(w.RefType(_tableHeapType, nullable: true), _table.length);
final WasmTableImporter _importedWasmTables;
CrossModuleFunctionTable(this.translator)
: _importedWasmTables = WasmTableImporter(
translator,
'cross-module-funcs-',
);
/// Gets the wasm table used to reference this table in [module].
///
/// This can either be the table definition itself or an import of it. Imports
/// the table into [module] if it is not imported yet.
w.Table getWasmTable(w.ModuleBuilder module) {
return _importedWasmTables.get(_definedWasmTable, module);
}
/// Returns the index for [function] in the table allocating one if necessary.
int indexForFunction(w.BaseFunction function) {
assert(function.type.isStructuralSubtypeOf(_tableHeapType));
return _table[function] ??= _table.length;
}
void output() {
final importedTables = _importedWasmTables;
_table.forEach((fun, index) {
final targetModule = translator.moduleToBuilder[fun.enclosingModule]!;
if (translator.isMainModule(targetModule)) {
_definedWasmTable.moduleBuilder.elements
.activeFunctionSegmentBuilderFor(_definedWasmTable)
.setFunctionAt(index, fun);
} else {
// This will generate the imported table if it doesn't already exist.
final importedTable = getWasmTable(targetModule) as w.ImportedTable;
targetModule.elements
.activeFunctionSegmentBuilderFor(importedTable)
.setFunctionAt(index, fun);
}
});
_definedWasmTable.minSize = _table.length;
for (final table in importedTables.imports) {
table.minSize = _table.length;
}
}
}