diff --git a/packages/flutter_tools/lib/src/compile.dart b/packages/flutter_tools/lib/src/compile.dart index d01745956e..d7994ffd78 100644 --- a/packages/flutter_tools/lib/src/compile.dart +++ b/packages/flutter_tools/lib/src/compile.dart @@ -186,6 +186,51 @@ class KernelCompiler { } } +/// Class that allows to serialize compilation requests to the compiler. +abstract class _CompilationRequest { + Completer completer; + + _CompilationRequest(this.completer); + + Future _run(ResidentCompiler compiler); + + Future run(ResidentCompiler compiler) async { + completer.complete(await _run(compiler)); + } +} + +class _RecompileRequest extends _CompilationRequest { + _RecompileRequest(Completer completer, this.mainPath, + this.invalidatedFiles, this.outputPath, this.packagesFilePath) : + super(completer); + + String mainPath; + List invalidatedFiles; + String outputPath; + String packagesFilePath; + + @override + Future _run(ResidentCompiler compiler) async => + compiler._recompile(this); +} + +class _CompileExpressionRequest extends _CompilationRequest { + _CompileExpressionRequest(Completer completer, this.expression, this.definitions, + this.typeDefinitions, this.libraryUri, this.klass, this.isStatic) : + super(completer); + + String expression; + List definitions; + List typeDefinitions; + String libraryUri; + String klass; + bool isStatic; + + @override + Future _run(ResidentCompiler compiler) async => + compiler._compileExpression(this); +} + /// Wrapper around incremental frontend server compiler, that communicates with /// server via stdin/stdout. /// @@ -200,7 +245,8 @@ class ResidentCompiler { _packagesPath = packagesPath, _fileSystemRoots = fileSystemRoots, _fileSystemScheme = fileSystemScheme, - _stdoutHandler = new _StdoutHandler(consumer: compilerMessageConsumer) { + _stdoutHandler = new _StdoutHandler(consumer: compilerMessageConsumer), + _controller = new StreamController<_CompilationRequest>() { // This is a URI, not a file path, so the forward slash is correct even on Windows. if (!_sdkRoot.endsWith('/')) _sdkRoot = '$_sdkRoot/'; @@ -214,6 +260,8 @@ class ResidentCompiler { Process _server; final _StdoutHandler _stdoutHandler; + final StreamController<_CompilationRequest> _controller; + /// If invoked for the first time, it compiles Dart script identified by /// [mainPath], [invalidatedFiles] list is ignored. /// On successive runs [invalidatedFiles] indicates which files need to be @@ -223,16 +271,30 @@ class ResidentCompiler { /// null is returned. Future recompile(String mainPath, List invalidatedFiles, {String outputPath, String packagesFilePath}) async { + if (!_controller.hasListener) { + _controller.stream.listen(_handleCompilationRequest); + } + + final Completer completer = new Completer(); + _controller.add( + new _RecompileRequest(completer, mainPath, invalidatedFiles, outputPath, packagesFilePath) + ); + return completer.future; + } + + Future _recompile(_RecompileRequest request) async { _stdoutHandler.reset(); // First time recompile is called we actually have to compile the app from // scratch ignoring list of invalidated files. - if (_server == null) - return _compile(_mapFilename(mainPath), outputPath, _mapFilename(packagesFilePath)); + if (_server == null) { + return _compile(_mapFilename(request.mainPath), + request.outputPath, _mapFilename(request.packagesFilePath)); + } final String inputKey = new Uuid().generateV4(); - _server.stdin.writeln('recompile ${mainPath != null ? _mapFilename(mainPath) + " ": ""}$inputKey'); - for (String fileUri in invalidatedFiles) { + _server.stdin.writeln('recompile ${request.mainPath != null ? _mapFilename(request.mainPath) + " ": ""}$inputKey'); + for (String fileUri in request.invalidatedFiles) { _server.stdin.writeln(_mapFileUri(fileUri)); } _server.stdin.writeln(inputKey); @@ -240,6 +302,23 @@ class ResidentCompiler { return _stdoutHandler.compilerOutput.future; } + final List<_CompilationRequest> compilationQueue = <_CompilationRequest>[]; + + void _handleCompilationRequest(_CompilationRequest request) async { + final bool isEmpty = compilationQueue.isEmpty; + compilationQueue.add(request); + // Only trigger processing if queue was empty - i.e. no other requests + // are currently being processed. This effectively enforces "one + // compilation request at a time". + if (isEmpty) { + while (compilationQueue.isNotEmpty) { + final _CompilationRequest request = compilationQueue.first; + await request.run(this); + compilationQueue.removeAt(0); + } + } + } + Future _compile(String scriptFilename, String outputPath, String packagesFilePath) async { final String frontendServer = artifacts.getArtifactPath( @@ -301,6 +380,20 @@ class ResidentCompiler { Future compileExpression(String expression, List definitions, List typeDefinitions, String libraryUri, String klass, bool isStatic) { + if (!_controller.hasListener) { + _controller.stream.listen(_handleCompilationRequest); + } + + final Completer completer = new Completer(); + _controller.add( + new _CompileExpressionRequest( + completer, expression, definitions, typeDefinitions, libraryUri, klass, isStatic) + ); + return completer.future; + } + + Future _compileExpression( + _CompileExpressionRequest request) async { _stdoutHandler.reset(); // 'compile-expression' should be invoked after compiler has been started, @@ -310,14 +403,14 @@ class ResidentCompiler { final String inputKey = new Uuid().generateV4(); _server.stdin.writeln('compile-expression $inputKey'); - _server.stdin.writeln(expression); - definitions?.forEach(_server.stdin.writeln); + _server.stdin.writeln(request.expression); + request.definitions?.forEach(_server.stdin.writeln); _server.stdin.writeln(inputKey); - typeDefinitions?.forEach(_server.stdin.writeln); + request.typeDefinitions?.forEach(_server.stdin.writeln); _server.stdin.writeln(inputKey); - _server.stdin.writeln(libraryUri ?? ''); - _server.stdin.writeln(klass ?? ''); - _server.stdin.writeln(isStatic ?? false); + _server.stdin.writeln(request.libraryUri ?? ''); + _server.stdin.writeln(request.klass ?? ''); + _server.stdin.writeln(request.isStatic ?? false); return _stdoutHandler.compilerOutput.future; } diff --git a/packages/flutter_tools/test/compile_test.dart b/packages/flutter_tools/test/compile_test.dart index 61f120876e..4b8e5c3abf 100644 --- a/packages/flutter_tools/test/compile_test.dart +++ b/packages/flutter_tools/test/compile_test.dart @@ -276,25 +276,88 @@ void main() { 'result abc\nline1\nline2\nabc /path/to/main.dart.dill 0\n' ))); - final CompilerOutput output = await generator.recompile( + generator.recompile( '/path/to/main.dart', null /* invalidatedFiles */ - ); - expect(mockFrontendServerStdIn.getAndClear(), - 'compile /path/to/main.dart\n'); - verifyNoMoreInteractions(mockFrontendServerStdIn); - expect(logger.errorText, - equals('compiler message: line1\ncompiler message: line2\n')); - expect(output.outputFilename, equals('/path/to/main.dart.dill')); + ).then((CompilerOutput output) { + expect(mockFrontendServerStdIn.getAndClear(), + 'compile /path/to/main.dart\n'); + verifyNoMoreInteractions(mockFrontendServerStdIn); + expect(logger.errorText, + equals('compiler message: line1\ncompiler message: line2\n')); + expect(output.outputFilename, equals('/path/to/main.dart.dill')); - compileExpressionResponseCompleter.complete(new Future>.value(utf8.encode( - 'result def\nline1\nline2\ndef /path/to/main.dart.dill.incremental 0\n' + compileExpressionResponseCompleter.complete( + new Future>.value(utf8.encode( + 'result def\nline1\nline2\ndef /path/to/main.dart.dill.incremental 0\n' + ))); + generator.compileExpression( + '2+2', null, null, null, null, false).then( + (CompilerOutput outputExpression) { + expect(outputExpression, isNotNull); + expect(outputExpression.outputFilename, equals('/path/to/main.dart.dill.incremental')); + expect(outputExpression.errorCount, 0); + } + ); + }); + + }, overrides: { + ProcessManager: () => mockProcessManager, + }); + + testUsingContext('compile expressions without awaiting', () async { + final BufferLogger logger = context[Logger]; + + final Completer> compileResponseCompleter = new Completer>(); + final Completer> compileExpressionResponseCompleter1 = new Completer>(); + final Completer> compileExpressionResponseCompleter2 = new Completer>(); + + when(mockFrontendServer.stdout) + .thenAnswer((Invocation invocation) => + new Stream>.fromFutures( + >>[ + compileResponseCompleter.future, + compileExpressionResponseCompleter1.future, + compileExpressionResponseCompleter2.future, + ])); + + generator.recompile( + '/path/to/main.dart', null /* invalidatedFiles */ + ).then((CompilerOutput outputCompile) { + expect(logger.errorText, + equals('compiler message: line1\ncompiler message: line2\n')); + expect(outputCompile.outputFilename, equals('/path/to/main.dart.dill')); + + compileExpressionResponseCompleter1.complete(new Future>.value(utf8.encode( + 'result def\nline1\nline2\ndef /path/to/main.dart.dill.incremental 0\n' + ))); + }); + + final Completer lastExpressionCompleted = new Completer(); + generator.compileExpression('0+1', null, null, null, null, false).then( + (CompilerOutput outputExpression) { + expect(outputExpression, isNotNull); + expect(outputExpression.outputFilename, + equals('/path/to/main.dart.dill.incremental')); + expect(outputExpression.errorCount, 0); + compileExpressionResponseCompleter2.complete(new Future>.value(utf8.encode( + 'result def\nline1\nline2\ndef /path/to/main.dart.dill.incremental 0\n' + ))); + }); + + generator.compileExpression('1+1', null, null, null, null, false).then( + (CompilerOutput outputExpression) { + expect(outputExpression, isNotNull); + expect(outputExpression.outputFilename, + equals('/path/to/main.dart.dill.incremental')); + expect(outputExpression.errorCount, 0); + lastExpressionCompleted.complete(true); + }); + + compileResponseCompleter.complete(new Future>.value(utf8.encode( + 'result abc\nline1\nline2\nabc /path/to/main.dart.dill 0\n' ))); - final CompilerOutput outputExpression = await generator.compileExpression( - '2+2', null, null, null, null, false); - expect(outputExpression, isNotNull); - expect(outputExpression.outputFilename, equals('/path/to/main.dart.dill.incremental')); - expect(outputExpression.errorCount, 0); + expect(await lastExpressionCompleted.future, isTrue); }, overrides: { ProcessManager: () => mockProcessManager, });