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 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
6 | 6 |
7 #include "bin/file.h" | 7 #include "bin/file.h" |
8 | 8 |
9 #include "bin/builtin.h" | 9 #include "bin/builtin.h" |
10 #include "bin/dartutils.h" | 10 #include "bin/dartutils.h" |
11 #include "bin/embedded_dart_io.h" | 11 #include "bin/embedded_dart_io.h" |
12 #include "bin/io_buffer.h" | 12 #include "bin/io_buffer.h" |
13 #include "bin/utils.h" | 13 #include "bin/utils.h" |
14 | |
15 #include "include/dart_api.h" | 14 #include "include/dart_api.h" |
16 #include "include/dart_tools_api.h" | 15 #include "include/dart_tools_api.h" |
| 16 #include "platform/globals.h" |
17 | 17 |
18 namespace dart { | 18 namespace dart { |
19 namespace bin { | 19 namespace bin { |
20 | 20 |
21 static const int kFileNativeFieldIndex = 0; | 21 static const int kFileNativeFieldIndex = 0; |
22 static const int kMSPerSecond = 1000; | |
23 | 22 |
24 // The file pointer has been passed into Dart as an intptr_t and it is safe | 23 // The file pointer has been passed into Dart as an intptr_t and it is safe |
25 // to pull it out of Dart as a 64-bit integer, cast it to an intptr_t and | 24 // to pull it out of Dart as a 64-bit integer, cast it to an intptr_t and |
26 // from there to a File pointer. | 25 // from there to a File pointer. |
27 static File* GetFile(Dart_NativeArguments args) { | 26 static File* GetFile(Dart_NativeArguments args) { |
28 File* file; | 27 File* file; |
29 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); | 28 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); |
30 ASSERT(Dart_IsInstance(dart_this)); | 29 ASSERT(Dart_IsInstance(dart_this)); |
31 ThrowIfError(Dart_GetNativeInstanceField(dart_this, kFileNativeFieldIndex, | 30 ThrowIfError(Dart_GetNativeInstanceField(dart_this, kFileNativeFieldIndex, |
32 reinterpret_cast<intptr_t*>(&file))); | 31 reinterpret_cast<intptr_t*>(&file))); |
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 } else { | 338 } else { |
340 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 339 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
341 } | 340 } |
342 } | 341 } |
343 | 342 |
344 | 343 |
345 void FUNCTION_NAME(File_LastModified)(Dart_NativeArguments args) { | 344 void FUNCTION_NAME(File_LastModified)(Dart_NativeArguments args) { |
346 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 345 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
347 int64_t return_value = File::LastModified(name); | 346 int64_t return_value = File::LastModified(name); |
348 if (return_value >= 0) { | 347 if (return_value >= 0) { |
349 Dart_SetReturnValue(args, Dart_NewInteger(return_value * kMSPerSecond)); | 348 Dart_SetReturnValue(args, |
| 349 Dart_NewInteger(return_value * kMillisecondsPerSecond)); |
350 } else { | 350 } else { |
351 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 351 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
352 } | 352 } |
353 } | 353 } |
354 | 354 |
355 | 355 |
| 356 void FUNCTION_NAME(File_SetLastModified)(Dart_NativeArguments args) { |
| 357 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
| 358 int64_t millis; |
| 359 if (!DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &millis)) { |
| 360 Dart_ThrowException(DartUtils::NewDartArgumentError( |
| 361 "The second argument must be a 64-bit int.")); |
| 362 } |
| 363 if (!File::SetLastModified(name, millis)) { |
| 364 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 365 } |
| 366 } |
| 367 |
| 368 |
| 369 void FUNCTION_NAME(File_LastAccessed)(Dart_NativeArguments args) { |
| 370 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
| 371 int64_t return_value = File::LastAccessed(name); |
| 372 if (return_value >= 0) { |
| 373 Dart_SetReturnValue(args, |
| 374 Dart_NewInteger(return_value * kMillisecondsPerSecond)); |
| 375 } else { |
| 376 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 377 } |
| 378 } |
| 379 |
| 380 |
| 381 void FUNCTION_NAME(File_SetLastAccessed)(Dart_NativeArguments args) { |
| 382 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
| 383 int64_t millis; |
| 384 if (!DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &millis)) { |
| 385 Dart_ThrowException(DartUtils::NewDartArgumentError( |
| 386 "The second argument must be a 64-bit int.")); |
| 387 } |
| 388 if (!File::SetLastAccessed(name, millis)) { |
| 389 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 390 } |
| 391 } |
| 392 |
| 393 |
356 void FUNCTION_NAME(File_Flush)(Dart_NativeArguments args) { | 394 void FUNCTION_NAME(File_Flush)(Dart_NativeArguments args) { |
357 File* file = GetFile(args); | 395 File* file = GetFile(args); |
358 ASSERT(file != NULL); | 396 ASSERT(file != NULL); |
359 if (file->Flush()) { | 397 if (file->Flush()) { |
360 Dart_SetReturnValue(args, Dart_True()); | 398 Dart_SetReturnValue(args, Dart_True()); |
361 } else { | 399 } else { |
362 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 400 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
363 } | 401 } |
364 } | 402 } |
365 | 403 |
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
839 if (return_value >= 0) { | 877 if (return_value >= 0) { |
840 return new CObjectInt64(CObject::NewInt64(return_value)); | 878 return new CObjectInt64(CObject::NewInt64(return_value)); |
841 } else { | 879 } else { |
842 return CObject::NewOSError(); | 880 return CObject::NewOSError(); |
843 } | 881 } |
844 } | 882 } |
845 return CObject::IllegalArgumentError(); | 883 return CObject::IllegalArgumentError(); |
846 } | 884 } |
847 | 885 |
848 | 886 |
| 887 CObject* File::LastAccessedRequest(const CObjectArray& request) { |
| 888 if ((request.Length() == 1) && request[0]->IsString()) { |
| 889 CObjectString filepath(request[0]); |
| 890 int64_t return_value = File::LastAccessed(filepath.CString()); |
| 891 if (return_value >= 0) { |
| 892 return new CObjectIntptr( |
| 893 CObject::NewInt64(return_value * kMillisecondsPerSecond)); |
| 894 } else { |
| 895 return CObject::NewOSError(); |
| 896 } |
| 897 } |
| 898 return CObject::IllegalArgumentError(); |
| 899 } |
| 900 |
| 901 |
| 902 CObject* File::SetLastAccessedRequest(const CObjectArray& request) { |
| 903 if ((request.Length() == 2) && request[0]->IsString() && |
| 904 request[1]->IsInt32OrInt64()) { |
| 905 CObjectString filepath(request[0]); |
| 906 const int64_t millis = CObjectInt32OrInt64ToInt64(request[1]); |
| 907 if (File::SetLastAccessed(filepath.CString(), millis)) { |
| 908 return CObject::Null(); |
| 909 } else { |
| 910 return CObject::NewOSError(); |
| 911 } |
| 912 } |
| 913 return CObject::IllegalArgumentError(); |
| 914 } |
| 915 |
| 916 |
849 CObject* File::LastModifiedRequest(const CObjectArray& request) { | 917 CObject* File::LastModifiedRequest(const CObjectArray& request) { |
850 if ((request.Length() == 1) && request[0]->IsString()) { | 918 if ((request.Length() == 1) && request[0]->IsString()) { |
851 CObjectString filepath(request[0]); | 919 CObjectString filepath(request[0]); |
852 int64_t return_value = File::LastModified(filepath.CString()); | 920 int64_t return_value = File::LastModified(filepath.CString()); |
853 if (return_value >= 0) { | 921 if (return_value >= 0) { |
854 return new CObjectIntptr(CObject::NewInt64(return_value * kMSPerSecond)); | 922 return new CObjectIntptr( |
| 923 CObject::NewInt64(return_value * kMillisecondsPerSecond)); |
855 } else { | 924 } else { |
856 return CObject::NewOSError(); | 925 return CObject::NewOSError(); |
857 } | 926 } |
| 927 } |
| 928 return CObject::IllegalArgumentError(); |
| 929 } |
| 930 |
| 931 |
| 932 CObject* File::SetLastModifiedRequest(const CObjectArray& request) { |
| 933 if ((request.Length() == 2) && request[0]->IsString() && |
| 934 request[1]->IsInt32OrInt64()) { |
| 935 CObjectString filepath(request[0]); |
| 936 const int64_t millis = CObjectInt32OrInt64ToInt64(request[1]); |
| 937 if (File::SetLastModified(filepath.CString(), millis)) { |
| 938 return CObject::Null(); |
| 939 } else { |
| 940 return CObject::NewOSError(); |
| 941 } |
858 } | 942 } |
859 return CObject::IllegalArgumentError(); | 943 return CObject::IllegalArgumentError(); |
860 } | 944 } |
861 | 945 |
862 | 946 |
863 CObject* File::FlushRequest(const CObjectArray& request) { | 947 CObject* File::FlushRequest(const CObjectArray& request) { |
864 if ((request.Length() == 1) && request[0]->IsIntptr()) { | 948 if ((request.Length() == 1) && request[0]->IsIntptr()) { |
865 File* file = CObjectToFilePointer(request[0]); | 949 File* file = CObjectToFilePointer(request[0]); |
866 RefCntReleaseScope<File> rs(file); | 950 RefCntReleaseScope<File> rs(file); |
867 if (!file->IsClosed()) { | 951 if (!file->IsClosed()) { |
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1201 return CObject::IllegalArgumentError(); | 1285 return CObject::IllegalArgumentError(); |
1202 } | 1286 } |
1203 } | 1287 } |
1204 return CObject::IllegalArgumentError(); | 1288 return CObject::IllegalArgumentError(); |
1205 } | 1289 } |
1206 | 1290 |
1207 } // namespace bin | 1291 } // namespace bin |
1208 } // namespace dart | 1292 } // namespace dart |
1209 | 1293 |
1210 #endif // !defined(DART_IO_DISABLED) | 1294 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |