Chromium Code Reviews| 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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 126 | 126 |
| 127 String lookupPatchPath(String dartLibraryName) { | 127 String lookupPatchPath(String dartLibraryName) { |
| 128 LibraryInfo info = LIBRARIES[dartLibraryName]; | 128 LibraryInfo info = LIBRARIES[dartLibraryName]; |
| 129 if (info == null) return null; | 129 if (info == null) return null; |
| 130 if (!info.isDart2jsLibrary) return null; | 130 if (!info.isDart2jsLibrary) return null; |
| 131 String path = info.dart2jsPatchPath; | 131 String path = info.dart2jsPatchPath; |
| 132 if (path == null) return null; | 132 if (path == null) return null; |
| 133 return "lib/$path"; | 133 return "lib/$path"; |
| 134 } | 134 } |
| 135 | 135 |
| 136 elements.LibraryElement scanBuiltinLibrary(String path) { | 136 Future<elements.LibraryElement> scanBuiltinLibrary(String path) { |
| 137 Uri uri = libraryRoot.resolve(lookupLibraryPath(path)); | 137 Uri uri = libraryRoot.resolve(lookupLibraryPath(path)); |
| 138 Uri canonicalUri = new Uri(scheme: "dart", path: path); | 138 Uri canonicalUri = new Uri(scheme: "dart", path: path); |
| 139 elements.LibraryElement library = | 139 return libraryLoader.loadLibrary(uri, null, canonicalUri); |
| 140 libraryLoader.loadLibrary(uri, null, canonicalUri); | |
| 141 return library; | |
| 142 } | 140 } |
| 143 | 141 |
| 144 void log(message) { | 142 void log(message) { |
| 145 handler(null, null, null, message, api.Diagnostic.VERBOSE_INFO); | 143 handler(null, null, null, message, api.Diagnostic.VERBOSE_INFO); |
| 146 } | 144 } |
| 147 | 145 |
| 148 /// See [leg.Compiler.translateResolvedUri]. | 146 /// See [leg.Compiler.translateResolvedUri]. |
| 149 Uri translateResolvedUri(elements.LibraryElement importingLibrary, | 147 Uri translateResolvedUri(elements.LibraryElement importingLibrary, |
| 150 Uri resolvedUri, tree.Node node) { | 148 Uri resolvedUri, tree.Node node) { |
| 151 if (resolvedUri.scheme == 'dart') { | 149 if (resolvedUri.scheme == 'dart') { |
| 152 return translateDartUri(importingLibrary, resolvedUri, node); | 150 return translateDartUri(importingLibrary, resolvedUri, node); |
| 153 } | 151 } |
| 154 return resolvedUri; | 152 return resolvedUri; |
| 155 } | 153 } |
| 156 | 154 |
| 157 /** | 155 /** |
| 158 * Reads the script designated by [readableUri]. | 156 * Reads the script designated by [readableUri]. |
| 159 */ | 157 */ |
| 160 leg.Script readScript(Uri readableUri, [tree.Node node]) { | 158 Future<leg.Script> readScript(Uri readableUri, |
| 159 [elements.Element element, tree.Node node]) { | |
| 161 if (!readableUri.isAbsolute) { | 160 if (!readableUri.isAbsolute) { |
| 162 internalError('Relative uri $readableUri provided to readScript(Uri)', | 161 internalError('Relative uri $readableUri provided to readScript(Uri)', |
| 163 node: node); | 162 node: node); |
| 164 } | 163 } |
| 165 return fileReadingTask.measure(() { | 164 |
| 166 Uri resourceUri = translateUri(readableUri, node); | 165 void reportReadError(String exception) { |
|
ahe
2013/09/02 13:43:24
Perhaps this method would be nice as a general met
Johnni Winther
2013/09/03 07:51:39
Added a TODO.
| |
| 167 String text = ""; | 166 withCurrentElement(element, () { |
| 168 try { | |
| 169 // TODO(ahe): We expect the future to be complete and call value | |
| 170 // directly. In effect, we don't support truly asynchronous API. | |
| 171 text = deprecatedFutureValue(provider(resourceUri)); | |
| 172 } catch (exception) { | |
| 173 reportError(node, | 167 reportError(node, |
| 174 leg.MessageKind.READ_SCRIPT_ERROR, | 168 leg.MessageKind.READ_SCRIPT_ERROR, |
| 175 {'uri': readableUri, 'exception': exception}); | 169 {'uri': readableUri, 'exception': exception}); |
| 170 }); | |
| 171 } | |
| 172 | |
| 173 Uri resourceUri = translateUri(readableUri, node); | |
| 174 // TODO(johnniwinther): Wrap the result from [provider] in a specialized | |
| 175 // [Future] to ensure that we never execute an asynchronous action without s etting | |
|
ahe
2013/09/02 13:43:24
Long line.
Johnni Winther
2013/09/03 07:51:39
Done.
| |
| 176 // up the current element of the compiler. | |
| 177 try { | |
| 178 return provider(resourceUri).then((String text) { | |
|
ahe
2013/09/02 13:43:24
I think this code would be clearer if it was:
Fut
Johnni Winther
2013/09/03 07:51:39
Done.
| |
| 179 SourceFile sourceFile = new SourceFile(resourceUri.toString(), text); | |
| 180 // We use [readableUri] as the URI for the script since need to preserve | |
| 181 // the scheme in the script because [Script.uri] is used for resolving | |
| 182 // relative URIs mentioned in the script. See the comment on | |
| 183 // [LibraryLoader] for more details. | |
| 184 return new leg.Script(readableUri, sourceFile); | |
| 185 }).catchError((error) { | |
| 186 reportReadError(error); | |
| 176 return null; | 187 return null; |
| 177 } | 188 }); |
| 178 SourceFile sourceFile = new SourceFile(resourceUri.toString(), text); | 189 } catch (error) { |
| 179 // We use [readableUri] as the URI for the script since need to preserve | 190 reportReadError(error); |
| 180 // the scheme in the script because [Script.uri] is used for resolving | 191 return new Future.value(null); |
| 181 // relative URIs mentioned in the script. See the comment on | 192 } |
| 182 // [LibraryLoader] for more details. | |
| 183 return new leg.Script(readableUri, sourceFile); | |
| 184 }); | |
| 185 } | 193 } |
| 186 | 194 |
| 187 /** | 195 /** |
| 188 * Translates a readable URI into a resource URI. | 196 * Translates a readable URI into a resource URI. |
| 189 * | 197 * |
| 190 * See [LibraryLoader] for terminology on URIs. | 198 * See [LibraryLoader] for terminology on URIs. |
| 191 */ | 199 */ |
| 192 Uri translateUri(Uri readableUri, tree.Node node) { | 200 Uri translateUri(Uri readableUri, tree.Node node) { |
| 193 switch (readableUri.scheme) { | 201 switch (readableUri.scheme) { |
| 194 case 'package': return translatePackageUri(readableUri, node); | 202 case 'package': return translatePackageUri(readableUri, node); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 247 } | 255 } |
| 248 | 256 |
| 249 Uri translatePackageUri(Uri uri, tree.Node node) { | 257 Uri translatePackageUri(Uri uri, tree.Node node) { |
| 250 if (packageRoot == null) { | 258 if (packageRoot == null) { |
| 251 reportFatalError( | 259 reportFatalError( |
| 252 node, leg.MessageKind.PACKAGE_ROOT_NOT_SET, {'uri': uri}); | 260 node, leg.MessageKind.PACKAGE_ROOT_NOT_SET, {'uri': uri}); |
| 253 } | 261 } |
| 254 return packageRoot.resolve(uri.path); | 262 return packageRoot.resolve(uri.path); |
| 255 } | 263 } |
| 256 | 264 |
| 257 bool run(Uri uri) { | 265 Future<bool> run(Uri uri) { |
| 258 log('Allowed library categories: $allowedLibraryCategories'); | 266 log('Allowed library categories: $allowedLibraryCategories'); |
| 259 bool success = super.run(uri); | 267 return super.run(uri).then((bool success) { |
| 260 int cumulated = 0; | 268 int cumulated = 0; |
| 261 for (final task in tasks) { | 269 for (final task in tasks) { |
| 262 cumulated += task.timing; | 270 cumulated += task.timing; |
| 263 log('${task.name} took ${task.timing}msec'); | 271 log('${task.name} took ${task.timing}msec'); |
| 264 } | 272 } |
| 265 int total = totalCompileTime.elapsedMilliseconds; | 273 int total = totalCompileTime.elapsedMilliseconds; |
| 266 log('Total compile-time ${total}msec;' | 274 log('Total compile-time ${total}msec;' |
| 267 ' unaccounted ${total - cumulated}msec'); | 275 ' unaccounted ${total - cumulated}msec'); |
| 268 return success; | 276 return success; |
| 277 }); | |
| 269 } | 278 } |
| 270 | 279 |
| 271 void reportDiagnostic(leg.SourceSpan span, String message, | 280 void reportDiagnostic(leg.SourceSpan span, String message, |
| 272 api.Diagnostic kind) { | 281 api.Diagnostic kind) { |
| 273 if (identical(kind, api.Diagnostic.ERROR) | 282 if (identical(kind, api.Diagnostic.ERROR) |
| 274 || identical(kind, api.Diagnostic.CRASH)) { | 283 || identical(kind, api.Diagnostic.CRASH)) { |
| 275 compilationFailed = true; | 284 compilationFailed = true; |
| 276 } | 285 } |
| 277 // [:span.uri:] might be [:null:] in case of a [Script] with no [uri]. For | 286 // [:span.uri:] might be [:null:] in case of a [Script] with no [uri]. For |
| 278 // instance in the [Types] constructor in typechecker.dart. | 287 // instance in the [Types] constructor in typechecker.dart. |
| 279 if (span == null || span.uri == null) { | 288 if (span == null || span.uri == null) { |
| 280 handler(null, null, null, message, kind); | 289 handler(null, null, null, message, kind); |
| 281 } else { | 290 } else { |
| 282 handler(translateUri(span.uri, null), span.begin, span.end, | 291 handler(translateUri(span.uri, null), span.begin, span.end, |
| 283 message, kind); | 292 message, kind); |
| 284 } | 293 } |
| 285 } | 294 } |
| 286 | 295 |
| 287 bool get isMockCompilation { | 296 bool get isMockCompilation { |
| 288 return mockableLibraryUsed | 297 return mockableLibraryUsed |
| 289 && (options.indexOf('--allow-mock-compilation') != -1); | 298 && (options.indexOf('--allow-mock-compilation') != -1); |
| 290 } | 299 } |
| 291 } | 300 } |
| OLD | NEW |