| 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 "include/dart_api.h" | 7 #include "include/dart_api.h" |
| 8 #include "include/dart_native_api.h" | 8 #include "include/dart_native_api.h" |
| 9 | 9 |
| 10 #include "platform/assert.h" | 10 #include "platform/assert.h" |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 return (strcmp(url_name, kBuiltinLibURL) == 0); | 169 return (strcmp(url_name, kBuiltinLibURL) == 0); |
| 170 } | 170 } |
| 171 | 171 |
| 172 | 172 |
| 173 Dart_Handle DartUtils::CanonicalizeURL(CommandLineOptions* url_mapping, | 173 Dart_Handle DartUtils::CanonicalizeURL(CommandLineOptions* url_mapping, |
| 174 Dart_Handle library, | 174 Dart_Handle library, |
| 175 const char* url_str) { | 175 const char* url_str) { |
| 176 // Get the url of the including library. | 176 // Get the url of the including library. |
| 177 Dart_Handle library_url = Dart_LibraryUrl(library); | 177 Dart_Handle library_url = Dart_LibraryUrl(library); |
| 178 if (Dart_IsError(library_url)) { | 178 if (Dart_IsError(library_url)) { |
| 179 return Dart_Error("accessing library url failed"); | 179 return Dart_NewApiError("accessing library url failed"); |
| 180 } | 180 } |
| 181 if (!Dart_IsString(library_url)) { | 181 if (!Dart_IsString(library_url)) { |
| 182 return Dart_Error("library url is not a string"); | 182 return Dart_NewApiError("library url is not a string"); |
| 183 } | 183 } |
| 184 const char* library_url_str = NULL; | 184 const char* library_url_str = NULL; |
| 185 Dart_Handle result = Dart_StringToCString(library_url, &library_url_str); | 185 Dart_Handle result = Dart_StringToCString(library_url, &library_url_str); |
| 186 if (Dart_IsError(result)) { | 186 if (Dart_IsError(result)) { |
| 187 return Dart_Error("accessing library url characters failed"); | 187 return Dart_NewApiError("accessing library url characters failed"); |
| 188 } | 188 } |
| 189 if (url_mapping != NULL) { | 189 if (url_mapping != NULL) { |
| 190 const char* mapped_library_url_str = MapLibraryUrl(url_mapping, | 190 const char* mapped_library_url_str = MapLibraryUrl(url_mapping, |
| 191 library_url_str); | 191 library_url_str); |
| 192 if (mapped_library_url_str != NULL) { | 192 if (mapped_library_url_str != NULL) { |
| 193 library_url_str = mapped_library_url_str; | 193 library_url_str = mapped_library_url_str; |
| 194 } | 194 } |
| 195 } | 195 } |
| 196 // Calculate the canonical path. | 196 // Calculate the canonical path. |
| 197 const char* canon_url_str = GetCanonicalPath(library_url_str, url_str); | 197 const char* canon_url_str = GetCanonicalPath(library_url_str, url_str); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 // Return error. | 285 // Return error. |
| 286 Dart_Handle responseStatus = | 286 Dart_Handle responseStatus = |
| 287 Dart_Invoke(builtin_lib, | 287 Dart_Invoke(builtin_lib, |
| 288 DartUtils::NewString("_getHttpRequestStatusString"), | 288 DartUtils::NewString("_getHttpRequestStatusString"), |
| 289 0, | 289 0, |
| 290 NULL); | 290 NULL); |
| 291 if (Dart_IsError(responseStatus)) { | 291 if (Dart_IsError(responseStatus)) { |
| 292 return responseStatus; | 292 return responseStatus; |
| 293 } | 293 } |
| 294 if (Dart_IsNull(responseStatus)) { | 294 if (Dart_IsNull(responseStatus)) { |
| 295 return Dart_Error("HTTP error."); | 295 return Dart_NewApiError("HTTP error."); |
| 296 } | 296 } |
| 297 return Dart_Error(DartUtils::GetStringValue(responseStatus)); | 297 return Dart_NewApiError(DartUtils::GetStringValue(responseStatus)); |
| 298 } | 298 } |
| 299 Dart_Handle response = | 299 Dart_Handle response = |
| 300 Dart_Invoke(builtin_lib, DartUtils::NewString("_getHttpRequestResponse"), | 300 Dart_Invoke(builtin_lib, DartUtils::NewString("_getHttpRequestResponse"), |
| 301 0, NULL); | 301 0, NULL); |
| 302 if (Dart_IsError(response)) { | 302 if (Dart_IsError(response)) { |
| 303 return response; | 303 return response; |
| 304 } | 304 } |
| 305 if (Dart_IsString(response)) { | 305 if (Dart_IsString(response)) { |
| 306 // Received response as string. | 306 // Received response as string. |
| 307 uint8_t* responseString = NULL; | 307 uint8_t* responseString = NULL; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 DartUtils::CloseFile(stream); | 379 DartUtils::CloseFile(stream); |
| 380 return text_buffer; | 380 return text_buffer; |
| 381 } | 381 } |
| 382 | 382 |
| 383 | 383 |
| 384 Dart_Handle DartUtils::ReadStringFromFile(const char* filename) { | 384 Dart_Handle DartUtils::ReadStringFromFile(const char* filename) { |
| 385 const char* error_msg = NULL; | 385 const char* error_msg = NULL; |
| 386 intptr_t len; | 386 intptr_t len; |
| 387 const uint8_t* text_buffer = ReadFileFully(filename, &len, &error_msg); | 387 const uint8_t* text_buffer = ReadFileFully(filename, &len, &error_msg); |
| 388 if (text_buffer == NULL) { | 388 if (text_buffer == NULL) { |
| 389 return Dart_Error(error_msg); | 389 return Dart_NewApiError(error_msg); |
| 390 } | 390 } |
| 391 Dart_Handle str = Dart_NewStringFromUTF8(text_buffer, len); | 391 Dart_Handle str = Dart_NewStringFromUTF8(text_buffer, len); |
| 392 free(const_cast<uint8_t *>(text_buffer)); | 392 free(const_cast<uint8_t *>(text_buffer)); |
| 393 return str; | 393 return str; |
| 394 } | 394 } |
| 395 | 395 |
| 396 | 396 |
| 397 Dart_Handle DartUtils::SetWorkingDirectory(Dart_Handle builtin_lib) { | 397 Dart_Handle DartUtils::SetWorkingDirectory(Dart_Handle builtin_lib) { |
| 398 Dart_Handle directory = NewString(original_working_directory); | 398 Dart_Handle directory = NewString(original_working_directory); |
| 399 return SingleArgDart_Invoke(builtin_lib, "_setWorkingDirectory", directory); | 399 return SingleArgDart_Invoke(builtin_lib, "_setWorkingDirectory", directory); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 dart_args[1] = url; | 433 dart_args[1] = url; |
| 434 return Dart_Invoke( | 434 return Dart_Invoke( |
| 435 builtin_lib, NewString("_resolveUri"), kNumArgs, dart_args); | 435 builtin_lib, NewString("_resolveUri"), kNumArgs, dart_args); |
| 436 } | 436 } |
| 437 | 437 |
| 438 | 438 |
| 439 Dart_Handle DartUtils::LibraryTagHandler(Dart_LibraryTag tag, | 439 Dart_Handle DartUtils::LibraryTagHandler(Dart_LibraryTag tag, |
| 440 Dart_Handle library, | 440 Dart_Handle library, |
| 441 Dart_Handle url) { | 441 Dart_Handle url) { |
| 442 if (!Dart_IsLibrary(library)) { | 442 if (!Dart_IsLibrary(library)) { |
| 443 return Dart_Error("not a library"); | 443 return Dart_NewApiError("not a library"); |
| 444 } | 444 } |
| 445 if (!Dart_IsString(url)) { | 445 if (!Dart_IsString(url)) { |
| 446 return Dart_Error("url is not a string"); | 446 return Dart_NewApiError("url is not a string"); |
| 447 } | 447 } |
| 448 const char* url_string = NULL; | 448 const char* url_string = NULL; |
| 449 Dart_Handle result = Dart_StringToCString(url, &url_string); | 449 Dart_Handle result = Dart_StringToCString(url, &url_string); |
| 450 if (Dart_IsError(result)) { | 450 if (Dart_IsError(result)) { |
| 451 return result; | 451 return result; |
| 452 } | 452 } |
| 453 Dart_Handle library_url = Dart_LibraryUrl(library); | 453 Dart_Handle library_url = Dart_LibraryUrl(library); |
| 454 const char* library_url_string = NULL; | 454 const char* library_url_string = NULL; |
| 455 result = Dart_StringToCString(library_url, &library_url_string); | 455 result = Dart_StringToCString(library_url, &library_url_string); |
| 456 if (Dart_IsError(result)) { | 456 if (Dart_IsError(result)) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 478 return ResolveUri(library_url, url, builtin_lib); | 478 return ResolveUri(library_url, url, builtin_lib); |
| 479 } | 479 } |
| 480 | 480 |
| 481 // Handle 'import' of dart scheme URIs (i.e they start with 'dart:'). | 481 // Handle 'import' of dart scheme URIs (i.e they start with 'dart:'). |
| 482 if (is_dart_scheme_url) { | 482 if (is_dart_scheme_url) { |
| 483 if (tag == Dart_kImportTag) { | 483 if (tag == Dart_kImportTag) { |
| 484 // Handle imports of other built-in libraries present in the SDK. | 484 // Handle imports of other built-in libraries present in the SDK. |
| 485 if (DartUtils::IsDartIOLibURL(url_string)) { | 485 if (DartUtils::IsDartIOLibURL(url_string)) { |
| 486 return Builtin::LoadAndCheckLibrary(Builtin::kIOLibrary); | 486 return Builtin::LoadAndCheckLibrary(Builtin::kIOLibrary); |
| 487 } | 487 } |
| 488 return Dart_Error("The built-in library '%s' is not available" | 488 return NewError("The built-in library '%s' is not available" |
| 489 " on the stand-alone VM.\n", url_string); | 489 " on the stand-alone VM.\n", url_string); |
| 490 } else { | 490 } else { |
| 491 ASSERT(tag == Dart_kSourceTag); | 491 ASSERT(tag == Dart_kSourceTag); |
| 492 return Dart_Error("Unable to load source '%s' ", url_string); | 492 return NewError("Unable to load source '%s' ", url_string); |
| 493 } | 493 } |
| 494 } | 494 } |
| 495 | 495 |
| 496 // Handle 'part' of IO library. | 496 // Handle 'part' of IO library. |
| 497 if (is_io_library) { | 497 if (is_io_library) { |
| 498 if (tag == Dart_kSourceTag) { | 498 if (tag == Dart_kSourceTag) { |
| 499 return Dart_LoadSource( | 499 return Dart_LoadSource( |
| 500 library, url, Builtin::PartSource(Builtin::kIOLibrary, url_string)); | 500 library, url, Builtin::PartSource(Builtin::kIOLibrary, url_string)); |
| 501 } else { | 501 } else { |
| 502 ASSERT(tag == Dart_kImportTag); | 502 ASSERT(tag == Dart_kImportTag); |
| 503 return Dart_Error("Unable to import '%s' ", url_string); | 503 return NewError("Unable to import '%s' ", url_string); |
| 504 } | 504 } |
| 505 } | 505 } |
| 506 | 506 |
| 507 // Handle 'import' or 'part' requests for all other URIs. | 507 // Handle 'import' or 'part' requests for all other URIs. |
| 508 // Get the file path out of the url. | 508 // Get the file path out of the url. |
| 509 Dart_Handle builtin_lib = | 509 Dart_Handle builtin_lib = |
| 510 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | 510 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
| 511 Dart_Handle file_path = FilePathFromUri(url, builtin_lib); | 511 Dart_Handle file_path = FilePathFromUri(url, builtin_lib); |
| 512 if (Dart_IsError(file_path)) { | 512 if (Dart_IsError(file_path)) { |
| 513 return file_path; | 513 return file_path; |
| 514 } | 514 } |
| 515 Dart_StringToCString(file_path, &url_string); | 515 Dart_StringToCString(file_path, &url_string); |
| 516 if (is_dart_extension_url) { | 516 if (is_dart_extension_url) { |
| 517 if (tag != Dart_kImportTag) { | 517 if (tag != Dart_kImportTag) { |
| 518 return Dart_Error("Dart extensions must use import: '%s'", url_string); | 518 return NewError("Dart extensions must use import: '%s'", url_string); |
| 519 } | 519 } |
| 520 return Extensions::LoadExtension(url_string, library); | 520 return Extensions::LoadExtension(url_string, library); |
| 521 } | 521 } |
| 522 result = DartUtils::LoadSource(NULL, | 522 result = DartUtils::LoadSource(NULL, |
| 523 library, | 523 library, |
| 524 url, | 524 url, |
| 525 tag, | 525 tag, |
| 526 url_string); | 526 url_string); |
| 527 return result; | 527 return result; |
| 528 } | 528 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 593 return script_path; | 593 return script_path; |
| 594 } | 594 } |
| 595 const char* script_path_cstr; | 595 const char* script_path_cstr; |
| 596 Dart_StringToCString(script_path, &script_path_cstr); | 596 Dart_StringToCString(script_path, &script_path_cstr); |
| 597 const char* error_msg = NULL; | 597 const char* error_msg = NULL; |
| 598 intptr_t len; | 598 intptr_t len; |
| 599 const uint8_t* buffer = ReadFileFully(script_path_cstr, | 599 const uint8_t* buffer = ReadFileFully(script_path_cstr, |
| 600 &len, | 600 &len, |
| 601 &error_msg); | 601 &error_msg); |
| 602 if (buffer == NULL) { | 602 if (buffer == NULL) { |
| 603 return Dart_Error(error_msg); | 603 return Dart_NewApiError(error_msg); |
| 604 } | 604 } |
| 605 bool is_snapshot = false; | 605 bool is_snapshot = false; |
| 606 const uint8_t *payload = SniffForMagicNumber(buffer, &len, &is_snapshot); | 606 const uint8_t *payload = SniffForMagicNumber(buffer, &len, &is_snapshot); |
| 607 Dart_Handle returnValue; | 607 Dart_Handle returnValue; |
| 608 if (is_snapshot) { | 608 if (is_snapshot) { |
| 609 returnValue = Dart_LoadScriptFromSnapshot(payload, len); | 609 returnValue = Dart_LoadScriptFromSnapshot(payload, len); |
| 610 } else { | 610 } else { |
| 611 Dart_Handle source = Dart_NewStringFromUTF8(buffer, len); | 611 Dart_Handle source = Dart_NewStringFromUTF8(buffer, len); |
| 612 if (Dart_IsError(source)) { | 612 if (Dart_IsError(source)) { |
| 613 returnValue = source; | 613 returnValue = source; |
| 614 } else { | 614 } else { |
| 615 returnValue = Dart_LoadScript(resolved_script_uri, source, 0, 0); | 615 returnValue = Dart_LoadScript(resolved_script_uri, source, 0, 0); |
| 616 } | 616 } |
| 617 } | 617 } |
| 618 free(const_cast<uint8_t *>(buffer)); | 618 free(const_cast<uint8_t *>(buffer)); |
| 619 return returnValue; | 619 return returnValue; |
| 620 } | 620 } |
| 621 | 621 |
| 622 | 622 |
| 623 Dart_Handle DartUtils::LoadSource(CommandLineOptions* url_mapping, | 623 Dart_Handle DartUtils::LoadSource(CommandLineOptions* url_mapping, |
| 624 Dart_Handle library, | 624 Dart_Handle library, |
| 625 Dart_Handle url, | 625 Dart_Handle url, |
| 626 Dart_LibraryTag tag, | 626 Dart_LibraryTag tag, |
| 627 const char* url_string) { | 627 const char* url_string) { |
| 628 bool is_http_scheme_url = DartUtils::IsHttpSchemeURL(url_string); | 628 bool is_http_scheme_url = DartUtils::IsHttpSchemeURL(url_string); |
| 629 if (url_mapping != NULL && IsDartSchemeURL(url_string)) { | 629 if (url_mapping != NULL && IsDartSchemeURL(url_string)) { |
| 630 const char* mapped_url_string = MapLibraryUrl(url_mapping, url_string); | 630 const char* mapped_url_string = MapLibraryUrl(url_mapping, url_string); |
| 631 if (mapped_url_string == NULL) { | 631 if (mapped_url_string == NULL) { |
| 632 return Dart_Error("Do not know how to load %s", url_string); | 632 return NewError("Do not know how to load %s", url_string); |
| 633 } | 633 } |
| 634 // We have a URL mapping specified, just read the file that the | 634 // We have a URL mapping specified, just read the file that the |
| 635 // URL mapping specifies and load it. | 635 // URL mapping specifies and load it. |
| 636 url_string = mapped_url_string; | 636 url_string = mapped_url_string; |
| 637 } | 637 } |
| 638 Dart_Handle source; | 638 Dart_Handle source; |
| 639 if (is_http_scheme_url) { | 639 if (is_http_scheme_url) { |
| 640 // Read the file over http. | 640 // Read the file over http. |
| 641 source = DartUtils::ReadStringFromHttp(url_string); | 641 source = DartUtils::ReadStringFromHttp(url_string); |
| 642 } else { | 642 } else { |
| 643 // Read the file. | 643 // Read the file. |
| 644 source = DartUtils::ReadStringFromFile(url_string); | 644 source = DartUtils::ReadStringFromFile(url_string); |
| 645 } | 645 } |
| 646 if (Dart_IsError(source)) { | 646 if (Dart_IsError(source)) { |
| 647 return source; // source contains the error string. | 647 return source; // source contains the error string. |
| 648 } | 648 } |
| 649 // The tag is either an import or a source tag. | 649 // The tag is either an import or a source tag. |
| 650 // Load it according to the specified tag. | 650 // Load it according to the specified tag. |
| 651 if (tag == Dart_kImportTag) { | 651 if (tag == Dart_kImportTag) { |
| 652 // Return library object or an error string. | 652 // Return library object or an error string. |
| 653 return Dart_LoadLibrary(url, source); | 653 return Dart_LoadLibrary(url, source); |
| 654 } else if (tag == Dart_kSourceTag) { | 654 } else if (tag == Dart_kSourceTag) { |
| 655 return Dart_LoadSource(library, url, source); | 655 return Dart_LoadSource(library, url, source); |
| 656 } | 656 } |
| 657 return Dart_Error("wrong tag"); | 657 return Dart_NewApiError("wrong tag"); |
| 658 } | 658 } |
| 659 | 659 |
| 660 | 660 |
| 661 Dart_Handle DartUtils::PrepareForScriptLoading(const char* package_root, | 661 Dart_Handle DartUtils::PrepareForScriptLoading(const char* package_root, |
| 662 Dart_Handle builtin_lib) { | 662 Dart_Handle builtin_lib) { |
| 663 // Setup the internal library's 'internalPrint' function. | 663 // Setup the internal library's 'internalPrint' function. |
| 664 Dart_Handle internal_lib = | 664 Dart_Handle internal_lib = |
| 665 Dart_LookupLibrary(NewString("dart:_collection-dev")); | 665 Dart_LookupLibrary(NewString("dart:_collection-dev")); |
| 666 DART_CHECK_VALID(internal_lib); | 666 DART_CHECK_VALID(internal_lib); |
| 667 Dart_Handle print = Dart_Invoke( | 667 Dart_Handle print = Dart_Invoke( |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 846 const char* message, | 846 const char* message, |
| 847 Dart_Handle os_error) { | 847 Dart_Handle os_error) { |
| 848 // Create a dart:io exception object of the given type. | 848 // Create a dart:io exception object of the given type. |
| 849 return NewDartExceptionWithOSError(kIOLibURL, | 849 return NewDartExceptionWithOSError(kIOLibURL, |
| 850 exception_name, | 850 exception_name, |
| 851 message, | 851 message, |
| 852 os_error); | 852 os_error); |
| 853 } | 853 } |
| 854 | 854 |
| 855 | 855 |
| 856 Dart_Handle DartUtils::NewError(const char* format, ...) { |
| 857 va_list args; |
| 858 va_start(args, format); |
| 859 intptr_t len = vsnprintf(NULL, 0, format, args); |
| 860 va_end(args); |
| 861 |
| 862 char* buffer = reinterpret_cast<char*>(Dart_ScopeAllocate(len + 1)); |
| 863 va_list args2; |
| 864 va_start(args2, format); |
| 865 vsnprintf(buffer, (len + 1), format, args2); |
| 866 va_end(args2); |
| 867 |
| 868 return Dart_NewApiError(buffer); |
| 869 } |
| 870 |
| 871 |
| 856 Dart_Handle DartUtils::NewInternalError(const char* message) { | 872 Dart_Handle DartUtils::NewInternalError(const char* message) { |
| 857 return NewDartExceptionWithMessage(kCoreLibURL, "_InternalError", message); | 873 return NewDartExceptionWithMessage(kCoreLibURL, "_InternalError", message); |
| 858 } | 874 } |
| 859 | 875 |
| 860 | 876 |
| 861 bool DartUtils::SetOriginalWorkingDirectory() { | 877 bool DartUtils::SetOriginalWorkingDirectory() { |
| 862 original_working_directory = Directory::Current(); | 878 original_working_directory = Directory::Current(); |
| 863 return original_working_directory != NULL; | 879 return original_working_directory != NULL; |
| 864 } | 880 } |
| 865 | 881 |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1026 new CObjectString(CObject::NewString(os_error->message())); | 1042 new CObjectString(CObject::NewString(os_error->message())); |
| 1027 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 1043 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
| 1028 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 1044 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
| 1029 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 1045 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
| 1030 result->SetAt(2, error_message); | 1046 result->SetAt(2, error_message); |
| 1031 return result; | 1047 return result; |
| 1032 } | 1048 } |
| 1033 | 1049 |
| 1034 } // namespace bin | 1050 } // namespace bin |
| 1035 } // namespace dart | 1051 } // namespace dart |
| OLD | NEW |