Enable expression evaluation in debugger for web platform (#53595)

This commit is contained in:
Anna Gringauze
2020-04-02 10:26:03 -07:00
committed by GitHub
parent 191f86ea05
commit 3a0d837741
11 changed files with 350 additions and 4 deletions

View File

@@ -36,6 +36,39 @@ import '../globals.dart' as globals;
import '../web/bootstrap.dart';
import '../web/chrome.dart';
/// An expression compiler connecting to FrontendServer
///
/// This is only used in development mode
class WebExpressionCompiler implements ExpressionCompiler {
WebExpressionCompiler(this._generator);
final ResidentCompiler _generator;
@override
Future<ExpressionCompilationResult> compileExpressionToJs(
String isolateId,
String libraryUri,
int line,
int column,
Map<String, String> jsModules,
Map<String, String> jsFrameValues,
String moduleName,
String expression,
) async {
final CompilerOutput compilerOutput = await _generator.compileExpressionToJs(libraryUri,
line, column, jsModules, jsFrameValues, moduleName, expression);
if (compilerOutput != null && compilerOutput.outputFilename != null) {
final String content = utf8.decode(
globals.fs.file(compilerOutput.outputFilename).readAsBytesSync());
return ExpressionCompilationResult(
content, compilerOutput.errorCount > 0);
}
throw Exception('Failed to compile $expression');
}
}
/// A web server which handles serving JavaScript and assets.
///
/// This is only used in development mode.
@@ -85,7 +118,8 @@ class WebAssetServer implements AssetReader {
UrlTunneller urlTunneller,
BuildMode buildMode,
bool enableDwds,
Uri entrypoint, {
Uri entrypoint,
ExpressionCompiler expressionCompiler, {
bool testMode = false,
}) async {
try {
@@ -170,6 +204,7 @@ class WebAssetServer implements AssetReader {
serverPathForModule,
serverPathForAppUri,
),
expressionCompiler: expressionCompiler
);
shelf.Pipeline pipeline = const shelf.Pipeline();
if (enableDwds) {
@@ -501,6 +536,7 @@ class WebDevFS implements DevFS {
@required this.buildMode,
@required this.enableDwds,
@required this.entrypoint,
@required this.expressionCompiler,
this.testMode = false,
});
@@ -512,6 +548,7 @@ class WebDevFS implements DevFS {
final BuildMode buildMode;
final bool enableDwds;
final bool testMode;
final ExpressionCompiler expressionCompiler;
WebAssetServer webAssetServer;
@@ -572,6 +609,7 @@ class WebDevFS implements DevFS {
buildMode,
enableDwds,
entrypoint,
expressionCompiler,
testMode: testMode,
);
_baseUri = Uri.parse('http://$hostname:$port');

View File

@@ -421,6 +421,7 @@ class _ResidentWebRunner extends ResidentWebRunner {
buildMode: debuggingOptions.buildInfo.mode,
enableDwds: _enableDwds,
entrypoint: globals.fs.file(target).uri,
expressionCompiler: WebExpressionCompiler(device.generator),
);
final Uri url = await device.devFS.create();
if (debuggingOptions.buildInfo.isDebug) {

View File

@@ -160,6 +160,15 @@ class CodeGeneratingResidentCompiler implements ResidentCompiler {
return _residentCompiler.compileExpression(expression, definitions, typeDefinitions, libraryUri, klass, isStatic);
}
@override
Future<CompilerOutput> compileExpressionToJs(
String libraryUri, int line, int column, Map<String, String> jsModules,
Map<String, String> jsFrameValues, String moduleName, String expression
) {
return _residentCompiler.compileExpressionToJs(
libraryUri, line, column, jsModules, jsFrameValues, moduleName, expression);
}
@override
Future<CompilerOutput> recompile(String mainPath, List<Uri> invalidatedFiles, {String outputPath, String packagesFilePath}) async {
if (_codegenDaemon.lastStatus != CodegenStatus.Succeeded && _codegenDaemon.lastStatus != CodegenStatus.Failed) {

View File

@@ -26,7 +26,8 @@ const Map<String, String> _kManuallyPinnedDependencies = <String, String>{
'mockito': '^4.1.0', // Prevent mockito from downgrading to 4.0.0
'vm_service_client': '0.2.6+2', // Final version before being marked deprecated.
'video_player': '0.10.6', // 0.10.7 fails a gallery smoke test for toString.
'package_config': '1.9.1'
'package_config': '1.9.1',
'flutter_template_images': '1.0.0', // 1.0.1 breaks windows tests
};
class UpdatePackagesCommand extends FlutterCommand {

View File

@@ -431,6 +431,31 @@ class _CompileExpressionRequest extends _CompilationRequest {
compiler._compileExpression(this);
}
class _CompileExpressionToJsRequest extends _CompilationRequest {
_CompileExpressionToJsRequest(
Completer<CompilerOutput> completer,
this.libraryUri,
this.line,
this.column,
this.jsModules,
this.jsFrameValues,
this.moduleName,
this.expression,
) : super(completer);
final String libraryUri;
final int line;
final int column;
final Map<String, String> jsModules;
final Map<String, String> jsFrameValues;
final String moduleName;
final String expression;
@override
Future<CompilerOutput> _run(DefaultResidentCompiler compiler) async =>
compiler._compileExpressionToJs(this);
}
class _RejectRequest extends _CompilationRequest {
_RejectRequest(Completer<CompilerOutput> completer) : super(completer);
@@ -489,6 +514,36 @@ abstract class ResidentCompiler {
bool isStatic,
);
/// Compiles [expression] in [libraryUri] at [line]:[column] to JavaScript
/// in [moduleName].
///
/// Values listed in [jsFrameValues] are substituted for their names in the
/// [expression].
///
/// Ensures that all [jsModules] are loaded and accessible inside the
/// expression.
///
/// Example values of parameters:
/// [moduleName] is of the form '/packages/hello_world_main.dart'
/// [jsFrameValues] is a map from js variable name to its primitive value
/// or another variable name, for example
/// { 'x': '1', 'y': 'y', 'o': 'null' }
/// [jsModules] is a map from variable name to the module name, where
/// variable name is the name originally used in JavaScript to contain the
/// module object, for example:
/// { 'dart':'dart_sdk', 'main': '/packages/hello_world_main.dart' }
/// Returns a [CompilerOutput] including the name of the file containing the
/// compilation result and a number of errors
Future<CompilerOutput> compileExpressionToJs(
String libraryUri,
int line,
int column,
Map<String, String> jsModules,
Map<String, String> jsFrameValues,
String moduleName,
String expression,
);
/// Should be invoked when results of compilation are accepted by the client.
///
/// Either [accept] or [reject] should be called after every [recompile] call.
@@ -778,6 +833,52 @@ class DefaultResidentCompiler implements ResidentCompiler {
return _stdoutHandler.compilerOutput.future;
}
@override
Future<CompilerOutput> compileExpressionToJs(
String libraryUri,
int line,
int column,
Map<String, String> jsModules,
Map<String, String> jsFrameValues,
String moduleName,
String expression,
) {
if (!_controller.hasListener) {
_controller.stream.listen(_handleCompilationRequest);
}
final Completer<CompilerOutput> completer = Completer<CompilerOutput>();
_controller.add(
_CompileExpressionToJsRequest(
completer, libraryUri, line, column, jsModules, jsFrameValues, moduleName, expression)
);
return completer.future;
}
Future<CompilerOutput> _compileExpressionToJs(_CompileExpressionToJsRequest request) async {
_stdoutHandler.reset(suppressCompilerMessages: true, expectSources: false);
// 'compile-expression-to-js' should be invoked after compiler has been started,
// program was compiled.
if (_server == null) {
return null;
}
final String inputKey = Uuid().generateV4();
_server.stdin.writeln('compile-expression-to-js $inputKey');
_server.stdin.writeln(request.libraryUri ?? '');
_server.stdin.writeln(request.line);
_server.stdin.writeln(request.column);
request.jsModules?.forEach((String k, String v) { _server.stdin.writeln('$k:$v'); });
_server.stdin.writeln(inputKey);
request.jsFrameValues?.forEach((String k, String v) { _server.stdin.writeln('$k:$v'); });
_server.stdin.writeln(inputKey);
_server.stdin.writeln(request.moduleName ?? '');
_server.stdin.writeln(request.expression ?? '');
return _stdoutHandler.compilerOutput.future;
}
@override
void accept() {
if (_compileRequestNeedsConfirmation) {