Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: runtime/bin/file.cc

Issue 14142008: Add support for more typed data types on native ports (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 893 matching lines...) Expand 10 before | Expand all | Expand 10 after
904 904
905 static CObject* FileReadRequest(const CObjectArray& request) { 905 static CObject* FileReadRequest(const CObjectArray& request) {
906 if (request.Length() == 3 && 906 if (request.Length() == 3 &&
907 request[1]->IsIntptr() && 907 request[1]->IsIntptr() &&
908 request[2]->IsInt32OrInt64()) { 908 request[2]->IsInt32OrInt64()) {
909 File* file = CObjectToFilePointer(request[1]); 909 File* file = CObjectToFilePointer(request[1]);
910 ASSERT(file != NULL); 910 ASSERT(file != NULL);
911 if (!file->IsClosed()) { 911 if (!file->IsClosed()) {
912 int64_t length = CObjectInt32OrInt64ToInt64(request[2]); 912 int64_t length = CObjectInt32OrInt64ToInt64(request[2]);
913 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); 913 Dart_CObject* io_buffer = CObject::NewIOBuffer(length);
914 uint8_t* data = io_buffer->value.as_external_byte_array.data; 914 uint8_t* data = io_buffer->value.as_external_typed_data.data;
915 int64_t bytes_read = file->Read(data, length); 915 int64_t bytes_read = file->Read(data, length);
916 if (bytes_read >= 0) { 916 if (bytes_read >= 0) {
917 CObjectExternalUint8Array* external_array = 917 CObjectExternalUint8Array* external_array =
918 new CObjectExternalUint8Array(io_buffer); 918 new CObjectExternalUint8Array(io_buffer);
919 external_array->SetLength(bytes_read); 919 external_array->SetLength(bytes_read);
920 CObjectArray* result = new CObjectArray(CObject::NewArray(2)); 920 CObjectArray* result = new CObjectArray(CObject::NewArray(2));
921 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0))); 921 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0)));
922 result->SetAt(1, external_array); 922 result->SetAt(1, external_array);
923 return result; 923 return result;
924 } else { 924 } else {
(...skipping 10 matching lines...) Expand all
935 935
936 static CObject* FileReadListRequest(const CObjectArray& request) { 936 static CObject* FileReadListRequest(const CObjectArray& request) {
937 if (request.Length() == 3 && 937 if (request.Length() == 3 &&
938 request[1]->IsIntptr() && 938 request[1]->IsIntptr() &&
939 request[2]->IsInt32OrInt64()) { 939 request[2]->IsInt32OrInt64()) {
940 File* file = CObjectToFilePointer(request[1]); 940 File* file = CObjectToFilePointer(request[1]);
941 ASSERT(file != NULL); 941 ASSERT(file != NULL);
942 if (!file->IsClosed()) { 942 if (!file->IsClosed()) {
943 int64_t length = CObjectInt32OrInt64ToInt64(request[2]); 943 int64_t length = CObjectInt32OrInt64ToInt64(request[2]);
944 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); 944 Dart_CObject* io_buffer = CObject::NewIOBuffer(length);
945 uint8_t* data = io_buffer->value.as_external_byte_array.data; 945 uint8_t* data = io_buffer->value.as_external_typed_data.data;
946 int64_t bytes_read = file->Read(data, length); 946 int64_t bytes_read = file->Read(data, length);
947 if (bytes_read >= 0) { 947 if (bytes_read >= 0) {
948 CObjectExternalUint8Array* external_array = 948 CObjectExternalUint8Array* external_array =
949 new CObjectExternalUint8Array(io_buffer); 949 new CObjectExternalUint8Array(io_buffer);
950 external_array->SetLength(bytes_read); 950 external_array->SetLength(bytes_read);
951 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); 951 CObjectArray* result = new CObjectArray(CObject::NewArray(3));
952 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0))); 952 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0)));
953 result->SetAt(1, new CObjectInt64(CObject::NewInt64(bytes_read))); 953 result->SetAt(1, new CObjectInt64(CObject::NewInt64(bytes_read)));
954 result->SetAt(2, external_array); 954 result->SetAt(2, external_array);
955 return result; 955 return result;
956 } else { 956 } else {
957 CObject::FreeIOBufferData(io_buffer); 957 CObject::FreeIOBufferData(io_buffer);
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::TypedDataType 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]->IsTypedData() || 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]->IsTypedData()) {
981 CObjectUint8Array byte_array(request[2]); 997 CObjectTypedData typed_data(request[2]);
982 buffer_start = byte_array.Buffer() + offset; 998 offset = offset * SizeInBytes(typed_data.Type());
999 length = length * SizeInBytes(typed_data.Type());
1000 buffer_start = typed_data.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]->IsTypedData()) {
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
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698