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 | 8 |
9 #include "platform/assert.h" | 9 #include "platform/assert.h" |
10 #include "platform/globals.h" | 10 #include "platform/globals.h" |
(...skipping 858 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
869 return Dart_PostCObject(port_id, CObject::Null()->AsApiCObject()); | 869 return Dart_PostCObject(port_id, CObject::Null()->AsApiCObject()); |
870 } | 870 } |
871 | 871 |
872 | 872 |
873 bool DartUtils::PostInt32(Dart_Port port_id, int32_t value) { | 873 bool DartUtils::PostInt32(Dart_Port port_id, int32_t value) { |
874 // Post a message with the integer value. | 874 // Post a message with the integer value. |
875 int32_t min = 0xc0000000; // -1073741824 | 875 int32_t min = 0xc0000000; // -1073741824 |
876 int32_t max = 0x3fffffff; // 1073741823 | 876 int32_t max = 0x3fffffff; // 1073741823 |
877 ASSERT(min <= value && value < max); | 877 ASSERT(min <= value && value < max); |
878 Dart_CObject object; | 878 Dart_CObject object; |
879 object.type = Dart_CObject::kInt32; | 879 object.type = Dart_CObject_kInt32; |
880 object.value.as_int32 = value; | 880 object.value.as_int32 = value; |
881 return Dart_PostCObject(port_id, &object); | 881 return Dart_PostCObject(port_id, &object); |
882 } | 882 } |
883 | 883 |
884 | 884 |
885 Dart_Handle DartUtils::GetDartClass(const char* library_url, | 885 Dart_Handle DartUtils::GetDartClass(const char* library_url, |
886 const char* class_name) { | 886 const char* class_name) { |
887 return Dart_GetClass(Dart_LookupLibrary(NewString(library_url)), | 887 return Dart_GetClass(Dart_LookupLibrary(NewString(library_url)), |
888 NewString(class_name)); | 888 NewString(class_name)); |
889 } | 889 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
946 | 946 |
947 void DartUtils::SetOriginalWorkingDirectory() { | 947 void DartUtils::SetOriginalWorkingDirectory() { |
948 original_working_directory = Directory::Current(); | 948 original_working_directory = Directory::Current(); |
949 } | 949 } |
950 | 950 |
951 | 951 |
952 // Statically allocated Dart_CObject instances for immutable | 952 // Statically allocated Dart_CObject instances for immutable |
953 // objects. As these will be used by different threads the use of | 953 // objects. As these will be used by different threads the use of |
954 // these depends on the fact that the marking internally in the | 954 // these depends on the fact that the marking internally in the |
955 // Dart_CObject structure is not marking simple value objects. | 955 // Dart_CObject structure is not marking simple value objects. |
956 Dart_CObject CObject::api_null_ = { Dart_CObject::kNull , { 0 } }; | 956 Dart_CObject CObject::api_null_ = { Dart_CObject_kNull , { 0 } }; |
957 Dart_CObject CObject::api_true_ = { Dart_CObject::kBool , { true } }; | 957 Dart_CObject CObject::api_true_ = { Dart_CObject_kBool , { true } }; |
958 Dart_CObject CObject::api_false_ = { Dart_CObject::kBool, { false } }; | 958 Dart_CObject CObject::api_false_ = { Dart_CObject_kBool, { false } }; |
959 CObject CObject::null_ = CObject(&api_null_); | 959 CObject CObject::null_ = CObject(&api_null_); |
960 CObject CObject::true_ = CObject(&api_true_); | 960 CObject CObject::true_ = CObject(&api_true_); |
961 CObject CObject::false_ = CObject(&api_false_); | 961 CObject CObject::false_ = CObject(&api_false_); |
962 | 962 |
963 | 963 |
964 CObject* CObject::Null() { | 964 CObject* CObject::Null() { |
965 return &null_; | 965 return &null_; |
966 } | 966 } |
967 | 967 |
968 | 968 |
969 CObject* CObject::True() { | 969 CObject* CObject::True() { |
970 return &true_; | 970 return &true_; |
971 } | 971 } |
972 | 972 |
973 | 973 |
974 CObject* CObject::False() { | 974 CObject* CObject::False() { |
975 return &false_; | 975 return &false_; |
976 } | 976 } |
977 | 977 |
978 | 978 |
979 CObject* CObject::Bool(bool value) { | 979 CObject* CObject::Bool(bool value) { |
980 return value ? &true_ : &false_; | 980 return value ? &true_ : &false_; |
981 } | 981 } |
982 | 982 |
983 | 983 |
984 Dart_CObject* CObject::New(Dart_CObject::Type type, int additional_bytes) { | 984 Dart_CObject* CObject::New(Dart_CObject_Type type, int additional_bytes) { |
985 Dart_CObject* cobject = reinterpret_cast<Dart_CObject*>( | 985 Dart_CObject* cobject = reinterpret_cast<Dart_CObject*>( |
986 Dart_ScopeAllocate(sizeof(Dart_CObject) + additional_bytes)); | 986 Dart_ScopeAllocate(sizeof(Dart_CObject) + additional_bytes)); |
987 cobject->type = type; | 987 cobject->type = type; |
988 return cobject; | 988 return cobject; |
989 } | 989 } |
990 | 990 |
991 | 991 |
992 Dart_CObject* CObject::NewInt32(int32_t value) { | 992 Dart_CObject* CObject::NewInt32(int32_t value) { |
993 Dart_CObject* cobject = New(Dart_CObject::kInt32); | 993 Dart_CObject* cobject = New(Dart_CObject_kInt32); |
994 cobject->value.as_int32 = value; | 994 cobject->value.as_int32 = value; |
995 return cobject; | 995 return cobject; |
996 } | 996 } |
997 | 997 |
998 | 998 |
999 Dart_CObject* CObject::NewInt64(int64_t value) { | 999 Dart_CObject* CObject::NewInt64(int64_t value) { |
1000 Dart_CObject* cobject = New(Dart_CObject::kInt64); | 1000 Dart_CObject* cobject = New(Dart_CObject_kInt64); |
1001 cobject->value.as_int64 = value; | 1001 cobject->value.as_int64 = value; |
1002 return cobject; | 1002 return cobject; |
1003 } | 1003 } |
1004 | 1004 |
1005 | 1005 |
1006 Dart_CObject* CObject::NewIntptr(intptr_t value) { | 1006 Dart_CObject* CObject::NewIntptr(intptr_t value) { |
1007 // Pointer values passed as intptr_t are always send as int64_t. | 1007 // Pointer values passed as intptr_t are always send as int64_t. |
1008 Dart_CObject* cobject = New(Dart_CObject::kInt64); | 1008 Dart_CObject* cobject = New(Dart_CObject_kInt64); |
1009 cobject->value.as_int64 = value; | 1009 cobject->value.as_int64 = value; |
1010 return cobject; | 1010 return cobject; |
1011 } | 1011 } |
1012 | 1012 |
1013 | 1013 |
1014 Dart_CObject* CObject::NewDouble(double value) { | 1014 Dart_CObject* CObject::NewDouble(double value) { |
1015 Dart_CObject* cobject = New(Dart_CObject::kDouble); | 1015 Dart_CObject* cobject = New(Dart_CObject_kDouble); |
1016 cobject->value.as_double = value; | 1016 cobject->value.as_double = value; |
1017 return cobject; | 1017 return cobject; |
1018 } | 1018 } |
1019 | 1019 |
1020 | 1020 |
1021 Dart_CObject* CObject::NewString(int length) { | 1021 Dart_CObject* CObject::NewString(int length) { |
1022 Dart_CObject* cobject = New(Dart_CObject::kString, length + 1); | 1022 Dart_CObject* cobject = New(Dart_CObject_kString, length + 1); |
1023 cobject->value.as_string = reinterpret_cast<char*>(cobject + 1); | 1023 cobject->value.as_string = reinterpret_cast<char*>(cobject + 1); |
1024 return cobject; | 1024 return cobject; |
1025 } | 1025 } |
1026 | 1026 |
1027 | 1027 |
1028 Dart_CObject* CObject::NewString(const char* str) { | 1028 Dart_CObject* CObject::NewString(const char* str) { |
1029 int length = strlen(str); | 1029 int length = strlen(str); |
1030 Dart_CObject* cobject = NewString(length); | 1030 Dart_CObject* cobject = NewString(length); |
1031 memmove(cobject->value.as_string, str, length + 1); | 1031 memmove(cobject->value.as_string, str, length + 1); |
1032 return cobject; | 1032 return cobject; |
1033 } | 1033 } |
1034 | 1034 |
1035 | 1035 |
1036 Dart_CObject* CObject::NewArray(int length) { | 1036 Dart_CObject* CObject::NewArray(int length) { |
1037 Dart_CObject* cobject = | 1037 Dart_CObject* cobject = |
1038 New(Dart_CObject::kArray, length * sizeof(Dart_CObject*)); // NOLINT | 1038 New(Dart_CObject_kArray, length * sizeof(Dart_CObject*)); // NOLINT |
1039 cobject->value.as_array.length = length; | 1039 cobject->value.as_array.length = length; |
1040 cobject->value.as_array.values = | 1040 cobject->value.as_array.values = |
1041 reinterpret_cast<Dart_CObject**>(cobject + 1); | 1041 reinterpret_cast<Dart_CObject**>(cobject + 1); |
1042 return cobject; | 1042 return cobject; |
1043 } | 1043 } |
1044 | 1044 |
1045 | 1045 |
1046 Dart_CObject* CObject::NewUint8Array(int length) { | 1046 Dart_CObject* CObject::NewUint8Array(int length) { |
1047 Dart_CObject* cobject = New(Dart_CObject::kTypedData, length); | 1047 Dart_CObject* cobject = New(Dart_CObject_kTypedData, length); |
1048 cobject->value.as_typed_data.type = Dart_CObject::kUint8Array; | 1048 cobject->value.as_typed_data.type = Dart_TypedData_kUint8; |
1049 cobject->value.as_typed_data.length = length; | 1049 cobject->value.as_typed_data.length = length; |
1050 cobject->value.as_typed_data.values = reinterpret_cast<uint8_t*>(cobject + 1); | 1050 cobject->value.as_typed_data.values = reinterpret_cast<uint8_t*>(cobject + 1); |
1051 return cobject; | 1051 return cobject; |
1052 } | 1052 } |
1053 | 1053 |
1054 | 1054 |
1055 Dart_CObject* CObject::NewExternalUint8Array( | 1055 Dart_CObject* CObject::NewExternalUint8Array( |
1056 int64_t length, uint8_t* data, void* peer, | 1056 int64_t length, uint8_t* data, void* peer, |
1057 Dart_WeakPersistentHandleFinalizer callback) { | 1057 Dart_WeakPersistentHandleFinalizer callback) { |
1058 Dart_CObject* cobject = New(Dart_CObject::kExternalTypedData); | 1058 Dart_CObject* cobject = New(Dart_CObject_kExternalTypedData); |
1059 cobject->value.as_external_typed_data.type = Dart_CObject::kUint8Array; | 1059 cobject->value.as_external_typed_data.type = Dart_TypedData_kUint8; |
1060 cobject->value.as_external_typed_data.length = length; | 1060 cobject->value.as_external_typed_data.length = length; |
1061 cobject->value.as_external_typed_data.data = data; | 1061 cobject->value.as_external_typed_data.data = data; |
1062 cobject->value.as_external_typed_data.peer = peer; | 1062 cobject->value.as_external_typed_data.peer = peer; |
1063 cobject->value.as_external_typed_data.callback = callback; | 1063 cobject->value.as_external_typed_data.callback = callback; |
1064 return cobject; | 1064 return cobject; |
1065 } | 1065 } |
1066 | 1066 |
1067 | 1067 |
1068 Dart_CObject* CObject::NewIOBuffer(int64_t length) { | 1068 Dart_CObject* CObject::NewIOBuffer(int64_t length) { |
1069 uint8_t* data = IOBuffer::Allocate(length); | 1069 uint8_t* data = IOBuffer::Allocate(length); |
1070 return NewExternalUint8Array(length, data, data, IOBuffer::Finalizer); | 1070 return NewExternalUint8Array(length, data, data, IOBuffer::Finalizer); |
1071 } | 1071 } |
1072 | 1072 |
1073 | 1073 |
1074 void CObject::FreeIOBufferData(Dart_CObject* cobject) { | 1074 void CObject::FreeIOBufferData(Dart_CObject* cobject) { |
1075 ASSERT(cobject->type == Dart_CObject::kExternalTypedData); | 1075 ASSERT(cobject->type == Dart_CObject_kExternalTypedData); |
1076 cobject->value.as_external_typed_data.callback( | 1076 cobject->value.as_external_typed_data.callback( |
1077 NULL, cobject->value.as_external_typed_data.peer); | 1077 NULL, cobject->value.as_external_typed_data.peer); |
1078 cobject->value.as_external_typed_data.data = NULL; | 1078 cobject->value.as_external_typed_data.data = NULL; |
1079 } | 1079 } |
1080 | 1080 |
1081 | 1081 |
1082 CObject* CObject::IllegalArgumentError() { | 1082 CObject* CObject::IllegalArgumentError() { |
1083 CObjectArray* result = new CObjectArray(CObject::NewArray(1)); | 1083 CObjectArray* result = new CObjectArray(CObject::NewArray(1)); |
1084 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kArgumentError))); | 1084 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kArgumentError))); |
1085 return result; | 1085 return result; |
(...skipping 17 matching lines...) Expand all Loading... |
1103 new CObjectString(CObject::NewString(os_error->message())); | 1103 new CObjectString(CObject::NewString(os_error->message())); |
1104 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 1104 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
1105 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 1105 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
1106 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 1106 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
1107 result->SetAt(2, error_message); | 1107 result->SetAt(2, error_message); |
1108 return result; | 1108 return result; |
1109 } | 1109 } |
1110 | 1110 |
1111 } // namespace bin | 1111 } // namespace bin |
1112 } // namespace dart | 1112 } // namespace dart |
OLD | NEW |