Files
sdk/pkg/dart2wasm/lib/source_map_utils.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

90 lines
3.5 KiB
Dart

// Copyright (c) 2026, 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.
const String _sourceMapExtensionName = "x_org_dartlang_dart2js";
/// Add dart2js source map extension fields to allow mapping "minified:Class123"
/// parts in error messages to unminified class names like "String".
///
/// This is only needed in minified builds. Non-minified builds will have the
/// full class names in the errors.
///
/// The extension format is described in
/// `pkg/compiler/doc/sourcemap_extensions.md`.
void addMinifiedClassNames(
Map<String, Object?> sourceMapJson,
List<String?> classNames,
) {
final List<dynamic> names = sourceMapJson['names'] as List<dynamic>;
// Some of the class names may already be in the names list. Create the
// name->index map with existing names to reuse existing names.
final Map<String, int> nameIndices = {};
for (var i = 0; i < names.length; i += 1) {
nameIndices[names[i]] = i;
}
final Map<String, int> minifiedClassNamesToUnminified = {};
for (var classId = 0; classId < classNames.length; classId += 1) {
final unminifiedClassName = classNames[classId];
if (unminifiedClassName == null) continue;
final minifiedClassName = "Class$classId";
int? unminifiedClassNameIndex = nameIndices[unminifiedClassName];
if (unminifiedClassNameIndex == null) {
unminifiedClassNameIndex = names.length;
names.add(unminifiedClassName);
}
minifiedClassNamesToUnminified[minifiedClassName] =
unminifiedClassNameIndex;
}
// 'names' is updated in-place above with the full class names, add the
// new section.
//
// We don't need the "instance" section, but some of the tools want it to be
// present always.
sourceMapJson[_sourceMapExtensionName] = <String, Object?>{
"minified_names": <String, Object?>{
"global": minifiedClassNamesToUnminified.entries
.map((entry) => "${entry.key},${entry.value}")
.join(","),
"instance": "",
},
};
}
/// If the source map has the custom section added by [addMinifiedClassNames],
/// parse the unminified class names and return the original list* passed to
/// [addMinifiedClassNames].
///
/// (*): With the exception that trailing `null` elements of the original list
/// will be missing in the return value of this function.
List<String?>? getMinifiedClassNames(Map<String, Object?> sourceMapJson) {
final List<dynamic> names = sourceMapJson["names"] as List<dynamic>;
final extensionField = sourceMapJson[_sourceMapExtensionName];
if (extensionField == null) return null;
final List<String> globalMinifiedNames =
(((extensionField as Map)["minified_names"] as Map)["global"] as String)
.split(",");
List<String?> unminifiedClassNames = [];
for (int i = 0; i < globalMinifiedNames.length; i += 2) {
final minifiedClassName = globalMinifiedNames[i];
assert(minifiedClassName.startsWith("Class"));
final classId = int.parse(minifiedClassName.substring("Class".length));
final unminifiedClassNameIndex = int.parse(globalMinifiedNames[i + 1]);
final unminifiedClassName = names[unminifiedClassNameIndex] as String;
while (unminifiedClassNames.length <= classId) {
unminifiedClassNames.add(null);
}
assert(unminifiedClassNames[classId] == null);
unminifiedClassNames[classId] = unminifiedClassName;
}
return unminifiedClassNames;
}