diff --git a/pkg/dev_compiler/pubspec.yaml b/pkg/dev_compiler/pubspec.yaml index d08bea697d5..f40fa8e3174 100644 --- a/pkg/dev_compiler/pubspec.yaml +++ b/pkg/dev_compiler/pubspec.yaml @@ -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 diff --git a/pkg/dev_compiler/web/stack_trace_mapper.dart b/pkg/dev_compiler/web/stack_trace_mapper.dart index 4cc41366404..382b231124a 100644 --- a/pkg/dev_compiler/web/stack_trace_mapper.dart +++ b/pkg/dev_compiler/web/stack_trace_mapper.dart @@ -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 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 mapper, + required JSFunction)> + 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() + ? (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 roots = rootDirectories.map((s) => '$s').toList(); +final List 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 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, ); }