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 // TODO(johnniwinther): Add [:report(..., {Element element}):] to |
167 String text = ""; | 166 // report methods in Compiler. |
168 try { | 167 void reportReadError(String exception) { |
169 // TODO(ahe): We expect the future to be complete and call value | 168 withCurrentElement(element, () { |
170 // directly. In effect, we don't support truly asynchronous API. | |
171 text = deprecatedFutureValue(provider(resourceUri)); | |
172 } catch (exception) { | |
173 reportError(node, | 169 reportError(node, |
174 leg.MessageKind.READ_SCRIPT_ERROR, | 170 leg.MessageKind.READ_SCRIPT_ERROR, |
175 {'uri': readableUri, 'exception': exception}); | 171 {'uri': readableUri, 'exception': exception}); |
176 return null; | 172 }); |
177 } | 173 } |
| 174 |
| 175 Uri resourceUri = translateUri(readableUri, node); |
| 176 // TODO(johnniwinther): Wrap the result from [provider] in a specialized |
| 177 // [Future] to ensure that we never execute an asynchronous action without s
etting |
| 178 // up the current element of the compiler. |
| 179 return new Future.sync(() => provider(resourceUri)).then((String text) { |
178 SourceFile sourceFile = new SourceFile(resourceUri.toString(), text); | 180 SourceFile sourceFile = new SourceFile(resourceUri.toString(), text); |
179 // We use [readableUri] as the URI for the script since need to preserve | 181 // We use [readableUri] as the URI for the script since need to preserve |
180 // the scheme in the script because [Script.uri] is used for resolving | 182 // the scheme in the script because [Script.uri] is used for resolving |
181 // relative URIs mentioned in the script. See the comment on | 183 // relative URIs mentioned in the script. See the comment on |
182 // [LibraryLoader] for more details. | 184 // [LibraryLoader] for more details. |
183 return new leg.Script(readableUri, sourceFile); | 185 return new leg.Script(readableUri, sourceFile); |
| 186 }).catchError((error) { |
| 187 reportReadError(error); |
| 188 return null; |
184 }); | 189 }); |
185 } | 190 } |
186 | 191 |
187 /** | 192 /** |
188 * Translates a readable URI into a resource URI. | 193 * Translates a readable URI into a resource URI. |
189 * | 194 * |
190 * See [LibraryLoader] for terminology on URIs. | 195 * See [LibraryLoader] for terminology on URIs. |
191 */ | 196 */ |
192 Uri translateUri(Uri readableUri, tree.Node node) { | 197 Uri translateUri(Uri readableUri, tree.Node node) { |
193 switch (readableUri.scheme) { | 198 switch (readableUri.scheme) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 } | 252 } |
248 | 253 |
249 Uri translatePackageUri(Uri uri, tree.Node node) { | 254 Uri translatePackageUri(Uri uri, tree.Node node) { |
250 if (packageRoot == null) { | 255 if (packageRoot == null) { |
251 reportFatalError( | 256 reportFatalError( |
252 node, leg.MessageKind.PACKAGE_ROOT_NOT_SET, {'uri': uri}); | 257 node, leg.MessageKind.PACKAGE_ROOT_NOT_SET, {'uri': uri}); |
253 } | 258 } |
254 return packageRoot.resolve(uri.path); | 259 return packageRoot.resolve(uri.path); |
255 } | 260 } |
256 | 261 |
257 bool run(Uri uri) { | 262 Future<bool> run(Uri uri) { |
258 log('Allowed library categories: $allowedLibraryCategories'); | 263 log('Allowed library categories: $allowedLibraryCategories'); |
259 bool success = super.run(uri); | 264 return super.run(uri).then((bool success) { |
260 int cumulated = 0; | 265 int cumulated = 0; |
261 for (final task in tasks) { | 266 for (final task in tasks) { |
262 cumulated += task.timing; | 267 cumulated += task.timing; |
263 log('${task.name} took ${task.timing}msec'); | 268 log('${task.name} took ${task.timing}msec'); |
264 } | 269 } |
265 int total = totalCompileTime.elapsedMilliseconds; | 270 int total = totalCompileTime.elapsedMilliseconds; |
266 log('Total compile-time ${total}msec;' | 271 log('Total compile-time ${total}msec;' |
267 ' unaccounted ${total - cumulated}msec'); | 272 ' unaccounted ${total - cumulated}msec'); |
268 return success; | 273 return success; |
| 274 }); |
269 } | 275 } |
270 | 276 |
271 void reportDiagnostic(leg.SourceSpan span, String message, | 277 void reportDiagnostic(leg.SourceSpan span, String message, |
272 api.Diagnostic kind) { | 278 api.Diagnostic kind) { |
273 if (identical(kind, api.Diagnostic.ERROR) | 279 if (identical(kind, api.Diagnostic.ERROR) |
274 || identical(kind, api.Diagnostic.CRASH)) { | 280 || identical(kind, api.Diagnostic.CRASH)) { |
275 compilationFailed = true; | 281 compilationFailed = true; |
276 } | 282 } |
277 // [:span.uri:] might be [:null:] in case of a [Script] with no [uri]. For | 283 // [:span.uri:] might be [:null:] in case of a [Script] with no [uri]. For |
278 // instance in the [Types] constructor in typechecker.dart. | 284 // instance in the [Types] constructor in typechecker.dart. |
279 if (span == null || span.uri == null) { | 285 if (span == null || span.uri == null) { |
280 handler(null, null, null, message, kind); | 286 handler(null, null, null, message, kind); |
281 } else { | 287 } else { |
282 handler(translateUri(span.uri, null), span.begin, span.end, | 288 handler(translateUri(span.uri, null), span.begin, span.end, |
283 message, kind); | 289 message, kind); |
284 } | 290 } |
285 } | 291 } |
286 | 292 |
287 bool get isMockCompilation { | 293 bool get isMockCompilation { |
288 return mockableLibraryUsed | 294 return mockableLibraryUsed |
289 && (options.indexOf('--allow-mock-compilation') != -1); | 295 && (options.indexOf('--allow-mock-compilation') != -1); |
290 } | 296 } |
291 } | 297 } |
OLD | NEW |