| 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/utils.h" | 10 #include "bin/utils.h" |
| 11 | 11 |
| 12 #include "include/dart_api.h" | 12 #include "include/dart_api.h" |
| 13 #include "include/dart_tools_api.h" |
| 13 | 14 |
| 14 namespace dart { | 15 namespace dart { |
| 15 namespace bin { | 16 namespace bin { |
| 16 | 17 |
| 17 static const int kMSPerSecond = 1000; | 18 static const int kMSPerSecond = 1000; |
| 18 | 19 |
| 20 // Are we capturing output from either stdout or stderr for the VM Service? |
| 21 bool capture_stdio = false; |
| 22 |
| 23 // Are we capturing output from stdout for the VM service? |
| 24 bool capture_stdout = false; |
| 25 |
| 26 // Are we capturing output from stderr for the VM service? |
| 27 bool capture_stderr = false; |
| 28 |
| 19 | 29 |
| 20 // The file pointer has been passed into Dart as an intptr_t and it is safe | 30 // The file pointer has been passed into Dart as an intptr_t and it is safe |
| 21 // to pull it out of Dart as a 64-bit integer, cast it to an intptr_t and | 31 // to pull it out of Dart as a 64-bit integer, cast it to an intptr_t and |
| 22 // from there to a File pointer. | 32 // from there to a File pointer. |
| 23 static File* GetFilePointer(Dart_Handle handle) { | 33 static File* GetFilePointer(Dart_Handle handle) { |
| 24 intptr_t value = DartUtils::GetIntptrValue(handle); | 34 intptr_t value = DartUtils::GetIntptrValue(handle); |
| 25 return reinterpret_cast<File*>(value); | 35 return reinterpret_cast<File*>(value); |
| 26 } | 36 } |
| 27 | 37 |
| 28 | 38 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 45 int64_t remaining = num_bytes; | 55 int64_t remaining = num_bytes; |
| 46 const char* current_buffer = reinterpret_cast<const char*>(buffer); | 56 const char* current_buffer = reinterpret_cast<const char*>(buffer); |
| 47 while (remaining > 0) { | 57 while (remaining > 0) { |
| 48 int64_t bytes_written = Write(current_buffer, remaining); | 58 int64_t bytes_written = Write(current_buffer, remaining); |
| 49 if (bytes_written < 0) { | 59 if (bytes_written < 0) { |
| 50 return false; | 60 return false; |
| 51 } | 61 } |
| 52 remaining -= bytes_written; // Reduce the number of remaining bytes. | 62 remaining -= bytes_written; // Reduce the number of remaining bytes. |
| 53 current_buffer += bytes_written; // Move the buffer forward. | 63 current_buffer += bytes_written; // Move the buffer forward. |
| 54 } | 64 } |
| 65 if (capture_stdio) { |
| 66 intptr_t fd = GetFD(); |
| 67 if (fd == STDOUT_FILENO && capture_stdout) { |
| 68 Dart_ServiceSendDataEvent("Stdout", "WriteEvent", |
| 69 reinterpret_cast<const uint8_t*>(buffer), |
| 70 num_bytes); |
| 71 } else if (fd == STDERR_FILENO && capture_stderr) { |
| 72 Dart_ServiceSendDataEvent("Stderr", "WriteEvent", |
| 73 reinterpret_cast<const uint8_t*>(buffer), |
| 74 num_bytes); |
| 75 } |
| 76 } |
| 55 return true; | 77 return true; |
| 56 } | 78 } |
| 57 | 79 |
| 58 | 80 |
| 59 File::FileOpenMode File::DartModeToFileMode(DartFileOpenMode mode) { | 81 File::FileOpenMode File::DartModeToFileMode(DartFileOpenMode mode) { |
| 60 ASSERT(mode == File::kDartRead || | 82 ASSERT(mode == File::kDartRead || |
| 61 mode == File::kDartWrite || | 83 mode == File::kDartWrite || |
| 62 mode == File::kDartAppend || | 84 mode == File::kDartAppend || |
| 63 mode == File::kDartWriteOnly || | 85 mode == File::kDartWriteOnly || |
| 64 mode == File::kDartWriteOnlyAppend); | 86 mode == File::kDartWriteOnlyAppend); |
| (...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 574 } else { | 596 } else { |
| 575 Dart_Handle err = DartUtils::NewDartOSError(); | 597 Dart_Handle err = DartUtils::NewDartOSError(); |
| 576 if (Dart_IsError(err)) Dart_PropagateError(err); | 598 if (Dart_IsError(err)) Dart_PropagateError(err); |
| 577 Dart_SetReturnValue(args, err); | 599 Dart_SetReturnValue(args, err); |
| 578 } | 600 } |
| 579 } | 601 } |
| 580 | 602 |
| 581 | 603 |
| 582 void FUNCTION_NAME(File_OpenStdio)(Dart_NativeArguments args) { | 604 void FUNCTION_NAME(File_OpenStdio)(Dart_NativeArguments args) { |
| 583 int64_t fd = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); | 605 int64_t fd = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); |
| 584 ASSERT(fd == 0 || fd == 1 || fd == 2); | 606 ASSERT(fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO); |
| 585 File* file = File::OpenStdio(static_cast<int>(fd)); | 607 File* file = File::OpenStdio(static_cast<int>(fd)); |
| 586 Dart_SetReturnValue(args, Dart_NewInteger(reinterpret_cast<intptr_t>(file))); | 608 Dart_SetReturnValue(args, Dart_NewInteger(reinterpret_cast<intptr_t>(file))); |
| 587 } | 609 } |
| 588 | 610 |
| 589 | 611 |
| 590 void FUNCTION_NAME(File_GetStdioHandleType)(Dart_NativeArguments args) { | 612 void FUNCTION_NAME(File_GetStdioHandleType)(Dart_NativeArguments args) { |
| 591 int64_t fd = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); | 613 int64_t fd = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); |
| 592 ASSERT(fd == 0 || fd == 1 || fd == 2); | 614 ASSERT(fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO); |
| 593 File::StdioHandleType type = File::GetStdioHandleType(static_cast<int>(fd)); | 615 File::StdioHandleType type = File::GetStdioHandleType(static_cast<int>(fd)); |
| 594 Dart_SetReturnValue(args, Dart_NewInteger(type)); | 616 Dart_SetReturnValue(args, Dart_NewInteger(type)); |
| 595 } | 617 } |
| 596 | 618 |
| 597 | 619 |
| 598 void FUNCTION_NAME(File_GetType)(Dart_NativeArguments args) { | 620 void FUNCTION_NAME(File_GetType)(Dart_NativeArguments args) { |
| 599 if (Dart_IsString(Dart_GetNativeArgument(args, 0)) && | 621 if (Dart_IsString(Dart_GetNativeArgument(args, 0)) && |
| 600 Dart_IsBoolean(Dart_GetNativeArgument(args, 1))) { | 622 Dart_IsBoolean(Dart_GetNativeArgument(args, 1))) { |
| 601 const char* str = | 623 const char* str = |
| 602 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 624 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
| (...skipping 653 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1256 } | 1278 } |
| 1257 } else { | 1279 } else { |
| 1258 return CObject::FileClosedError(); | 1280 return CObject::FileClosedError(); |
| 1259 } | 1281 } |
| 1260 } | 1282 } |
| 1261 return CObject::IllegalArgumentError(); | 1283 return CObject::IllegalArgumentError(); |
| 1262 } | 1284 } |
| 1263 | 1285 |
| 1264 } // namespace bin | 1286 } // namespace bin |
| 1265 } // namespace dart | 1287 } // namespace dart |
| OLD | NEW |