Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(203)

Side by Side Diff: runtime/bin/builtin.dart

Issue 16368002: Make implicit things explicit when loading scripts. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | runtime/bin/dartutils.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 builtin; 5 library builtin;
6 import 'dart:io';
7
8 int _httpRequestResponseCode = 0;
9 String _httpRequestStatusString;
10 var _httpRequestResponse;
11
12 void _requestCompleted(HttpClientResponseBody body) {
13 _httpRequestResponseCode = body.statusCode;
14 _httpRequestStatusString = '${body.statusCode} ${body.reasonPhrase}';
15 _httpRequestResponse = null;
16 if (body.statusCode != 200 || body.type == "json") {
17 return;
18 }
19 _httpRequestResponse = body.body;
20 }
21
22 void _requestFailed(error) {
23 _httpRequestResponseCode = 0;
24 _httpRequestStatusString = error.toString();
25 _httpRequestResponse = null;
26 }
27
28 HttpClient _client = new HttpClient();
29 void _makeHttpRequest(String uri) {
30 _httpRequestResponseCode = 0;
31 _httpRequestStatusString = null;
32 _httpRequestResponse = null;
33 Uri requestUri = Uri.parse(uri);
34 _client.getUrl(requestUri)
35 .then((HttpClientRequest request) => request.close())
36 .then(HttpBodyHandler.processResponse)
37 .then((HttpClientResponseBody body) {
38 _requestCompleted(body);
39 }).catchError((error) {
40 _requestFailed(error);
41 });
42 }
43 6
44 // Corelib 'print' implementation. 7 // Corelib 'print' implementation.
45 void _print(arg) { 8 void _print(arg) {
46 _Logger._printString(arg.toString()); 9 _Logger._printString(arg.toString());
47 } 10 }
48 11
12
49 class _Logger { 13 class _Logger {
50 static void _printString(String s) native "Logger_PrintString"; 14 static void _printString(String s) native "Logger_PrintString";
51 } 15 }
52 16
17
53 _getPrintClosure() => _print; 18 _getPrintClosure() => _print;
54 19
55 // The URI that the entrypoint script was loaded from. Remembered so that
56 // package imports can be resolved relative to it.
57 var _entrypoint;
58
59 // The directory to look in to resolve "package:" scheme URIs.
60 var _packageRoot;
61 20
62 void _logResolution(String msg) { 21 void _logResolution(String msg) {
63 final enabled = false; 22 final enabled = false;
64 if (enabled) { 23 if (enabled) {
65 _Logger._printString(msg); 24 _Logger._printString(msg);
66 } 25 }
67 } 26 }
68 27
69 _setPackageRoot(String packageRoot) { 28
70 // TODO(mattsh) - refactor windows drive and path handling code 29 // Are we running on Windows?
71 // so it can be used here if needed. 30 var _isWindows = false;
72 _packageRoot = packageRoot; 31 // The current working directory
32 var _workingDirectoryUri;
33 // The URI that the entry point script was loaded from. Remembered so that
34 // package imports can be resolved relative to it.
35 var _entryPointScript;
36 // The directory to look in to resolve "package:" scheme URIs.
37 var _packageRoot;
38
39
40 void _setWindows() {
41 _isWindows = true;
73 } 42 }
74 43
75 String _resolveScriptUri(String cwd, String scriptName, bool isWindows) { 44
76 var scriptUri = Uri.parse(scriptName); 45 void _setWorkingDirectory(cwd) {
77 if (scriptUri.scheme == 'http') { 46 if (_isWindows) {
78 _entrypoint = scriptUri;
79 _logResolution("# Resolved script to: $_entrypoint");
80 return _entrypoint.toString();
81 }
82 _logResolution("# Current working directory: $cwd");
83 _logResolution("# ScriptName: $scriptName");
84 if (isWindows) {
85 // For Windows we need to massage the paths a bit according to 47 // For Windows we need to massage the paths a bit according to
86 // http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx 48 // http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx
87 // 49 //
88 // Convert 50 // Convert
89 // C:\one\two\three 51 // C:\one\two\three
90 // to 52 // to
91 // /C:/one/two/three 53 // /C:/one/two/three
92 cwd = "/${cwd.replaceAll('\\', '/')}"; 54 cwd = "/${cwd.replaceAll('\\', '/')}";
93 _logResolution("## cwd: $cwd");
94 if ((scriptName.length > 2) && (scriptName[1] == ":")) {
95 // This is an absolute path.
96 scriptName = "/${scriptName.replaceAll('\\', '/')}";
97 } else {
98 scriptName = scriptName.replaceAll('\\', '/');
99 }
100 _logResolution("## scriptName: $scriptName");
101 } 55 }
102 var base =
103 new Uri(scheme: "file",
104 path: cwd.endsWith("/") ? cwd : "$cwd/");
105 _entrypoint = base.resolve(scriptName);
106 _logResolution("# Resolved script to: $_entrypoint");
107 56
108 return _entrypoint.toString(); 57 // Ensure we have a trailing slash character.
58 if (cwd.endsWith('/')) {
59 cwd = cwd;
60 } else {
61 cwd = '$cwd/';
62 }
63 _workingDirectoryUri = new Uri(scheme: 'file', path: cwd);
64 _logResolution('# Working Directory: $cwd');
109 } 65 }
110 66
67
68 _setPackageRoot(String packageRoot) {
69 if (!packageRoot.endsWith('/')) {
70 // Ensure we have a trailing slash character.
71 packageRoot = '$packageRoot/';
72 }
73 _packageRoot = Uri.parse(packageRoot);
74 }
75
76
77 String _resolveScriptUri(String scriptName) {
78 if (_workingDirectoryUri == null) {
79 throw 'No current working directory set.';
80 }
81 var scriptUri = Uri.parse(scriptName);
82 if (scriptUri.scheme != '') {
83 // Script has a scheme, assume that it is fully formed.
84 _entryPointScript = scriptUri;
85 } else {
86 // Script does not have a scheme, assume that it is a path,
87 // resolve it against the working directory.
88 _entryPointScript = _workingDirectoryUri.resolve(scriptName);
89 }
90 _logResolution('# Resolved entry point to: $_entryPointScript');
91 return _entryPointScript.toString();
92 }
93
94
111 String _resolveUri(String base, String userString) { 95 String _resolveUri(String base, String userString) {
112 var baseUri = Uri.parse(base); 96 var baseUri = Uri.parse(base);
113 _logResolution("# Resolving: $userString from $base"); 97 _logResolution('# Resolving: $userString from $base');
114 98
115 var uri = Uri.parse(userString); 99 var uri = Uri.parse(userString);
116 var resolved; 100 var resolved;
117 if ('dart-ext' == uri.scheme) { 101 if ('dart-ext' == uri.scheme) {
118 // Relative URIs with scheme dart-ext should be resolved as if with no 102 // Relative URIs with scheme dart-ext should be resolved as if with no
119 // scheme. 103 // scheme.
120 resolved = baseUri.resolve(uri.path); 104 resolved = baseUri.resolve(uri.path);
121 var path = resolved.path; 105 var path = resolved.path;
122 if (resolved.scheme == 'package') { 106 if (resolved.scheme == 'package') {
123 // If we are resolving relative to a package URI we go directly to the 107 // If we are resolving relative to a package URI we go directly to the
124 // file path and keep the dart-ext scheme. Otherwise, we will lose the 108 // file path and keep the dart-ext scheme. Otherwise, we will lose the
125 // package URI path part. 109 // package URI path part.
126 path = _filePathFromPackageUri(resolved); 110 path = _filePathFromPackageUri(resolved);
127 } 111 }
128 resolved = new Uri(scheme: "dart-ext", path: path); 112 resolved = new Uri(scheme: 'dart-ext', path: path);
129 } else { 113 } else {
130 resolved = baseUri.resolve(userString); 114 resolved = baseUri.resolve(userString);
131 } 115 }
132 _logResolution("# Resolved to: $resolved"); 116 _logResolution('# Resolved to: $resolved');
133 return resolved.toString(); 117 return resolved.toString();
134 } 118 }
135 119
136 120
137 String _filePathFromUri(String userUri, bool isWindows) { 121 String _filePathFromUri(String userUri) {
138 var uri = Uri.parse(userUri); 122 var uri = Uri.parse(userUri);
139 _logResolution("# Getting file path from: $uri"); 123 _logResolution('# Getting file path from: $uri');
140 124
141 var path; 125 var path;
142 switch (uri.scheme) { 126 switch (uri.scheme) {
143 case 'file': 127 case 'file':
144 path = _filePathFromFileUri(uri); 128 path = _filePathFromFileUri(uri);
145 break; 129 break;
146 case 'dart-ext': 130 case 'dart-ext':
147 path = _filePathFromOtherUri(uri); 131 path = _filePathFromOtherUri(uri);
148 break; 132 break;
149 case 'package': 133 case 'package':
150 path = _filePathFromPackageUri(uri); 134 path = _filePathFromPackageUri(uri);
151 break; 135 break;
152 case 'http': 136 case 'http':
153 path = _filePathFromHttpUri(uri); 137 path = _filePathFromHttpUri(uri);
154 break; 138 break;
155 default: 139 default:
156 // Only handling file and package URIs in standalone binary. 140 // Only handling file and package URIs in standalone binary.
157 _logResolution("# Unknown scheme (${uri.scheme}) in $uri."); 141 _logResolution('# Unknown scheme (${uri.scheme}) in $uri.');
158 throw "Not a known scheme: $uri"; 142 throw 'Not a known scheme: $uri';
159 } 143 }
160 144
161 if (isWindows && path.startsWith("/")) { 145 if (_isWindows && path.startsWith('/')) {
162 // For Windows we need to massage the paths a bit according to 146 // For Windows we need to massage the paths a bit according to
163 // http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx 147 // http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx
164 // 148 //
165 // Drop the leading / before the drive letter. 149 // Drop the leading / before the drive letter.
166 path = path.substring(1); 150 path = path.substring(1);
167 _logResolution("# path: $path"); 151 _logResolution('# Path: Removed leading / -> $path');
168 } 152 }
169 153
170 return path; 154 return path;
171 } 155 }
172 156
157
173 String _filePathFromFileUri(Uri uri) { 158 String _filePathFromFileUri(Uri uri) {
174 if (!uri.host.isEmpty) { 159 if (!uri.host.isEmpty) {
175 throw "URIs using the 'file:' scheme may not contain a host."; 160 throw "URIs using the 'file:' scheme may not contain a host.";
176 } 161 }
177 162
178 _logResolution("# Path: ${uri.path}"); 163 _logResolution('# Path: $uri -> ${uri.path}');
179 return uri.path; 164 return uri.path;
180 } 165 }
181 166
167
182 String _filePathFromOtherUri(Uri uri) { 168 String _filePathFromOtherUri(Uri uri) {
183 if (!uri.host.isEmpty) { 169 if (!uri.host.isEmpty) {
184 throw "URIs whose paths are used as file paths may not contain a host."; 170 throw 'URIs whose paths are used as file paths may not contain a host.';
185 } 171 }
186 172
187 _logResolution("# Path: ${uri.path}"); 173 _logResolution('# Path: $uri -> ${uri.path}');
188 return uri.path; 174 return uri.path;
189 } 175 }
190 176
177
191 String _filePathFromPackageUri(Uri uri) { 178 String _filePathFromPackageUri(Uri uri) {
192 if (!uri.host.isEmpty) { 179 if (!uri.host.isEmpty) {
193 var path = (uri.path != '') ? '${uri.host}${uri.path}' : uri.host; 180 var path = (uri.path != '') ? '${uri.host}${uri.path}' : uri.host;
194 var right = 'package:$path'; 181 var right = 'package:$path';
195 var wrong = 'package://$path'; 182 var wrong = 'package://$path';
196 183
197 throw "URIs using the 'package:' scheme should look like " 184 throw "URIs using the 'package:' scheme should look like "
198 "'$right', not '$wrong'."; 185 "'$right', not '$wrong'.";
199 } 186 }
200 187
188 var packageUri;
201 var path; 189 var path;
202 if (_packageRoot != null) { 190 if (_packageRoot != null) {
203 path = "${_packageRoot}${uri.path}"; 191 // Resolve against package root.
192 packageUri = _packageRoot.resolve(uri.path);
204 } else { 193 } else {
205 if (_entrypoint.scheme == 'http') { 194 // Resolve against working directory.
206 path = _entrypoint.resolve('packages/${uri.path}').toString(); 195 packageUri = _entryPointScript.resolve('packages/${uri.path}');
207 } else {
208 path = _entrypoint.resolve('packages/${uri.path}').path;
209 }
210 } 196 }
211 197
212 _logResolution("# Package: $path"); 198 if (packageUri.scheme == 'file') {
199 path = packageUri.path;
200 } else {
201 path = packageUri.toString();
202 }
203 _logResolution('# Package: $uri -> $path');
213 return path; 204 return path;
214 } 205 }
215 206
207
216 String _filePathFromHttpUri(Uri uri) { 208 String _filePathFromHttpUri(Uri uri) {
217 _logResolution('# Path: $uri'); 209 _logResolution('# Path: $uri -> $uri');
218 return uri.toString(); 210 return uri.toString();
219 } 211 }
212
213
214 String _pathFromHttpUri(String userUri) {
215 var uri = Uri.parse(userUri);
216 return uri.path;
217 }
218
219
220 String _hostFromHttpUri(String userUri) {
221 var uri = Uri.parse(userUri);
222 return uri.host;
223 }
224
225
226 int _portFromHttpUri(String userUri) {
227 var uri = Uri.parse(userUri);
228 return uri.port == 0 ? 80 : uri.port;
229 }
OLDNEW
« no previous file with comments | « no previous file | runtime/bin/dartutils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698