OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 fasta.translate_uri; | 5 library fasta.translate_uri; |
6 | 6 |
7 import 'dart:async' show Future; | 7 import 'dart:async' show Future; |
8 | 8 |
9 import 'package:front_end/file_system.dart'; | 9 import 'dart:convert' show JSON; |
| 10 |
| 11 import 'package:front_end/file_system.dart' |
| 12 show FileSystem, FileSystemException; |
| 13 |
10 import 'package:package_config/packages_file.dart' as packages_file show parse; | 14 import 'package:package_config/packages_file.dart' as packages_file show parse; |
11 | 15 |
12 import 'errors.dart' show inputError; | 16 import 'errors.dart' show inputError; |
13 | 17 |
14 class TranslateUri { | 18 class TranslateUri { |
15 final Map<String, Uri> packages; | 19 final Map<String, Uri> packages; |
16 final Map<String, Uri> dartLibraries; | 20 final Map<String, Uri> dartLibraries; |
17 | 21 |
18 TranslateUri(this.packages, this.dartLibraries); | 22 // TODO(ahe): We probably want this to be `Map<String, Uri>`, that is, just |
| 23 // one patch library (with parts). |
| 24 final Map<String, List<Uri>> patches; |
| 25 |
| 26 TranslateUri(this.packages, this.dartLibraries, this.patches); |
19 | 27 |
20 Uri translate(Uri uri) { | 28 Uri translate(Uri uri) { |
21 if (uri.scheme == "dart") return translateDartUri(uri); | 29 if (uri.scheme == "dart") return translateDartUri(uri); |
22 if (uri.scheme == "package") return translatePackageUri(uri); | 30 if (uri.scheme == "package") return translatePackageUri(uri); |
23 return null; | 31 return null; |
24 } | 32 } |
25 | 33 |
26 Uri translateDartUri(Uri uri) { | 34 Uri translateDartUri(Uri uri) { |
27 if (!uri.isScheme('dart')) return null; | 35 if (!uri.isScheme('dart')) return null; |
28 String path = uri.path; | 36 String path = uri.path; |
(...skipping 10 matching lines...) Expand all Loading... |
39 Uri translatePackageUri(Uri uri) { | 47 Uri translatePackageUri(Uri uri) { |
40 int index = uri.path.indexOf("/"); | 48 int index = uri.path.indexOf("/"); |
41 if (index == -1) return null; | 49 if (index == -1) return null; |
42 String name = uri.path.substring(0, index); | 50 String name = uri.path.substring(0, index); |
43 String path = uri.path.substring(index + 1); | 51 String path = uri.path.substring(index + 1); |
44 Uri root = packages[name]; | 52 Uri root = packages[name]; |
45 if (root == null) return null; | 53 if (root == null) return null; |
46 return root.resolve(path); | 54 return root.resolve(path); |
47 } | 55 } |
48 | 56 |
| 57 /// Returns true if [uri] is private to the platform libraries (and thus not |
| 58 /// accessible from user code). |
| 59 bool isPlatformImplementation(Uri uri) { |
| 60 if (uri.scheme != "dart") return false; |
| 61 String path = uri.path; |
| 62 return dartLibraries[path] == null || path.startsWith("_"); |
| 63 } |
| 64 |
49 static Future<TranslateUri> parse(FileSystem fileSystem, Uri sdk, | 65 static Future<TranslateUri> parse(FileSystem fileSystem, Uri sdk, |
50 [Uri uri]) async { | 66 {Uri packages}) async { |
51 // This list below is generated with [bin/generate_dart_libraries.dart] and | 67 Uri librariesJson = sdk?.resolve("lib/libraries.json"); |
52 // additional entries for _builtin, _vmservice, profiler, and vmservice_io. | 68 |
53 // | 69 // TODO(ahe): Provide a value for this file. |
54 // TODO(ahe): This is only used with the option --compile-sdk, and | 70 Uri patches = null; |
55 // currently doesn't work outside the SDK source tree. | 71 |
56 Map<String, Uri> dartLibraries = <String, Uri>{}; | 72 packages ??= Uri.base.resolve(".packages"); |
57 if (sdk != null) { | |
58 dartLibraries = <String, Uri>{ | |
59 "_async_await_error_codes": sdk.resolve( | |
60 "lib/_internal/js_runtime/lib/shared/async_await_error_codes.dart"), | |
61 "_blink": sdk.resolve("lib/_blink/dartium/_blink_dartium.dart"), | |
62 "_builtin": sdk.resolve("lib/_builtin/_builtin.dart"), | |
63 "_chrome": sdk.resolve("lib/_chrome/dart2js/chrome_dart2js.dart"), | |
64 "_foreign_helper": | |
65 sdk.resolve("lib/_internal/js_runtime/lib/foreign_helper.dart"), | |
66 "_interceptors": | |
67 sdk.resolve("lib/_internal/js_runtime/lib/interceptors.dart"), | |
68 "_internal": sdk.resolve("lib/internal/internal.dart"), | |
69 "_isolate_helper": | |
70 sdk.resolve("lib/_internal/js_runtime/lib/isolate_helper.dart"), | |
71 "_js_embedded_names": sdk | |
72 .resolve("lib/_internal/js_runtime/lib/shared/embedded_names.dart"), | |
73 "_js_helper": | |
74 sdk.resolve("lib/_internal/js_runtime/lib/js_helper.dart"), | |
75 "_js_mirrors": | |
76 sdk.resolve("lib/_internal/js_runtime/lib/js_mirrors.dart"), | |
77 "_js_names": sdk.resolve("lib/_internal/js_runtime/lib/js_names.dart"), | |
78 "_js_primitives": | |
79 sdk.resolve("lib/_internal/js_runtime/lib/js_primitives.dart"), | |
80 "_metadata": sdk.resolve("lib/html/html_common/metadata.dart"), | |
81 "_native_typed_data": | |
82 sdk.resolve("lib/_internal/js_runtime/lib/native_typed_data.dart"), | |
83 "_vmservice": sdk.resolve("lib/vmservice/vmservice.dart"), | |
84 "async": sdk.resolve("lib/async/async.dart"), | |
85 "collection": sdk.resolve("lib/collection/collection.dart"), | |
86 "convert": sdk.resolve("lib/convert/convert.dart"), | |
87 "core": sdk.resolve("lib/core/core.dart"), | |
88 "developer": sdk.resolve("lib/developer/developer.dart"), | |
89 "html": sdk.resolve("lib/html/dart2js/html_dart2js.dart"), | |
90 "html_common": | |
91 sdk.resolve("lib/html/html_common/html_common_dart2js.dart"), | |
92 "indexed_db": | |
93 sdk.resolve("lib/indexed_db/dart2js/indexed_db_dart2js.dart"), | |
94 "io": sdk.resolve("lib/io/io.dart"), | |
95 "isolate": sdk.resolve("lib/isolate/isolate.dart"), | |
96 "js": sdk.resolve("lib/js/dart2js/js_dart2js.dart"), | |
97 "js_util": sdk.resolve("lib/js_util/dart2js/js_util_dart2js.dart"), | |
98 "math": sdk.resolve("lib/math/math.dart"), | |
99 "mirrors": sdk.resolve("lib/mirrors/mirrors.dart"), | |
100 "nativewrappers": sdk.resolve("lib/html/dartium/nativewrappers.dart"), | |
101 "profiler": sdk.resolve("lib/profiler/profiler.dart"), | |
102 "svg": sdk.resolve("lib/svg/dart2js/svg_dart2js.dart"), | |
103 "typed_data": sdk.resolve("lib/typed_data/typed_data.dart"), | |
104 "vmservice_io": sdk.resolve("lib/vmservice_io/vmservice_io.dart"), | |
105 "web_audio": | |
106 sdk.resolve("lib/web_audio/dart2js/web_audio_dart2js.dart"), | |
107 "web_gl": sdk.resolve("lib/web_gl/dart2js/web_gl_dart2js.dart"), | |
108 "web_sql": sdk.resolve("lib/web_sql/dart2js/web_sql_dart2js.dart"), | |
109 }; | |
110 } | |
111 uri ??= Uri.base.resolve(".packages"); | |
112 | 73 |
113 List<int> bytes; | 74 List<int> bytes; |
114 try { | 75 try { |
115 bytes = await fileSystem.entityForUri(uri).readAsBytes(); | 76 bytes = await fileSystem.entityForUri(packages).readAsBytes(); |
116 } on FileSystemException catch (e) { | 77 } on FileSystemException catch (e) { |
117 inputError(uri, -1, e.message); | 78 inputError(packages, -1, e.message); |
118 } | 79 } |
119 | 80 |
120 Map<String, Uri> packages = const <String, Uri>{}; | 81 Map<String, Uri> parsedPackages; |
121 try { | 82 try { |
122 packages = packages_file.parse(bytes, uri); | 83 parsedPackages = packages_file.parse(bytes, packages); |
123 } on FormatException catch (e) { | 84 } on FormatException catch (e) { |
124 return inputError(uri, e.offset, e.message); | 85 return inputError(packages, e.offset, e.message); |
125 } | 86 } |
126 return new TranslateUri(packages, dartLibraries); | 87 return new TranslateUri( |
| 88 parsedPackages, |
| 89 await computeLibraries(fileSystem, librariesJson), |
| 90 await computePatches(fileSystem, patches)); |
127 } | 91 } |
128 } | 92 } |
| 93 |
| 94 Future<Map<String, Uri>> computeLibraries( |
| 95 FileSystem fileSystem, Uri uri) async { |
| 96 if (uri == null) return const <String, Uri>{}; |
| 97 Map<String, String> libraries = JSON |
| 98 .decode(await fileSystem.entityForUri(uri).readAsString())["libraries"]; |
| 99 Map<String, Uri> result = <String, Uri>{}; |
| 100 libraries.forEach((String name, String path) { |
| 101 result[name] = uri.resolve(path); |
| 102 }); |
| 103 return result; |
| 104 } |
| 105 |
| 106 Future<Map<String, List<Uri>>> computePatches( |
| 107 FileSystem fileSystem, Uri uri) async { |
| 108 // TODO(ahe): Read patch information. |
| 109 return const <String, List<Uri>>{}; |
| 110 } |
OLD | NEW |