diff --git a/dwds/CHANGELOG.md b/dwds/CHANGELOG.md index 747365d77..0b25f55e2 100644 --- a/dwds/CHANGELOG.md +++ b/dwds/CHANGELOG.md @@ -1,4 +1,5 @@ ## 24.3.4-wip +- Added support for some debugging APIs with the DDC library bundle format. - [#2566](https://github.com/dart-lang/webdev/issues/2566) ## 24.3.3 diff --git a/dwds/lib/src/debugging/dart_runtime_debugger.dart b/dwds/lib/src/debugging/dart_runtime_debugger.dart index ed633ee48..bc0d79a9b 100644 --- a/dwds/lib/src/debugging/dart_runtime_debugger.dart +++ b/dwds/lib/src/debugging/dart_runtime_debugger.dart @@ -197,10 +197,38 @@ class DartRuntimeDebugger { ); } + /// Generates a JS expression to invoke a Dart extension method. String invokeExtensionJsExpression(String methodName, String encodedJson) { return _generateJsExpression( "${_loadStrategy.loadModuleSnippet}('dart_sdk').developer.invokeExtension('$methodName', JSON.stringify($encodedJson));", "dartDevEmbedder.debugger.invokeExtension('$methodName', JSON.stringify($encodedJson));", ); } + + /// Generates a JS expression for calling a library method. + String callLibraryMethodJsExpression( + String libraryUri, + String methodName, + ) { + final findLibraryExpression = ''' + (function() { + const sdk = ${_loadStrategy.loadModuleSnippet}('dart_sdk'); + const dart = sdk.dart; + const library = dart.getLibrary('$libraryUri'); + if (!library) throw 'cannot find library for $libraryUri'; + return library; + })(); + '''; + + // `callLibraryMethod` expects an array of arguments. Chrome DevTools spreads + // arguments individually when calling functions. This code reconstructs the + // expected argument array. + return _generateJsExpression( + findLibraryExpression, + _wrapWithBundleLoader( + '', + 'callLibraryMethod("$libraryUri", "$methodName", Array.from(arguments))', + ), + ); + } } diff --git a/dwds/lib/src/debugging/inspector.dart b/dwds/lib/src/debugging/inspector.dart index 4f557d47f..f3ae1264c 100644 --- a/dwds/lib/src/debugging/inspector.dart +++ b/dwds/lib/src/debugging/inspector.dart @@ -14,6 +14,7 @@ import 'package:dwds/src/debugging/instance.dart'; import 'package:dwds/src/debugging/libraries.dart'; import 'package:dwds/src/debugging/location.dart'; import 'package:dwds/src/debugging/remote_debugger.dart'; +import 'package:dwds/src/loaders/ddc_library_bundle.dart'; import 'package:dwds/src/readers/asset_reader.dart'; import 'package:dwds/src/utilities/conversions.dart'; import 'package:dwds/src/utilities/dart_uri.dart'; @@ -301,11 +302,17 @@ class AppInspector implements AppInspectorInterface { String selector, List arguments, ) { - return _evaluateInLibrary( - library, - 'function () { return this.$selector.apply(this, arguments);}', - arguments, - ); + return globalToolConfiguration.loadStrategy is DdcLibraryBundleStrategy + ? _evaluateLibraryMethodWithDdcLibraryBundle( + library, + selector, + arguments, + ) + : _evaluateInLibrary( + library, + 'function () { return this.$selector.apply(this, arguments); }', + arguments, + ); } /// Evaluate [expression] by calling Chrome's Runtime.evaluate. @@ -340,19 +347,31 @@ class AppInspector implements AppInspectorInterface { if (libraryUri == null) { throwInvalidParam('invoke', 'library uri is null'); } - final findLibrary = ''' - (function() { - const sdk = ${globalToolConfiguration.loadStrategy.loadModuleSnippet}('dart_sdk'); - const dart = sdk.dart; - const library = dart.getLibrary('$libraryUri'); - if (!library) throw 'cannot find library for $libraryUri'; - return library; - })(); - '''; - final remoteLibrary = await jsEvaluate(findLibrary); + final findLibraryJsExpression = globalToolConfiguration + .loadStrategy.dartRuntimeDebugger + .callLibraryMethodJsExpression(libraryUri, jsFunction); + + final remoteLibrary = await jsEvaluate(findLibraryJsExpression); return jsCallFunctionOn(remoteLibrary, jsFunction, arguments); } + /// Evaluates the specified top-level method [methodName] within [library] + /// using the Dart Development Compiler (DDC) library bundle strategy with + /// the given [arguments]. + Future _evaluateLibraryMethodWithDdcLibraryBundle( + Library library, + String methodName, + List arguments, + ) { + final libraryUri = library.uri; + if (libraryUri == null) { + throwInvalidParam('invoke', 'library uri is null'); + } + final expression = globalToolConfiguration.loadStrategy.dartRuntimeDebugger + .callLibraryMethodJsExpression(libraryUri, methodName); + return _jsCallFunction(expression, arguments); + } + /// Call [function] with objects referred by [argumentIds] as arguments. @override Future callFunction( diff --git a/dwds/test/common/chrome_proxy_service_common.dart b/dwds/test/common/chrome_proxy_service_common.dart index f640ae0d1..650e0b883 100644 --- a/dwds/test/common/chrome_proxy_service_common.dart +++ b/dwds/test/common/chrome_proxy_service_common.dart @@ -1733,9 +1733,6 @@ void runTests({ .having((instance) => instance.kind, 'kind', 'String'), ); }, - skip: moduleFormat == ModuleFormat.ddc && canaryFeatures == true - ? 'https://github.com/dart-lang/webdev/issues/2566' - : null, ); test( @@ -1761,9 +1758,6 @@ void runTests({ .having((instance) => instance.kind, 'kind', 'Null'), ); }, - skip: moduleFormat == ModuleFormat.ddc && canaryFeatures == true - ? 'https://github.com/dart-lang/webdev/issues/2566' - : null, ); test( @@ -1789,9 +1783,6 @@ void runTests({ .having((instance) => instance.kind, 'kind', 'Bool'), ); }, - skip: moduleFormat == ModuleFormat.ddc && canaryFeatures == true - ? 'https://github.com/dart-lang/webdev/issues/2566' - : null, ); test( @@ -1817,9 +1808,6 @@ void runTests({ .having((instance) => instance.kind, 'kind', 'Double'), ); }, - skip: moduleFormat == ModuleFormat.ddc && canaryFeatures == true - ? 'https://github.com/dart-lang/webdev/issues/2566' - : null, ); test( @@ -1845,9 +1833,6 @@ void runTests({ .having((instance) => instance.kind, 'kind', 'String'), ); }, - skip: moduleFormat == ModuleFormat.ddc && canaryFeatures == true - ? 'https://github.com/dart-lang/webdev/issues/2566' - : null, ); });