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

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

Powered by Google App Engine
This is Rietveld 408576698