From ba434981e3ccc4bd0e1a929d0e70982de9ef4152 Mon Sep 17 00:00:00 2001 From: Anna Gringauze Date: Wed, 13 Jul 2022 11:38:27 -0700 Subject: [PATCH 1/7] Migrate debugging/inspector.dart to null safety --- dwds/lib/src/debugging/debugger.dart | 6 +- dwds/lib/src/debugging/inspector.dart | 142 ++++++++++-------- dwds/lib/src/debugging/instance.dart | 3 + .../src/services/chrome_proxy_service.dart | 4 +- dwds/lib/src/utilities/domain.dart | 10 +- dwds/lib/src/utilities/shared.dart | 50 ++++-- dwds/test/inspector_test.dart | 38 +++++ 7 files changed, 170 insertions(+), 83 deletions(-) diff --git a/dwds/lib/src/debugging/debugger.dart b/dwds/lib/src/debugging/debugger.dart index a8f226e3f..9ab72eb76 100644 --- a/dwds/lib/src/debugging/debugger.dart +++ b/dwds/lib/src/debugging/debugger.dart @@ -12,7 +12,6 @@ import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart' hide StackTrace; import '../loaders/strategy.dart'; -import '../services/chrome_debug_exception.dart'; import '../utilities/conversions.dart'; import '../utilities/dart_uri.dart'; import '../utilities/domain.dart'; @@ -722,12 +721,9 @@ class Debugger extends Domain { try { return await _remoteDebugger.evaluateOnCallFrame(callFrameId, expression); } on ExceptionDetails catch (e) { - throw ChromeDebugException( + throwChromeDebugException( e.json, evalContents: expression, - additionalDetails: { - 'Dart expression': expression, - }, ); } } diff --git a/dwds/lib/src/debugging/inspector.dart b/dwds/lib/src/debugging/inspector.dart index dc4d50228..080dfb1be 100644 --- a/dwds/lib/src/debugging/inspector.dart +++ b/dwds/lib/src/debugging/inspector.dart @@ -2,8 +2,6 @@ // 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:async'; -// @dart = 2.9 - import 'package:async/async.dart'; import 'package:logging/logging.dart'; import 'package:vm_service/vm_service.dart'; @@ -67,9 +65,9 @@ class AppInspector implements AppInspectorInterface { final ExecutionContext _executionContext; - LibraryHelper _libraryHelper; - ClassHelper _classHelper; - InstanceHelper _instanceHelper; + late final LibraryHelper _libraryHelper; + late final ClassHelper _classHelper; + late final InstanceHelper _instanceHelper; final AssetReader _assetReader; final Locations _locations; @@ -106,15 +104,16 @@ class AppInspector implements AppInspectorInterface { final libraries = await _libraryHelper.libraryRefs; isolate.rootLib = await _libraryHelper.rootLib; - isolate.libraries.addAll(libraries); + isolate.libraries?.addAll(libraries); final scripts = await scriptRefs; await DartUri.initialize(_sdkConfiguration); - await DartUri.recordAbsoluteUris(libraries.map((lib) => lib.uri)); - await DartUri.recordAbsoluteUris(scripts.map((script) => script.uri)); + await DartUri.recordAbsoluteUris(libraries.map((lib) => lib.uri).cast()); + await DartUri.recordAbsoluteUris( + scripts.map((script) => script.uri).cast()); - isolate.extensionRPCs.addAll(await _getExtensionRpcs()); + isolate.extensionRPCs?.addAll(await _getExtensionRpcs()); } static IsolateRef _toIsolateRef(Isolate isolate) => IsolateRef( @@ -187,7 +186,7 @@ class AppInspector implements AppInspectorInterface { /// Returns the ID for the execution context or null if not found. @override - Future get contextId async { + Future get contextId async { try { return await _executionContext.id; } catch (e, s) { @@ -236,15 +235,16 @@ class AppInspector implements AppInspectorInterface { String evalExpression, List arguments, {bool returnByValue = false}) async { final jsArguments = arguments.map(callArgumentFor).toList(); - final result = + final response = await remoteDebugger.sendCommand('Runtime.callFunctionOn', params: { 'functionDeclaration': evalExpression, 'arguments': jsArguments, 'objectId': receiver.objectId, 'returnByValue': returnByValue, }); - handleErrorIfPresent(result, evalContents: evalExpression); - return RemoteObject(result.result['result'] as Map); + final result = + getResultOrHandleError(response, evalContents: evalExpression); + return RemoteObject(result); } /// Calls Chrome's Runtime.callFunctionOn method with a global function. @@ -255,15 +255,16 @@ class AppInspector implements AppInspectorInterface { String evalExpression, List arguments, {bool returnByValue = false}) async { final jsArguments = arguments.map(callArgumentFor).toList(); - final result = + final response = await remoteDebugger.sendCommand('Runtime.callFunctionOn', params: { 'functionDeclaration': evalExpression, 'arguments': jsArguments, 'executionContextId': await contextId, 'returnByValue': returnByValue, }); - handleErrorIfPresent(result, evalContents: evalExpression); - return RemoteObject(result.result['result'] as Map); + final result = + getResultOrHandleError(response, evalContents: evalExpression); + return RemoteObject(result); } /// Invoke the function named [selector] on the object identified by @@ -303,26 +304,28 @@ class AppInspector implements AppInspectorInterface { Future jsEvaluate(String expression, {bool returnByValue = false, bool awaitPromise = false}) async { // TODO(alanknight): Support a version with arguments if needed. - WipResponse result; - result = await remoteDebugger.sendCommand('Runtime.evaluate', params: { + final response = + await remoteDebugger.sendCommand('Runtime.evaluate', params: { 'expression': expression, 'returnByValue': returnByValue, 'awaitPromise': awaitPromise, 'contextId': await contextId, }); - handleErrorIfPresent(result, evalContents: expression, additionalDetails: { - 'Dart expression': expression, - }); - return RemoteObject(result.result['result'] as Map); + final result = getResultOrHandleError(response, evalContents: expression); + return RemoteObject(result); } /// Evaluate the JS function with source [jsFunction] in the context of /// [library] with [arguments]. Future _evaluateInLibrary( Library library, String jsFunction, List arguments) async { + final libraryUri = library.uri; + if (libraryUri == null) { + throwInvalidParam('invoke', 'library uri is null'); + } final findLibrary = ''' (function() { - ${globalLoadStrategy.loadLibrarySnippet(library.uri)}; + ${globalLoadStrategy.loadLibrarySnippet(libraryUri)}; return library; })(); '''; @@ -339,25 +342,25 @@ class AppInspector implements AppInspectorInterface { } @override - Future instanceRefFor(Object value) => + Future instanceRefFor(Object value) => _instanceHelper.instanceRefFor(value); - Future instanceFor(Object value) => + Future instanceFor(RemoteObject value) => _instanceHelper.instanceFor(value); @override - Future libraryRefFor(String objectId) => + Future libraryRefFor(String objectId) => _libraryHelper.libraryRefFor(objectId); @override - Future getLibrary(String objectId) async { + Future getLibrary(String objectId) async { final libraryRef = await libraryRefFor(objectId); if (libraryRef == null) return null; return _libraryHelper.libraryFor(libraryRef); } @override - Future getObject(String objectId, {int offset, int count}) async { + Future getObject(String objectId, {int? offset, int? count}) async { try { final library = await getLibrary(objectId); if (library != null) { @@ -384,9 +387,13 @@ class AppInspector implements AppInspectorInterface { 'are supported for getObject'); } - Future