[ddc] Migrate stack_trace_mapper to static JS interop

Also removes the dependency on `package:js` as this was the only remaining usage.

Fixes https://github.com/dart-lang/sdk/issues/54574

Change-Id: Icba5bce7fbc087ed2284754656bf06493c516009
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/510100
Reviewed-by: Nicholas Shahan <nshahan@google.com>
Commit-Queue: Srujan Gaddam <srujzs@google.com>
Reviewed-by: Srujan Gaddam <srujzs@google.com>
This commit is contained in:
Parker Lougheed
2026-06-11 09:32:44 -07:00
committed by dart-scoped@luci-project-accounts.iam.gserviceaccount.com
parent c3c2986b82
commit 84088f6a8b
2 changed files with 32 additions and 32 deletions
-1
View File
@@ -30,7 +30,6 @@ dev_dependencies:
browser_launcher: any
expect: any
http_multi_server: any
js: any
lints: any
modular_test: any
reload_test: any
+32 -31
View File
@@ -6,23 +6,23 @@
/// on the page compiled with DDC.
///
/// Example JavaScript usage:
/// $dartStackTraceUtility.addLoadedListener(function() {
/// // All Dart source maps are now loaded. It is now safe to start your
/// // Dart application compiled with DDC.
/// dart_library.start('your_dart_application');
/// })
///
/// If $dartStackTraceUtility is set, the dart:core StackTrace class calls
/// $dartStackTraceUtility.mapper(someJSStackTrace)
/// ```js
/// $dartStackTraceUtility.setSourceMapProvider(function(modulePath) {
/// return dartDevEmbedder.debugger.getSourceMap(modulePath);
/// });
/// ```
///
/// If `$dartStackTraceUtility` is set, the dart:core StackTrace class calls
/// `$dartStackTraceUtility.mapper(someJSStackTrace)`
/// to apply source maps.
///
/// This utility can be compiled to JavaScript using Dart2JS while the rest
/// of the application is compiled with DDC or could be compiled with DDC.
@JS()
library;
// ignore: deprecated_member_use
import 'package:js/js.dart';
import 'dart:js_interop';
import 'package:path/path.dart' as p;
import 'package:source_maps/source_maps.dart';
import 'package:source_span/source_span.dart';
@@ -30,32 +30,27 @@ import 'package:stack_trace/stack_trace.dart';
import 'source_map_stack_trace.dart';
typedef ReadyCallback = void Function();
/// Global object DDC uses to see if a stack trace utility has been registered.
@JS(r'$dartStackTraceUtility')
external set dartStackTraceUtility(DartStackTraceUtility value);
@JS(r'$dartLoader.rootDirectories')
external List get rootDirectories;
external JSArray<JSString> get _rootDirectories;
typedef StackTraceMapper = String Function(String stackTrace);
typedef SourceMapProvider = dynamic Function(String modulePath);
typedef SetSourceMapProvider = void Function(SourceMapProvider);
typedef SourceMapProvider = JSAny? Function(String modulePath);
@JS()
@anonymous
class DartStackTraceUtility {
extension type DartStackTraceUtility._(JSObject _) implements JSObject {
external factory DartStackTraceUtility({
StackTraceMapper? mapper,
SetSourceMapProvider? setSourceMapProvider,
required JSFunction<String Function(String rawStackTrace)> mapper,
required JSFunction<void Function(JSFunction<SourceMapProvider>)>
setSourceMapProvider,
});
}
@JS('JSON.stringify')
external String _stringify(dynamic json);
external String _stringify(JSAny? json);
/// Source mapping that is waits to parse source maps until they match the uri
/// Source mapping that waits to parse source maps until they match the uri
/// of a requested source map.
///
/// This improves startup performance compared to using MappingBundle directly.
@@ -83,7 +78,9 @@ class LazyMapping extends Mapping {
if (!_bundle.containsMapping(uri)) {
var rawMap = _provider(uri);
if (rawMap != null) {
var strMap = rawMap is String ? rawMap : _stringify(rawMap);
var strMap = rawMap.isA<JSString>()
? (rawMap as JSString).toDart
: _stringify(rawMap);
var mapping = parse(strMap) as SingleMapping;
mapping
..targetUrl = uri
@@ -103,27 +100,31 @@ class LazyMapping extends Mapping {
LazyMapping? _mapping;
List<String> roots = rootDirectories.map((s) => '$s').toList();
final List<String> roots = _rootDirectories.toDart
.map((s) => s.toDart)
.toList();
String mapper(String rawStackTrace) {
var mapping = _mapping;
if (mapping == null) {
// This should not happen if the user has waited for the ReadyCallback
// to start the application.
// This shouldn't happen if `setSourceMapProvider` was called
// before the application was started.
throw StateError('Source maps are not done loading.');
}
var trace = Trace.parse(rawStackTrace);
return mapStackTrace(mapping, trace, roots: roots).toString();
}
void setSourceMapProvider(SourceMapProvider provider) {
_mapping = LazyMapping(provider);
void setSourceMapProvider(JSFunction<SourceMapProvider> provider) {
_mapping = LazyMapping(
(modulePath) => provider.callAsFunction(null, modulePath.toJS),
);
}
void main() {
// Register with DDC.
dartStackTraceUtility = DartStackTraceUtility(
mapper: allowInterop(mapper),
setSourceMapProvider: allowInterop(setSourceMapProvider),
mapper: mapper.toJS,
setSourceMapProvider: setSourceMapProvider.toJS,
);
}