| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library dart2js.test.memory_compiler; | 5 library dart2js.test.memory_compiler; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:compiler/compiler.dart' show | 9 import 'package:compiler/compiler.dart' show DiagnosticHandler; |
| 10 DiagnosticHandler; | 10 import 'package:compiler/compiler_new.dart' |
| 11 import 'package:compiler/compiler_new.dart' show | 11 show |
| 12 CompilationResult, | 12 CompilationResult, |
| 13 CompilerDiagnostics, | 13 CompilerDiagnostics, |
| 14 CompilerOutput, | 14 CompilerOutput, |
| 15 Diagnostic, | 15 Diagnostic, |
| 16 PackagesDiscoveryProvider; | 16 PackagesDiscoveryProvider; |
| 17 import 'package:compiler/src/diagnostics/messages.dart' show | 17 import 'package:compiler/src/diagnostics/messages.dart' show Message; |
| 18 Message; | 18 import 'package:compiler/src/null_compiler_output.dart' show NullCompilerOutput; |
| 19 import 'package:compiler/src/null_compiler_output.dart' show | 19 import 'package:compiler/src/library_loader.dart' show LoadedLibraries; |
| 20 NullCompilerOutput; | 20 import 'package:compiler/src/options.dart' show CompilerOptions; |
| 21 import 'package:compiler/src/library_loader.dart' show | |
| 22 LoadedLibraries; | |
| 23 import 'package:compiler/src/options.dart' show | |
| 24 CompilerOptions; | |
| 25 | 21 |
| 26 import 'memory_source_file_helper.dart'; | 22 import 'memory_source_file_helper.dart'; |
| 27 | 23 |
| 28 export 'output_collector.dart'; | 24 export 'output_collector.dart'; |
| 29 export 'package:compiler/compiler_new.dart' show | 25 export 'package:compiler/compiler_new.dart' show CompilationResult; |
| 30 CompilationResult; | |
| 31 export 'diagnostic_helper.dart'; | 26 export 'diagnostic_helper.dart'; |
| 32 | 27 |
| 33 class MultiDiagnostics implements CompilerDiagnostics { | 28 class MultiDiagnostics implements CompilerDiagnostics { |
| 34 final List<CompilerDiagnostics> diagnosticsList; | 29 final List<CompilerDiagnostics> diagnosticsList; |
| 35 | 30 |
| 36 const MultiDiagnostics([this.diagnosticsList = const []]); | 31 const MultiDiagnostics([this.diagnosticsList = const []]); |
| 37 | 32 |
| 38 @override | 33 @override |
| 39 void report(Message message, Uri uri, int begin, int end, | 34 void report(Message message, Uri uri, int begin, int end, String text, |
| 40 String text, Diagnostic kind) { | 35 Diagnostic kind) { |
| 41 for (CompilerDiagnostics diagnostics in diagnosticsList) { | 36 for (CompilerDiagnostics diagnostics in diagnosticsList) { |
| 42 diagnostics.report(message, uri, begin, end, text, kind); | 37 diagnostics.report(message, uri, begin, end, text, kind); |
| 43 } | 38 } |
| 44 } | 39 } |
| 45 } | 40 } |
| 46 | 41 |
| 47 CompilerDiagnostics createCompilerDiagnostics( | 42 CompilerDiagnostics createCompilerDiagnostics( |
| 48 CompilerDiagnostics diagnostics, | 43 CompilerDiagnostics diagnostics, SourceFileProvider provider, |
| 49 SourceFileProvider provider, | 44 {bool showDiagnostics: true, bool verbose: false}) { |
| 50 {bool showDiagnostics: true, | |
| 51 bool verbose: false}) { | |
| 52 CompilerDiagnostics handler = diagnostics; | 45 CompilerDiagnostics handler = diagnostics; |
| 53 if (showDiagnostics) { | 46 if (showDiagnostics) { |
| 54 if (diagnostics == null) { | 47 if (diagnostics == null) { |
| 55 handler = new FormattingDiagnosticHandler(provider)..verbose = verbose; | 48 handler = new FormattingDiagnosticHandler(provider)..verbose = verbose; |
| 56 } else { | 49 } else { |
| 57 var formattingHandler = | 50 var formattingHandler = new FormattingDiagnosticHandler(provider) |
| 58 new FormattingDiagnosticHandler(provider)..verbose = verbose; | 51 ..verbose = verbose; |
| 59 handler = new MultiDiagnostics([diagnostics, formattingHandler]); | 52 handler = new MultiDiagnostics([diagnostics, formattingHandler]); |
| 60 } | 53 } |
| 61 } else if (diagnostics == null) { | 54 } else if (diagnostics == null) { |
| 62 handler = new MultiDiagnostics(); | 55 handler = new MultiDiagnostics(); |
| 63 } | 56 } |
| 64 return handler; | 57 return handler; |
| 65 } | 58 } |
| 66 | 59 |
| 67 Expando<MemorySourceFileProvider> expando = | 60 Expando<MemorySourceFileProvider> expando = |
| 68 new Expando<MemorySourceFileProvider>(); | 61 new Expando<MemorySourceFileProvider>(); |
| 69 | 62 |
| 70 Future<CompilationResult> runCompiler( | 63 Future<CompilationResult> runCompiler( |
| 71 {Map<String, String> memorySourceFiles: const <String, String>{}, | 64 {Map<String, String> memorySourceFiles: const <String, String>{}, |
| 72 Uri entryPoint, | 65 Uri entryPoint, |
| 73 List<Uri> entryPoints, | 66 List<Uri> entryPoints, |
| 74 List<Uri> resolutionInputs, | 67 List<Uri> resolutionInputs, |
| 75 CompilerDiagnostics diagnosticHandler, | 68 CompilerDiagnostics diagnosticHandler, |
| 76 CompilerOutput outputProvider, | 69 CompilerOutput outputProvider, |
| 77 List<String> options: const <String>[], | 70 List<String> options: const <String>[], |
| 78 CompilerImpl cachedCompiler, | 71 CompilerImpl cachedCompiler, |
| 79 bool showDiagnostics: true, | 72 bool showDiagnostics: true, |
| 80 Uri packageRoot, | 73 Uri packageRoot, |
| 81 Uri packageConfig, | 74 Uri packageConfig, |
| 82 PackagesDiscoveryProvider packagesDiscoveryProvider, | 75 PackagesDiscoveryProvider packagesDiscoveryProvider, |
| 83 void beforeRun(CompilerImpl compiler)}) async { | 76 void beforeRun(CompilerImpl compiler)}) async { |
| 84 if (entryPoint == null) { | 77 if (entryPoint == null) { |
| 85 entryPoint = Uri.parse('memory:main.dart'); | 78 entryPoint = Uri.parse('memory:main.dart'); |
| 86 } | 79 } |
| 87 CompilerImpl compiler = compilerFor( | 80 CompilerImpl compiler = compilerFor( |
| 88 entryPoint: entryPoint, | 81 entryPoint: entryPoint, |
| 89 resolutionInputs: resolutionInputs, | 82 resolutionInputs: resolutionInputs, |
| 90 memorySourceFiles: memorySourceFiles, | 83 memorySourceFiles: memorySourceFiles, |
| 91 diagnosticHandler: diagnosticHandler, | 84 diagnosticHandler: diagnosticHandler, |
| 92 outputProvider: outputProvider, | 85 outputProvider: outputProvider, |
| 93 options: options, | 86 options: options, |
| 94 cachedCompiler: cachedCompiler, | 87 cachedCompiler: cachedCompiler, |
| 95 showDiagnostics: showDiagnostics, | 88 showDiagnostics: showDiagnostics, |
| 96 packageRoot: packageRoot, | 89 packageRoot: packageRoot, |
| 97 packageConfig: packageConfig, | 90 packageConfig: packageConfig, |
| 98 packagesDiscoveryProvider: packagesDiscoveryProvider); | 91 packagesDiscoveryProvider: packagesDiscoveryProvider); |
| 99 compiler.librariesToAnalyzeWhenRun = entryPoints; | 92 compiler.librariesToAnalyzeWhenRun = entryPoints; |
| 100 if (beforeRun != null) { | 93 if (beforeRun != null) { |
| 101 beforeRun(compiler); | 94 beforeRun(compiler); |
| 102 } | 95 } |
| 103 bool isSuccess = await compiler.run(entryPoint); | 96 bool isSuccess = await compiler.run(entryPoint); |
| 104 return new CompilationResult(compiler, isSuccess: isSuccess); | 97 return new CompilationResult(compiler, isSuccess: isSuccess); |
| 105 } | 98 } |
| 106 | 99 |
| 107 CompilerImpl compilerFor( | 100 CompilerImpl compilerFor( |
| 108 {Uri entryPoint, | 101 {Uri entryPoint, |
| 109 List<Uri> resolutionInputs, | 102 List<Uri> resolutionInputs, |
| 110 Map<String, String> memorySourceFiles: const <String, String>{}, | 103 Map<String, String> memorySourceFiles: const <String, String>{}, |
| 111 CompilerDiagnostics diagnosticHandler, | 104 CompilerDiagnostics diagnosticHandler, |
| 112 CompilerOutput outputProvider, | 105 CompilerOutput outputProvider, |
| 113 List<String> options: const <String>[], | 106 List<String> options: const <String>[], |
| 114 CompilerImpl cachedCompiler, | 107 CompilerImpl cachedCompiler, |
| 115 bool showDiagnostics: true, | 108 bool showDiagnostics: true, |
| 116 Uri packageRoot, | 109 Uri packageRoot, |
| 117 Uri packageConfig, | 110 Uri packageConfig, |
| 118 PackagesDiscoveryProvider packagesDiscoveryProvider}) { | 111 PackagesDiscoveryProvider packagesDiscoveryProvider}) { |
| 119 Uri libraryRoot = Uri.base.resolve('sdk/'); | 112 Uri libraryRoot = Uri.base.resolve('sdk/'); |
| 120 if (packageRoot == null && | 113 if (packageRoot == null && |
| 121 packageConfig == null && | 114 packageConfig == null && |
| 122 packagesDiscoveryProvider == null) { | 115 packagesDiscoveryProvider == null) { |
| 123 if (Platform.packageRoot != null) { | 116 if (Platform.packageRoot != null) { |
| 124 packageRoot = Uri.base.resolve(Platform.packageRoot); | 117 packageRoot = Uri.base.resolve(Platform.packageRoot); |
| 125 } else if (Platform.packageConfig != null) { | 118 } else if (Platform.packageConfig != null) { |
| 126 packageConfig = Uri.base.resolve(Platform.packageConfig); | 119 packageConfig = Uri.base.resolve(Platform.packageConfig); |
| 127 } else { | 120 } else { |
| 128 // The tests are run with the base directory as the SDK root | 121 // The tests are run with the base directory as the SDK root |
| 129 // so just use the .packages file there. | 122 // so just use the .packages file there. |
| 130 packageConfig = Uri.base.resolve('.packages'); | 123 packageConfig = Uri.base.resolve('.packages'); |
| 131 } | 124 } |
| 132 } | 125 } |
| 133 | 126 |
| 134 MemorySourceFileProvider provider; | 127 MemorySourceFileProvider provider; |
| 135 if (cachedCompiler == null) { | 128 if (cachedCompiler == null) { |
| 136 provider = new MemorySourceFileProvider(memorySourceFiles); | 129 provider = new MemorySourceFileProvider(memorySourceFiles); |
| 137 // Saving the provider in case we need it later for a cached compiler. | 130 // Saving the provider in case we need it later for a cached compiler. |
| 138 expando[provider] = provider; | 131 expando[provider] = provider; |
| 139 } else { | 132 } else { |
| 140 // When using a cached compiler, it has read a number of files from disk | 133 // When using a cached compiler, it has read a number of files from disk |
| 141 // already (and will not attempt to read them again due to caching). These | 134 // already (and will not attempt to read them again due to caching). These |
| 142 // files must be available to the new diagnostic handler. | 135 // files must be available to the new diagnostic handler. |
| 143 provider = expando[cachedCompiler.provider]; | 136 provider = expando[cachedCompiler.provider]; |
| 144 provider.memorySourceFiles = memorySourceFiles; | 137 provider.memorySourceFiles = memorySourceFiles; |
| 145 } | 138 } |
| 146 diagnosticHandler = createCompilerDiagnostics( | 139 diagnosticHandler = createCompilerDiagnostics(diagnosticHandler, provider, |
| 147 diagnosticHandler, provider, | |
| 148 showDiagnostics: showDiagnostics, | 140 showDiagnostics: showDiagnostics, |
| 149 verbose: options.contains('-v') || options.contains('--verbose')); | 141 verbose: options.contains('-v') || options.contains('--verbose')); |
| 150 | 142 |
| 151 if (outputProvider == null) { | 143 if (outputProvider == null) { |
| 152 outputProvider = const NullCompilerOutput(); | 144 outputProvider = const NullCompilerOutput(); |
| 153 } | 145 } |
| 154 | 146 |
| 155 CompilerImpl compiler = new CompilerImpl( | 147 CompilerImpl compiler = new CompilerImpl( |
| 156 provider, | 148 provider, |
| 157 outputProvider, | 149 outputProvider, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 180 compiler.onLibraryCreated(patchLibrary); | 172 compiler.onLibraryCreated(patchLibrary); |
| 181 compiler.onLibraryScanned(patchLibrary, null); | 173 compiler.onLibraryScanned(patchLibrary, null); |
| 182 } | 174 } |
| 183 copiedLibraries[library.canonicalUri] = library; | 175 copiedLibraries[library.canonicalUri] = library; |
| 184 } | 176 } |
| 185 }); | 177 }); |
| 186 // TODO(johnniwinther): Assert that no libraries are loaded lazily from | 178 // TODO(johnniwinther): Assert that no libraries are loaded lazily from |
| 187 // this call. | 179 // this call. |
| 188 compiler.onLibrariesLoaded(new MemoryLoadedLibraries(copiedLibraries)); | 180 compiler.onLibrariesLoaded(new MemoryLoadedLibraries(copiedLibraries)); |
| 189 | 181 |
| 190 compiler.backend.constantCompilerTask.copyConstantValues( | 182 compiler.backend.constantCompilerTask |
| 191 cachedCompiler.backend.constantCompilerTask); | 183 .copyConstantValues(cachedCompiler.backend.constantCompilerTask); |
| 192 | 184 |
| 193 Iterable cachedTreeElements = | 185 Iterable cachedTreeElements = |
| 194 cachedCompiler.enqueuer.resolution.processedElements; | 186 cachedCompiler.enqueuer.resolution.processedElements; |
| 195 cachedTreeElements.forEach((element) { | 187 cachedTreeElements.forEach((element) { |
| 196 if (element.library.isPlatformLibrary) { | 188 if (element.library.isPlatformLibrary) { |
| 197 compiler.enqueuer.resolution.registerProcessedElement(element); | 189 compiler.enqueuer.resolution.registerProcessedElement(element); |
| 198 } | 190 } |
| 199 }); | 191 }); |
| 200 | 192 |
| 201 // One potential problem that can occur when reusing elements is that there | 193 // One potential problem that can occur when reusing elements is that there |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 @override | 226 @override |
| 235 void forEachLibrary(f) {} | 227 void forEachLibrary(f) {} |
| 236 | 228 |
| 237 @override | 229 @override |
| 238 getLibrary(Uri uri) => copiedLibraries[uri]; | 230 getLibrary(Uri uri) => copiedLibraries[uri]; |
| 239 | 231 |
| 240 @override | 232 @override |
| 241 Uri get rootUri => null; | 233 Uri get rootUri => null; |
| 242 } | 234 } |
| 243 | 235 |
| 244 | |
| 245 DiagnosticHandler createDiagnosticHandler(DiagnosticHandler diagnosticHandler, | 236 DiagnosticHandler createDiagnosticHandler(DiagnosticHandler diagnosticHandler, |
| 246 SourceFileProvider provider, | 237 SourceFileProvider provider, bool showDiagnostics) { |
| 247 bool showDiagnostics) { | |
| 248 var handler = diagnosticHandler; | 238 var handler = diagnosticHandler; |
| 249 if (showDiagnostics) { | 239 if (showDiagnostics) { |
| 250 if (diagnosticHandler == null) { | 240 if (diagnosticHandler == null) { |
| 251 handler = new FormattingDiagnosticHandler(provider); | 241 handler = new FormattingDiagnosticHandler(provider); |
| 252 } else { | 242 } else { |
| 253 var formattingHandler = new FormattingDiagnosticHandler(provider); | 243 var formattingHandler = new FormattingDiagnosticHandler(provider); |
| 254 handler = (Uri uri, int begin, int end, String message, Diagnostic kind) { | 244 handler = (Uri uri, int begin, int end, String message, Diagnostic kind) { |
| 255 diagnosticHandler(uri, begin, end, message, kind); | 245 diagnosticHandler(uri, begin, end, message, kind); |
| 256 formattingHandler(uri, begin, end, message, kind); | 246 formattingHandler(uri, begin, end, message, kind); |
| 257 }; | 247 }; |
| 258 } | 248 } |
| 259 } else if (diagnosticHandler == null) { | 249 } else if (diagnosticHandler == null) { |
| 260 handler = (Uri uri, int begin, int end, String message, Diagnostic kind) {}; | 250 handler = (Uri uri, int begin, int end, String message, Diagnostic kind) {}; |
| 261 } | 251 } |
| 262 return handler; | 252 return handler; |
| 263 } | 253 } |
| OLD | NEW |