| 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/dartutils.h" | 5 #include "bin/dartutils.h" |
| 6 #include "bin/process.h" | 6 #include "bin/process.h" |
| 7 | 7 |
| 8 #include "include/dart_api.h" | 8 #include "include/dart_api.h" |
| 9 | 9 |
| 10 void FUNCTION_NAME(Process_Start)(Dart_NativeArguments args) { | 10 void FUNCTION_NAME(Process_Start)(Dart_NativeArguments args) { |
| 11 Dart_EnterScope(); | 11 Dart_EnterScope(); |
| 12 Dart_Handle process = Dart_GetNativeArgument(args, 0); | 12 Dart_Handle process = Dart_GetNativeArgument(args, 0); |
| 13 intptr_t in; | 13 intptr_t in; |
| 14 intptr_t out; | 14 intptr_t out; |
| 15 intptr_t err; | 15 intptr_t err; |
| 16 intptr_t exit_event; | 16 intptr_t exit_event; |
| 17 Dart_Handle status_handle = Dart_GetNativeArgument(args, 8); | 17 Dart_Handle status_handle = Dart_GetNativeArgument(args, 8); |
| 18 Dart_Handle path_handle = Dart_GetNativeArgument(args, 1); | 18 Dart_Handle path_handle = Dart_GetNativeArgument(args, 1); |
| 19 // The Dart code verifies that the path implements the String | 19 // The Dart code verifies that the path implements the String |
| 20 // interface. However, only builtin Strings are handled by | 20 // interface. However, only builtin Strings are handled by |
| 21 // GetStringValue. | 21 // GetStringValue. |
| 22 if (!Dart_IsString(path_handle)) { | 22 if (!Dart_IsString(path_handle)) { |
| 23 DartUtils::SetIntegerInstanceField(status_handle, "_errorCode", 0); | 23 DartUtils::SetIntegerField(status_handle, "_errorCode", 0); |
| 24 DartUtils::SetStringInstanceField( | 24 DartUtils::SetStringField( |
| 25 status_handle, "_errorMessage", "Path must be a builtin string"); | 25 status_handle, "_errorMessage", "Path must be a builtin string"); |
| 26 Dart_SetReturnValue(args, Dart_NewBoolean(false)); | 26 Dart_SetReturnValue(args, Dart_NewBoolean(false)); |
| 27 Dart_ExitScope(); | 27 Dart_ExitScope(); |
| 28 return; | 28 return; |
| 29 } | 29 } |
| 30 const char* path = DartUtils::GetStringValue(path_handle); | 30 const char* path = DartUtils::GetStringValue(path_handle); |
| 31 Dart_Handle arguments = Dart_GetNativeArgument(args, 2); | 31 Dart_Handle arguments = Dart_GetNativeArgument(args, 2); |
| 32 // The arguments are copied into a non-extensible array in the | 32 // The arguments are copied into a non-extensible array in the |
| 33 // dart code so this should not fail. | 33 // dart code so this should not fail. |
| 34 ASSERT(Dart_IsList(arguments)); | 34 ASSERT(Dart_IsList(arguments)); |
| 35 intptr_t length = 0; | 35 intptr_t length = 0; |
| 36 Dart_Handle result = Dart_ListLength(arguments, &length); | 36 Dart_Handle result = Dart_ListLength(arguments, &length); |
| 37 ASSERT(!Dart_IsError(result)); | 37 ASSERT(!Dart_IsError(result)); |
| 38 char** string_args = new char*[length]; | 38 char** string_args = new char*[length]; |
| 39 for (int i = 0; i < length; i++) { | 39 for (int i = 0; i < length; i++) { |
| 40 Dart_Handle arg = Dart_ListGetAt(arguments, i); | 40 Dart_Handle arg = Dart_ListGetAt(arguments, i); |
| 41 ASSERT(!Dart_IsError(arg)); | 41 ASSERT(!Dart_IsError(arg)); |
| 42 // The Dart code verifies that the arguments implement the String | 42 // The Dart code verifies that the arguments implement the String |
| 43 // interface. However, only builtin Strings are handled by | 43 // interface. However, only builtin Strings are handled by |
| 44 // GetStringValue. | 44 // GetStringValue. |
| 45 if (!Dart_IsString(arg)) { | 45 if (!Dart_IsString(arg)) { |
| 46 DartUtils::SetIntegerInstanceField(status_handle, "_errorCode", 0); | 46 DartUtils::SetIntegerField(status_handle, "_errorCode", 0); |
| 47 DartUtils::SetStringInstanceField( | 47 DartUtils::SetStringField( |
| 48 status_handle, "_errorMessage", "Arguments must be builtin strings"); | 48 status_handle, "_errorMessage", "Arguments must be builtin strings"); |
| 49 delete[] string_args; | 49 delete[] string_args; |
| 50 Dart_SetReturnValue(args, Dart_NewBoolean(false)); | 50 Dart_SetReturnValue(args, Dart_NewBoolean(false)); |
| 51 Dart_ExitScope(); | 51 Dart_ExitScope(); |
| 52 return; | 52 return; |
| 53 } | 53 } |
| 54 string_args[i] = const_cast<char *>(DartUtils::GetStringValue(arg)); | 54 string_args[i] = const_cast<char *>(DartUtils::GetStringValue(arg)); |
| 55 } | 55 } |
| 56 Dart_Handle working_directory_handle = Dart_GetNativeArgument(args, 3); | 56 Dart_Handle working_directory_handle = Dart_GetNativeArgument(args, 3); |
| 57 // Defaults to the current working directoy. | 57 // Defaults to the current working directoy. |
| 58 const char* working_directory = NULL; | 58 const char* working_directory = NULL; |
| 59 if (Dart_IsString(working_directory_handle)) { | 59 if (Dart_IsString(working_directory_handle)) { |
| 60 working_directory = DartUtils::GetStringValue(working_directory_handle); | 60 working_directory = DartUtils::GetStringValue(working_directory_handle); |
| 61 } else if (!Dart_IsNull(working_directory_handle)) { | 61 } else if (!Dart_IsNull(working_directory_handle)) { |
| 62 delete[] string_args; | 62 delete[] string_args; |
| 63 DartUtils::SetIntegerInstanceField(status_handle, "_errorCode", 0); | 63 DartUtils::SetIntegerField(status_handle, "_errorCode", 0); |
| 64 DartUtils::SetStringInstanceField( | 64 DartUtils::SetStringField( |
| 65 status_handle, "_errorMessage", | 65 status_handle, "_errorMessage", |
| 66 "WorkingDirectory must be a builtin string"); | 66 "WorkingDirectory must be a builtin string"); |
| 67 Dart_SetReturnValue(args, Dart_NewBoolean(false)); | 67 Dart_SetReturnValue(args, Dart_NewBoolean(false)); |
| 68 Dart_ExitScope(); | 68 Dart_ExitScope(); |
| 69 return; | 69 return; |
| 70 } | 70 } |
| 71 Dart_Handle in_handle = Dart_GetNativeArgument(args, 4); | 71 Dart_Handle in_handle = Dart_GetNativeArgument(args, 4); |
| 72 Dart_Handle out_handle = Dart_GetNativeArgument(args, 5); | 72 Dart_Handle out_handle = Dart_GetNativeArgument(args, 5); |
| 73 Dart_Handle err_handle = Dart_GetNativeArgument(args, 6); | 73 Dart_Handle err_handle = Dart_GetNativeArgument(args, 6); |
| 74 Dart_Handle exit_handle = Dart_GetNativeArgument(args, 7); | 74 Dart_Handle exit_handle = Dart_GetNativeArgument(args, 7); |
| 75 intptr_t pid = -1; | 75 intptr_t pid = -1; |
| 76 static const int kMaxChildOsErrorMessageLength = 256; | 76 static const int kMaxChildOsErrorMessageLength = 256; |
| 77 char os_error_message[kMaxChildOsErrorMessageLength]; | 77 char os_error_message[kMaxChildOsErrorMessageLength]; |
| 78 | 78 |
| 79 int error_code = Process::Start(path, | 79 int error_code = Process::Start(path, |
| 80 string_args, | 80 string_args, |
| 81 length, | 81 length, |
| 82 working_directory, | 82 working_directory, |
| 83 &in, | 83 &in, |
| 84 &out, | 84 &out, |
| 85 &err, | 85 &err, |
| 86 &pid, | 86 &pid, |
| 87 &exit_event, | 87 &exit_event, |
| 88 os_error_message, kMaxChildOsErrorMessageLength); | 88 os_error_message, kMaxChildOsErrorMessageLength); |
| 89 if (error_code == 0) { | 89 if (error_code == 0) { |
| 90 DartUtils::SetIntegerInstanceField(in_handle, DartUtils::kIdFieldName, in); | 90 DartUtils::SetIntegerField(in_handle, DartUtils::kIdFieldName, in); |
| 91 DartUtils::SetIntegerInstanceField( | 91 DartUtils::SetIntegerField( |
| 92 out_handle, DartUtils::kIdFieldName, out); | 92 out_handle, DartUtils::kIdFieldName, out); |
| 93 DartUtils::SetIntegerInstanceField( | 93 DartUtils::SetIntegerField( |
| 94 err_handle, DartUtils::kIdFieldName, err); | 94 err_handle, DartUtils::kIdFieldName, err); |
| 95 DartUtils::SetIntegerInstanceField( | 95 DartUtils::SetIntegerField( |
| 96 exit_handle, DartUtils::kIdFieldName, exit_event); | 96 exit_handle, DartUtils::kIdFieldName, exit_event); |
| 97 DartUtils::SetIntegerInstanceField(process, "_pid", pid); | 97 DartUtils::SetIntegerField(process, "_pid", pid); |
| 98 } else { | 98 } else { |
| 99 DartUtils::SetIntegerInstanceField( | 99 DartUtils::SetIntegerField( |
| 100 status_handle, "_errorCode", error_code); | 100 status_handle, "_errorCode", error_code); |
| 101 DartUtils::SetStringInstanceField( | 101 DartUtils::SetStringField( |
| 102 status_handle, "_errorMessage", os_error_message); | 102 status_handle, "_errorMessage", os_error_message); |
| 103 } | 103 } |
| 104 delete[] string_args; | 104 delete[] string_args; |
| 105 Dart_SetReturnValue(args, Dart_NewBoolean(error_code == 0)); | 105 Dart_SetReturnValue(args, Dart_NewBoolean(error_code == 0)); |
| 106 Dart_ExitScope(); | 106 Dart_ExitScope(); |
| 107 } | 107 } |
| 108 | 108 |
| 109 | 109 |
| 110 void FUNCTION_NAME(Process_Kill)(Dart_NativeArguments args) { | 110 void FUNCTION_NAME(Process_Kill)(Dart_NativeArguments args) { |
| 111 Dart_EnterScope(); | 111 Dart_EnterScope(); |
| 112 intptr_t pid = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); | 112 intptr_t pid = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
| 113 bool success = Process::Kill(pid); | 113 bool success = Process::Kill(pid); |
| 114 Dart_SetReturnValue(args, Dart_NewBoolean(success)); | 114 Dart_SetReturnValue(args, Dart_NewBoolean(success)); |
| 115 Dart_ExitScope(); | 115 Dart_ExitScope(); |
| 116 } | 116 } |
| OLD | NEW |