Skip to content

Commit

Permalink
Added support for callLibraryMethod with the DDC library bundle forma…
Browse files Browse the repository at this point in the history
…t. (#2577)

* Added support for getClassMetadata with the DDc library bundle format

* Added support for some debugging APIs with the DDC library bundle format.

* Update pattern test to account for new DDC JS variable naming

* reverting change to pattern test

* Added support for debugging API  with the DDC library bundle format.

* updated licenses

* updated licenses and remove new line from changelog

* Added support for some debugging APIs with the DDC library bundle format - getRecordTypeFieldsJsExpression, callInstanceMethodJsExpression

* Added support for some debugging APIs with the DDC library bundle format

* updated CHANGELOG

* refactored test to use common file

* delete main_ddc_library_bundle.dart

* fixed broken test - decoded response body to match source and renamed stack property

* updated changelog

* updated dart_scope to not renamed wildcard and skipped related test case

* formatted test/common/chrome_proxy_service_common.dart

* WIP

* Added support for callLibraryMethod  with the DDC library bundle format.

* revert changes to callFunction

* updated function name and comments

* updated CHANGELOG
  • Loading branch information
jyameo authored Jan 24, 2025
1 parent 1d0b32c commit 5dc7950
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 30 deletions.
1 change: 1 addition & 0 deletions dwds/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
28 changes: 28 additions & 0 deletions dwds/lib/src/debugging/dart_runtime_debugger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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))',
),
);
}
}
49 changes: 34 additions & 15 deletions dwds/lib/src/debugging/inspector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -301,11 +302,17 @@ class AppInspector implements AppInspectorInterface {
String selector,
List<RemoteObject> 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.
Expand Down Expand Up @@ -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<RemoteObject> _evaluateLibraryMethodWithDdcLibraryBundle(
Library library,
String methodName,
List<RemoteObject> 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<RemoteObject> callFunction(
Expand Down
15 changes: 0 additions & 15 deletions dwds/test/common/chrome_proxy_service_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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,
);
});

Expand Down

0 comments on commit 5dc7950

Please sign in to comment.