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 900 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
911 } | 911 } |
912 | 912 |
913 | 913 |
914 Dart_CObject* CObject::NewDouble(double value) { | 914 Dart_CObject* CObject::NewDouble(double value) { |
915 Dart_CObject* cobject = New(Dart_CObject_kDouble); | 915 Dart_CObject* cobject = New(Dart_CObject_kDouble); |
916 cobject->value.as_double = value; | 916 cobject->value.as_double = value; |
917 return cobject; | 917 return cobject; |
918 } | 918 } |
919 | 919 |
920 | 920 |
921 Dart_CObject* CObject::NewString(int length) { | 921 Dart_CObject* CObject::NewString(intptr_t length) { |
922 Dart_CObject* cobject = New(Dart_CObject_kString, length + 1); | 922 Dart_CObject* cobject = New(Dart_CObject_kString, length + 1); |
923 cobject->value.as_string = reinterpret_cast<char*>(cobject + 1); | 923 cobject->value.as_string = reinterpret_cast<char*>(cobject + 1); |
924 return cobject; | 924 return cobject; |
925 } | 925 } |
926 | 926 |
927 | 927 |
928 Dart_CObject* CObject::NewString(const char* str) { | 928 Dart_CObject* CObject::NewString(const char* str) { |
929 int length = strlen(str); | 929 int length = strlen(str); |
930 Dart_CObject* cobject = NewString(length); | 930 Dart_CObject* cobject = NewString(length); |
931 memmove(cobject->value.as_string, str, length + 1); | 931 memmove(cobject->value.as_string, str, length + 1); |
932 return cobject; | 932 return cobject; |
933 } | 933 } |
934 | 934 |
935 | 935 |
936 Dart_CObject* CObject::NewArray(int length) { | 936 Dart_CObject* CObject::NewArray(intptr_t length) { |
937 Dart_CObject* cobject = | 937 Dart_CObject* cobject = |
938 New(Dart_CObject_kArray, length * sizeof(Dart_CObject*)); // NOLINT | 938 New(Dart_CObject_kArray, length * sizeof(Dart_CObject*)); // NOLINT |
939 cobject->value.as_array.length = length; | 939 cobject->value.as_array.length = length; |
940 cobject->value.as_array.values = | 940 cobject->value.as_array.values = |
941 reinterpret_cast<Dart_CObject**>(cobject + 1); | 941 reinterpret_cast<Dart_CObject**>(cobject + 1); |
942 return cobject; | 942 return cobject; |
943 } | 943 } |
944 | 944 |
945 | 945 |
946 Dart_CObject* CObject::NewUint8Array(int length) { | 946 Dart_CObject* CObject::NewUint8Array(intptr_t length) { |
947 Dart_CObject* cobject = New(Dart_CObject_kTypedData, length); | 947 Dart_CObject* cobject = New(Dart_CObject_kTypedData, length); |
948 cobject->value.as_typed_data.type = Dart_TypedData_kUint8; | 948 cobject->value.as_typed_data.type = Dart_TypedData_kUint8; |
949 cobject->value.as_typed_data.length = length; | 949 cobject->value.as_typed_data.length = length; |
950 cobject->value.as_typed_data.values = reinterpret_cast<uint8_t*>(cobject + 1); | 950 cobject->value.as_typed_data.values = reinterpret_cast<uint8_t*>(cobject + 1); |
951 return cobject; | 951 return cobject; |
952 } | 952 } |
953 | 953 |
954 | 954 |
955 Dart_CObject* CObject::NewExternalUint8Array( | 955 Dart_CObject* CObject::NewExternalUint8Array( |
956 int64_t length, uint8_t* data, void* peer, | 956 intptr_t length, uint8_t* data, void* peer, |
957 Dart_WeakPersistentHandleFinalizer callback) { | 957 Dart_WeakPersistentHandleFinalizer callback) { |
958 Dart_CObject* cobject = New(Dart_CObject_kExternalTypedData); | 958 Dart_CObject* cobject = New(Dart_CObject_kExternalTypedData); |
959 cobject->value.as_external_typed_data.type = Dart_TypedData_kUint8; | 959 cobject->value.as_external_typed_data.type = Dart_TypedData_kUint8; |
960 cobject->value.as_external_typed_data.length = length; | 960 cobject->value.as_external_typed_data.length = length; |
961 cobject->value.as_external_typed_data.data = data; | 961 cobject->value.as_external_typed_data.data = data; |
962 cobject->value.as_external_typed_data.peer = peer; | 962 cobject->value.as_external_typed_data.peer = peer; |
963 cobject->value.as_external_typed_data.callback = callback; | 963 cobject->value.as_external_typed_data.callback = callback; |
964 return cobject; | 964 return cobject; |
965 } | 965 } |
966 | 966 |
967 | 967 |
968 Dart_CObject* CObject::NewIOBuffer(int64_t length) { | 968 Dart_CObject* CObject::NewIOBuffer(int64_t length) { |
| 969 // Make sure that we do not have an integer overflow here. Actual check |
| 970 // against max elements will be done at the time of writing, as the constant |
| 971 // is not part of the public API. |
| 972 if (length > kIntptrMax) { |
| 973 return NULL; |
| 974 } |
969 uint8_t* data = IOBuffer::Allocate(length); | 975 uint8_t* data = IOBuffer::Allocate(length); |
970 return NewExternalUint8Array(length, data, data, IOBuffer::Finalizer); | 976 ASSERT(data != NULL); |
| 977 return NewExternalUint8Array( |
| 978 static_cast<intptr_t>(length), data, data, IOBuffer::Finalizer); |
971 } | 979 } |
972 | 980 |
973 | 981 |
974 void CObject::FreeIOBufferData(Dart_CObject* cobject) { | 982 void CObject::FreeIOBufferData(Dart_CObject* cobject) { |
975 ASSERT(cobject->type == Dart_CObject_kExternalTypedData); | 983 ASSERT(cobject->type == Dart_CObject_kExternalTypedData); |
976 cobject->value.as_external_typed_data.callback( | 984 cobject->value.as_external_typed_data.callback( |
977 NULL, cobject->value.as_external_typed_data.peer); | 985 NULL, cobject->value.as_external_typed_data.peer); |
978 cobject->value.as_external_typed_data.data = NULL; | 986 cobject->value.as_external_typed_data.data = NULL; |
979 } | 987 } |
980 | 988 |
(...skipping 22 matching lines...) Expand all Loading... |
1003 new CObjectString(CObject::NewString(os_error->message())); | 1011 new CObjectString(CObject::NewString(os_error->message())); |
1004 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 1012 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
1005 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 1013 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
1006 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 1014 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
1007 result->SetAt(2, error_message); | 1015 result->SetAt(2, error_message); |
1008 return result; | 1016 return result; |
1009 } | 1017 } |
1010 | 1018 |
1011 } // namespace bin | 1019 } // namespace bin |
1012 } // namespace dart | 1020 } // namespace dart |
OLD | NEW |