| 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 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 bool DartUtils::IsDartIOLibURL(const char* url_name) { | 167 bool DartUtils::IsDartIOLibURL(const char* url_name) { |
| 168 return (strcmp(url_name, kIOLibURL) == 0); | 168 return (strcmp(url_name, kIOLibURL) == 0); |
| 169 } | 169 } |
| 170 | 170 |
| 171 | 171 |
| 172 bool DartUtils::IsDartBuiltinLibURL(const char* url_name) { | 172 bool DartUtils::IsDartBuiltinLibURL(const char* url_name) { |
| 173 return (strcmp(url_name, kBuiltinLibURL) == 0); | 173 return (strcmp(url_name, kBuiltinLibURL) == 0); |
| 174 } | 174 } |
| 175 | 175 |
| 176 | 176 |
| 177 Dart_Handle DartUtils::CanonicalizeURL(CommandLineOptions* url_mapping, | |
| 178 Dart_Handle library, | |
| 179 const char* url_str) { | |
| 180 // Get the url of the including library. | |
| 181 Dart_Handle library_url = Dart_LibraryUrl(library); | |
| 182 if (Dart_IsError(library_url)) { | |
| 183 return Dart_NewApiError("accessing library url failed"); | |
| 184 } | |
| 185 if (!Dart_IsString(library_url)) { | |
| 186 return Dart_NewApiError("library url is not a string"); | |
| 187 } | |
| 188 const char* library_url_str = NULL; | |
| 189 Dart_Handle result = Dart_StringToCString(library_url, &library_url_str); | |
| 190 if (Dart_IsError(result)) { | |
| 191 return Dart_NewApiError("accessing library url characters failed"); | |
| 192 } | |
| 193 if (url_mapping != NULL) { | |
| 194 const char* mapped_library_url_str = MapLibraryUrl(url_mapping, | |
| 195 library_url_str); | |
| 196 if (mapped_library_url_str != NULL) { | |
| 197 library_url_str = mapped_library_url_str; | |
| 198 } | |
| 199 } | |
| 200 // Calculate the canonical path. | |
| 201 const char* canon_url_str = GetCanonicalPath(library_url_str, url_str); | |
| 202 Dart_Handle canon_url = NewString(canon_url_str); | |
| 203 free(const_cast<char*>(canon_url_str)); | |
| 204 | |
| 205 return canon_url; | |
| 206 } | |
| 207 | |
| 208 | |
| 209 void* DartUtils::OpenFile(const char* name, bool write) { | 177 void* DartUtils::OpenFile(const char* name, bool write) { |
| 210 File* file = File::Open(name, write ? File::kWriteTruncate : File::kRead); | 178 File* file = File::Open(name, write ? File::kWriteTruncate : File::kRead); |
| 211 return reinterpret_cast<void*>(file); | 179 return reinterpret_cast<void*>(file); |
| 212 } | 180 } |
| 213 | 181 |
| 214 | 182 |
| 215 void DartUtils::ReadFile(const uint8_t** data, | 183 void DartUtils::ReadFile(const uint8_t** data, |
| 216 intptr_t* len, | 184 intptr_t* len, |
| 217 void* stream) { | 185 void* stream) { |
| 218 ASSERT(data != NULL); | 186 ASSERT(data != NULL); |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 library); | 529 library); |
| 562 } else { | 530 } else { |
| 563 // Handle 'import' or 'part' requests for all other URIs. | 531 // Handle 'import' or 'part' requests for all other URIs. |
| 564 // Get the file path out of the url. | 532 // Get the file path out of the url. |
| 565 Dart_Handle file_path = DartUtils::FilePathFromUri(url, builtin_lib); | 533 Dart_Handle file_path = DartUtils::FilePathFromUri(url, builtin_lib); |
| 566 if (Dart_IsError(file_path)) { | 534 if (Dart_IsError(file_path)) { |
| 567 return file_path; | 535 return file_path; |
| 568 } | 536 } |
| 569 const char* final_path = NULL; | 537 const char* final_path = NULL; |
| 570 Dart_StringToCString(file_path, &final_path); | 538 Dart_StringToCString(file_path, &final_path); |
| 571 result = DartUtils::LoadSource(NULL, | 539 result = DartUtils::LoadSource(library, url, tag, final_path); |
| 572 library, | |
| 573 url, | |
| 574 tag, | |
| 575 final_path); | |
| 576 return result; | 540 return result; |
| 577 } | 541 } |
| 578 } | 542 } |
| 579 | 543 |
| 580 | 544 |
| 581 const uint8_t* DartUtils::SniffForMagicNumber(const uint8_t* text_buffer, | 545 const uint8_t* DartUtils::SniffForMagicNumber(const uint8_t* text_buffer, |
| 582 intptr_t* buffer_len, | 546 intptr_t* buffer_len, |
| 583 bool* is_snapshot) { | 547 bool* is_snapshot) { |
| 584 intptr_t len = sizeof(magic_number); | 548 intptr_t len = sizeof(magic_number); |
| 585 for (intptr_t i = 0; i < len; i++) { | 549 for (intptr_t i = 0; i < len; i++) { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 663 returnValue = NewError("%s is not a valid UTF-8 script", script_uri); | 627 returnValue = NewError("%s is not a valid UTF-8 script", script_uri); |
| 664 } else { | 628 } else { |
| 665 returnValue = Dart_LoadScript(resolved_script_uri, source, 0, 0); | 629 returnValue = Dart_LoadScript(resolved_script_uri, source, 0, 0); |
| 666 } | 630 } |
| 667 } | 631 } |
| 668 free(const_cast<uint8_t *>(buffer)); | 632 free(const_cast<uint8_t *>(buffer)); |
| 669 return returnValue; | 633 return returnValue; |
| 670 } | 634 } |
| 671 | 635 |
| 672 | 636 |
| 673 Dart_Handle DartUtils::LoadSource(CommandLineOptions* url_mapping, | 637 Dart_Handle DartUtils::LoadSource(Dart_Handle library, |
| 674 Dart_Handle library, | |
| 675 Dart_Handle url, | 638 Dart_Handle url, |
| 676 Dart_LibraryTag tag, | 639 Dart_LibraryTag tag, |
| 677 const char* url_string) { | 640 const char* url_string) { |
| 678 bool is_http_scheme_url = DartUtils::IsHttpSchemeURL(url_string); | 641 bool is_http_scheme_url = DartUtils::IsHttpSchemeURL(url_string); |
| 679 if (url_mapping != NULL && IsDartSchemeURL(url_string)) { | |
| 680 const char* mapped_url_string = MapLibraryUrl(url_mapping, url_string); | |
| 681 if (mapped_url_string == NULL) { | |
| 682 return NewError("Do not know how to load %s", url_string); | |
| 683 } | |
| 684 // We have a URL mapping specified, just read the file that the | |
| 685 // URL mapping specifies and load it. | |
| 686 url_string = mapped_url_string; | |
| 687 } | |
| 688 Dart_Handle source; | 642 Dart_Handle source; |
| 689 if (is_http_scheme_url) { | 643 if (is_http_scheme_url) { |
| 690 // Read the file over http. | 644 // Read the file over http. |
| 691 source = DartUtils::ReadStringFromHttp(url_string); | 645 source = DartUtils::ReadStringFromHttp(url_string); |
| 692 } else { | 646 } else { |
| 693 // Read the file. | 647 // Read the file. |
| 694 source = DartUtils::ReadStringFromFile(url_string); | 648 source = DartUtils::ReadStringFromFile(url_string); |
| 695 } | 649 } |
| 696 if (Dart_IsError(source)) { | 650 if (Dart_IsError(source)) { |
| 697 return source; // source contains the error string. | 651 return source; // source contains the error string. |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 784 return Dart_Invoke(builtin_lib, | 738 return Dart_Invoke(builtin_lib, |
| 785 NewString("_setPackageRoot"), | 739 NewString("_setPackageRoot"), |
| 786 kNumArgs, | 740 kNumArgs, |
| 787 dart_args); | 741 dart_args); |
| 788 } | 742 } |
| 789 } | 743 } |
| 790 return result; | 744 return result; |
| 791 } | 745 } |
| 792 | 746 |
| 793 | 747 |
| 794 const char* DartUtils::GetCanonicalPath(const char* reference_dir, | |
| 795 const char* filename) { | |
| 796 if (File::IsAbsolutePath(filename)) { | |
| 797 return strdup(filename); | |
| 798 } | |
| 799 | |
| 800 char* canonical_path = File::GetCanonicalPath(reference_dir); | |
| 801 if (canonical_path == NULL) { | |
| 802 canonical_path = strdup(reference_dir); | |
| 803 ASSERT(canonical_path != NULL); | |
| 804 } | |
| 805 ASSERT(File::PathSeparator() != NULL && strlen(File::PathSeparator()) == 1); | |
| 806 char* path_sep = strrchr(canonical_path, File::PathSeparator()[0]); | |
| 807 if (path_sep == NULL) { | |
| 808 // No separator found: Reference is a file in local directory. | |
| 809 free(canonical_path); | |
| 810 return strdup(filename); | |
| 811 } | |
| 812 *path_sep = '\0'; | |
| 813 intptr_t len = snprintf(NULL, 0, "%s%s%s", | |
| 814 canonical_path, File::PathSeparator(), filename); | |
| 815 char* absolute_filename = reinterpret_cast<char*>(malloc(len + 1)); | |
| 816 ASSERT(absolute_filename != NULL); | |
| 817 | |
| 818 snprintf(absolute_filename, len + 1, "%s%s%s", | |
| 819 canonical_path, File::PathSeparator(), filename); | |
| 820 free(canonical_path); | |
| 821 canonical_path = File::GetCanonicalPath(absolute_filename); | |
| 822 if (canonical_path == NULL) { | |
| 823 return absolute_filename; | |
| 824 } | |
| 825 free(absolute_filename); | |
| 826 return canonical_path; | |
| 827 } | |
| 828 | |
| 829 | |
| 830 bool DartUtils::PostNull(Dart_Port port_id) { | 748 bool DartUtils::PostNull(Dart_Port port_id) { |
| 831 // Post a message with just the null object. | 749 // Post a message with just the null object. |
| 832 return Dart_PostCObject(port_id, CObject::Null()->AsApiCObject()); | 750 return Dart_PostCObject(port_id, CObject::Null()->AsApiCObject()); |
| 833 } | 751 } |
| 834 | 752 |
| 835 | 753 |
| 836 bool DartUtils::PostInt32(Dart_Port port_id, int32_t value) { | 754 bool DartUtils::PostInt32(Dart_Port port_id, int32_t value) { |
| 837 // Post a message with the integer value. | 755 // Post a message with the integer value. |
| 838 int32_t min = 0xc0000000; // -1073741824 | 756 int32_t min = 0xc0000000; // -1073741824 |
| 839 int32_t max = 0x3fffffff; // 1073741823 | 757 int32_t max = 0x3fffffff; // 1073741823 |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1118 new CObjectString(CObject::NewString(os_error->message())); | 1036 new CObjectString(CObject::NewString(os_error->message())); |
| 1119 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 1037 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
| 1120 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 1038 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
| 1121 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 1039 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
| 1122 result->SetAt(2, error_message); | 1040 result->SetAt(2, error_message); |
| 1123 return result; | 1041 return result; |
| 1124 } | 1042 } |
| 1125 | 1043 |
| 1126 } // namespace bin | 1044 } // namespace bin |
| 1127 } // namespace dart | 1045 } // namespace dart |
| OLD | NEW |