| 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/thread.h" | 9 #include "bin/thread.h" |
| 10 #include "bin/utils.h" | 10 #include "bin/utils.h" |
| (...skipping 750 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 761 } | 761 } |
| 762 | 762 |
| 763 | 763 |
| 764 static CObject* FileReadListRequest(const CObjectArray& request) { | 764 static CObject* FileReadListRequest(const CObjectArray& request) { |
| 765 if (request.Length() == 3 && | 765 if (request.Length() == 3 && |
| 766 request[1]->IsIntptr() && | 766 request[1]->IsIntptr() && |
| 767 request[2]->IsInt32OrInt64()) { | 767 request[2]->IsInt32OrInt64()) { |
| 768 File* file = CObjectToFilePointer(request[1]); | 768 File* file = CObjectToFilePointer(request[1]); |
| 769 if (file != NULL && !file->IsClosed()) { | 769 if (file != NULL && !file->IsClosed()) { |
| 770 int64_t length = CObjectInt32OrInt64ToInt64(request[2]); | 770 int64_t length = CObjectInt32OrInt64ToInt64(request[2]); |
| 771 CObjectByteArray* byte_array = | 771 CObjectUint8Array* byte_array = |
| 772 new CObjectByteArray(CObject::NewByteArray(length)); | 772 new CObjectUint8Array(CObject::NewUint8Array(length)); |
| 773 void* buffer = reinterpret_cast<void*>(byte_array->Buffer()); | 773 void* buffer = reinterpret_cast<void*>(byte_array->Buffer()); |
| 774 int bytes_read = file->Read(buffer, byte_array->Length()); | 774 int bytes_read = file->Read(buffer, byte_array->Length()); |
| 775 if (bytes_read >= 0) { | 775 if (bytes_read >= 0) { |
| 776 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 776 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
| 777 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0))); | 777 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0))); |
| 778 result->SetAt(1, new CObjectIntptr(CObject::NewIntptr(bytes_read))); | 778 result->SetAt(1, new CObjectIntptr(CObject::NewIntptr(bytes_read))); |
| 779 result->SetAt(2, byte_array); | 779 result->SetAt(2, byte_array); |
| 780 return result; | 780 return result; |
| 781 } else { | 781 } else { |
| 782 return CObject::NewOSError(); | 782 return CObject::NewOSError(); |
| 783 } | 783 } |
| 784 } else { | 784 } else { |
| 785 return CObject::FileClosedError(); | 785 return CObject::FileClosedError(); |
| 786 } | 786 } |
| 787 } | 787 } |
| 788 return CObject::IllegalArgumentError(); | 788 return CObject::IllegalArgumentError(); |
| 789 } | 789 } |
| 790 | 790 |
| 791 | 791 |
| 792 static CObject* FileWriteListRequest(const CObjectArray& request) { | 792 static CObject* FileWriteListRequest(const CObjectArray& request) { |
| 793 if (request.Length() == 5 && | 793 if (request.Length() == 5 && |
| 794 request[1]->IsIntptr() && | 794 request[1]->IsIntptr() && |
| 795 (request[2]->IsByteArray() || request[2]->IsArray()) && | 795 (request[2]->IsUint8Array() || request[2]->IsArray()) && |
| 796 request[3]->IsInt32OrInt64() && | 796 request[3]->IsInt32OrInt64() && |
| 797 request[4]->IsInt32OrInt64()) { | 797 request[4]->IsInt32OrInt64()) { |
| 798 File* file = CObjectToFilePointer(request[1]); | 798 File* file = CObjectToFilePointer(request[1]); |
| 799 if (file != NULL && !file->IsClosed()) { | 799 if (file != NULL && !file->IsClosed()) { |
| 800 int64_t offset = CObjectInt32OrInt64ToInt64(request[3]); | 800 int64_t offset = CObjectInt32OrInt64ToInt64(request[3]); |
| 801 int64_t length = CObjectInt32OrInt64ToInt64(request[4]); | 801 int64_t length = CObjectInt32OrInt64ToInt64(request[4]); |
| 802 uint8_t* buffer_start; | 802 uint8_t* buffer_start; |
| 803 if (request[2]->IsByteArray()) { | 803 if (request[2]->IsUint8Array()) { |
| 804 CObjectByteArray byte_array(request[2]); | 804 CObjectUint8Array byte_array(request[2]); |
| 805 buffer_start = byte_array.Buffer() + offset; | 805 buffer_start = byte_array.Buffer() + offset; |
| 806 } else { | 806 } else { |
| 807 CObjectArray array(request[2]); | 807 CObjectArray array(request[2]); |
| 808 buffer_start = new uint8_t[length]; | 808 buffer_start = new uint8_t[length]; |
| 809 for (int i = 0; i < length; i++) { | 809 for (int i = 0; i < length; i++) { |
| 810 if (array[i + offset]->IsInt32OrInt64()) { | 810 if (array[i + offset]->IsInt32OrInt64()) { |
| 811 int64_t value = CObjectInt32OrInt64ToInt64(array[i + offset]); | 811 int64_t value = CObjectInt32OrInt64ToInt64(array[i + offset]); |
| 812 buffer_start[i] = value & 0xFF; | 812 buffer_start[i] = value & 0xFF; |
| 813 } else { | 813 } else { |
| 814 // Unsupported type. | 814 // Unsupported type. |
| 815 delete[] buffer_start; | 815 delete[] buffer_start; |
| 816 return CObject::IllegalArgumentError(); | 816 return CObject::IllegalArgumentError(); |
| 817 } | 817 } |
| 818 } | 818 } |
| 819 offset = 0; | 819 offset = 0; |
| 820 } | 820 } |
| 821 int64_t bytes_written = | 821 int64_t bytes_written = |
| 822 file->Write(reinterpret_cast<void*>(buffer_start), length); | 822 file->Write(reinterpret_cast<void*>(buffer_start), length); |
| 823 if (!request[2]->IsByteArray()) { | 823 if (!request[2]->IsUint8Array()) { |
| 824 delete[] buffer_start; | 824 delete[] buffer_start; |
| 825 } | 825 } |
| 826 if (bytes_written >= 0) { | 826 if (bytes_written >= 0) { |
| 827 return new CObjectIntptr(CObject::NewIntptr(bytes_written)); | 827 return new CObjectIntptr(CObject::NewIntptr(bytes_written)); |
| 828 } else { | 828 } else { |
| 829 return CObject::NewOSError(); | 829 return CObject::NewOSError(); |
| 830 } | 830 } |
| 831 } else { | 831 } else { |
| 832 return CObject::FileClosedError(); | 832 return CObject::FileClosedError(); |
| 833 } | 833 } |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 956 Dart_EnterScope(); | 956 Dart_EnterScope(); |
| 957 Dart_SetReturnValue(args, Dart_Null()); | 957 Dart_SetReturnValue(args, Dart_Null()); |
| 958 Dart_Port service_port = File::GetServicePort(); | 958 Dart_Port service_port = File::GetServicePort(); |
| 959 if (service_port != kIllegalPort) { | 959 if (service_port != kIllegalPort) { |
| 960 // Return a send port for the service port. | 960 // Return a send port for the service port. |
| 961 Dart_Handle send_port = Dart_NewSendPort(service_port); | 961 Dart_Handle send_port = Dart_NewSendPort(service_port); |
| 962 Dart_SetReturnValue(args, send_port); | 962 Dart_SetReturnValue(args, send_port); |
| 963 } | 963 } |
| 964 Dart_ExitScope(); | 964 Dart_ExitScope(); |
| 965 } | 965 } |
| OLD | NEW |