| 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/file.h" | 5 #include "bin/file.h" |
| 6 | 6 |
| 7 #include "bin/builtin.h" | 7 #include "bin/builtin.h" |
| 8 #include "bin/dartutils.h" | 8 #include "bin/dartutils.h" |
| 9 #include "bin/io_buffer.h" |
| 9 #include "bin/thread.h" | 10 #include "bin/thread.h" |
| 10 #include "bin/utils.h" | 11 #include "bin/utils.h" |
| 11 | 12 |
| 12 #include "include/dart_api.h" | 13 #include "include/dart_api.h" |
| 13 | 14 |
| 14 static const int kMSPerSecond = 1000; | 15 static const int kMSPerSecond = 1000; |
| 15 | 16 |
| 16 dart::Mutex File::mutex_; | 17 dart::Mutex File::mutex_; |
| 17 int File::service_ports_size_ = 0; | 18 int File::service_ports_size_ = 0; |
| 18 Dart_Port* File::service_ports_ = NULL; | 19 Dart_Port* File::service_ports_ = NULL; |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 } else { | 152 } else { |
| 152 OSError os_error(-1, "Invalid argument", OSError::kUnknown); | 153 OSError os_error(-1, "Invalid argument", OSError::kUnknown); |
| 153 Dart_Handle err = DartUtils::NewDartOSError(&os_error); | 154 Dart_Handle err = DartUtils::NewDartOSError(&os_error); |
| 154 if (Dart_IsError(err)) Dart_PropagateError(err); | 155 if (Dart_IsError(err)) Dart_PropagateError(err); |
| 155 Dart_SetReturnValue(args, err); | 156 Dart_SetReturnValue(args, err); |
| 156 } | 157 } |
| 157 Dart_ExitScope(); | 158 Dart_ExitScope(); |
| 158 } | 159 } |
| 159 | 160 |
| 160 | 161 |
| 162 void FUNCTION_NAME(File_Read)(Dart_NativeArguments args) { |
| 163 Dart_EnterScope(); |
| 164 intptr_t value = |
| 165 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); |
| 166 File* file = reinterpret_cast<File*>(value); |
| 167 ASSERT(file != NULL); |
| 168 Dart_Handle length_object = Dart_GetNativeArgument(args, 1); |
| 169 int64_t length = 0; |
| 170 if (DartUtils::GetInt64Value(length_object, &length)) { |
| 171 uint8_t* buffer = NULL; |
| 172 Dart_Handle external_array = IOBuffer::Allocate(length, &buffer); |
| 173 int bytes_read = file->Read(reinterpret_cast<void*>(buffer), length); |
| 174 if (bytes_read < 0) { |
| 175 Dart_Handle err = DartUtils::NewDartOSError(); |
| 176 if (Dart_IsError(err)) Dart_PropagateError(err); |
| 177 Dart_SetReturnValue(args, err); |
| 178 } else { |
| 179 if (bytes_read < length) { |
| 180 // TODO(ager): cache the 'length' string if this becomes a bottle neck. |
| 181 Dart_SetField(external_array, |
| 182 DartUtils::NewString("length"), |
| 183 Dart_NewInteger(bytes_read)); |
| 184 } |
| 185 Dart_SetReturnValue(args, external_array); |
| 186 } |
| 187 } else { |
| 188 OSError os_error(-1, "Invalid argument", OSError::kUnknown); |
| 189 Dart_Handle err = DartUtils::NewDartOSError(&os_error); |
| 190 if (Dart_IsError(err)) Dart_PropagateError(err); |
| 191 Dart_SetReturnValue(args, err); |
| 192 } |
| 193 Dart_ExitScope(); |
| 194 } |
| 195 |
| 196 |
| 161 void FUNCTION_NAME(File_ReadList)(Dart_NativeArguments args) { | 197 void FUNCTION_NAME(File_ReadList)(Dart_NativeArguments args) { |
| 162 Dart_EnterScope(); | 198 Dart_EnterScope(); |
| 163 intptr_t value = | 199 intptr_t value = |
| 164 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); | 200 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); |
| 165 File* file = reinterpret_cast<File*>(value); | 201 File* file = reinterpret_cast<File*>(value); |
| 166 ASSERT(file != NULL); | 202 ASSERT(file != NULL); |
| 167 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 203 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
| 168 ASSERT(Dart_IsList(buffer_obj)); | 204 ASSERT(Dart_IsList(buffer_obj)); |
| 169 // Offset and length arguments are checked in Dart code to be | 205 // Offset and length arguments are checked in Dart code to be |
| 170 // integers and have the property that (offset + length) <= | 206 // integers and have the property that (offset + length) <= |
| (...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 751 } | 787 } |
| 752 return CObject::IllegalArgumentError(); | 788 return CObject::IllegalArgumentError(); |
| 753 } | 789 } |
| 754 | 790 |
| 755 | 791 |
| 756 static void FinalizeExternalByteArray(void* peer) { | 792 static void FinalizeExternalByteArray(void* peer) { |
| 757 delete[] reinterpret_cast<uint8_t*>(peer); | 793 delete[] reinterpret_cast<uint8_t*>(peer); |
| 758 } | 794 } |
| 759 | 795 |
| 760 | 796 |
| 797 static CObject* FileReadRequest(const CObjectArray& request) { |
| 798 if (request.Length() == 3 && |
| 799 request[1]->IsIntptr() && |
| 800 request[2]->IsInt32OrInt64()) { |
| 801 File* file = CObjectToFilePointer(request[1]); |
| 802 ASSERT(file != NULL); |
| 803 if (!file->IsClosed()) { |
| 804 int64_t length = CObjectInt32OrInt64ToInt64(request[2]); |
| 805 uint8_t* buffer = new uint8_t[length]; |
| 806 int bytes_read = file->Read(buffer, length); |
| 807 if (bytes_read >= 0) { |
| 808 void* peer = reinterpret_cast<void*>(buffer); |
| 809 CObject* external_array = |
| 810 new CObjectExternalUint8Array( |
| 811 CObject::NewExternalUint8Array(bytes_read, |
| 812 buffer, |
| 813 peer, |
| 814 FinalizeExternalByteArray)); |
| 815 CObjectArray* result = new CObjectArray(CObject::NewArray(2)); |
| 816 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0))); |
| 817 result->SetAt(1, external_array); |
| 818 return result; |
| 819 } else { |
| 820 return CObject::NewOSError(); |
| 821 } |
| 822 } else { |
| 823 return CObject::FileClosedError(); |
| 824 } |
| 825 } |
| 826 return CObject::IllegalArgumentError(); |
| 827 } |
| 828 |
| 829 |
| 761 static CObject* FileReadListRequest(const CObjectArray& request) { | 830 static CObject* FileReadListRequest(const CObjectArray& request) { |
| 762 if (request.Length() == 3 && | 831 if (request.Length() == 3 && |
| 763 request[1]->IsIntptr() && | 832 request[1]->IsIntptr() && |
| 764 request[2]->IsInt32OrInt64()) { | 833 request[2]->IsInt32OrInt64()) { |
| 765 File* file = CObjectToFilePointer(request[1]); | 834 File* file = CObjectToFilePointer(request[1]); |
| 766 ASSERT(file != NULL); | 835 ASSERT(file != NULL); |
| 767 if (!file->IsClosed()) { | 836 if (!file->IsClosed()) { |
| 768 int64_t length = CObjectInt32OrInt64ToInt64(request[2]); | 837 int64_t length = CObjectInt32OrInt64ToInt64(request[2]); |
| 769 uint8_t* buffer = new uint8_t[length]; | 838 uint8_t* buffer = new uint8_t[length]; |
| 770 int bytes_read = file->Read(buffer, length); | 839 int bytes_read = file->Read(buffer, length); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 890 break; | 959 break; |
| 891 case File::kFlushRequest: | 960 case File::kFlushRequest: |
| 892 response = FileFlushRequest(request); | 961 response = FileFlushRequest(request); |
| 893 break; | 962 break; |
| 894 case File::kReadByteRequest: | 963 case File::kReadByteRequest: |
| 895 response = FileReadByteRequest(request); | 964 response = FileReadByteRequest(request); |
| 896 break; | 965 break; |
| 897 case File::kWriteByteRequest: | 966 case File::kWriteByteRequest: |
| 898 response = FileWriteByteRequest(request); | 967 response = FileWriteByteRequest(request); |
| 899 break; | 968 break; |
| 969 case File::kReadRequest: |
| 970 response = FileReadRequest(request); |
| 971 break; |
| 900 case File::kReadListRequest: | 972 case File::kReadListRequest: |
| 901 response = FileReadListRequest(request); | 973 response = FileReadListRequest(request); |
| 902 break; | 974 break; |
| 903 case File::kWriteListRequest: | 975 case File::kWriteListRequest: |
| 904 response = FileWriteListRequest(request); | 976 response = FileWriteListRequest(request); |
| 905 break; | 977 break; |
| 906 default: | 978 default: |
| 907 UNREACHABLE(); | 979 UNREACHABLE(); |
| 908 } | 980 } |
| 909 } | 981 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 942 Dart_EnterScope(); | 1014 Dart_EnterScope(); |
| 943 Dart_SetReturnValue(args, Dart_Null()); | 1015 Dart_SetReturnValue(args, Dart_Null()); |
| 944 Dart_Port service_port = File::GetServicePort(); | 1016 Dart_Port service_port = File::GetServicePort(); |
| 945 if (service_port != ILLEGAL_PORT) { | 1017 if (service_port != ILLEGAL_PORT) { |
| 946 // Return a send port for the service port. | 1018 // Return a send port for the service port. |
| 947 Dart_Handle send_port = Dart_NewSendPort(service_port); | 1019 Dart_Handle send_port = Dart_NewSendPort(service_port); |
| 948 Dart_SetReturnValue(args, send_port); | 1020 Dart_SetReturnValue(args, send_port); |
| 949 } | 1021 } |
| 950 Dart_ExitScope(); | 1022 Dart_ExitScope(); |
| 951 } | 1023 } |
| OLD | NEW |