| 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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 bool DartUtils::GetBooleanValue(Dart_Handle bool_obj) { | 120 bool DartUtils::GetBooleanValue(Dart_Handle bool_obj) { |
| 121 bool value = false; | 121 bool value = false; |
| 122 Dart_Handle result = Dart_BooleanValue(bool_obj, &value); | 122 Dart_Handle result = Dart_BooleanValue(bool_obj, &value); |
| 123 if (Dart_IsError(result)) Dart_PropagateError(result); | 123 if (Dart_IsError(result)) Dart_PropagateError(result); |
| 124 return value; | 124 return value; |
| 125 } | 125 } |
| 126 | 126 |
| 127 | 127 |
| 128 void DartUtils::SetIntegerField(Dart_Handle handle, | 128 void DartUtils::SetIntegerField(Dart_Handle handle, |
| 129 const char* name, | 129 const char* name, |
| 130 intptr_t val) { | 130 int64_t val) { |
| 131 Dart_Handle result = Dart_SetField(handle, | 131 Dart_Handle result = Dart_SetField(handle, |
| 132 NewString(name), | 132 NewString(name), |
| 133 Dart_NewInteger(val)); | 133 Dart_NewInteger(val)); |
| 134 if (Dart_IsError(result)) Dart_PropagateError(result); | 134 if (Dart_IsError(result)) Dart_PropagateError(result); |
| 135 } | 135 } |
| 136 | 136 |
| 137 | 137 |
| 138 intptr_t DartUtils::GetIntegerField(Dart_Handle handle, | |
| 139 const char* name) { | |
| 140 Dart_Handle result = Dart_GetField(handle, NewString(name)); | |
| 141 if (Dart_IsError(result)) Dart_PropagateError(result); | |
| 142 intptr_t value = DartUtils::GetIntegerValue(result); | |
| 143 return value; | |
| 144 } | |
| 145 | |
| 146 | |
| 147 void DartUtils::SetStringField(Dart_Handle handle, | 138 void DartUtils::SetStringField(Dart_Handle handle, |
| 148 const char* name, | 139 const char* name, |
| 149 const char* val) { | 140 const char* val) { |
| 150 Dart_Handle result = Dart_SetField(handle, NewString(name), NewString(val)); | 141 Dart_Handle result = Dart_SetField(handle, NewString(name), NewString(val)); |
| 151 if (Dart_IsError(result)) Dart_PropagateError(result); | 142 if (Dart_IsError(result)) Dart_PropagateError(result); |
| 152 } | 143 } |
| 153 | 144 |
| 154 | 145 |
| 155 bool DartUtils::IsDartSchemeURL(const char* url_name) { | 146 bool DartUtils::IsDartSchemeURL(const char* url_name) { |
| 156 static const intptr_t kDartSchemeLen = strlen(kDartScheme); | 147 static const intptr_t kDartSchemeLen = strlen(kDartScheme); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 } | 208 } |
| 218 | 209 |
| 219 | 210 |
| 220 void* DartUtils::OpenFile(const char* name, bool write) { | 211 void* DartUtils::OpenFile(const char* name, bool write) { |
| 221 File* file = File::Open(name, write ? File::kWriteTruncate : File::kRead); | 212 File* file = File::Open(name, write ? File::kWriteTruncate : File::kRead); |
| 222 return reinterpret_cast<void*>(file); | 213 return reinterpret_cast<void*>(file); |
| 223 } | 214 } |
| 224 | 215 |
| 225 | 216 |
| 226 void DartUtils::ReadFile(const uint8_t** data, | 217 void DartUtils::ReadFile(const uint8_t** data, |
| 227 intptr_t* file_len, | 218 intptr_t* len, |
| 228 void* stream) { | 219 void* stream) { |
| 229 ASSERT(data != NULL); | 220 ASSERT(data != NULL); |
| 230 ASSERT(file_len != NULL); | 221 ASSERT(len != NULL); |
| 231 ASSERT(stream != NULL); | 222 ASSERT(stream != NULL); |
| 232 File* file_stream = reinterpret_cast<File*>(stream); | 223 File* file_stream = reinterpret_cast<File*>(stream); |
| 233 *file_len = file_stream->Length(); | 224 int64_t file_len = file_stream->Length(); |
| 234 ASSERT(*file_len > 0); | 225 if ((file_len < 0) || (file_len > kIntptrMax)) { |
| 235 uint8_t* text_buffer = reinterpret_cast<uint8_t*>(malloc(*file_len)); | 226 *data = NULL; |
| 227 *len = -1; // Indicates read was not successful. |
| 228 return; |
| 229 } |
| 230 *len = static_cast<intptr_t>(file_len); |
| 231 uint8_t* text_buffer = reinterpret_cast<uint8_t*>(malloc(*len)); |
| 236 ASSERT(text_buffer != NULL); | 232 ASSERT(text_buffer != NULL); |
| 237 if (!file_stream->ReadFully(text_buffer, *file_len)) { | 233 if (!file_stream->ReadFully(text_buffer, *len)) { |
| 238 *data = NULL; | 234 *data = NULL; |
| 239 *file_len = -1; // Indicates read was not successful. | 235 *len = -1; // Indicates read was not successful. |
| 240 return; | 236 return; |
| 241 } | 237 } |
| 242 *data = text_buffer; | 238 *data = text_buffer; |
| 243 } | 239 } |
| 244 | 240 |
| 245 | 241 |
| 246 void DartUtils::WriteFile(const void* buffer, | 242 void DartUtils::WriteFile(const void* buffer, |
| 247 intptr_t num_bytes, | 243 intptr_t num_bytes, |
| 248 void* stream) { | 244 void* stream) { |
| 249 ASSERT(stream != NULL); | 245 ASSERT(stream != NULL); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 if (Dart_IsError(result)) { | 289 if (Dart_IsError(result)) { |
| 294 return result; | 290 return result; |
| 295 } | 291 } |
| 296 result = Dart_Invoke(builtin_lib, | 292 result = Dart_Invoke(builtin_lib, |
| 297 DartUtils::NewString("_getHttpRequestResponseCode"), | 293 DartUtils::NewString("_getHttpRequestResponseCode"), |
| 298 0, | 294 0, |
| 299 NULL); | 295 NULL); |
| 300 if (Dart_IsError(result)) { | 296 if (Dart_IsError(result)) { |
| 301 return result; | 297 return result; |
| 302 } | 298 } |
| 303 intptr_t responseCode = DartUtils::GetIntegerValue(result); | 299 int64_t responseCode = DartUtils::GetIntegerValue(result); |
| 304 if (responseCode != HttpResponseCodeOK) { | 300 if (responseCode != HttpResponseCodeOK) { |
| 305 // Return error. | 301 // Return error. |
| 306 Dart_Handle responseStatus = | 302 Dart_Handle responseStatus = |
| 307 Dart_Invoke(builtin_lib, | 303 Dart_Invoke(builtin_lib, |
| 308 DartUtils::NewString("_getHttpRequestStatusString"), | 304 DartUtils::NewString("_getHttpRequestStatusString"), |
| 309 0, | 305 0, |
| 310 NULL); | 306 NULL); |
| 311 if (Dart_IsError(responseStatus)) { | 307 if (Dart_IsError(responseStatus)) { |
| 312 return responseStatus; | 308 return responseStatus; |
| 313 } | 309 } |
| (...skipping 714 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1028 cobject->value.as_external_typed_data.peer = peer; | 1024 cobject->value.as_external_typed_data.peer = peer; |
| 1029 cobject->value.as_external_typed_data.callback = callback; | 1025 cobject->value.as_external_typed_data.callback = callback; |
| 1030 return cobject; | 1026 return cobject; |
| 1031 } | 1027 } |
| 1032 | 1028 |
| 1033 | 1029 |
| 1034 Dart_CObject* CObject::NewIOBuffer(int64_t length) { | 1030 Dart_CObject* CObject::NewIOBuffer(int64_t length) { |
| 1035 // Make sure that we do not have an integer overflow here. Actual check | 1031 // Make sure that we do not have an integer overflow here. Actual check |
| 1036 // against max elements will be done at the time of writing, as the constant | 1032 // against max elements will be done at the time of writing, as the constant |
| 1037 // is not part of the public API. | 1033 // is not part of the public API. |
| 1038 if (length > kIntptrMax) { | 1034 if ((length < 0) || (length > kIntptrMax)) { |
| 1039 return NULL; | 1035 return NULL; |
| 1040 } | 1036 } |
| 1041 uint8_t* data = IOBuffer::Allocate(length); | 1037 uint8_t* data = IOBuffer::Allocate(static_cast<intptr_t>(length)); |
| 1042 ASSERT(data != NULL); | 1038 ASSERT(data != NULL); |
| 1043 return NewExternalUint8Array( | 1039 return NewExternalUint8Array( |
| 1044 static_cast<intptr_t>(length), data, data, IOBuffer::Finalizer); | 1040 static_cast<intptr_t>(length), data, data, IOBuffer::Finalizer); |
| 1045 } | 1041 } |
| 1046 | 1042 |
| 1047 | 1043 |
| 1048 void CObject::FreeIOBufferData(Dart_CObject* cobject) { | 1044 void CObject::FreeIOBufferData(Dart_CObject* cobject) { |
| 1049 ASSERT(cobject->type == Dart_CObject_kExternalTypedData); | 1045 ASSERT(cobject->type == Dart_CObject_kExternalTypedData); |
| 1050 cobject->value.as_external_typed_data.callback( | 1046 cobject->value.as_external_typed_data.callback( |
| 1051 NULL, cobject->value.as_external_typed_data.peer); | 1047 NULL, cobject->value.as_external_typed_data.peer); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1077 new CObjectString(CObject::NewString(os_error->message())); | 1073 new CObjectString(CObject::NewString(os_error->message())); |
| 1078 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 1074 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
| 1079 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 1075 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
| 1080 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 1076 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
| 1081 result->SetAt(2, error_message); | 1077 result->SetAt(2, error_message); |
| 1082 return result; | 1078 return result; |
| 1083 } | 1079 } |
| 1084 | 1080 |
| 1085 } // namespace bin | 1081 } // namespace bin |
| 1086 } // namespace dart | 1082 } // namespace dart |
| OLD | NEW |