| 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 builtin; | 5 library builtin; |
| 6 import 'dart:io'; | 6 import 'dart:io'; |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 // import 'root_library'; happens here from C Code | 9 // import 'root_library'; happens here from C Code |
| 10 | 10 |
| 11 // The root library (aka the script) is imported into this library. The | 11 // The root library (aka the script) is imported into this library. The |
| 12 // standalone embedder uses this to lookup the main entrypoint in the | 12 // standalone embedder uses this to lookup the main entrypoint in the |
| 13 // root library's namespace. | 13 // root library's namespace. |
| 14 Function _getMainClosure() => main; | 14 Function _getMainClosure() => main; |
| 15 | 15 |
| 16 | 16 |
| 17 // Corelib 'print' implementation. | 17 // Corelib 'print' implementation. |
| 18 void _print(arg) { | 18 void _print(arg) { |
| 19 _Logger._printString(arg.toString()); | 19 _Logger._printString(arg.toString()); |
| 20 } | 20 } |
| 21 | 21 |
| 22 | 22 |
| 23 class _Logger { | 23 class _Logger { |
| 24 static void _printString(String s) native "Logger_PrintString"; | 24 static void _printString(String s) native "Logger_PrintString"; |
| 25 } | 25 } |
| 26 | 26 |
| 27 | 27 |
| 28 _getPrintClosure() => _print; | 28 _getPrintClosure() => _print; |
| 29 | 29 |
| 30 | 30 const _logBuiltin = false; |
| 31 void _logResolution(String msg) { | |
| 32 final enabled = false; | |
| 33 if (enabled) { | |
| 34 _Logger._printString(msg); | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 | 31 |
| 39 // Corelib 'Uri.base' implementation. | 32 // Corelib 'Uri.base' implementation. |
| 40 Uri _uriBase() { | 33 Uri _uriBase() { |
| 41 return new Uri.file(Directory.current.path + "/"); | 34 return new Uri.file(Directory.current.path + "/"); |
| 42 } | 35 } |
| 43 | 36 |
| 44 | 37 |
| 45 _getUriBaseClosure() => _uriBase; | 38 _getUriBaseClosure() => _uriBase; |
| 46 | 39 |
| 47 | 40 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 } | 98 } |
| 106 return null; | 99 return null; |
| 107 } | 100 } |
| 108 | 101 |
| 109 | 102 |
| 110 void _setWorkingDirectory(cwd) { | 103 void _setWorkingDirectory(cwd) { |
| 111 _workingWindowsDrivePrefix = _extractDriveLetterPrefix(cwd); | 104 _workingWindowsDrivePrefix = _extractDriveLetterPrefix(cwd); |
| 112 cwd = _sanitizeWindowsPath(cwd); | 105 cwd = _sanitizeWindowsPath(cwd); |
| 113 cwd = _enforceTrailingSlash(cwd); | 106 cwd = _enforceTrailingSlash(cwd); |
| 114 _workingDirectoryUri = new Uri(scheme: 'file', path: cwd); | 107 _workingDirectoryUri = new Uri(scheme: 'file', path: cwd); |
| 115 _logResolution('# Working Directory: $cwd'); | 108 if (_logBuiltin) { |
| 109 _print('# Working Directory: $cwd'); |
| 110 } |
| 116 } | 111 } |
| 117 | 112 |
| 118 | 113 |
| 119 _setPackageRoot(String packageRoot) { | 114 _setPackageRoot(String packageRoot) { |
| 120 packageRoot = _enforceTrailingSlash(packageRoot); | 115 packageRoot = _enforceTrailingSlash(packageRoot); |
| 121 if (packageRoot.startsWith('file:') || | 116 if (packageRoot.startsWith('file:') || |
| 122 packageRoot.startsWith('http:') || | 117 packageRoot.startsWith('http:') || |
| 123 packageRoot.startsWith('https:')) { | 118 packageRoot.startsWith('https:')) { |
| 124 _packageRoot = _workingDirectoryUri.resolve(packageRoot); | 119 _packageRoot = _workingDirectoryUri.resolve(packageRoot); |
| 125 } else { | 120 } else { |
| 126 _packageRoot = _workingDirectoryUri.resolveUri(new Uri.file(packageRoot)); | 121 _packageRoot = _workingDirectoryUri.resolveUri(new Uri.file(packageRoot)); |
| 127 } | 122 } |
| 128 _logResolution('# Package root: $packageRoot -> $_packageRoot'); | 123 if (_logBuiltin) { |
| 124 _print('# Package root: $packageRoot -> $_packageRoot'); |
| 125 } |
| 129 } | 126 } |
| 130 | 127 |
| 131 | 128 |
| 132 String _resolveScriptUri(String scriptName) { | 129 String _resolveScriptUri(String scriptName) { |
| 133 if (_workingDirectoryUri == null) { | 130 if (_workingDirectoryUri == null) { |
| 134 throw 'No current working directory set.'; | 131 throw 'No current working directory set.'; |
| 135 } | 132 } |
| 136 scriptName = _sanitizeWindowsPath(scriptName); | 133 scriptName = _sanitizeWindowsPath(scriptName); |
| 137 | 134 |
| 138 var scriptUri = Uri.parse(scriptName); | 135 var scriptUri = Uri.parse(scriptName); |
| 139 if (scriptUri.scheme != '') { | 136 if (scriptUri.scheme != '') { |
| 140 // Script has a scheme, assume that it is fully formed. | 137 // Script has a scheme, assume that it is fully formed. |
| 141 _entryPointScript = scriptUri; | 138 _entryPointScript = scriptUri; |
| 142 } else { | 139 } else { |
| 143 // Script does not have a scheme, assume that it is a path, | 140 // Script does not have a scheme, assume that it is a path, |
| 144 // resolve it against the working directory. | 141 // resolve it against the working directory. |
| 145 _entryPointScript = _workingDirectoryUri.resolve(scriptName); | 142 _entryPointScript = _workingDirectoryUri.resolve(scriptName); |
| 146 } | 143 } |
| 147 _logResolution('# Resolved entry point to: $_entryPointScript'); | 144 if (_logBuiltin) { |
| 145 _print('# Resolved entry point to: $_entryPointScript'); |
| 146 } |
| 148 return _entryPointScript.toString(); | 147 return _entryPointScript.toString(); |
| 149 } | 148 } |
| 150 | 149 |
| 151 const _DART_EXT = 'dart-ext:'; | 150 const _DART_EXT = 'dart-ext:'; |
| 152 | 151 |
| 153 String _resolveUri(String base, String userString) { | 152 String _resolveUri(String base, String userString) { |
| 154 _logResolution('# Resolving: $userString from $base'); | 153 if (_logBuiltin) { |
| 154 _print('# Resolving: $userString from $base'); |
| 155 } |
| 155 var baseUri = Uri.parse(base); | 156 var baseUri = Uri.parse(base); |
| 156 if (userString.startsWith(_DART_EXT)) { | 157 if (userString.startsWith(_DART_EXT)) { |
| 157 var uri = userString.substring(_DART_EXT.length); | 158 var uri = userString.substring(_DART_EXT.length); |
| 158 return '$_DART_EXT${baseUri.resolve(uri)}'; | 159 return '$_DART_EXT${baseUri.resolve(uri)}'; |
| 159 } else { | 160 } else { |
| 160 return baseUri.resolve(userString).toString(); | 161 return baseUri.resolve(userString).toString(); |
| 161 } | 162 } |
| 162 } | 163 } |
| 163 | 164 |
| 164 | 165 |
| 165 // Returns either a file path or a URI starting with http:, as a String. | 166 // Returns either a file path or a URI starting with http:, as a String. |
| 166 String _filePathFromUri(String userUri) { | 167 String _filePathFromUri(String userUri) { |
| 167 var uri = Uri.parse(userUri); | 168 var uri = Uri.parse(userUri); |
| 168 _logResolution('# Getting file path from: $uri'); | 169 if (_logBuiltin) { |
| 170 _print('# Getting file path from: $uri'); |
| 171 } |
| 169 | 172 |
| 170 var path; | 173 var path; |
| 171 switch (uri.scheme) { | 174 switch (uri.scheme) { |
| 172 case '': | 175 case '': |
| 173 case 'file': | 176 case 'file': |
| 174 return uri.toFilePath(); | 177 return uri.toFilePath(); |
| 175 case 'package': | 178 case 'package': |
| 176 return _filePathFromPackageUri(uri); | 179 return _filePathFromPackageUri(uri); |
| 177 case 'http': | 180 case 'http': |
| 178 return uri.toString(); | 181 return uri.toString(); |
| 179 default: | 182 default: |
| 180 // Only handling file, http, and package URIs | 183 // Only handling file, http, and package URIs |
| 181 // in standalone binary. | 184 // in standalone binary. |
| 182 _logResolution('# Unknown scheme (${uri.scheme}) in $uri.'); | 185 if (_logBuiltin) { |
| 186 _print('# Unknown scheme (${uri.scheme}) in $uri.'); |
| 187 } |
| 183 throw 'Not a known scheme: $uri'; | 188 throw 'Not a known scheme: $uri'; |
| 184 } | 189 } |
| 185 } | 190 } |
| 186 | 191 |
| 187 | 192 |
| 188 String _filePathFromPackageUri(Uri uri) { | 193 String _filePathFromPackageUri(Uri uri) { |
| 189 if (!uri.host.isEmpty) { | 194 if (!uri.host.isEmpty) { |
| 190 var path = '${uri.host}${uri.path}'; | 195 var path = '${uri.host}${uri.path}'; |
| 191 var right = 'package:$path'; | 196 var right = 'package:$path'; |
| 192 var wrong = 'package://$path'; | 197 var wrong = 'package://$path'; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 void _loadScriptCallback(int tag, String uri, String libraryUri, List<int> data) | 254 void _loadScriptCallback(int tag, String uri, String libraryUri, List<int> data) |
| 250 native "Builtin_LoadScript"; | 255 native "Builtin_LoadScript"; |
| 251 | 256 |
| 252 void _loadScript(int tag, String uri, String libraryUri, List<int> data) { | 257 void _loadScript(int tag, String uri, String libraryUri, List<int> data) { |
| 253 // TODO: Currently a compilation error while loading the script is | 258 // TODO: Currently a compilation error while loading the script is |
| 254 // fatal for the isolate. _loadScriptCallback() does not return and | 259 // fatal for the isolate. _loadScriptCallback() does not return and |
| 255 // the _numOutstandingLoadRequests counter remains out of sync. | 260 // the _numOutstandingLoadRequests counter remains out of sync. |
| 256 _loadScriptCallback(tag, uri, libraryUri, data); | 261 _loadScriptCallback(tag, uri, libraryUri, data); |
| 257 assert(_numOutstandingLoadRequests > 0); | 262 assert(_numOutstandingLoadRequests > 0); |
| 258 _numOutstandingLoadRequests--; | 263 _numOutstandingLoadRequests--; |
| 259 _logResolution("native Builtin_LoadScript($uri) completed, " | 264 if (_logBuiltin) { |
| 260 "${_numOutstandingLoadRequests} requests remaining"); | 265 _print("native Builtin_LoadScript($uri) completed, " |
| 266 "${_numOutstandingLoadRequests} requests remaining"); |
| 267 } |
| 261 if (_numOutstandingLoadRequests == 0) { | 268 if (_numOutstandingLoadRequests == 0) { |
| 262 _signalDoneLoading(); | 269 _signalDoneLoading(); |
| 263 _cleanup(); | 270 _cleanup(); |
| 264 } | 271 } |
| 265 } | 272 } |
| 266 | 273 |
| 267 | 274 |
| 268 void _asyncLoadErrorCallback(uri, libraryUri, error) | 275 void _asyncLoadErrorCallback(uri, libraryUri, error) |
| 269 native "Builtin_AsyncLoadError"; | 276 native "Builtin_AsyncLoadError"; |
| 270 | 277 |
| 271 void _asyncLoadError(uri, libraryUri, error) { | 278 void _asyncLoadError(uri, libraryUri, error) { |
| 272 assert(_numOutstandingLoadRequests > 0); | 279 assert(_numOutstandingLoadRequests > 0); |
| 273 _logResolution("_asyncLoadError($uri), error: $error"); | 280 if (_logBuiltin) { |
| 281 _print("_asyncLoadError($uri), error: $error"); |
| 282 } |
| 274 _numOutstandingLoadRequests--; | 283 _numOutstandingLoadRequests--; |
| 275 _asyncLoadErrorCallback(uri, libraryUri, error); | 284 _asyncLoadErrorCallback(uri, libraryUri, error); |
| 276 if (_numOutstandingLoadRequests == 0) { | 285 if (_numOutstandingLoadRequests == 0) { |
| 277 _signalDoneLoading(); | 286 _signalDoneLoading(); |
| 278 _cleanup(); | 287 _cleanup(); |
| 279 } | 288 } |
| 280 } | 289 } |
| 281 | 290 |
| 282 | 291 |
| 283 // Create a Uri of 'userUri'. If the input uri is a package uri, then the | 292 // Create a Uri of 'userUri'. If the input uri is a package uri, then the |
| 284 // package uri is resolved. | 293 // package uri is resolved. |
| 285 Uri _createUri(String userUri) { | 294 Uri _createUri(String userUri) { |
| 286 var uri = Uri.parse(userUri); | 295 var uri = Uri.parse(userUri); |
| 287 _logResolution('# Creating uri for: $uri'); | 296 if (_logBuiltin) { |
| 297 _print('# Creating uri for: $uri'); |
| 298 } |
| 288 | 299 |
| 289 switch (uri.scheme) { | 300 switch (uri.scheme) { |
| 290 case '': | 301 case '': |
| 291 case 'file': | 302 case 'file': |
| 292 case 'http': | 303 case 'http': |
| 293 return uri; | 304 return uri; |
| 294 case 'package': | 305 case 'package': |
| 295 return Uri.parse(_filePathFromPackageUri(uri)); | 306 return Uri.parse(_filePathFromPackageUri(uri)); |
| 296 default: | 307 default: |
| 297 // Only handling file, http, and package URIs | 308 // Only handling file, http, and package URIs |
| 298 // in standalone binary. | 309 // in standalone binary. |
| 299 _logResolution('# Unknown scheme (${uri.scheme}) in $uri.'); | 310 _logResolution('# Unknown scheme (${uri.scheme}) in $uri.'); |
| 300 throw 'Not a known scheme: $uri'; | 311 throw 'Not a known scheme: $uri'; |
| 301 } | 312 } |
| 302 } | 313 } |
| 303 | 314 |
| 304 | 315 |
| 305 // Asynchronously loads script data through a http or file uri. | 316 // Asynchronously loads script data through a http or file uri. |
| 306 _loadDataAsync(int tag, String uri, String libraryUri) { | 317 _loadDataAsync(int tag, String uri, String libraryUri) { |
| 307 if (tag == null) { | 318 if (tag == null) { |
| 308 uri = _resolveScriptUri(uri); | 319 uri = _resolveScriptUri(uri); |
| 309 } | 320 } |
| 310 Uri resourceUri = _createUri(uri); | 321 Uri resourceUri = _createUri(uri); |
| 311 _numOutstandingLoadRequests++; | 322 _numOutstandingLoadRequests++; |
| 312 _logResolution("_loadDataAsync($uri), " | 323 if (_logBuiltin) { |
| 313 "${_numOutstandingLoadRequests} requests outstanding"); | 324 _print("_loadDataAsync($uri), " |
| 325 "${_numOutstandingLoadRequests} requests outstanding"); |
| 326 } |
| 314 if (resourceUri.scheme == 'http') { | 327 if (resourceUri.scheme == 'http') { |
| 315 _httpGet(resourceUri, libraryUri, (data) { | 328 _httpGet(resourceUri, libraryUri, (data) { |
| 316 _loadScript(tag, uri, libraryUri, data); | 329 _loadScript(tag, uri, libraryUri, data); |
| 317 }); | 330 }); |
| 318 } else { | 331 } else { |
| 319 var sourceFile = new File(resourceUri.toFilePath()); | 332 var sourceFile = new File(resourceUri.toFilePath()); |
| 320 sourceFile.readAsBytes().then((data) { | 333 sourceFile.readAsBytes().then((data) { |
| 321 _loadScript(tag, uri, libraryUri, data); | 334 _loadScript(tag, uri, libraryUri, data); |
| 322 }, | 335 }, |
| 323 onError: (e) { | 336 onError: (e) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 | 370 |
| 358 path = _filePathFromUri(path); | 371 path = _filePathFromUri(path); |
| 359 | 372 |
| 360 if (Platform.isLinux || Platform.isAndroid) { | 373 if (Platform.isLinux || Platform.isAndroid) { |
| 361 filename = 'lib$name.so'; | 374 filename = 'lib$name.so'; |
| 362 } else if (Platform.isMacOS) { | 375 } else if (Platform.isMacOS) { |
| 363 filename = 'lib$name.dylib'; | 376 filename = 'lib$name.dylib'; |
| 364 } else if (Platform.isWindows) { | 377 } else if (Platform.isWindows) { |
| 365 filename = '$name.dll'; | 378 filename = '$name.dll'; |
| 366 } else { | 379 } else { |
| 367 _logResolution( | 380 if (_logBuiltin) { |
| 368 'Native extensions not supported on ${Platform.operatingSystem}'); | 381 _print('Native extensions not supported on ${Platform.operatingSystem}'); |
| 382 } |
| 369 throw 'Native extensions not supported on ${Platform.operatingSystem}'; | 383 throw 'Native extensions not supported on ${Platform.operatingSystem}'; |
| 370 } | 384 } |
| 371 | 385 |
| 372 return [path, filename, name]; | 386 return [path, filename, name]; |
| 373 } | 387 } |
| OLD | NEW |