Skip to content

Commit

Permalink
Send over script uri
Browse files Browse the repository at this point in the history
This is different than library uri when working in parts,
put also in patched libraries and in the future (possibly) in ammended
libraries.
  • Loading branch information
jensjoha committed Dec 13, 2023
1 parent c26b8c8 commit 655ddb2
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 15 deletions.
22 changes: 21 additions & 1 deletion dwds/lib/src/debugging/modules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class Modules {

// The Dart server path to library import uri
final _sourceToLibrary = <String, Uri>{};

// The Dart server path to resolved uri aka "file uri".
final _sourceToResolvedUri = <String, Uri>{};

var _moduleMemoizer = AsyncMemoizer<void>();

final Map<String, String> _libraryToModule = {};
Expand All @@ -34,6 +38,7 @@ class Modules {
// across hot reloads.
_sourceToModule.clear();
_sourceToLibrary.clear();
_sourceToResolvedUri.clear();
_libraryToModule.clear();
_moduleMemoizer = AsyncMemoizer();
_entrypoint = entrypoint;
Expand All @@ -51,6 +56,12 @@ class Modules {
return _sourceToLibrary[serverPath];
}

/// Returns the resolived uri (or file uri) for the provided Dart server path.
Future<Uri?> resolvedUriForSource(String serverPath) async {
await _moduleMemoizer.runOnce(_initializeMapping);
return _sourceToResolvedUri[serverPath];
}

Future<String?> moduleForLibrary(String libraryUri) async {
await _moduleMemoizer.runOnce(_initializeMapping);
return _libraryToModule[libraryUri];
Expand All @@ -62,7 +73,8 @@ class Modules {
return _sourceToModule;
}

/// Initializes [_sourceToModule] and [_sourceToLibrary].
/// Initializes [_sourceToModule], [_sourceToLibrary] and
/// [_sourceToResolvedUri].
Future<void> _initializeMapping() async {
final provider =
globalToolConfiguration.loadStrategy.metadataProviderFor(_entrypoint);
Expand All @@ -81,15 +93,23 @@ class Modules {

_sourceToModule[libraryServerPath] = module;
_sourceToLibrary[libraryServerPath] = Uri.parse(library);
final resolvedUri = DartUri.uriToResolvedUri[library];
if (resolvedUri != null) {
_sourceToResolvedUri[libraryServerPath] = Uri.parse(resolvedUri);
}
_libraryToModule[library] = module;

for (var script in scripts) {
final scriptServerPath = script.startsWith('dart:')
? script
: DartUri(script, _root).serverPath;
final resolvedUri = DartUri.uriToResolvedUri[script];

_sourceToModule[scriptServerPath] = module;
_sourceToLibrary[scriptServerPath] = Uri.parse(library);
if (resolvedUri != null) {
_sourceToResolvedUri[scriptServerPath] = Uri.parse(resolvedUri);
}
}
} else {
_logger.warning('No module found for library $library');
Expand Down
15 changes: 13 additions & 2 deletions dwds/lib/src/services/batched_expression_evaluator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart';
class EvaluateRequest {
final String isolateId;
final String? libraryUri;
final String? scriptUri;
final String expression;
final Map<String, String>? scope;
final completer = Completer<RemoteObject>();

EvaluateRequest(this.isolateId, this.libraryUri, this.expression, this.scope);
EvaluateRequest(
this.isolateId,
this.libraryUri,
this.scriptUri,
this.expression,
this.scope,
);
}

class BatchedExpressionEvaluator extends ExpressionEvaluator {
Expand Down Expand Up @@ -56,6 +63,7 @@ class BatchedExpressionEvaluator extends ExpressionEvaluator {
Future<RemoteObject> evaluateExpression(
String isolateId,
String? libraryUri,
String? scriptUri,
String expression,
Map<String, String>? scope,
) async {
Expand All @@ -65,7 +73,8 @@ class BatchedExpressionEvaluator extends ExpressionEvaluator {
'Batched expression evaluator closed',
);
}
final request = EvaluateRequest(isolateId, libraryUri, expression, scope);
final request =
EvaluateRequest(isolateId, libraryUri, scriptUri, expression, scope);
_requestController.sink.add(request);
return request.completer.future;
}
Expand Down Expand Up @@ -116,6 +125,7 @@ class BatchedExpressionEvaluator extends ExpressionEvaluator {
.evaluateExpression(
first.isolateId,
first.libraryUri,
first.scriptUri,
first.expression,
first.scope,
)
Expand All @@ -130,6 +140,7 @@ class BatchedExpressionEvaluator extends ExpressionEvaluator {
final RemoteObject list = await super.evaluateExpression(
first.isolateId,
first.libraryUri,
first.scriptUri,
batchedExpression,
first.scope,
);
Expand Down
2 changes: 2 additions & 0 deletions dwds/lib/src/services/chrome_proxy_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,8 @@ ${globalToolConfiguration.loadStrategy.loadModuleSnippet}("dart_sdk").developer.
() => evaluator.evaluateExpression(
isolateId,
library?.uri,
// TODO: Get correct script uri.
library?.uri,
expression,
scope,
),
Expand Down
1 change: 1 addition & 0 deletions dwds/lib/src/services/expression_compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ abstract class ExpressionCompiler {
Future<ExpressionCompilationResult> compileExpressionToJs(
String isolateId,
String libraryUri,
String scriptUri,
int line,
int column,
Map<String, String> jsModules,
Expand Down
3 changes: 3 additions & 0 deletions dwds/lib/src/services/expression_compiler_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class _Compiler {

Future<ExpressionCompilationResult> compileExpressionToJs(
String libraryUri,
String scriptUri,
int line,
int column,
Map<String, String> jsModules,
Expand Down Expand Up @@ -256,6 +257,7 @@ class ExpressionCompilerService implements ExpressionCompiler {
Future<ExpressionCompilationResult> compileExpressionToJs(
String isolateId,
String libraryUri,
String scriptUri,
int line,
int column,
Map<String, String> jsModules,
Expand All @@ -265,6 +267,7 @@ class ExpressionCompilerService implements ExpressionCompiler {
) async =>
(await _compiler.future).compileExpressionToJs(
libraryUri,
scriptUri,
line,
column,
jsModules,
Expand Down
15 changes: 15 additions & 0 deletions dwds/lib/src/services/expression_evaluator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ class ExpressionEvaluator {
///
/// [isolateId] current isolate ID.
/// [libraryUri] dart library to evaluate the expression in.
/// [scriptUri] dart script or file to evaluate the expression in.
/// [expression] dart expression to evaluate.
Future<RemoteObject> evaluateExpression(
String isolateId,
String? libraryUri,
String? scriptUri,
String expression,
Map<String, String>? scope,
) async {
Expand All @@ -114,6 +116,13 @@ class ExpressionEvaluator {
);
}

if (scriptUri == null) {
return createError(
EvaluationErrorKind.invalidInput,
'no script uri',
);
}

final module = await _modules.moduleForLibrary(libraryUri);
if (module == null) {
return createError(
Expand All @@ -131,6 +140,7 @@ class ExpressionEvaluator {
final compilationResult = await _compiler.compileExpressionToJs(
isolateId,
libraryUri.toString(),
libraryUri.toString(),
0,
0,
{},
Expand Down Expand Up @@ -283,7 +293,11 @@ class ExpressionEvaluator {

final dartLocation = locationMap.dartLocation;
final dartSourcePath = dartLocation.uri.serverPath;
print("dartSourcePath = $dartSourcePath");
final libraryUri = await _modules.libraryForSource(dartSourcePath);
print("libraryUri = $libraryUri");
final resolvedUri = await _modules.resolvedUriForSource(dartSourcePath);
print("resolvedUri = $resolvedUri");
if (libraryUri == null) {
return createError(
EvaluationErrorKind.internal,
Expand Down Expand Up @@ -319,6 +333,7 @@ class ExpressionEvaluator {
final compilationResult = await _compiler.compileExpressionToJs(
isolateId,
libraryUri.toString(),
(resolvedUri ?? libraryUri).toString(),
dartLocation.line,
dartLocation.column,
{},
Expand Down
4 changes: 4 additions & 0 deletions dwds/lib/src/utilities/dart_uri.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ class DartUri {
/// file:///.pub-cache/hosted/pub.dev/path-1.8.0/lib/src/path_set.dart,
static final Map<String, String> _uriToResolvedUri = {};

static Map<String, String> get uriToResolvedUri => _uriToResolvedUri;

/// All of the known libraries, indexed by their absolute file URL.
static final Map<String, String> _resolvedUriToUri = {};

Expand All @@ -140,6 +142,8 @@ class DartUri {
/// This map is empty if not a google3 app.
static final Map<String, String> _g3RelativeUriToResolvedUri = {};

static Map<String, String> get resolvedUriToUri => _resolvedUriToUri;

/// Returns package, app, or dart uri for a resolved path.
static String? toPackageUri(String uri) {
final packageUri = _resolvedUriToUri[uri];
Expand Down
5 changes: 5 additions & 0 deletions dwds/test/expression_compiler_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ void main() async {
final compilationResult = await service.compileExpressionToJs(
'0',
'org-dartlang-app:/try.dart',
'org-dartlang-app:/try.dart',
2,
1,
{},
Expand Down Expand Up @@ -247,6 +248,7 @@ void main() async {
final compilationResult1 = await service.compileExpressionToJs(
'0',
'org-dartlang-app:/try.dart',
'org-dartlang-app:/try.dart',
2,
1,
{},
Expand All @@ -257,6 +259,7 @@ void main() async {
final compilationResult2 = await service.compileExpressionToJs(
'0',
'org-dartlang-app:/try.dart',
'org-dartlang-app:/try.dart',
2,
1,
{},
Expand Down Expand Up @@ -312,6 +315,7 @@ void main() async {
final compilationResult1 = service.compileExpressionToJs(
'0',
'org-dartlang-app:/try.dart',
'org-dartlang-app:/try.dart',
2,
1,
{},
Expand All @@ -322,6 +326,7 @@ void main() async {
final compilationResult2 = service.compileExpressionToJs(
'0',
'org-dartlang-app:/try.dart',
'org-dartlang-app:/try.dart',
2,
1,
{},
Expand Down
16 changes: 8 additions & 8 deletions dwds/test/expression_evaluator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ void main() async {
});

test('can evaluate expression', () async {
final result =
await evaluator.evaluateExpression('1', 'main.dart', 'true', {});
final result = await evaluator
.evaluateExpression('1', 'main.dart', 'main.dart', 'true', {});
expect(
result,
const TypeMatcher<RemoteObject>()
Expand Down Expand Up @@ -162,8 +162,8 @@ void main() async {

test('returns error if closed', () async {
evaluator.close();
final result =
await evaluator.evaluateExpression('1', 'main.dart', 'true', {});
final result = await evaluator
.evaluateExpression('1', 'main.dart', 'main.dart', 'true', {});
expect(
result,
const TypeMatcher<RemoteObject>()
Expand All @@ -181,8 +181,8 @@ void main() async {
});

test('can evaluate expression', () async {
final result =
await evaluator.evaluateExpression('1', 'main.dart', 'true', {});
final result = await evaluator
.evaluateExpression('1', 'main.dart', 'main.dart', 'true', {});
expect(
result,
const TypeMatcher<RemoteObject>()
Expand All @@ -192,8 +192,8 @@ void main() async {

test('returns error if closed', () async {
evaluator.close();
final result =
await evaluator.evaluateExpression('1', 'main.dart', 'true', {});
final result = await evaluator
.evaluateExpression('1', 'main.dart', 'main.dart', 'true', {});
expect(
result,
const TypeMatcher<RemoteObject>()
Expand Down
5 changes: 5 additions & 0 deletions dwds/test/fixtures/fakes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ class FakeModules implements Modules {
@override
Future<Uri> libraryForSource(String serverPath) async => Uri(path: _library);

@override
Future<Uri> resolvedUriForSource(String serverPath) async =>
Uri(path: _library);

@override
Future<String> moduleForSource(String serverPath) async => _module;

Expand Down Expand Up @@ -438,6 +442,7 @@ class FakeExpressionCompiler implements ExpressionCompiler {
Future<ExpressionCompilationResult> compileExpressionToJs(
String isolateId,
String libraryUri,
String scriptUri,
int line,
int column,
Map<String, String> jsModules,
Expand Down
Loading

0 comments on commit 655ddb2

Please sign in to comment.