| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 leg_apiimpl; | 5 library leg_apiimpl; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import '../compiler.dart' as api; | 9 import '../compiler.dart' as api; |
| 10 import 'dart2jslib.dart' as leg; | 10 import 'dart2jslib.dart' as leg; |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 | 130 |
| 131 String lookupPatchPath(String dartLibraryName) { | 131 String lookupPatchPath(String dartLibraryName) { |
| 132 LibraryInfo info = LIBRARIES[dartLibraryName]; | 132 LibraryInfo info = LIBRARIES[dartLibraryName]; |
| 133 if (info == null) return null; | 133 if (info == null) return null; |
| 134 if (!info.isDart2jsLibrary) return null; | 134 if (!info.isDart2jsLibrary) return null; |
| 135 String path = info.dart2jsPatchPath; | 135 String path = info.dart2jsPatchPath; |
| 136 if (path == null) return null; | 136 if (path == null) return null; |
| 137 return "lib/$path"; | 137 return "lib/$path"; |
| 138 } | 138 } |
| 139 | 139 |
| 140 elements.LibraryElement scanBuiltinLibrary(String path) { | 140 Future<elements.LibraryElement> scanBuiltinLibrary(String path) { |
| 141 Uri uri = libraryRoot.resolve(lookupLibraryPath(path)); | 141 Uri uri = libraryRoot.resolve(lookupLibraryPath(path)); |
| 142 Uri canonicalUri = new Uri(scheme: "dart", path: path); | 142 Uri canonicalUri = new Uri(scheme: "dart", path: path); |
| 143 elements.LibraryElement library = | 143 return libraryLoader.loadLibrary(uri, null, canonicalUri); |
| 144 libraryLoader.loadLibrary(uri, null, canonicalUri); | |
| 145 return library; | |
| 146 } | 144 } |
| 147 | 145 |
| 148 void log(message) { | 146 void log(message) { |
| 149 handler(null, null, null, message, api.Diagnostic.VERBOSE_INFO); | 147 handler(null, null, null, message, api.Diagnostic.VERBOSE_INFO); |
| 150 } | 148 } |
| 151 | 149 |
| 152 /// See [leg.Compiler.translateResolvedUri]. | 150 /// See [leg.Compiler.translateResolvedUri]. |
| 153 Uri translateResolvedUri(elements.LibraryElement importingLibrary, | 151 Uri translateResolvedUri(elements.LibraryElement importingLibrary, |
| 154 Uri resolvedUri, tree.Node node) { | 152 Uri resolvedUri, tree.Node node) { |
| 155 if (resolvedUri.scheme == 'dart') { | 153 if (resolvedUri.scheme == 'dart') { |
| 156 return translateDartUri(importingLibrary, resolvedUri, node); | 154 return translateDartUri(importingLibrary, resolvedUri, node); |
| 157 } | 155 } |
| 158 return resolvedUri; | 156 return resolvedUri; |
| 159 } | 157 } |
| 160 | 158 |
| 161 /** | 159 /** |
| 162 * Reads the script designated by [readableUri]. | 160 * Reads the script designated by [readableUri]. |
| 163 */ | 161 */ |
| 164 leg.Script readScript(Uri readableUri, [tree.Node node]) { | 162 Future<leg.Script> readScript(Uri readableUri, [tree.Node node]) { |
| 165 if (!readableUri.isAbsolute) { | 163 if (!readableUri.isAbsolute) { |
| 166 internalError('Relative uri $readableUri provided to readScript(Uri)', | 164 internalError('Relative uri $readableUri provided to readScript(Uri)', |
| 167 node: node); | 165 node: node); |
| 168 } | 166 } |
| 169 return fileReadingTask.measure(() { | 167 |
| 168 return fileReadingTask.measureAsync(() { |
| 170 Uri resourceUri = translateUri(readableUri, node); | 169 Uri resourceUri = translateUri(readableUri, node); |
| 171 String text = ""; | 170 return provider(resourceUri).then((text) { |
| 172 try { | 171 SourceFile sourceFile = new SourceFile(resourceUri.toString(), text); |
| 173 // TODO(ahe): We expect the future to be complete and call value | 172 // We use [readableUri] as the URI for the script since need to preserve |
| 174 // directly. In effect, we don't support truly asynchronous API. | 173 // the scheme in the script because [Script.uri] is used for resolving |
| 175 text = deprecatedFutureValue(provider(resourceUri)); | 174 // relative URIs mentioned in the script. See the comment on |
| 176 } catch (exception) { | 175 // [LibraryLoader] for more details. |
| 176 return new leg.Script(readableUri, sourceFile); |
| 177 }).catchError((error) { |
| 177 if (node != null) { | 178 if (node != null) { |
| 178 cancel("$exception", node: node); | 179 cancel("$error", node: node); |
| 179 } else { | 180 } else { |
| 180 reportDiagnostic(null, "$exception", api.Diagnostic.ERROR); | 181 reportDiagnostic(null, "$error", api.Diagnostic.ERROR); |
| 181 throw new leg.CompilerCancelledException("$exception"); | 182 throw new leg.CompilerCancelledException("$error"); |
| 182 } | 183 } |
| 183 } | 184 }); |
| 184 SourceFile sourceFile = new SourceFile(resourceUri.toString(), text); | |
| 185 // We use [readableUri] as the URI for the script since need to preserve | |
| 186 // the scheme in the script because [Script.uri] is used for resolving | |
| 187 // relative URIs mentioned in the script. See the comment on | |
| 188 // [LibraryLoader] for more details. | |
| 189 return new leg.Script(readableUri, sourceFile); | |
| 190 }); | 185 }); |
| 191 } | 186 } |
| 192 | 187 |
| 193 /** | 188 /** |
| 194 * Translates a readable URI into a resource URI. | 189 * Translates a readable URI into a resource URI. |
| 195 * | 190 * |
| 196 * See [LibraryLoader] for terminology on URIs. | 191 * See [LibraryLoader] for terminology on URIs. |
| 197 */ | 192 */ |
| 198 Uri translateUri(Uri readableUri, tree.Node node) { | 193 Uri translateUri(Uri readableUri, tree.Node node) { |
| 199 switch (readableUri.scheme) { | 194 switch (readableUri.scheme) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 } | 245 } |
| 251 | 246 |
| 252 Uri resolvePatchUri(String dartLibraryPath) { | 247 Uri resolvePatchUri(String dartLibraryPath) { |
| 253 String patchPath = lookupPatchPath(dartLibraryPath); | 248 String patchPath = lookupPatchPath(dartLibraryPath); |
| 254 if (patchPath == null) return null; | 249 if (patchPath == null) return null; |
| 255 return libraryRoot.resolve(patchPath); | 250 return libraryRoot.resolve(patchPath); |
| 256 } | 251 } |
| 257 | 252 |
| 258 translatePackageUri(Uri uri, tree.Node node) => packageRoot.resolve(uri.path); | 253 translatePackageUri(Uri uri, tree.Node node) => packageRoot.resolve(uri.path); |
| 259 | 254 |
| 260 bool run(Uri uri) { | 255 Future<bool> run(Uri uri) { |
| 261 log('Allowed library categories: $allowedLibraryCategories'); | 256 log('Allowed library categories: $allowedLibraryCategories'); |
| 262 bool success = super.run(uri); | 257 return super.run(uri).then((success) { |
| 263 int cumulated = 0; | 258 int cumulated = 0; |
| 264 for (final task in tasks) { | 259 for (final task in tasks) { |
| 265 cumulated += task.timing; | 260 cumulated += task.timing; |
| 266 log('${task.name} took ${task.timing}msec'); | 261 log('${task.name} took ${task.timing}msec'); |
| 267 } | 262 } |
| 268 int total = totalCompileTime.elapsedMilliseconds; | 263 int total = totalCompileTime.elapsedMilliseconds; |
| 269 log('Total compile-time ${total}msec;' | 264 log('Total compile-time ${total}msec;' |
| 270 ' unaccounted ${total - cumulated}msec'); | 265 ' unaccounted ${total - cumulated}msec'); |
| 271 return success; | 266 return success; |
| 267 }); |
| 272 } | 268 } |
| 273 | 269 |
| 274 void reportDiagnostic(leg.SourceSpan span, String message, | 270 void reportDiagnostic(leg.SourceSpan span, String message, |
| 275 api.Diagnostic kind) { | 271 api.Diagnostic kind) { |
| 276 if (identical(kind, api.Diagnostic.ERROR) | 272 if (identical(kind, api.Diagnostic.ERROR) |
| 277 || identical(kind, api.Diagnostic.CRASH)) { | 273 || identical(kind, api.Diagnostic.CRASH)) { |
| 278 compilationFailed = true; | 274 compilationFailed = true; |
| 279 } | 275 } |
| 280 // [:span.uri:] might be [:null:] in case of a [Script] with no [uri]. For | 276 // [:span.uri:] might be [:null:] in case of a [Script] with no [uri]. For |
| 281 // instance in the [Types] constructor in typechecker.dart. | 277 // instance in the [Types] constructor in typechecker.dart. |
| 282 if (span == null || span.uri == null) { | 278 if (span == null || span.uri == null) { |
| 283 handler(null, null, null, message, kind); | 279 handler(null, null, null, message, kind); |
| 284 } else { | 280 } else { |
| 285 handler(translateUri(span.uri, null), span.begin, span.end, | 281 handler(translateUri(span.uri, null), span.begin, span.end, |
| 286 message, kind); | 282 message, kind); |
| 287 } | 283 } |
| 288 } | 284 } |
| 289 | 285 |
| 290 bool get isMockCompilation { | 286 bool get isMockCompilation { |
| 291 return mockableLibraryUsed | 287 return mockableLibraryUsed |
| 292 && (options.indexOf('--allow-mock-compilation') != -1); | 288 && (options.indexOf('--allow-mock-compilation') != -1); |
| 293 } | 289 } |
| 294 } | 290 } |
| OLD | NEW |