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, |
163 [elements.Element element, tree.Node node]) { | |
165 if (!readableUri.isAbsolute) { | 164 if (!readableUri.isAbsolute) { |
166 internalError('Relative uri $readableUri provided to readScript(Uri)', | 165 internalError('Relative uri $readableUri provided to readScript(Uri)', |
167 node: node); | 166 node: node); |
168 } | 167 } |
169 return fileReadingTask.measure(() { | 168 |
170 Uri resourceUri = translateUri(readableUri, node); | 169 void reportError(String error) { |
171 String text = ""; | 170 withCurrentElement(element, () { |
172 try { | 171 reportFatalError( |
173 // TODO(ahe): We expect the future to be complete and call value | 172 node, |
174 // directly. In effect, we don't support truly asynchronous API. | 173 leg.MessageKind.GENERIC, {'text': 'Error: $error'}); |
175 text = deprecatedFutureValue(provider(resourceUri)); | 174 }); |
176 } catch (exception) { | 175 } |
177 if (node != null) { | 176 |
178 cancel("$exception", node: node); | 177 Uri resourceUri = translateUri(readableUri, node); |
179 } else { | 178 // TODO(johnniwinther): Wrap the result from [provider] in a specialized |
180 reportError( | 179 // [Future] to ensure that we exexute an asynchronous action without setting |
ahe
2013/08/06 16:29:17
"we exexute" -> "we never execute"
Johnni Winther
2013/08/27 11:05:00
Done.
| |
181 null, | 180 // up the current element of the compiler. |
182 leg.MessageKind.GENERIC, {'text': 'Error: $exception'}); | 181 try { |
183 throw new leg.CompilerCancelledException("$exception"); | 182 return provider(resourceUri).then((String text) { |
184 } | 183 SourceFile sourceFile = new SourceFile(resourceUri.toString(), text); |
185 } | 184 // We use [readableUri] as the URI for the script since need to preserve |
186 SourceFile sourceFile = new SourceFile(resourceUri.toString(), text); | 185 // the scheme in the script because [Script.uri] is used for resolving |
187 // We use [readableUri] as the URI for the script since need to preserve | 186 // relative URIs mentioned in the script. See the comment on |
188 // the scheme in the script because [Script.uri] is used for resolving | 187 // [LibraryLoader] for more details. |
189 // relative URIs mentioned in the script. See the comment on | 188 return new leg.Script(readableUri, sourceFile); |
190 // [LibraryLoader] for more details. | 189 }).catchError((error) { |
191 return new leg.Script(readableUri, sourceFile); | 190 reportError(error); |
192 }); | 191 }); |
192 } catch (error) { | |
193 reportError(error); | |
194 } | |
193 } | 195 } |
194 | 196 |
195 /** | 197 /** |
196 * Translates a readable URI into a resource URI. | 198 * Translates a readable URI into a resource URI. |
197 * | 199 * |
198 * See [LibraryLoader] for terminology on URIs. | 200 * See [LibraryLoader] for terminology on URIs. |
199 */ | 201 */ |
200 Uri translateUri(Uri readableUri, tree.Node node) { | 202 Uri translateUri(Uri readableUri, tree.Node node) { |
201 switch (readableUri.scheme) { | 203 switch (readableUri.scheme) { |
202 case 'package': return translatePackageUri(readableUri, node); | 204 case 'package': return translatePackageUri(readableUri, node); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
266 } | 268 } |
267 | 269 |
268 Uri translatePackageUri(Uri uri, tree.Node node) { | 270 Uri translatePackageUri(Uri uri, tree.Node node) { |
269 if (packageRoot == null) { | 271 if (packageRoot == null) { |
270 reportFatalError( | 272 reportFatalError( |
271 node, leg.MessageKind.PACKAGE_ROOT_NOT_SET, {'uri': uri}); | 273 node, leg.MessageKind.PACKAGE_ROOT_NOT_SET, {'uri': uri}); |
272 } | 274 } |
273 return packageRoot.resolve(uri.path); | 275 return packageRoot.resolve(uri.path); |
274 } | 276 } |
275 | 277 |
276 bool run(Uri uri) { | 278 Future<bool> run(Uri uri) { |
277 log('Allowed library categories: $allowedLibraryCategories'); | 279 log('Allowed library categories: $allowedLibraryCategories'); |
278 bool success = super.run(uri); | 280 return super.run(uri).then((bool success) { |
279 int cumulated = 0; | 281 int cumulated = 0; |
280 for (final task in tasks) { | 282 for (final task in tasks) { |
281 cumulated += task.timing; | 283 cumulated += task.timing; |
282 log('${task.name} took ${task.timing}msec'); | 284 log('${task.name} took ${task.timing}msec'); |
283 } | 285 } |
284 int total = totalCompileTime.elapsedMilliseconds; | 286 int total = totalCompileTime.elapsedMilliseconds; |
285 log('Total compile-time ${total}msec;' | 287 log('Total compile-time ${total}msec;' |
286 ' unaccounted ${total - cumulated}msec'); | 288 ' unaccounted ${total - cumulated}msec'); |
287 return success; | 289 return success; |
290 }); | |
288 } | 291 } |
289 | 292 |
290 void reportDiagnostic(leg.SourceSpan span, String message, | 293 void reportDiagnostic(leg.SourceSpan span, String message, |
291 api.Diagnostic kind) { | 294 api.Diagnostic kind) { |
292 if (identical(kind, api.Diagnostic.ERROR) | 295 if (identical(kind, api.Diagnostic.ERROR) |
293 || identical(kind, api.Diagnostic.CRASH)) { | 296 || identical(kind, api.Diagnostic.CRASH)) { |
294 compilationFailed = true; | 297 compilationFailed = true; |
295 } | 298 } |
296 // [:span.uri:] might be [:null:] in case of a [Script] with no [uri]. For | 299 // [:span.uri:] might be [:null:] in case of a [Script] with no [uri]. For |
297 // instance in the [Types] constructor in typechecker.dart. | 300 // instance in the [Types] constructor in typechecker.dart. |
298 if (span == null || span.uri == null) { | 301 if (span == null || span.uri == null) { |
299 handler(null, null, null, message, kind); | 302 handler(null, null, null, message, kind); |
300 } else { | 303 } else { |
301 handler(translateUri(span.uri, null), span.begin, span.end, | 304 handler(translateUri(span.uri, null), span.begin, span.end, |
302 message, kind); | 305 message, kind); |
303 } | 306 } |
304 } | 307 } |
305 | 308 |
306 bool get isMockCompilation { | 309 bool get isMockCompilation { |
307 return mockableLibraryUsed | 310 return mockableLibraryUsed |
308 && (options.indexOf('--allow-mock-compilation') != -1); | 311 && (options.indexOf('--allow-mock-compilation') != -1); |
309 } | 312 } |
310 } | 313 } |
OLD | NEW |