| 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/io_buffer.h" |
| 10 #include "bin/thread.h" | 10 #include "bin/thread.h" |
| (...skipping 761 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 772 return CObject::NewOSError(); | 772 return CObject::NewOSError(); |
| 773 } | 773 } |
| 774 } else { | 774 } else { |
| 775 return CObject::FileClosedError(); | 775 return CObject::FileClosedError(); |
| 776 } | 776 } |
| 777 } | 777 } |
| 778 return CObject::IllegalArgumentError(); | 778 return CObject::IllegalArgumentError(); |
| 779 } | 779 } |
| 780 | 780 |
| 781 | 781 |
| 782 static void FinalizeExternalByteArray(void* peer) { | |
| 783 delete[] reinterpret_cast<uint8_t*>(peer); | |
| 784 } | |
| 785 | |
| 786 | |
| 787 static CObject* FileReadRequest(const CObjectArray& request) { | 782 static CObject* FileReadRequest(const CObjectArray& request) { |
| 788 if (request.Length() == 3 && | 783 if (request.Length() == 3 && |
| 789 request[1]->IsIntptr() && | 784 request[1]->IsIntptr() && |
| 790 request[2]->IsInt32OrInt64()) { | 785 request[2]->IsInt32OrInt64()) { |
| 791 File* file = CObjectToFilePointer(request[1]); | 786 File* file = CObjectToFilePointer(request[1]); |
| 792 ASSERT(file != NULL); | 787 ASSERT(file != NULL); |
| 793 if (!file->IsClosed()) { | 788 if (!file->IsClosed()) { |
| 794 int64_t length = CObjectInt32OrInt64ToInt64(request[2]); | 789 int64_t length = CObjectInt32OrInt64ToInt64(request[2]); |
| 795 uint8_t* buffer = new uint8_t[length]; | 790 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); |
| 796 int64_t bytes_read = file->Read(buffer, length); | 791 uint8_t* data = io_buffer->value.as_external_byte_array.data; |
| 792 int64_t bytes_read = file->Read(data, length); |
| 797 if (bytes_read >= 0) { | 793 if (bytes_read >= 0) { |
| 798 void* peer = reinterpret_cast<void*>(buffer); | 794 CObjectExternalUint8Array* external_array = |
| 799 CObject* external_array = | 795 new CObjectExternalUint8Array(io_buffer); |
| 800 new CObjectExternalUint8Array( | 796 external_array->SetLength(bytes_read); |
| 801 CObject::NewExternalUint8Array(bytes_read, | |
| 802 buffer, | |
| 803 peer, | |
| 804 FinalizeExternalByteArray)); | |
| 805 CObjectArray* result = new CObjectArray(CObject::NewArray(2)); | 797 CObjectArray* result = new CObjectArray(CObject::NewArray(2)); |
| 806 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0))); | 798 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0))); |
| 807 result->SetAt(1, external_array); | 799 result->SetAt(1, external_array); |
| 808 return result; | 800 return result; |
| 809 } else { | 801 } else { |
| 802 CObject::FreeIOBufferData(io_buffer); |
| 810 return CObject::NewOSError(); | 803 return CObject::NewOSError(); |
| 811 } | 804 } |
| 812 } else { | 805 } else { |
| 813 return CObject::FileClosedError(); | 806 return CObject::FileClosedError(); |
| 814 } | 807 } |
| 815 } | 808 } |
| 816 return CObject::IllegalArgumentError(); | 809 return CObject::IllegalArgumentError(); |
| 817 } | 810 } |
| 818 | 811 |
| 819 | 812 |
| 820 static CObject* FileReadListRequest(const CObjectArray& request) { | 813 static CObject* FileReadListRequest(const CObjectArray& request) { |
| 821 if (request.Length() == 3 && | 814 if (request.Length() == 3 && |
| 822 request[1]->IsIntptr() && | 815 request[1]->IsIntptr() && |
| 823 request[2]->IsInt32OrInt64()) { | 816 request[2]->IsInt32OrInt64()) { |
| 824 File* file = CObjectToFilePointer(request[1]); | 817 File* file = CObjectToFilePointer(request[1]); |
| 825 ASSERT(file != NULL); | 818 ASSERT(file != NULL); |
| 826 if (!file->IsClosed()) { | 819 if (!file->IsClosed()) { |
| 827 int64_t length = CObjectInt32OrInt64ToInt64(request[2]); | 820 int64_t length = CObjectInt32OrInt64ToInt64(request[2]); |
| 828 uint8_t* buffer = new uint8_t[length]; | 821 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); |
| 829 int64_t bytes_read = file->Read(buffer, length); | 822 uint8_t* data = io_buffer->value.as_external_byte_array.data; |
| 823 int64_t bytes_read = file->Read(data, length); |
| 830 if (bytes_read >= 0) { | 824 if (bytes_read >= 0) { |
| 831 void* peer = reinterpret_cast<void*>(buffer); | 825 CObjectExternalUint8Array* external_array = |
| 832 CObject* external_array = | 826 new CObjectExternalUint8Array(io_buffer); |
| 833 new CObjectExternalUint8Array( | 827 external_array->SetLength(bytes_read); |
| 834 CObject::NewExternalUint8Array(length, | |
| 835 buffer, | |
| 836 peer, | |
| 837 FinalizeExternalByteArray)); | |
| 838 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 828 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
| 839 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0))); | 829 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0))); |
| 840 result->SetAt(1, new CObjectInt64(CObject::NewInt64(bytes_read))); | 830 result->SetAt(1, new CObjectInt64(CObject::NewInt64(bytes_read))); |
| 841 result->SetAt(2, external_array); | 831 result->SetAt(2, external_array); |
| 842 return result; | 832 return result; |
| 843 } else { | 833 } else { |
| 834 CObject::FreeIOBufferData(io_buffer); |
| 844 return CObject::NewOSError(); | 835 return CObject::NewOSError(); |
| 845 } | 836 } |
| 846 } else { | 837 } else { |
| 847 return CObject::FileClosedError(); | 838 return CObject::FileClosedError(); |
| 848 } | 839 } |
| 849 } | 840 } |
| 850 return CObject::IllegalArgumentError(); | 841 return CObject::IllegalArgumentError(); |
| 851 } | 842 } |
| 852 | 843 |
| 853 | 844 |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1004 Dart_EnterScope(); | 995 Dart_EnterScope(); |
| 1005 Dart_SetReturnValue(args, Dart_Null()); | 996 Dart_SetReturnValue(args, Dart_Null()); |
| 1006 Dart_Port service_port = File::GetServicePort(); | 997 Dart_Port service_port = File::GetServicePort(); |
| 1007 if (service_port != ILLEGAL_PORT) { | 998 if (service_port != ILLEGAL_PORT) { |
| 1008 // Return a send port for the service port. | 999 // Return a send port for the service port. |
| 1009 Dart_Handle send_port = Dart_NewSendPort(service_port); | 1000 Dart_Handle send_port = Dart_NewSendPort(service_port); |
| 1010 Dart_SetReturnValue(args, send_port); | 1001 Dart_SetReturnValue(args, send_port); |
| 1011 } | 1002 } |
| 1012 Dart_ExitScope(); | 1003 Dart_ExitScope(); |
| 1013 } | 1004 } |
| OLD | NEW |