Files
sdk/tests/web/wasm/utils.dart
Ömer Ağacan 992a07a552 [dart2wasm] Generate dart2js sourcemap extensions to map minified class names to full names
Reuse dart2js's source map extension format to map minified class names
shown in error messages and runtime type strings ("minified:Class123")
to original class names.

The extension field is only generated when minifying, and with multiple
modules, only in the main module's source map.

- Without minification runtime type strings and error messages already
  include the full class name, so mapping is not necessary.

- With multiple modules, the existing tools use the main module's source
  map to deobfuscate errors.

Because wasm-opt is not aware of this custom section and it also changes
the names section, when optimizing, we read the original names before
calling wasm-opt, and write the section back to the source map file for
the optimized Wasm.

When not optimizing we generate the source map with the custom section
directly.

Example deobfuscation using the new source maps:

    $ dart pkg/dart2js_tools/bin/lookup_name.dart test.wasm.map Class123
    Class123 => AsyncError (a global name)

The custom section is a bit verbose for what dart2wasm needs: we could
map numbers to name indices instead of strings to name indices, as
dart2wasm minifies class names to numbers. However to avoid updating a
bunch of tools in g3, SDK, maybe also in Flutter and devtools, we reuse
dart2js's format, at least for now.

Issue: https://github.com/dart-lang/sdk/issues/60711
Change-Id: I2cda331723c6f0c7e7ef5f4772feaf420dc8c6ba
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/474660
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2026-01-28 02:48:30 -08:00

25 lines
860 B
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.
import 'dart:js_interop';
import 'dart:typed_data';
String getSourceMapFilePath(String testName, int moduleId) {
final compilationDir = const String.fromEnvironment('TEST_COMPILATION_DIR');
if (moduleId == 0) {
return '$compilationDir/${testName}_test.wasm.map';
} else {
return '$compilationDir/${testName}_test_module$moduleId.wasm.map';
}
}
/// Read the file at the given [path].
///
/// This relies on the `readbuffer` function provided by d8.
@JS()
external JSArrayBuffer readbuffer(JSString path);
/// Read the file at the given [path].
Uint8List readfile(String path) => Uint8List.view(readbuffer(path.toJS).toDart);