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