| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 'package:expect/expect.dart'; | 7 import 'package:expect/expect.dart'; |
| 8 import 'memory_source_file_helper.dart'; | 8 import 'memory_source_file_helper.dart'; |
| 9 | 9 |
| 10 import '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart' | 10 import '../../../sdk/lib/_internal/compiler/implementation/dart2jslib.dart' |
| 11 show NullSink; | 11 show NullSink; |
| 12 | 12 |
| 13 import '../../../sdk/lib/_internal/compiler/compiler.dart' | 13 import '../../../sdk/lib/_internal/compiler/compiler.dart' |
| 14 show DiagnosticHandler; | 14 show Diagnostic, DiagnosticHandler; |
| 15 | 15 |
| 16 import 'dart:async'; | 16 import 'dart:async'; |
| 17 | 17 |
| 18 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart'
; | 18 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart'
; |
| 19 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirro
r.dart'; | 19 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirro
r.dart'; |
| 20 | 20 |
| 21 class DiagnosticMessage { |
| 22 final Uri uri; |
| 23 final int begin; |
| 24 final int end; |
| 25 final String message; |
| 26 final Diagnostic kind; |
| 27 |
| 28 DiagnosticMessage(this.uri, this.begin, this.end, this.message, this.kind); |
| 29 } |
| 30 |
| 31 class DiagnosticCollector { |
| 32 List<DiagnosticMessage> messages = <DiagnosticMessage>[]; |
| 33 |
| 34 void call(Uri uri, int begin, int end, String message, |
| 35 Diagnostic kind) { |
| 36 messages.add(new DiagnosticMessage(uri, begin, end, message, kind)); |
| 37 } |
| 38 |
| 39 Iterable<DiagnosticMessage> filterMessagesByKind(Diagnostic kind) { |
| 40 return messages.where( |
| 41 (DiagnosticMessage message) => message.kind == kind); |
| 42 } |
| 43 |
| 44 Iterable<DiagnosticMessage> get errors { |
| 45 return filterMessagesByKind(Diagnostic.ERROR); |
| 46 } |
| 47 |
| 48 Iterable<DiagnosticMessage> get warnings { |
| 49 return filterMessagesByKind(Diagnostic.WARNING); |
| 50 } |
| 51 |
| 52 Iterable<DiagnosticMessage> get hints { |
| 53 return filterMessagesByKind(Diagnostic.HINT); |
| 54 } |
| 55 } |
| 56 |
| 57 DiagnosticHandler createDiagnosticHandler(DiagnosticHandler diagnosticHandler, |
| 58 SourceFileProvider provider, |
| 59 bool showDiagnostics) { |
| 60 var handler = diagnosticHandler; |
| 61 if (showDiagnostics) { |
| 62 if (diagnosticHandler == null) { |
| 63 handler = new FormattingDiagnosticHandler(provider); |
| 64 } else { |
| 65 var formattingHandler = new FormattingDiagnosticHandler(provider); |
| 66 handler = (Uri uri, int begin, int end, String message, Diagnostic kind) { |
| 67 diagnosticHandler(uri, begin, end, message, kind); |
| 68 formattingHandler(uri, begin, end, message, kind); |
| 69 }; |
| 70 } |
| 71 } else if (diagnosticHandler == null) { |
| 72 handler = (Uri uri, int begin, int end, String message, Diagnostic kind) {}; |
| 73 } |
| 74 return handler; |
| 75 } |
| 76 |
| 21 Compiler compilerFor(Map<String,String> memorySourceFiles, | 77 Compiler compilerFor(Map<String,String> memorySourceFiles, |
| 22 {DiagnosticHandler diagnosticHandler, | 78 {DiagnosticHandler diagnosticHandler, |
| 23 List<String> options: const [], | 79 List<String> options: const [], |
| 24 Compiler cachedCompiler}) { | 80 Compiler cachedCompiler, |
| 81 bool showDiagnostics: true}) { |
| 25 Uri script = currentDirectory.resolve(nativeToUriPath(Platform.script)); | 82 Uri script = currentDirectory.resolve(nativeToUriPath(Platform.script)); |
| 26 Uri libraryRoot = script.resolve('../../../sdk/'); | 83 Uri libraryRoot = script.resolve('../../../sdk/'); |
| 27 Uri packageRoot = script.resolve('./packages/'); | 84 Uri packageRoot = script.resolve('./packages/'); |
| 28 | 85 |
| 29 MemorySourceFileProvider.MEMORY_SOURCE_FILES = memorySourceFiles; | 86 MemorySourceFileProvider.MEMORY_SOURCE_FILES = memorySourceFiles; |
| 30 var provider = new MemorySourceFileProvider(); | 87 var provider = new MemorySourceFileProvider(); |
| 31 if (diagnosticHandler == null) { | 88 var handler = |
| 32 diagnosticHandler = new FormattingDiagnosticHandler(provider); | 89 createDiagnosticHandler(diagnosticHandler, provider, showDiagnostics); |
| 33 } | |
| 34 | 90 |
| 35 EventSink<String> outputProvider(String name, String extension) { | 91 EventSink<String> outputProvider(String name, String extension) { |
| 36 if (name != '') throw 'Attempt to output file "$name.$extension"'; | 92 if (name != '') throw 'Attempt to output file "$name.$extension"'; |
| 37 return new NullSink('$name.$extension'); | 93 return new NullSink('$name.$extension'); |
| 38 } | 94 } |
| 39 | 95 |
| 40 Compiler compiler = new Compiler(provider.readStringFromUri, | 96 Compiler compiler = new Compiler(provider, |
| 41 outputProvider, | 97 outputProvider, |
| 42 diagnosticHandler, | 98 handler, |
| 43 libraryRoot, | 99 libraryRoot, |
| 44 packageRoot, | 100 packageRoot, |
| 45 options); | 101 options); |
| 46 if (cachedCompiler != null) { | 102 if (cachedCompiler != null) { |
| 47 compiler.coreLibrary = cachedCompiler.libraries['dart:core']; | 103 compiler.coreLibrary = cachedCompiler.libraries['dart:core']; |
| 48 compiler.types = cachedCompiler.types; | 104 compiler.types = cachedCompiler.types; |
| 49 cachedCompiler.libraries.forEach((String uri, library) { | 105 cachedCompiler.libraries.forEach((String uri, library) { |
| 50 if (library.isPlatformLibrary) { | 106 if (library.isPlatformLibrary) { |
| 51 compiler.libraries[uri] = library; | 107 compiler.libraries[uri] = library; |
| 52 compiler.onLibraryScanned(library, library.canonicalUri); | 108 compiler.onLibraryScanned(library, library.canonicalUri); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 72 compiler.enqueuer.resolution.resolvedElements[element] = | 128 compiler.enqueuer.resolution.resolvedElements[element] = |
| 73 treeElements; | 129 treeElements; |
| 74 } | 130 } |
| 75 }); | 131 }); |
| 76 } | 132 } |
| 77 return compiler; | 133 return compiler; |
| 78 } | 134 } |
| 79 | 135 |
| 80 Future<MirrorSystem> mirrorSystemFor(Map<String,String> memorySourceFiles, | 136 Future<MirrorSystem> mirrorSystemFor(Map<String,String> memorySourceFiles, |
| 81 {DiagnosticHandler diagnosticHandler, | 137 {DiagnosticHandler diagnosticHandler, |
| 82 List<String> options: const []}) { | 138 List<String> options: const [], |
| 139 bool showDiagnostics: true}) { |
| 83 Uri script = currentDirectory.resolve(nativeToUriPath(Platform.script)); | 140 Uri script = currentDirectory.resolve(nativeToUriPath(Platform.script)); |
| 84 Uri libraryRoot = script.resolve('../../../sdk/'); | 141 Uri libraryRoot = script.resolve('../../../sdk/'); |
| 85 Uri packageRoot = script.resolve('./packages/'); | 142 Uri packageRoot = script.resolve('./packages/'); |
| 86 | 143 |
| 87 MemorySourceFileProvider.MEMORY_SOURCE_FILES = memorySourceFiles; | 144 MemorySourceFileProvider.MEMORY_SOURCE_FILES = memorySourceFiles; |
| 88 var provider = new MemorySourceFileProvider(); | 145 var provider = new MemorySourceFileProvider(); |
| 89 if (diagnosticHandler == null) { | 146 var handler = |
| 90 diagnosticHandler = new FormattingDiagnosticHandler(provider); | 147 createDiagnosticHandler(diagnosticHandler, provider, showDiagnostics); |
| 91 } | |
| 92 | 148 |
| 93 List<Uri> libraries = <Uri>[]; | 149 List<Uri> libraries = <Uri>[]; |
| 94 memorySourceFiles.forEach((String path, _) { | 150 memorySourceFiles.forEach((String path, _) { |
| 95 libraries.add(new Uri(scheme: 'memory', path: path)); | 151 libraries.add(new Uri(scheme: 'memory', path: path)); |
| 96 }); | 152 }); |
| 97 | 153 |
| 98 return analyze(libraries, libraryRoot, packageRoot, | 154 return analyze(libraries, libraryRoot, packageRoot, |
| 99 provider, diagnosticHandler, options); | 155 provider, handler, options); |
| 100 } | 156 } |
| OLD | NEW |