| 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 #include "bin/dartutils.h" | 5 #include "bin/dartutils.h" |
| 6 | 6 |
| 7 #include "bin/extensions.h" |
| 8 #include "bin/directory.h" |
| 7 #include "bin/file.h" | 9 #include "bin/file.h" |
| 8 #include "include/dart_api.h" | 10 #include "include/dart_api.h" |
| 9 #include "platform/assert.h" | 11 #include "platform/assert.h" |
| 10 #include "platform/globals.h" | 12 #include "platform/globals.h" |
| 11 | 13 |
| 14 const char* DartUtils::original_working_directory = NULL; |
| 12 const char* DartUtils::kDartScheme = "dart:"; | 15 const char* DartUtils::kDartScheme = "dart:"; |
| 13 const char* DartUtils::kDartExtensionScheme = "dart-ext:"; | 16 const char* DartUtils::kDartExtensionScheme = "dart-ext:"; |
| 14 const char* DartUtils::kBuiltinLibURL = "dart:builtin"; | 17 const char* DartUtils::kBuiltinLibURL = "dart:builtin"; |
| 15 const char* DartUtils::kCoreLibURL = "dart:core"; | 18 const char* DartUtils::kCoreLibURL = "dart:core"; |
| 16 const char* DartUtils::kCoreImplLibURL = "dart:coreimpl"; | 19 const char* DartUtils::kCoreImplLibURL = "dart:coreimpl"; |
| 17 const char* DartUtils::kCryptoLibURL = "dart:crypto"; | 20 const char* DartUtils::kCryptoLibURL = "dart:crypto"; |
| 18 const char* DartUtils::kIOLibURL = "dart:io"; | 21 const char* DartUtils::kIOLibURL = "dart:io"; |
| 19 const char* DartUtils::kJsonLibURL = "dart:json"; | 22 const char* DartUtils::kJsonLibURL = "dart:json"; |
| 20 const char* DartUtils::kUriLibURL = "dart:uri"; | 23 const char* DartUtils::kUriLibURL = "dart:uri"; |
| 21 const char* DartUtils::kUtfLibURL = "dart:utf"; | 24 const char* DartUtils::kUtfLibURL = "dart:utf"; |
| 22 const char* DartUtils::kIsolateLibURL = "dart:isolate"; | 25 const char* DartUtils::kIsolateLibURL = "dart:isolate"; |
| 23 const char* DartUtils::kWebLibURL = "dart:web"; | 26 const char* DartUtils::kWebLibURL = "dart:web"; |
| 24 | 27 |
| 25 | 28 |
| 26 const char* DartUtils::kIdFieldName = "_id"; | 29 const char* DartUtils::kIdFieldName = "_id"; |
| 27 | 30 |
| 28 | 31 |
| 32 static bool IsWindowsHost() { |
| 33 #if defined(TARGET_OS_WINDOWS) |
| 34 return true; |
| 35 #else // defined(TARGET_OS_WINDOWS) |
| 36 return false; |
| 37 #endif // defined(TARGET_OS_WINDOWS) |
| 38 } |
| 39 |
| 40 |
| 29 static const char* MapLibraryUrl(CommandLineOptions* url_mapping, | 41 static const char* MapLibraryUrl(CommandLineOptions* url_mapping, |
| 30 const char* url_string) { | 42 const char* url_string) { |
| 31 ASSERT(url_mapping != NULL); | 43 ASSERT(url_mapping != NULL); |
| 32 // We need to check if the passed in url is found in the url_mapping array, | 44 // We need to check if the passed in url is found in the url_mapping array, |
| 33 // in that case use the mapped entry. | 45 // in that case use the mapped entry. |
| 34 int len = strlen(url_string); | 46 int len = strlen(url_string); |
| 35 for (int idx = 0; idx < url_mapping->count(); idx++) { | 47 for (int idx = 0; idx < url_mapping->count(); idx++) { |
| 36 const char* url_name = url_mapping->GetArgument(idx); | 48 const char* url_name = url_mapping->GetArgument(idx); |
| 37 if (!strncmp(url_string, url_name, len) && (url_name[len] == ',')) { | 49 if (!strncmp(url_string, url_name, len) && (url_name[len] == ',')) { |
| 38 const char* url_mapped_name = url_name + len + 1; | 50 const char* url_mapped_name = url_name + len + 1; |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 return Dart_Error("Unable to fully read contents"); | 225 return Dart_Error("Unable to fully read contents"); |
| 214 } | 226 } |
| 215 text_buffer[len] = '\0'; | 227 text_buffer[len] = '\0'; |
| 216 delete file; | 228 delete file; |
| 217 Dart_Handle str = Dart_NewString(text_buffer); | 229 Dart_Handle str = Dart_NewString(text_buffer); |
| 218 free(text_buffer); | 230 free(text_buffer); |
| 219 return str; | 231 return str; |
| 220 } | 232 } |
| 221 | 233 |
| 222 | 234 |
| 235 static Dart_Handle ResolveScriptUri(Dart_Handle script_uri, |
| 236 Dart_Handle builtin_lib) { |
| 237 const int kNumArgs = 3; |
| 238 Dart_Handle dart_args[kNumArgs]; |
| 239 dart_args[0] = Dart_NewString(DartUtils::original_working_directory); |
| 240 dart_args[1] = script_uri; |
| 241 dart_args[2] = (IsWindowsHost() ? Dart_True() : Dart_False()); |
| 242 return Dart_Invoke( |
| 243 builtin_lib, Dart_NewString("_resolveScriptUri"), kNumArgs, dart_args); |
| 244 } |
| 245 |
| 246 |
| 247 static Dart_Handle FilePathFromUri(Dart_Handle script_uri, |
| 248 Dart_Handle builtin_lib) { |
| 249 const int kNumArgs = 2; |
| 250 Dart_Handle dart_args[kNumArgs]; |
| 251 dart_args[0] = script_uri; |
| 252 dart_args[1] = (IsWindowsHost() ? Dart_True() : Dart_False()); |
| 253 Dart_Handle script_path = Dart_Invoke( |
| 254 builtin_lib, Dart_NewString("_filePathFromUri"), kNumArgs, dart_args); |
| 255 return script_path; |
| 256 } |
| 257 |
| 258 |
| 259 Dart_Handle DartUtils::LibraryTagHandler(Dart_LibraryTag tag, |
| 260 Dart_Handle library, |
| 261 Dart_Handle url) { |
| 262 if (!Dart_IsLibrary(library)) { |
| 263 return Dart_Error("not a library"); |
| 264 } |
| 265 if (!Dart_IsString8(url)) { |
| 266 return Dart_Error("url is not a string"); |
| 267 } |
| 268 const char* url_string = NULL; |
| 269 Dart_Handle result = Dart_StringToCString(url, &url_string); |
| 270 if (Dart_IsError(result)) { |
| 271 return result; |
| 272 } |
| 273 bool is_dart_scheme_url = DartUtils::IsDartSchemeURL(url_string); |
| 274 bool is_dart_extension_url = DartUtils::IsDartExtensionSchemeURL(url_string); |
| 275 if (tag == kCanonicalizeUrl) { |
| 276 // If this is a Dart Scheme URL then it is not modified as it will be |
| 277 // handled by the VM internally. |
| 278 if (is_dart_scheme_url) { |
| 279 return url; |
| 280 } |
| 281 // Resolve the url within the context of the library's URL. |
| 282 Dart_Handle builtin_lib = Builtin::LoadLibrary(Builtin::kBuiltinLibrary); |
| 283 Dart_Handle library_url = Dart_LibraryUrl(library); |
| 284 if (Dart_IsError(library_url)) { |
| 285 return library_url; |
| 286 } |
| 287 const int kNumArgs = 2; |
| 288 Dart_Handle dart_args[kNumArgs]; |
| 289 dart_args[0] = library_url; |
| 290 dart_args[1] = url; |
| 291 return Dart_Invoke( |
| 292 builtin_lib, Dart_NewString("_resolveUri"), kNumArgs, dart_args); |
| 293 } |
| 294 if (is_dart_scheme_url) { |
| 295 ASSERT(tag == kImportTag); |
| 296 // Handle imports of other built-in libraries present in the SDK. |
| 297 Builtin::BuiltinLibraryId id; |
| 298 if (DartUtils::IsDartCryptoLibURL(url_string)) { |
| 299 id = Builtin::kCryptoLibrary; |
| 300 } else if (DartUtils::IsDartIOLibURL(url_string)) { |
| 301 id = Builtin::kIOLibrary; |
| 302 } else if (DartUtils::IsDartJsonLibURL(url_string)) { |
| 303 id = Builtin::kJsonLibrary; |
| 304 } else if (DartUtils::IsDartUriLibURL(url_string)) { |
| 305 id = Builtin::kUriLibrary; |
| 306 } else if (DartUtils::IsDartUtfLibURL(url_string)) { |
| 307 id = Builtin::kUtfLibrary; |
| 308 } else if (DartUtils::IsDartWebLibURL(url_string)) { |
| 309 id = Builtin::kWebLibrary; |
| 310 } else { |
| 311 return Dart_Error("Do not know how to load '%s'", url_string); |
| 312 } |
| 313 return Builtin::LoadLibrary(id); |
| 314 } else { |
| 315 // Get the file path out of the url. |
| 316 Dart_Handle builtin_lib = Builtin::LoadLibrary(Builtin::kBuiltinLibrary); |
| 317 Dart_Handle file_path = FilePathFromUri(url, builtin_lib); |
| 318 if (Dart_IsError(file_path)) { |
| 319 return file_path; |
| 320 } |
| 321 Dart_StringToCString(file_path, &url_string); |
| 322 } |
| 323 if (is_dart_extension_url) { |
| 324 if (tag != kImportTag) { |
| 325 return Dart_Error("Dart extensions must use import: '%s'", url_string); |
| 326 } |
| 327 return Extensions::LoadExtension(url_string, library); |
| 328 } |
| 329 result = DartUtils::LoadSource(NULL, |
| 330 library, |
| 331 url, |
| 332 tag, |
| 333 url_string); |
| 334 if (!Dart_IsError(result) && (tag == kImportTag)) { |
| 335 Builtin::ImportLibrary(result, Builtin::kBuiltinLibrary); |
| 336 } |
| 337 return result; |
| 338 } |
| 339 |
| 340 |
| 341 static Dart_Handle ReadSource(Dart_Handle script_uri, |
| 342 Dart_Handle builtin_lib) { |
| 343 Dart_Handle script_path = FilePathFromUri(script_uri, builtin_lib); |
| 344 if (Dart_IsError(script_path)) { |
| 345 return script_path; |
| 346 } |
| 347 const char* script_path_cstr; |
| 348 Dart_StringToCString(script_path, &script_path_cstr); |
| 349 Dart_Handle source = DartUtils::ReadStringFromFile(script_path_cstr); |
| 350 return source; |
| 351 } |
| 352 |
| 353 |
| 354 Dart_Handle DartUtils::LoadScript(const char* script_uri, |
| 355 bool resolve_script, |
| 356 Dart_Handle builtin_lib) { |
| 357 Dart_Handle resolved_script_uri; |
| 358 if (resolve_script) { |
| 359 resolved_script_uri = ResolveScriptUri(Dart_NewString(script_uri), |
| 360 builtin_lib); |
| 361 if (Dart_IsError(resolved_script_uri)) { |
| 362 return resolved_script_uri; |
| 363 } |
| 364 } else { |
| 365 resolved_script_uri = Dart_NewString(script_uri); |
| 366 } |
| 367 Dart_Handle source = ReadSource(resolved_script_uri, builtin_lib); |
| 368 if (Dart_IsError(source)) { |
| 369 return source; |
| 370 } |
| 371 return Dart_LoadScript(resolved_script_uri, source); |
| 372 } |
| 373 |
| 374 |
| 223 Dart_Handle DartUtils::LoadSource(CommandLineOptions* url_mapping, | 375 Dart_Handle DartUtils::LoadSource(CommandLineOptions* url_mapping, |
| 224 Dart_Handle library, | 376 Dart_Handle library, |
| 225 Dart_Handle url, | 377 Dart_Handle url, |
| 226 Dart_LibraryTag tag, | 378 Dart_LibraryTag tag, |
| 227 const char* url_string) { | 379 const char* url_string) { |
| 228 if (url_mapping != NULL && IsDartSchemeURL(url_string)) { | 380 if (url_mapping != NULL && IsDartSchemeURL(url_string)) { |
| 229 const char* mapped_url_string = MapLibraryUrl(url_mapping, url_string); | 381 const char* mapped_url_string = MapLibraryUrl(url_mapping, url_string); |
| 230 if (mapped_url_string == NULL) { | 382 if (mapped_url_string == NULL) { |
| 231 return Dart_Error("Do not know how to load %s", url_string); | 383 return Dart_Error("Do not know how to load %s", url_string); |
| 232 } | 384 } |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 Dart_Handle args[2]; | 476 Dart_Handle args[2]; |
| 325 args[0] = Dart_NewString(os_error->message()); | 477 args[0] = Dart_NewString(os_error->message()); |
| 326 if (Dart_IsError(args[0])) return args[0]; | 478 if (Dart_IsError(args[0])) return args[0]; |
| 327 args[1] = Dart_NewInteger(os_error->code()); | 479 args[1] = Dart_NewInteger(os_error->code()); |
| 328 if (Dart_IsError(args[1])) return args[1]; | 480 if (Dart_IsError(args[1])) return args[1]; |
| 329 Dart_Handle err = Dart_New(clazz, Dart_Null(), 2, args); | 481 Dart_Handle err = Dart_New(clazz, Dart_Null(), 2, args); |
| 330 return err; | 482 return err; |
| 331 } | 483 } |
| 332 | 484 |
| 333 | 485 |
| 486 void DartUtils::SetOriginalWorkingDirectory() { |
| 487 original_working_directory = Directory::Current(); |
| 488 } |
| 489 |
| 490 |
| 334 // Statically allocated Dart_CObject instances for immutable | 491 // Statically allocated Dart_CObject instances for immutable |
| 335 // objects. As these will be used by different threads the use of | 492 // objects. As these will be used by different threads the use of |
| 336 // these depends on the fact that the marking internally in the | 493 // these depends on the fact that the marking internally in the |
| 337 // Dart_CObject structure is not marking simple value objects. | 494 // Dart_CObject structure is not marking simple value objects. |
| 338 Dart_CObject CObject::api_null_ = { Dart_CObject::kNull , { 0 } }; | 495 Dart_CObject CObject::api_null_ = { Dart_CObject::kNull , { 0 } }; |
| 339 Dart_CObject CObject::api_true_ = { Dart_CObject::kBool , { true } }; | 496 Dart_CObject CObject::api_true_ = { Dart_CObject::kBool , { true } }; |
| 340 Dart_CObject CObject::api_false_ = { Dart_CObject::kBool, { false } }; | 497 Dart_CObject CObject::api_false_ = { Dart_CObject::kBool, { false } }; |
| 341 CObject CObject::null_ = CObject(&api_null_); | 498 CObject CObject::null_ = CObject(&api_null_); |
| 342 CObject CObject::true_ = CObject(&api_true_); | 499 CObject CObject::true_ = CObject(&api_true_); |
| 343 CObject CObject::false_ = CObject(&api_false_); | 500 CObject CObject::false_ = CObject(&api_false_); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 | 629 |
| 473 CObject* CObject::NewOSError(OSError* os_error) { | 630 CObject* CObject::NewOSError(OSError* os_error) { |
| 474 CObject* error_message = | 631 CObject* error_message = |
| 475 new CObjectString(CObject::NewString(os_error->message())); | 632 new CObjectString(CObject::NewString(os_error->message())); |
| 476 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 633 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
| 477 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 634 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
| 478 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 635 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
| 479 result->SetAt(2, error_message); | 636 result->SetAt(2, error_message); |
| 480 return result; | 637 return result; |
| 481 } | 638 } |
| OLD | NEW |