OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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/io_buffer.h" |
10 #include "bin/thread.h" | 10 #include "bin/thread.h" |
(...skipping 947 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
958 return CObject::NewOSError(); | 958 return CObject::NewOSError(); |
959 } | 959 } |
960 } else { | 960 } else { |
961 return CObject::FileClosedError(); | 961 return CObject::FileClosedError(); |
962 } | 962 } |
963 } | 963 } |
964 return CObject::IllegalArgumentError(); | 964 return CObject::IllegalArgumentError(); |
965 } | 965 } |
966 | 966 |
967 | 967 |
| 968 static int SizeInBytes(Dart_CObject::ByteArrayType type) { |
| 969 switch (type) { |
| 970 case Dart_CObject::kInt8Array: |
| 971 case Dart_CObject::kUint8Array: |
| 972 return 1; |
| 973 case Dart_CObject::kInt16Array: |
| 974 case Dart_CObject::kUint16Array: |
| 975 return 2; |
| 976 default: |
| 977 break; |
| 978 } |
| 979 UNREACHABLE(); |
| 980 return -1; |
| 981 } |
| 982 |
| 983 |
968 static CObject* FileWriteListRequest(const CObjectArray& request) { | 984 static CObject* FileWriteListRequest(const CObjectArray& request) { |
969 if (request.Length() == 5 && | 985 if (request.Length() == 5 && |
970 request[1]->IsIntptr() && | 986 request[1]->IsIntptr() && |
971 (request[2]->IsUint8Array() || request[2]->IsArray()) && | 987 (request[2]->IsByteArray() || request[2]->IsArray()) && |
972 request[3]->IsInt32OrInt64() && | 988 request[3]->IsInt32OrInt64() && |
973 request[4]->IsInt32OrInt64()) { | 989 request[4]->IsInt32OrInt64()) { |
974 File* file = CObjectToFilePointer(request[1]); | 990 File* file = CObjectToFilePointer(request[1]); |
975 ASSERT(file != NULL); | 991 ASSERT(file != NULL); |
976 if (!file->IsClosed()) { | 992 if (!file->IsClosed()) { |
977 int64_t offset = CObjectInt32OrInt64ToInt64(request[3]); | 993 int64_t offset = CObjectInt32OrInt64ToInt64(request[3]); |
978 int64_t length = CObjectInt32OrInt64ToInt64(request[4]); | 994 int64_t length = CObjectInt32OrInt64ToInt64(request[4]); |
979 uint8_t* buffer_start; | 995 uint8_t* buffer_start; |
980 if (request[2]->IsUint8Array()) { | 996 if (request[2]->IsByteArray()) { |
981 CObjectUint8Array byte_array(request[2]); | 997 CObjectByteArray byte_array(request[2]); |
| 998 offset = offset * SizeInBytes(byte_array.Type()); |
| 999 length = length * SizeInBytes(byte_array.Type()); |
982 buffer_start = byte_array.Buffer() + offset; | 1000 buffer_start = byte_array.Buffer() + offset; |
983 } else { | 1001 } else { |
984 CObjectArray array(request[2]); | 1002 CObjectArray array(request[2]); |
985 buffer_start = new uint8_t[length]; | 1003 buffer_start = new uint8_t[length]; |
986 for (int i = 0; i < length; i++) { | 1004 for (int i = 0; i < length; i++) { |
987 if (array[i + offset]->IsInt32OrInt64()) { | 1005 if (array[i + offset]->IsInt32OrInt64()) { |
988 int64_t value = CObjectInt32OrInt64ToInt64(array[i + offset]); | 1006 int64_t value = CObjectInt32OrInt64ToInt64(array[i + offset]); |
989 buffer_start[i] = static_cast<uint8_t>(value & 0xFF); | 1007 buffer_start[i] = static_cast<uint8_t>(value & 0xFF); |
990 } else { | 1008 } else { |
991 // Unsupported type. | 1009 // Unsupported type. |
992 delete[] buffer_start; | 1010 delete[] buffer_start; |
993 return CObject::IllegalArgumentError(); | 1011 return CObject::IllegalArgumentError(); |
994 } | 1012 } |
995 } | 1013 } |
996 offset = 0; | 1014 offset = 0; |
997 } | 1015 } |
998 int64_t bytes_written = | 1016 int64_t bytes_written = |
999 file->Write(reinterpret_cast<void*>(buffer_start), length); | 1017 file->Write(reinterpret_cast<void*>(buffer_start), length); |
1000 if (!request[2]->IsUint8Array()) { | 1018 if (!request[2]->IsByteArray()) { |
1001 delete[] buffer_start; | 1019 delete[] buffer_start; |
1002 } | 1020 } |
1003 if (bytes_written >= 0) { | 1021 if (bytes_written >= 0) { |
1004 return new CObjectInt64(CObject::NewInt64(bytes_written)); | 1022 return new CObjectInt64(CObject::NewInt64(bytes_written)); |
1005 } else { | 1023 } else { |
1006 return CObject::NewOSError(); | 1024 return CObject::NewOSError(); |
1007 } | 1025 } |
1008 } else { | 1026 } else { |
1009 return CObject::FileClosedError(); | 1027 return CObject::FileClosedError(); |
1010 } | 1028 } |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1134 Dart_EnterScope(); | 1152 Dart_EnterScope(); |
1135 Dart_SetReturnValue(args, Dart_Null()); | 1153 Dart_SetReturnValue(args, Dart_Null()); |
1136 Dart_Port service_port = File::GetServicePort(); | 1154 Dart_Port service_port = File::GetServicePort(); |
1137 if (service_port != ILLEGAL_PORT) { | 1155 if (service_port != ILLEGAL_PORT) { |
1138 // Return a send port for the service port. | 1156 // Return a send port for the service port. |
1139 Dart_Handle send_port = Dart_NewSendPort(service_port); | 1157 Dart_Handle send_port = Dart_NewSendPort(service_port); |
1140 Dart_SetReturnValue(args, send_port); | 1158 Dart_SetReturnValue(args, send_port); |
1141 } | 1159 } |
1142 Dart_ExitScope(); | 1160 Dart_ExitScope(); |
1143 } | 1161 } |
OLD | NEW |