Skip to content

Commit 82c38ad

Browse files
author
Anna Gringauze
authored
Return only scripts included in a library for library object (#1424)
* Return only scripts included in a library for library object * Addressed CR comments
1 parent 89dcded commit 82c38ad

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

dwds/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Various VM API
1212
- Hot restart
1313
- Http request handling exceptions
14+
- Only return scripts included in the library with Library object.
1415
- Add `ext.dwds.sendEvent` service extension to dwds so other tools
1516
can send events to the debugger.
1617
Event format:

dwds/lib/src/debugging/inspector.dart

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class AppInspector extends Domain {
4545
/// Map of [ScriptRef] id to containing [LibraryRef] id.
4646
final _scriptIdToLibraryId = <String, String>{};
4747

48+
/// Map of [Library] id to included [ScriptRef]s.
49+
final _libraryIdToScriptRefs = <String, List<ScriptRef>>{};
50+
4851
final RemoteDebugger remoteDebugger;
4952
final Debugger debugger;
5053
final Isolate isolate;
@@ -232,7 +235,7 @@ class AppInspector extends Domain {
232235
String isolateId, String targetId, String expression,
233236
{Map<String, String> scope}) async {
234237
scope ??= {};
235-
var library = await _getLibrary(isolateId, targetId);
238+
var library = await getLibrary(isolateId, targetId);
236239
if (library == null) {
237240
throw UnsupportedError(
238241
'Evaluate is only supported when `targetId` is a library.');
@@ -341,10 +344,6 @@ function($argsString) {
341344
}
342345

343346
Future<Library> getLibrary(String isolateId, String objectId) async {
344-
return await _getLibrary(isolateId, objectId);
345-
}
346-
347-
Future<Library> _getLibrary(String isolateId, String objectId) async {
348347
if (isolateId != isolate.id) return null;
349348
var libraryRef = await libraryHelper.libraryRefFor(objectId);
350349
if (libraryRef == null) return null;
@@ -354,7 +353,7 @@ function($argsString) {
354353
Future<Obj> getObject(String isolateId, String objectId,
355354
{int offset, int count}) async {
356355
try {
357-
var library = await _getLibrary(isolateId, objectId);
356+
var library = await getLibrary(isolateId, objectId);
358357
if (library != null) {
359358
return library;
360359
}
@@ -410,13 +409,16 @@ function($argsString) {
410409

411410
/// Returns the [ScriptRef] for the provided Dart server path [uri].
412411
Future<ScriptRef> scriptRefFor(String uri) async {
413-
if (_serverPathToScriptRef.isEmpty) {
414-
// TODO(grouma) - populate the server path cache a better way.
415-
await getScripts(isolate.id);
416-
}
412+
await _populateScriptCaches();
417413
return _serverPathToScriptRef[uri];
418414
}
419415

416+
/// Returns the [ScriptRef]s in the library with [libraryId].
417+
Future<List<ScriptRef>> scriptRefsForLibrary(String libraryId) async {
418+
await _populateScriptCaches();
419+
return _libraryIdToScriptRefs[libraryId];
420+
}
421+
420422
/// Return the VM SourceReport for the given parameters.
421423
///
422424
/// Currently this implements the 'PossibleBreakpoints' report kind.
@@ -486,8 +488,10 @@ function($argsString) {
486488

487489
/// Request and cache <ScriptRef>s for all the scripts in the application.
488490
///
489-
/// This populates [_scriptRefsById], [_scriptIdToLibraryId] and
490-
/// [_serverPathToScriptRef]. It is a one-time operation, because if we do a
491+
/// This populates [_scriptRefsById], [_scriptIdToLibraryId],
492+
/// [_libraryIdToScriptRefs] and [_serverPathToScriptRef].
493+
///
494+
/// It is a one-time operation, because if we do a
491495
/// reload the inspector will get re-created.
492496
///
493497
/// Returns the list of scripts refs cached.
@@ -507,11 +511,13 @@ function($argsString) {
507511
for (var part in parts) ScriptRef(uri: part, id: createId())
508512
];
509513
var libraryRef = await libraryHelper.libraryRefFor(uri);
514+
_libraryIdToScriptRefs.putIfAbsent(libraryRef.id, () => <ScriptRef>[]);
510515
for (var scriptRef in scriptRefs) {
511516
_scriptRefsById[scriptRef.id] = scriptRef;
512517
_scriptIdToLibraryId[scriptRef.id] = libraryRef.id;
513518
_serverPathToScriptRef[DartUri(scriptRef.uri, _root).serverPath] =
514519
scriptRef;
520+
_libraryIdToScriptRefs[libraryRef.id].add(scriptRef);
515521
}
516522
}
517523
return _scriptRefsById.values.toList();

dwds/lib/src/debugging/libraries.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class LibraryHelper extends Domain {
118118
uri: libraryRef.uri,
119119
debuggable: true,
120120
dependencies: [],
121-
scripts: await inspector.scriptRefs,
121+
scripts: await inspector.scriptRefsForLibrary(libraryRef.id),
122122
variables: [],
123123
functions: [],
124124
classes: classRefs,

dwds/test/chrome_proxy_service_test.dart

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ void main() {
412412

413413
setUp(setCurrentLogWriter);
414414

415-
test('Libraries', () async {
415+
test('root Library', () async {
416416
expect(rootLibrary, isNotNull);
417417
// TODO: library names change with kernel dart-lang/sdk#36736
418418
expect(rootLibrary.uri, endsWith('main.dart'));
@@ -421,6 +421,31 @@ void main() {
421421
expect(testClass.name, 'MyTestClass');
422422
});
423423

424+
test('Library only contains included scripts', () async {
425+
var library =
426+
await service.getObject(isolate.id, rootLibrary.id) as Library;
427+
expect(library.scripts, hasLength(2));
428+
expect(
429+
library.scripts,
430+
unorderedEquals([
431+
predicate((ScriptRef s) =>
432+
s.uri == 'org-dartlang-app:///example/hello_world/main.dart'),
433+
predicate((ScriptRef s) =>
434+
s.uri == 'org-dartlang-app:///example/hello_world/part.dart'),
435+
]));
436+
});
437+
438+
test('Can get the same library in parallel', () async {
439+
var futures = [
440+
service.getObject(isolate.id, rootLibrary.id),
441+
service.getObject(isolate.id, rootLibrary.id),
442+
];
443+
var results = await Future.wait(futures);
444+
var library1 = results[0] as Library;
445+
var library2 = results[1] as Library;
446+
expect(library1, equals(library2));
447+
});
448+
424449
test('Classes', () async {
425450
var testClass = await service.getObject(
426451
isolate.id, rootLibrary.classes.first.id) as Class;

0 commit comments

Comments
 (0)