| 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 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 if (node == null) node = leg.NO_LOCATION_SPANNABLE; | 201 if (node == null) node = leg.NO_LOCATION_SPANNABLE; |
| 202 internalError(node, | 202 internalError(node, |
| 203 'Relative uri $readableUri provided to readScript(Uri).'); | 203 'Relative uri $readableUri provided to readScript(Uri).'); |
| 204 } | 204 } |
| 205 | 205 |
| 206 // We need to store the current element since we are reporting read errors | 206 // We need to store the current element since we are reporting read errors |
| 207 // asynchronously and therefore need to restore the current element for | 207 // asynchronously and therefore need to restore the current element for |
| 208 // [node] to be valid. | 208 // [node] to be valid. |
| 209 elements.Element element = currentElement; | 209 elements.Element element = currentElement; |
| 210 void reportReadError(exception) { | 210 void reportReadError(exception) { |
| 211 withCurrentElement(element, () { | 211 if (element == null || node == null) { |
| 212 reportError(node, | 212 reportError( |
| 213 leg.MessageKind.READ_SCRIPT_ERROR, | 213 new leg.SourceSpan(readableUri, 0, 0), |
| 214 {'uri': readableUri, 'exception': exception}); | 214 leg.MessageKind.READ_SELF_ERROR, |
| 215 }); | 215 {'uri': readableUri, 'exception': exception}); |
| 216 } else { |
| 217 withCurrentElement(element, () { |
| 218 reportError( |
| 219 node, |
| 220 leg.MessageKind.READ_SCRIPT_ERROR, |
| 221 {'uri': readableUri, 'exception': exception}); |
| 222 }); |
| 223 } |
| 216 } | 224 } |
| 217 | 225 |
| 218 Uri resourceUri = translateUri(node, readableUri); | 226 Uri resourceUri = translateUri(node, readableUri); |
| 219 String resourceUriString = resourceUri.toString(); | 227 String resourceUriString = '$resourceUri'; |
| 220 if (resourceUri.scheme == 'dart-ext') { | 228 if (resourceUri.scheme == 'dart-ext') { |
| 221 if (!allowNativeExtensions) { | 229 if (!allowNativeExtensions) { |
| 222 withCurrentElement(element, () { | 230 withCurrentElement(element, () { |
| 223 reportError(node, leg.MessageKind.DART_EXT_NOT_SUPPORTED); | 231 reportError(node, leg.MessageKind.DART_EXT_NOT_SUPPORTED); |
| 224 }); | 232 }); |
| 225 } | 233 } |
| 226 return new Future.value(new leg.Script(readableUri, resourceUri, | 234 return synthesizeScript(node, readableUri); |
| 227 new StringSourceFile(resourceUriString, | |
| 228 "// Synthetic source file generated for '$readableUri'."))); | |
| 229 } | 235 } |
| 230 | 236 |
| 231 // TODO(johnniwinther): Wrap the result from [provider] in a specialized | 237 // TODO(johnniwinther): Wrap the result from [provider] in a specialized |
| 232 // [Future] to ensure that we never execute an asynchronous action without | 238 // [Future] to ensure that we never execute an asynchronous action without |
| 233 // setting up the current element of the compiler. | 239 // setting up the current element of the compiler. |
| 234 return new Future.sync(() => callUserProvider(resourceUri)).then((data) { | 240 return new Future.sync(() => callUserProvider(resourceUri)).then((data) { |
| 235 SourceFile sourceFile; | 241 SourceFile sourceFile; |
| 236 if (data is List<int>) { | 242 if (data is List<int>) { |
| 237 sourceFile = new Utf8BytesSourceFile(resourceUriString, data); | 243 sourceFile = new Utf8BytesSourceFile(resourceUriString, data); |
| 238 } else if (data is String) { | 244 } else if (data is String) { |
| 239 sourceFile = new StringSourceFile(resourceUriString, data); | 245 sourceFile = new StringSourceFile(resourceUriString, data); |
| 240 } else { | 246 } else { |
| 241 String message = "Expected a 'String' or a 'List<int>' from the input " | 247 String message = "Expected a 'String' or a 'List<int>' from the input " |
| 242 "provider, but got: ${Error.safeToString(data)}."; | 248 "provider, but got: ${Error.safeToString(data)}."; |
| 243 reportReadError(message); | 249 reportReadError(message); |
| 244 } | 250 } |
| 245 // We use [readableUri] as the URI for the script since need to preserve | 251 // We use [readableUri] as the URI for the script since need to preserve |
| 246 // the scheme in the script because [Script.uri] is used for resolving | 252 // the scheme in the script because [Script.uri] is used for resolving |
| 247 // relative URIs mentioned in the script. See the comment on | 253 // relative URIs mentioned in the script. See the comment on |
| 248 // [LibraryLoader] for more details. | 254 // [LibraryLoader] for more details. |
| 249 return new leg.Script(readableUri, resourceUri, sourceFile); | 255 return new leg.Script(readableUri, resourceUri, sourceFile); |
| 250 }).catchError((error) { | 256 }).catchError((error) { |
| 251 reportReadError(error); | 257 reportReadError(error); |
| 252 return null; | 258 return synthesizeScript(node, readableUri); |
| 253 }); | 259 }); |
| 254 } | 260 } |
| 255 | 261 |
| 262 Future<leg.Script> synthesizeScript(leg.Spannable node, Uri readableUri) { |
| 263 Uri resourceUri = translateUri(node, readableUri); |
| 264 return new Future.value( |
| 265 new leg.Script( |
| 266 readableUri, resourceUri, |
| 267 new StringSourceFile( |
| 268 '$resourceUri', |
| 269 "// Synthetic source file generated for '$readableUri'."), |
| 270 isSynthesized: true)); |
| 271 } |
| 272 |
| 256 /** | 273 /** |
| 257 * Translates a readable URI into a resource URI. | 274 * Translates a readable URI into a resource URI. |
| 258 * | 275 * |
| 259 * See [LibraryLoader] for terminology on URIs. | 276 * See [LibraryLoader] for terminology on URIs. |
| 260 */ | 277 */ |
| 261 Uri translateUri(leg.Spannable node, Uri readableUri) { | 278 Uri translateUri(leg.Spannable node, Uri readableUri) { |
| 262 switch (readableUri.scheme) { | 279 switch (readableUri.scheme) { |
| 263 case 'package': return translatePackageUri(node, readableUri); | 280 case 'package': return translatePackageUri(node, readableUri); |
| 264 default: return readableUri; | 281 default: return readableUri; |
| 265 } | 282 } |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 print('$message: ${tryToString(exception)}'); | 404 print('$message: ${tryToString(exception)}'); |
| 388 print(tryToString(stackTrace)); | 405 print(tryToString(stackTrace)); |
| 389 } | 406 } |
| 390 | 407 |
| 391 fromEnvironment(String name) => environment[name]; | 408 fromEnvironment(String name) => environment[name]; |
| 392 | 409 |
| 393 LibraryInfo lookupLibraryInfo(String libraryName) { | 410 LibraryInfo lookupLibraryInfo(String libraryName) { |
| 394 return library_info.LIBRARIES[libraryName]; | 411 return library_info.LIBRARIES[libraryName]; |
| 395 } | 412 } |
| 396 } | 413 } |
| OLD | NEW |