| 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/dartutils.h" | 5 #include "bin/dartutils.h" |
| 6 #include "bin/eventhandler.h" |
| 6 #include "bin/io_buffer.h" | 7 #include "bin/io_buffer.h" |
| 8 #include "bin/log.h" |
| 7 #include "bin/platform.h" | 9 #include "bin/platform.h" |
| 8 #include "bin/process.h" | 10 #include "bin/process.h" |
| 9 #include "bin/socket.h" | 11 #include "bin/socket.h" |
| 10 #include "bin/utils.h" | 12 #include "bin/utils.h" |
| 11 | 13 |
| 12 #include "include/dart_api.h" | 14 #include "include/dart_api.h" |
| 13 | 15 |
| 14 namespace dart { | 16 namespace dart { |
| 15 namespace bin { | 17 namespace bin { |
| 16 | 18 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 27 static const intptr_t kMaxArgumentListLength = 1024 * 1024; | 29 static const intptr_t kMaxArgumentListLength = 1024 * 1024; |
| 28 ASSERT(Dart_IsList(strings)); | 30 ASSERT(Dart_IsList(strings)); |
| 29 intptr_t len = 0; | 31 intptr_t len = 0; |
| 30 Dart_Handle result = Dart_ListLength(strings, &len); | 32 Dart_Handle result = Dart_ListLength(strings, &len); |
| 31 if (Dart_IsError(result)) { | 33 if (Dart_IsError(result)) { |
| 32 Dart_PropagateError(result); | 34 Dart_PropagateError(result); |
| 33 } | 35 } |
| 34 // Protect against user-defined list implementations that can have | 36 // Protect against user-defined list implementations that can have |
| 35 // arbitrary length. | 37 // arbitrary length. |
| 36 if (len < 0 || len > kMaxArgumentListLength) { | 38 if (len < 0 || len > kMaxArgumentListLength) { |
| 37 DartUtils::SetIntegerField(status_handle, "_errorCode", 0); | 39 result = DartUtils::SetIntegerField(status_handle, "_errorCode", 0); |
| 38 DartUtils::SetStringField( | 40 if (Dart_IsError(result)) { |
| 41 Dart_PropagateError(result); |
| 42 } |
| 43 result = DartUtils::SetStringField( |
| 39 status_handle, "_errorMessage", "Max argument list length exceeded"); | 44 status_handle, "_errorMessage", "Max argument list length exceeded"); |
| 45 if (Dart_IsError(result)) { |
| 46 Dart_PropagateError(result); |
| 47 } |
| 40 return NULL; | 48 return NULL; |
| 41 } | 49 } |
| 42 *length = len; | 50 *length = len; |
| 43 char** string_args = new char*[len]; | 51 char** string_args = new char*[len]; |
| 44 for (int i = 0; i < len; i++) { | 52 for (int i = 0; i < len; i++) { |
| 45 Dart_Handle arg = Dart_ListGetAt(strings, i); | 53 Dart_Handle arg = Dart_ListGetAt(strings, i); |
| 46 if (Dart_IsError(arg)) { | 54 if (Dart_IsError(arg)) { |
| 47 delete[] string_args; | 55 delete[] string_args; |
| 48 Dart_PropagateError(arg); | 56 Dart_PropagateError(arg); |
| 49 } | 57 } |
| 50 if (!Dart_IsString(arg)) { | 58 if (!Dart_IsString(arg)) { |
| 51 DartUtils::SetIntegerField(status_handle, "_errorCode", 0); | 59 result = DartUtils::SetIntegerField(status_handle, "_errorCode", 0); |
| 52 DartUtils::SetStringField( | 60 if (Dart_IsError(result)) { |
| 61 Dart_PropagateError(result); |
| 62 } |
| 63 result = DartUtils::SetStringField( |
| 53 status_handle, "_errorMessage", error_msg); | 64 status_handle, "_errorMessage", error_msg); |
| 65 if (Dart_IsError(result)) { |
| 66 Dart_PropagateError(result); |
| 67 } |
| 54 delete[] string_args; | 68 delete[] string_args; |
| 55 return NULL; | 69 return NULL; |
| 56 } | 70 } |
| 57 string_args[i] = const_cast<char *>(DartUtils::GetStringValue(arg)); | 71 string_args[i] = const_cast<char *>(DartUtils::GetStringValue(arg)); |
| 58 } | 72 } |
| 59 return string_args; | 73 return string_args; |
| 60 } | 74 } |
| 61 | 75 |
| 62 void FUNCTION_NAME(Process_Start)(Dart_NativeArguments args) { | 76 void FUNCTION_NAME(Process_Start)(Dart_NativeArguments args) { |
| 63 Dart_Handle process = Dart_GetNativeArgument(args, 0); | 77 Dart_Handle process = Dart_GetNativeArgument(args, 0); |
| 64 intptr_t process_stdin; | 78 intptr_t process_stdin; |
| 65 intptr_t process_stdout; | 79 intptr_t process_stdout; |
| 66 intptr_t process_stderr; | 80 intptr_t process_stderr; |
| 67 intptr_t exit_event; | 81 intptr_t exit_event; |
| 82 Dart_Handle result; |
| 68 Dart_Handle status_handle = Dart_GetNativeArgument(args, 10); | 83 Dart_Handle status_handle = Dart_GetNativeArgument(args, 10); |
| 69 Dart_Handle path_handle = Dart_GetNativeArgument(args, 1); | 84 Dart_Handle path_handle = Dart_GetNativeArgument(args, 1); |
| 70 // The Dart code verifies that the path implements the String | 85 // The Dart code verifies that the path implements the String |
| 71 // interface. However, only builtin Strings are handled by | 86 // interface. However, only builtin Strings are handled by |
| 72 // GetStringValue. | 87 // GetStringValue. |
| 73 if (!Dart_IsString(path_handle)) { | 88 if (!Dart_IsString(path_handle)) { |
| 74 DartUtils::SetIntegerField(status_handle, "_errorCode", 0); | 89 result = DartUtils::SetIntegerField(status_handle, "_errorCode", 0); |
| 75 DartUtils::SetStringField( | 90 if (Dart_IsError(result)) { |
| 91 Dart_PropagateError(result); |
| 92 } |
| 93 result = DartUtils::SetStringField( |
| 76 status_handle, "_errorMessage", "Path must be a builtin string"); | 94 status_handle, "_errorMessage", "Path must be a builtin string"); |
| 95 if (Dart_IsError(result)) { |
| 96 Dart_PropagateError(result); |
| 97 } |
| 77 Dart_SetReturnValue(args, Dart_NewBoolean(false)); | 98 Dart_SetReturnValue(args, Dart_NewBoolean(false)); |
| 78 return; | 99 return; |
| 79 } | 100 } |
| 80 const char* path = DartUtils::GetStringValue(path_handle); | 101 const char* path = DartUtils::GetStringValue(path_handle); |
| 81 Dart_Handle arguments = Dart_GetNativeArgument(args, 2); | 102 Dart_Handle arguments = Dart_GetNativeArgument(args, 2); |
| 82 intptr_t args_length = 0; | 103 intptr_t args_length = 0; |
| 83 char** string_args = | 104 char** string_args = |
| 84 ExtractCStringList(arguments, | 105 ExtractCStringList(arguments, |
| 85 status_handle, | 106 status_handle, |
| 86 "Arguments must be builtin strings", | 107 "Arguments must be builtin strings", |
| 87 &args_length); | 108 &args_length); |
| 88 if (string_args == NULL) { | 109 if (string_args == NULL) { |
| 89 Dart_SetReturnValue(args, Dart_NewBoolean(false)); | 110 Dart_SetReturnValue(args, Dart_NewBoolean(false)); |
| 90 return; | 111 return; |
| 91 } | 112 } |
| 92 Dart_Handle working_directory_handle = Dart_GetNativeArgument(args, 3); | 113 Dart_Handle working_directory_handle = Dart_GetNativeArgument(args, 3); |
| 93 // Defaults to the current working directoy. | 114 // Defaults to the current working directoy. |
| 94 const char* working_directory = NULL; | 115 const char* working_directory = NULL; |
| 95 if (Dart_IsString(working_directory_handle)) { | 116 if (Dart_IsString(working_directory_handle)) { |
| 96 working_directory = DartUtils::GetStringValue(working_directory_handle); | 117 working_directory = DartUtils::GetStringValue(working_directory_handle); |
| 97 } else if (!Dart_IsNull(working_directory_handle)) { | 118 } else if (!Dart_IsNull(working_directory_handle)) { |
| 98 delete[] string_args; | 119 delete[] string_args; |
| 99 DartUtils::SetIntegerField(status_handle, "_errorCode", 0); | 120 result = DartUtils::SetIntegerField(status_handle, "_errorCode", 0); |
| 100 DartUtils::SetStringField( | 121 if (Dart_IsError(result)) { |
| 122 Dart_PropagateError(result); |
| 123 } |
| 124 result = DartUtils::SetStringField( |
| 101 status_handle, "_errorMessage", | 125 status_handle, "_errorMessage", |
| 102 "WorkingDirectory must be a builtin string"); | 126 "WorkingDirectory must be a builtin string"); |
| 127 if (Dart_IsError(result)) { |
| 128 Dart_PropagateError(result); |
| 129 } |
| 103 Dart_SetReturnValue(args, Dart_NewBoolean(false)); | 130 Dart_SetReturnValue(args, Dart_NewBoolean(false)); |
| 104 return; | 131 return; |
| 105 } | 132 } |
| 106 Dart_Handle environment = Dart_GetNativeArgument(args, 4); | 133 Dart_Handle environment = Dart_GetNativeArgument(args, 4); |
| 107 intptr_t environment_length = 0; | 134 intptr_t environment_length = 0; |
| 108 char** string_environment = NULL; | 135 char** string_environment = NULL; |
| 109 if (!Dart_IsNull(environment)) { | 136 if (!Dart_IsNull(environment)) { |
| 110 string_environment = | 137 string_environment = |
| 111 ExtractCStringList(environment, | 138 ExtractCStringList(environment, |
| 112 status_handle, | 139 status_handle, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 if (mode != kDetached) { | 171 if (mode != kDetached) { |
| 145 Socket::SetSocketIdNativeField(stdin_handle, process_stdin); | 172 Socket::SetSocketIdNativeField(stdin_handle, process_stdin); |
| 146 Socket::SetSocketIdNativeField(stdout_handle, process_stdout); | 173 Socket::SetSocketIdNativeField(stdout_handle, process_stdout); |
| 147 Socket::SetSocketIdNativeField(stderr_handle, process_stderr); | 174 Socket::SetSocketIdNativeField(stderr_handle, process_stderr); |
| 148 } | 175 } |
| 149 if (mode == kNormal) { | 176 if (mode == kNormal) { |
| 150 Socket::SetSocketIdNativeField(exit_handle, exit_event); | 177 Socket::SetSocketIdNativeField(exit_handle, exit_event); |
| 151 } | 178 } |
| 152 Process::SetProcessIdNativeField(process, pid); | 179 Process::SetProcessIdNativeField(process, pid); |
| 153 } else { | 180 } else { |
| 154 DartUtils::SetIntegerField( | 181 result = DartUtils::SetIntegerField( |
| 155 status_handle, "_errorCode", error_code); | 182 status_handle, "_errorCode", error_code); |
| 156 DartUtils::SetStringField( | 183 if (Dart_IsError(result)) { |
| 184 Dart_PropagateError(result); |
| 185 } |
| 186 result = DartUtils::SetStringField( |
| 157 status_handle, | 187 status_handle, |
| 158 "_errorMessage", | 188 "_errorMessage", |
| 159 os_error_message != NULL ? os_error_message | 189 os_error_message != NULL ? os_error_message |
| 160 : "Cannot get error message"); | 190 : "Cannot get error message"); |
| 191 if (Dart_IsError(result)) { |
| 192 Dart_PropagateError(result); |
| 193 } |
| 161 } | 194 } |
| 162 delete[] string_args; | 195 delete[] string_args; |
| 163 delete[] string_environment; | 196 delete[] string_environment; |
| 164 free(os_error_message); | 197 free(os_error_message); |
| 165 Dart_SetReturnValue(args, Dart_NewBoolean(error_code == 0)); | 198 Dart_SetReturnValue(args, Dart_NewBoolean(error_code == 0)); |
| 166 } | 199 } |
| 167 | 200 |
| 168 | 201 |
| 169 void FUNCTION_NAME(Process_Wait)(Dart_NativeArguments args) { | 202 void FUNCTION_NAME(Process_Wait)(Dart_NativeArguments args) { |
| 170 Dart_Handle process = Dart_GetNativeArgument(args, 0); | 203 Dart_Handle process = Dart_GetNativeArgument(args, 0); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 intptr_t signal = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); | 242 intptr_t signal = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); |
| 210 bool success = Process::Kill(pid, signal); | 243 bool success = Process::Kill(pid, signal); |
| 211 Dart_SetReturnValue(args, Dart_NewBoolean(success)); | 244 Dart_SetReturnValue(args, Dart_NewBoolean(success)); |
| 212 } | 245 } |
| 213 | 246 |
| 214 | 247 |
| 215 void FUNCTION_NAME(Process_Exit)(Dart_NativeArguments args) { | 248 void FUNCTION_NAME(Process_Exit)(Dart_NativeArguments args) { |
| 216 int64_t status = 0; | 249 int64_t status = 0; |
| 217 // Ignore result if passing invalid argument and just exit 0. | 250 // Ignore result if passing invalid argument and just exit 0. |
| 218 DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 0), &status); | 251 DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 0), &status); |
| 219 Dart_ExitIsolate(); | 252 Dart_ShutdownIsolate(); |
| 220 Dart_Cleanup(); | 253 Process::TerminateExitCodeHandler(); |
| 254 char* error = Dart_Cleanup(); |
| 255 if (error != NULL) { |
| 256 Log::PrintErr("VM cleanup failed: %s\n", error); |
| 257 free(error); |
| 258 } |
| 259 EventHandler::Stop(); |
| 221 exit(static_cast<int>(status)); | 260 exit(static_cast<int>(status)); |
| 222 } | 261 } |
| 223 | 262 |
| 224 | 263 |
| 225 void FUNCTION_NAME(Process_SetExitCode)(Dart_NativeArguments args) { | 264 void FUNCTION_NAME(Process_SetExitCode)(Dart_NativeArguments args) { |
| 226 int64_t status = 0; | 265 int64_t status = 0; |
| 227 // Ignore result if passing invalid argument and just set exit code to 0. | 266 // Ignore result if passing invalid argument and just set exit code to 0. |
| 228 DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 0), &status); | 267 DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 0), &status); |
| 229 Process::SetGlobalExitCode(status); | 268 Process::SetGlobalExitCode(status); |
| 230 } | 269 } |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 free(const_cast<char*>(system_string)); | 375 free(const_cast<char*>(system_string)); |
| 337 Dart_PropagateError(result); | 376 Dart_PropagateError(result); |
| 338 } | 377 } |
| 339 memmove(buffer, system_string, system_len); | 378 memmove(buffer, system_string, system_len); |
| 340 free(const_cast<char*>(system_string)); | 379 free(const_cast<char*>(system_string)); |
| 341 Dart_SetReturnValue(args, external_array); | 380 Dart_SetReturnValue(args, external_array); |
| 342 } | 381 } |
| 343 | 382 |
| 344 } // namespace bin | 383 } // namespace bin |
| 345 } // namespace dart | 384 } // namespace dart |
| OLD | NEW |