| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 "include/dart_api.h" | 5 #include "include/dart_api.h" |
| 6 | 6 |
| 7 #include "vm/bigint_operations.h" | 7 #include "vm/bigint_operations.h" |
| 8 #include "vm/class_finalizer.h" | 8 #include "vm/class_finalizer.h" |
| 9 #include "vm/compiler.h" | 9 #include "vm/compiler.h" |
| 10 #include "vm/dart.h" | 10 #include "vm/dart.h" |
| (...skipping 705 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 716 HandleScope scope; // Setup a VM handle scope. | 716 HandleScope scope; // Setup a VM handle scope. |
| 717 const String& obj = String::Handle(String::New(codepoints, length)); | 717 const String& obj = String::Handle(String::New(codepoints, length)); |
| 718 return Api::NewLocalHandle(obj); | 718 return Api::NewLocalHandle(obj); |
| 719 } | 719 } |
| 720 | 720 |
| 721 | 721 |
| 722 DART_EXPORT Dart_Handle Dart_NewString32(const uint32_t* codepoints, | 722 DART_EXPORT Dart_Handle Dart_NewString32(const uint32_t* codepoints, |
| 723 intptr_t length) { | 723 intptr_t length) { |
| 724 Zone zone; // Setup a VM zone as we are creating some handles. | 724 Zone zone; // Setup a VM zone as we are creating some handles. |
| 725 HandleScope scope; // Setup a VM handle scope. | 725 HandleScope scope; // Setup a VM handle scope. |
| 726 UNIMPLEMENTED(); | |
| 727 const String& obj = String::Handle(String::New(codepoints, length)); | 726 const String& obj = String::Handle(String::New(codepoints, length)); |
| 728 return Api::NewLocalHandle(obj); | 727 return Api::NewLocalHandle(obj); |
| 729 } | 728 } |
| 730 | 729 |
| 731 | 730 |
| 731 DART_EXPORT Dart_Handle Dart_NewExternalString8( |
| 732 uint8_t* codepoints, |
| 733 intptr_t length, |
| 734 void* peer, |
| 735 Dart_ExternalString8Finalizer finalizer) { |
| 736 Zone zone; // Setup a VM zone as we are creating some handles. |
| 737 HandleScope scope; // Setup a VM handle scope. |
| 738 const String& obj = |
| 739 String::Handle(String::NewExternal(codepoints, length, peer)); |
| 740 return Api::NewLocalHandle(obj); |
| 741 } |
| 742 |
| 743 |
| 744 DART_EXPORT Dart_Handle Dart_NewExternalString16( |
| 745 uint16_t* codepoints, |
| 746 intptr_t length, |
| 747 void* peer, |
| 748 Dart_ExternalString16Finalizer finalizer) { |
| 749 Zone zone; // Setup a VM zone as we are creating some handles. |
| 750 HandleScope scope; // Setup a VM handle scope. |
| 751 const String& obj = |
| 752 String::Handle(String::NewExternal(codepoints, length, peer)); |
| 753 return Api::NewLocalHandle(obj); |
| 754 } |
| 755 |
| 756 |
| 757 DART_EXPORT Dart_Handle Dart_NewExternalString32( |
| 758 uint32_t* codepoints, |
| 759 intptr_t length, |
| 760 void* peer, |
| 761 Dart_ExternalString32Finalizer finalizer) { |
| 762 Zone zone; // Setup a VM zone as we are creating some handles. |
| 763 HandleScope scope; // Setup a VM handle scope. |
| 764 const String& obj = |
| 765 String::Handle(String::NewExternal(codepoints, length, peer)); |
| 766 return Api::NewLocalHandle(obj); |
| 767 } |
| 768 |
| 769 |
| 732 DART_EXPORT bool Dart_IsString8(Dart_Handle object) { | 770 DART_EXPORT bool Dart_IsString8(Dart_Handle object) { |
| 733 Zone zone; // Setup a VM zone as we are creating some handles. | 771 Zone zone; // Setup a VM zone as we are creating some handles. |
| 734 HandleScope scope; // Setup a VM handle scope. | 772 HandleScope scope; // Setup a VM handle scope. |
| 735 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); | 773 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); |
| 736 return obj.IsOneByteString(); | 774 return obj.IsOneByteString() || obj.IsExternalOneByteString(); |
| 737 } | 775 } |
| 738 | 776 |
| 739 | 777 |
| 740 DART_EXPORT bool Dart_IsString16(Dart_Handle object) { | 778 DART_EXPORT bool Dart_IsString16(Dart_Handle object) { |
| 741 Zone zone; // Setup a VM zone as we are creating some handles. | 779 Zone zone; // Setup a VM zone as we are creating some handles. |
| 742 HandleScope scope; // Setup a VM handle scope. | 780 HandleScope scope; // Setup a VM handle scope. |
| 743 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); | 781 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); |
| 744 return obj.IsOneByteString() || obj.IsTwoByteString(); | 782 return (obj.IsOneByteString() || obj.IsExternalOneByteString() || |
| 783 obj.IsTwoByteString() || obj.IsExternalOneByteString()); |
| 745 } | 784 } |
| 746 | 785 |
| 747 | 786 |
| 748 DART_EXPORT Dart_Result Dart_StringGet8(Dart_Handle str, | 787 DART_EXPORT Dart_Result Dart_StringGet8(Dart_Handle str, |
| 749 uint8_t* codepoints, | 788 uint8_t* codepoints, |
| 750 intptr_t length) { | 789 intptr_t length) { |
| 751 Zone zone; // Setup a VM zone as we are creating some handles. | 790 Zone zone; // Setup a VM zone as we are creating some handles. |
| 752 HandleScope scope; // Setup a VM handle scope. | 791 HandleScope scope; // Setup a VM handle scope. |
| 753 const Object& obj = Object::Handle(Api::UnwrapHandle(str)); | 792 const Object& obj = Object::Handle(Api::UnwrapHandle(str)); |
| 754 if (obj.IsOneByteString()) { | 793 if (obj.IsString()) { |
| 755 OneByteString& string_obj = OneByteString::Handle(); | 794 String& string_obj = String::Handle(); |
| 756 string_obj ^= obj.raw(); | 795 string_obj ^= obj.raw(); |
| 757 intptr_t str_len = string_obj.Length(); | 796 if (string_obj.CharSize() == String::kOneByteChar) { |
| 758 intptr_t copy_len = (str_len > length) ? length : str_len; | 797 intptr_t str_len = string_obj.Length(); |
| 759 for (intptr_t i = 0; i < copy_len; i++) { | 798 intptr_t copy_len = (str_len > length) ? length : str_len; |
| 760 codepoints[i] = static_cast<uint8_t>(string_obj.CharAt(i)); | 799 for (intptr_t i = 0; i < copy_len; i++) { |
| 800 codepoints[i] = static_cast<uint8_t>(string_obj.CharAt(i)); |
| 801 } |
| 802 RETURN_CINT(copy_len); |
| 761 } | 803 } |
| 762 RETURN_CINT(copy_len); | |
| 763 } | 804 } |
| 764 RETURN_FAILURE(obj.IsString() ? "Object is not a String8" : | 805 RETURN_FAILURE(obj.IsString() ? "Object is not a String8" : |
| 765 "Object is not a String"); | 806 "Object is not a String"); |
| 766 } | 807 } |
| 767 | 808 |
| 768 | 809 |
| 769 DART_EXPORT Dart_Result Dart_StringGet16(Dart_Handle str, | 810 DART_EXPORT Dart_Result Dart_StringGet16(Dart_Handle str, |
| 770 uint16_t* codepoints, | 811 uint16_t* codepoints, |
| 771 intptr_t length) { | 812 intptr_t length) { |
| 772 Zone zone; // Setup a VM zone as we are creating some handles. | 813 Zone zone; // Setup a VM zone as we are creating some handles. |
| 773 HandleScope scope; // Setup a VM handle scope. | 814 HandleScope scope; // Setup a VM handle scope. |
| 774 const Object& obj = Object::Handle(Api::UnwrapHandle(str)); | 815 const Object& obj = Object::Handle(Api::UnwrapHandle(str)); |
| 775 if (obj.IsOneByteString() || obj.IsTwoByteString()) { | 816 if (obj.IsString()) { |
| 776 String& string_obj = String::Handle(); | 817 String& string_obj = String::Handle(); |
| 777 string_obj ^= obj.raw(); | 818 string_obj ^= obj.raw(); |
| 778 intptr_t str_len = string_obj.Length(); | 819 if (string_obj.CharSize() <= String::kTwoByteChar) { |
| 779 intptr_t copy_len = (str_len > length) ? length : str_len; | 820 intptr_t str_len = string_obj.Length(); |
| 780 for (intptr_t i = 0; i < copy_len; i++) { | 821 intptr_t copy_len = (str_len > length) ? length : str_len; |
| 781 codepoints[i] = static_cast<uint16_t>(string_obj.CharAt(i)); | 822 for (intptr_t i = 0; i < copy_len; i++) { |
| 823 codepoints[i] = static_cast<uint16_t>(string_obj.CharAt(i)); |
| 824 } |
| 825 RETURN_CINT(copy_len); |
| 782 } | 826 } |
| 783 RETURN_CINT(copy_len); | |
| 784 } | 827 } |
| 785 RETURN_FAILURE(obj.IsString() ? "Object is not a String16" : | 828 RETURN_FAILURE(obj.IsString() ? "Object is not a String16" : |
| 786 "Object is not a String"); | 829 "Object is not a String"); |
| 787 } | 830 } |
| 788 | 831 |
| 789 | 832 |
| 790 DART_EXPORT Dart_Result Dart_StringGet32(Dart_Handle str, | 833 DART_EXPORT Dart_Result Dart_StringGet32(Dart_Handle str, |
| 791 uint32_t* codepoints, | 834 uint32_t* codepoints, |
| 792 intptr_t length) { | 835 intptr_t length) { |
| 793 Zone zone; // Setup a VM zone as we are creating some handles. | 836 Zone zone; // Setup a VM zone as we are creating some handles. |
| 794 HandleScope scope; // Setup a VM handle scope. | 837 HandleScope scope; // Setup a VM handle scope. |
| 795 const Object& obj = Object::Handle(Api::UnwrapHandle(str)); | 838 const Object& obj = Object::Handle(Api::UnwrapHandle(str)); |
| 796 if (obj.IsString()) { | 839 if (obj.IsString()) { |
| 797 String& string_obj = String::Handle(); | 840 String& string_obj = String::Handle(); |
| 798 string_obj ^= obj.raw(); | 841 string_obj ^= obj.raw(); |
| 799 intptr_t str_len = string_obj.Length(); | 842 if (string_obj.CharSize() <= String::kFourByteChar) { |
| 800 intptr_t copy_len = (str_len > length) ? length : str_len; | 843 intptr_t str_len = string_obj.Length(); |
| 801 for (intptr_t i = 0; i < copy_len; i++) { | 844 intptr_t copy_len = (str_len > length) ? length : str_len; |
| 802 codepoints[i] = static_cast<uint32_t>(string_obj.CharAt(i)); | 845 for (intptr_t i = 0; i < copy_len; i++) { |
| 846 codepoints[i] = static_cast<uint32_t>(string_obj.CharAt(i)); |
| 847 } |
| 848 RETURN_CINT(copy_len); |
| 803 } | 849 } |
| 804 RETURN_CINT(copy_len); | |
| 805 } | 850 } |
| 806 RETURN_FAILURE("Object is not a String"); | 851 RETURN_FAILURE("Object is not a String"); |
| 807 } | 852 } |
| 808 | 853 |
| 809 | 854 |
| 810 DART_EXPORT Dart_Result Dart_StringToCString(Dart_Handle object) { | 855 DART_EXPORT Dart_Result Dart_StringToCString(Dart_Handle object) { |
| 811 Zone zone; // Setup a VM zone as we are creating some handles. | 856 Zone zone; // Setup a VM zone as we are creating some handles. |
| 812 HandleScope scope; // Setup a VM handle scope. | 857 HandleScope scope; // Setup a VM handle scope. |
| 813 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); | 858 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); |
| 814 if (obj.IsString()) { | 859 if (obj.IsString()) { |
| 815 const char* string_value = obj.ToCString(); | 860 const char* string_value = obj.ToCString(); |
| 816 intptr_t string_length = strlen(string_value); | 861 intptr_t string_length = strlen(string_value); |
| 817 char* result = reinterpret_cast<char*>(Api::Allocate(string_length + 1)); | 862 char* result = reinterpret_cast<char*>(Api::Allocate(string_length + 1)); |
| 818 if (result == NULL) { | 863 if (result == NULL) { |
| 819 RETURN_FAILURE("Unable to allocate memory"); | 864 RETURN_FAILURE("Unable to allocate memory"); |
| 820 } | 865 } |
| 821 strncpy(result, string_value, string_length + 1); | 866 strncpy(result, string_value, string_length + 1); |
| 822 ASSERT(result[string_length] == '\0'); | 867 ASSERT(result[string_length] == '\0'); |
| 823 RETURN_CSTRING(result); | 868 RETURN_CSTRING(result); |
| 824 } | 869 } |
| 825 RETURN_FAILURE("Object is not a String"); | 870 RETURN_FAILURE("Object is not a String"); |
| 826 } | 871 } |
| 827 | 872 |
| 828 | 873 |
| 874 DART_EXPORT Dart_Result Dart_ExternalStringPeer(Dart_Handle object, |
| 875 void** peer) { |
| 876 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); |
| 877 if (obj.IsExternalOneByteString()) { |
| 878 ExternalOneByteString& str = ExternalOneByteString::Handle(); |
| 879 str ^= obj.raw(); |
| 880 *peer = str.Peer(); |
| 881 RETURN_CBOOLEAN(true); |
| 882 } |
| 883 if (obj.IsExternalTwoByteString()) { |
| 884 ExternalTwoByteString& str = ExternalTwoByteString::Handle(); |
| 885 str ^= obj.raw(); |
| 886 *peer = str.Peer(); |
| 887 RETURN_CBOOLEAN(true); |
| 888 } |
| 889 if (obj.IsExternalFourByteString()) { |
| 890 ExternalFourByteString& str = ExternalFourByteString::Handle(); |
| 891 str ^= obj.raw(); |
| 892 *peer = str.Peer(); |
| 893 RETURN_CBOOLEAN(true); |
| 894 } |
| 895 RETURN_FAILURE("Object is not an external String"); |
| 896 } |
| 897 |
| 898 |
| 829 DART_EXPORT bool Dart_IsArray(Dart_Handle object) { | 899 DART_EXPORT bool Dart_IsArray(Dart_Handle object) { |
| 830 Zone zone; // Setup a VM zone as we are creating some handles. | 900 Zone zone; // Setup a VM zone as we are creating some handles. |
| 831 HandleScope scope; // Setup a VM handle scope. | 901 HandleScope scope; // Setup a VM handle scope. |
| 832 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); | 902 const Object& obj = Object::Handle(Api::UnwrapHandle(object)); |
| 833 return obj.IsArray(); | 903 return obj.IsArray(); |
| 834 } | 904 } |
| 835 | 905 |
| 836 | 906 |
| 837 DART_EXPORT Dart_Handle Dart_NewArray(intptr_t length) { | 907 DART_EXPORT Dart_Handle Dart_NewArray(intptr_t length) { |
| 838 Zone zone; // Setup a VM zone as we are creating some handles. | 908 Zone zone; // Setup a VM zone as we are creating some handles. |
| (...skipping 882 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1721 ASSERT(isolate != NULL); | 1791 ASSERT(isolate != NULL); |
| 1722 ApiState* state = isolate->api_state(); | 1792 ApiState* state = isolate->api_state(); |
| 1723 ASSERT(state != NULL); | 1793 ASSERT(state != NULL); |
| 1724 ApiLocalScope* scope = state->top_scope(); | 1794 ApiLocalScope* scope = state->top_scope(); |
| 1725 ASSERT(scope != NULL); | 1795 ASSERT(scope != NULL); |
| 1726 return scope->zone().Reallocate(ptr, old_size, new_size); | 1796 return scope->zone().Reallocate(ptr, old_size, new_size); |
| 1727 } | 1797 } |
| 1728 | 1798 |
| 1729 | 1799 |
| 1730 } // namespace dart | 1800 } // namespace dart |
| OLD | NEW |