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