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 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
6 | 6 |
7 #include "bin/process.h" | 7 #include "bin/process.h" |
8 | 8 |
9 #include "bin/dartutils.h" | 9 #include "bin/dartutils.h" |
10 #include "bin/io_buffer.h" | 10 #include "bin/io_buffer.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 if (Dart_IsError(result)) { | 32 if (Dart_IsError(result)) { |
33 Dart_PropagateError(result); | 33 Dart_PropagateError(result); |
34 } | 34 } |
35 // Protect against user-defined list implementations that can have | 35 // Protect against user-defined list implementations that can have |
36 // arbitrary length. | 36 // arbitrary length. |
37 if ((len < 0) || (len > kMaxArgumentListLength)) { | 37 if ((len < 0) || (len > kMaxArgumentListLength)) { |
38 result = DartUtils::SetIntegerField(status_handle, "_errorCode", 0); | 38 result = DartUtils::SetIntegerField(status_handle, "_errorCode", 0); |
39 if (Dart_IsError(result)) { | 39 if (Dart_IsError(result)) { |
40 Dart_PropagateError(result); | 40 Dart_PropagateError(result); |
41 } | 41 } |
42 result = DartUtils::SetStringField( | 42 result = DartUtils::SetStringField(status_handle, "_errorMessage", |
43 status_handle, "_errorMessage", "Max argument list length exceeded"); | 43 "Max argument list length exceeded"); |
44 if (Dart_IsError(result)) { | 44 if (Dart_IsError(result)) { |
45 Dart_PropagateError(result); | 45 Dart_PropagateError(result); |
46 } | 46 } |
47 return NULL; | 47 return NULL; |
48 } | 48 } |
49 *length = len; | 49 *length = len; |
50 char** string_args; | 50 char** string_args; |
51 string_args = reinterpret_cast<char**>( | 51 string_args = |
52 Dart_ScopeAllocate(len * sizeof(*string_args))); | 52 reinterpret_cast<char**>(Dart_ScopeAllocate(len * sizeof(*string_args))); |
53 for (int i = 0; i < len; i++) { | 53 for (int i = 0; i < len; i++) { |
54 Dart_Handle arg = Dart_ListGetAt(strings, i); | 54 Dart_Handle arg = Dart_ListGetAt(strings, i); |
55 if (Dart_IsError(arg)) { | 55 if (Dart_IsError(arg)) { |
56 Dart_PropagateError(arg); | 56 Dart_PropagateError(arg); |
57 } | 57 } |
58 if (!Dart_IsString(arg)) { | 58 if (!Dart_IsString(arg)) { |
59 result = DartUtils::SetIntegerField(status_handle, "_errorCode", 0); | 59 result = DartUtils::SetIntegerField(status_handle, "_errorCode", 0); |
60 if (Dart_IsError(result)) { | 60 if (Dart_IsError(result)) { |
61 Dart_PropagateError(result); | 61 Dart_PropagateError(result); |
62 } | 62 } |
63 result = DartUtils::SetStringField( | 63 result = |
64 status_handle, "_errorMessage", error_msg); | 64 DartUtils::SetStringField(status_handle, "_errorMessage", error_msg); |
65 if (Dart_IsError(result)) { | 65 if (Dart_IsError(result)) { |
66 Dart_PropagateError(result); | 66 Dart_PropagateError(result); |
67 } | 67 } |
68 return NULL; | 68 return NULL; |
69 } | 69 } |
70 string_args[i] = const_cast<char *>(DartUtils::GetStringValue(arg)); | 70 string_args[i] = const_cast<char*>(DartUtils::GetStringValue(arg)); |
71 } | 71 } |
72 return string_args; | 72 return string_args; |
73 } | 73 } |
74 | 74 |
75 void FUNCTION_NAME(Process_Start)(Dart_NativeArguments args) { | 75 void FUNCTION_NAME(Process_Start)(Dart_NativeArguments args) { |
76 Dart_Handle process = Dart_GetNativeArgument(args, 0); | 76 Dart_Handle process = Dart_GetNativeArgument(args, 0); |
77 intptr_t process_stdin; | 77 intptr_t process_stdin; |
78 intptr_t process_stdout; | 78 intptr_t process_stdout; |
79 intptr_t process_stderr; | 79 intptr_t process_stderr; |
80 intptr_t exit_event; | 80 intptr_t exit_event; |
81 Dart_Handle result; | 81 Dart_Handle result; |
82 Dart_Handle status_handle = Dart_GetNativeArgument(args, 10); | 82 Dart_Handle status_handle = Dart_GetNativeArgument(args, 10); |
83 Dart_Handle path_handle = Dart_GetNativeArgument(args, 1); | 83 Dart_Handle path_handle = Dart_GetNativeArgument(args, 1); |
84 // The Dart code verifies that the path implements the String | 84 // The Dart code verifies that the path implements the String |
85 // interface. However, only builtin Strings are handled by | 85 // interface. However, only builtin Strings are handled by |
86 // GetStringValue. | 86 // GetStringValue. |
87 if (!Dart_IsString(path_handle)) { | 87 if (!Dart_IsString(path_handle)) { |
88 result = DartUtils::SetIntegerField(status_handle, "_errorCode", 0); | 88 result = DartUtils::SetIntegerField(status_handle, "_errorCode", 0); |
89 if (Dart_IsError(result)) { | 89 if (Dart_IsError(result)) { |
90 Dart_PropagateError(result); | 90 Dart_PropagateError(result); |
91 } | 91 } |
92 result = DartUtils::SetStringField( | 92 result = DartUtils::SetStringField(status_handle, "_errorMessage", |
93 status_handle, "_errorMessage", "Path must be a builtin string"); | 93 "Path must be a builtin string"); |
94 if (Dart_IsError(result)) { | 94 if (Dart_IsError(result)) { |
95 Dart_PropagateError(result); | 95 Dart_PropagateError(result); |
96 } | 96 } |
97 Dart_SetReturnValue(args, Dart_NewBoolean(false)); | 97 Dart_SetReturnValue(args, Dart_NewBoolean(false)); |
98 return; | 98 return; |
99 } | 99 } |
100 const char* path = DartUtils::GetStringValue(path_handle); | 100 const char* path = DartUtils::GetStringValue(path_handle); |
101 Dart_Handle arguments = Dart_GetNativeArgument(args, 2); | 101 Dart_Handle arguments = Dart_GetNativeArgument(args, 2); |
102 intptr_t args_length = 0; | 102 intptr_t args_length = 0; |
103 char** string_args = | 103 char** string_args = |
104 ExtractCStringList(arguments, | 104 ExtractCStringList(arguments, status_handle, |
105 status_handle, | 105 "Arguments must be builtin strings", &args_length); |
106 "Arguments must be builtin strings", | |
107 &args_length); | |
108 if (string_args == NULL) { | 106 if (string_args == NULL) { |
109 Dart_SetReturnValue(args, Dart_NewBoolean(false)); | 107 Dart_SetReturnValue(args, Dart_NewBoolean(false)); |
110 return; | 108 return; |
111 } | 109 } |
112 Dart_Handle working_directory_handle = Dart_GetNativeArgument(args, 3); | 110 Dart_Handle working_directory_handle = Dart_GetNativeArgument(args, 3); |
113 // Defaults to the current working directoy. | 111 // Defaults to the current working directoy. |
114 const char* working_directory = NULL; | 112 const char* working_directory = NULL; |
115 if (Dart_IsString(working_directory_handle)) { | 113 if (Dart_IsString(working_directory_handle)) { |
116 working_directory = DartUtils::GetStringValue(working_directory_handle); | 114 working_directory = DartUtils::GetStringValue(working_directory_handle); |
117 } else if (!Dart_IsNull(working_directory_handle)) { | 115 } else if (!Dart_IsNull(working_directory_handle)) { |
118 result = DartUtils::SetIntegerField(status_handle, "_errorCode", 0); | 116 result = DartUtils::SetIntegerField(status_handle, "_errorCode", 0); |
119 if (Dart_IsError(result)) { | 117 if (Dart_IsError(result)) { |
120 Dart_PropagateError(result); | 118 Dart_PropagateError(result); |
121 } | 119 } |
122 result = DartUtils::SetStringField( | 120 result = |
123 status_handle, "_errorMessage", | 121 DartUtils::SetStringField(status_handle, "_errorMessage", |
124 "WorkingDirectory must be a builtin string"); | 122 "WorkingDirectory must be a builtin string"); |
125 if (Dart_IsError(result)) { | 123 if (Dart_IsError(result)) { |
126 Dart_PropagateError(result); | 124 Dart_PropagateError(result); |
127 } | 125 } |
128 Dart_SetReturnValue(args, Dart_NewBoolean(false)); | 126 Dart_SetReturnValue(args, Dart_NewBoolean(false)); |
129 return; | 127 return; |
130 } | 128 } |
131 Dart_Handle environment = Dart_GetNativeArgument(args, 4); | 129 Dart_Handle environment = Dart_GetNativeArgument(args, 4); |
132 intptr_t environment_length = 0; | 130 intptr_t environment_length = 0; |
133 char** string_environment = NULL; | 131 char** string_environment = NULL; |
134 if (!Dart_IsNull(environment)) { | 132 if (!Dart_IsNull(environment)) { |
135 string_environment = | 133 string_environment = ExtractCStringList( |
136 ExtractCStringList(environment, | 134 environment, status_handle, |
137 status_handle, | 135 "Environment values must be builtin strings", &environment_length); |
138 "Environment values must be builtin strings", | |
139 &environment_length); | |
140 if (string_environment == NULL) { | 136 if (string_environment == NULL) { |
141 Dart_SetReturnValue(args, Dart_NewBoolean(false)); | 137 Dart_SetReturnValue(args, Dart_NewBoolean(false)); |
142 return; | 138 return; |
143 } | 139 } |
144 } | 140 } |
145 int64_t mode = | 141 int64_t mode = |
146 DartUtils::GetInt64ValueCheckRange(Dart_GetNativeArgument(args, 5), 0, 2); | 142 DartUtils::GetInt64ValueCheckRange(Dart_GetNativeArgument(args, 5), 0, 2); |
147 Dart_Handle stdin_handle = Dart_GetNativeArgument(args, 6); | 143 Dart_Handle stdin_handle = Dart_GetNativeArgument(args, 6); |
148 Dart_Handle stdout_handle = Dart_GetNativeArgument(args, 7); | 144 Dart_Handle stdout_handle = Dart_GetNativeArgument(args, 7); |
149 Dart_Handle stderr_handle = Dart_GetNativeArgument(args, 8); | 145 Dart_Handle stderr_handle = Dart_GetNativeArgument(args, 8); |
150 Dart_Handle exit_handle = Dart_GetNativeArgument(args, 9); | 146 Dart_Handle exit_handle = Dart_GetNativeArgument(args, 9); |
151 intptr_t pid = -1; | 147 intptr_t pid = -1; |
152 char* os_error_message = NULL; // Scope allocated by Process::Start. | 148 char* os_error_message = NULL; // Scope allocated by Process::Start. |
153 | 149 |
154 int error_code = Process::Start(path, | 150 int error_code = Process::Start( |
155 string_args, | 151 path, string_args, args_length, working_directory, string_environment, |
156 args_length, | 152 environment_length, static_cast<ProcessStartMode>(mode), &process_stdout, |
157 working_directory, | 153 &process_stdin, &process_stderr, &pid, &exit_event, &os_error_message); |
158 string_environment, | |
159 environment_length, | |
160 static_cast<ProcessStartMode>(mode), | |
161 &process_stdout, | |
162 &process_stdin, | |
163 &process_stderr, | |
164 &pid, | |
165 &exit_event, | |
166 &os_error_message); | |
167 if (error_code == 0) { | 154 if (error_code == 0) { |
168 if (mode != kDetached) { | 155 if (mode != kDetached) { |
169 Socket::SetSocketIdNativeField(stdin_handle, process_stdin); | 156 Socket::SetSocketIdNativeField(stdin_handle, process_stdin); |
170 Socket::SetSocketIdNativeField(stdout_handle, process_stdout); | 157 Socket::SetSocketIdNativeField(stdout_handle, process_stdout); |
171 Socket::SetSocketIdNativeField(stderr_handle, process_stderr); | 158 Socket::SetSocketIdNativeField(stderr_handle, process_stderr); |
172 } | 159 } |
173 if (mode == kNormal) { | 160 if (mode == kNormal) { |
174 Socket::SetSocketIdNativeField(exit_handle, exit_event); | 161 Socket::SetSocketIdNativeField(exit_handle, exit_event); |
175 } | 162 } |
176 Process::SetProcessIdNativeField(process, pid); | 163 Process::SetProcessIdNativeField(process, pid); |
177 } else { | 164 } else { |
178 result = DartUtils::SetIntegerField( | 165 result = |
179 status_handle, "_errorCode", error_code); | 166 DartUtils::SetIntegerField(status_handle, "_errorCode", error_code); |
180 if (Dart_IsError(result)) { | 167 if (Dart_IsError(result)) { |
181 Dart_PropagateError(result); | 168 Dart_PropagateError(result); |
182 } | 169 } |
183 result = DartUtils::SetStringField( | 170 result = DartUtils::SetStringField(status_handle, "_errorMessage", |
184 status_handle, | 171 os_error_message != NULL |
185 "_errorMessage", | 172 ? os_error_message |
186 os_error_message != NULL ? os_error_message | 173 : "Cannot get error message"); |
187 : "Cannot get error message"); | |
188 if (Dart_IsError(result)) { | 174 if (Dart_IsError(result)) { |
189 Dart_PropagateError(result); | 175 Dart_PropagateError(result); |
190 } | 176 } |
191 } | 177 } |
192 Dart_SetReturnValue(args, Dart_NewBoolean(error_code == 0)); | 178 Dart_SetReturnValue(args, Dart_NewBoolean(error_code == 0)); |
193 } | 179 } |
194 | 180 |
195 | 181 |
196 void FUNCTION_NAME(Process_Wait)(Dart_NativeArguments args) { | 182 void FUNCTION_NAME(Process_Wait)(Dart_NativeArguments args) { |
197 Dart_Handle process = Dart_GetNativeArgument(args, 0); | 183 Dart_Handle process = Dart_GetNativeArgument(args, 0); |
198 intptr_t process_stdin = | 184 intptr_t process_stdin = |
199 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 1)); | 185 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 1)); |
200 intptr_t process_stdout = | 186 intptr_t process_stdout = |
201 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 2)); | 187 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 2)); |
202 intptr_t process_stderr = | 188 intptr_t process_stderr = |
203 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 3)); | 189 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 3)); |
204 intptr_t exit_event = | 190 intptr_t exit_event = |
205 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 4)); | 191 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 4)); |
206 ProcessResult result; | 192 ProcessResult result; |
207 intptr_t pid; | 193 intptr_t pid; |
208 Process::GetProcessIdNativeField(process, &pid); | 194 Process::GetProcessIdNativeField(process, &pid); |
209 if (Process::Wait(pid, | 195 if (Process::Wait(pid, process_stdin, process_stdout, process_stderr, |
210 process_stdin, | 196 exit_event, &result)) { |
211 process_stdout, | |
212 process_stderr, | |
213 exit_event, | |
214 &result)) { | |
215 Dart_Handle out = result.stdout_data(); | 197 Dart_Handle out = result.stdout_data(); |
216 if (Dart_IsError(out)) { | 198 if (Dart_IsError(out)) { |
217 Dart_PropagateError(out); | 199 Dart_PropagateError(out); |
218 } | 200 } |
219 Dart_Handle err = result.stderr_data(); | 201 Dart_Handle err = result.stderr_data(); |
220 if (Dart_IsError(err)) { | 202 if (Dart_IsError(err)) { |
221 Dart_PropagateError(err); | 203 Dart_PropagateError(err); |
222 } | 204 } |
223 Dart_Handle list = Dart_NewList(4); | 205 Dart_Handle list = Dart_NewList(4); |
224 Dart_ListSetAt(list, 0, Dart_NewInteger(pid)); | 206 Dart_ListSetAt(list, 0, Dart_NewInteger(pid)); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 if (Dart_IsError(result)) { | 305 if (Dart_IsError(result)) { |
324 Dart_PropagateError(result); | 306 Dart_PropagateError(result); |
325 } | 307 } |
326 uint8_t* buffer = Dart_ScopeAllocate(bytes_length + 1); | 308 uint8_t* buffer = Dart_ScopeAllocate(bytes_length + 1); |
327 result = Dart_ListGetAsBytes(bytes, 0, buffer, bytes_length); | 309 result = Dart_ListGetAsBytes(bytes, 0, buffer, bytes_length); |
328 buffer[bytes_length] = '\0'; | 310 buffer[bytes_length] = '\0'; |
329 if (Dart_IsError(result)) { | 311 if (Dart_IsError(result)) { |
330 Dart_PropagateError(result); | 312 Dart_PropagateError(result); |
331 } | 313 } |
332 intptr_t len; | 314 intptr_t len; |
333 char* str = StringUtils::ConsoleStringToUtf8( | 315 char* str = StringUtils::ConsoleStringToUtf8(reinterpret_cast<char*>(buffer), |
334 reinterpret_cast<char*>(buffer), bytes_length, &len); | 316 bytes_length, &len); |
335 if (str == NULL) { | 317 if (str == NULL) { |
336 Dart_ThrowException( | 318 Dart_ThrowException( |
337 DartUtils::NewInternalError("SystemEncodingToString failed")); | 319 DartUtils::NewInternalError("SystemEncodingToString failed")); |
338 } | 320 } |
339 result = | 321 result = Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(str), len); |
340 Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(str), len); | |
341 Dart_SetReturnValue(args, result); | 322 Dart_SetReturnValue(args, result); |
342 } | 323 } |
343 | 324 |
344 | 325 |
345 void FUNCTION_NAME(StringToSystemEncoding)(Dart_NativeArguments args) { | 326 void FUNCTION_NAME(StringToSystemEncoding)(Dart_NativeArguments args) { |
346 Dart_Handle str = Dart_GetNativeArgument(args, 0); | 327 Dart_Handle str = Dart_GetNativeArgument(args, 0); |
347 char* utf8; | 328 char* utf8; |
348 intptr_t utf8_len; | 329 intptr_t utf8_len; |
349 Dart_Handle result = Dart_StringToUTF8( | 330 Dart_Handle result = |
350 str, reinterpret_cast<uint8_t **>(&utf8), &utf8_len); | 331 Dart_StringToUTF8(str, reinterpret_cast<uint8_t**>(&utf8), &utf8_len); |
351 if (Dart_IsError(result)) { | 332 if (Dart_IsError(result)) { |
352 Dart_PropagateError(result); | 333 Dart_PropagateError(result); |
353 } | 334 } |
354 intptr_t system_len; | 335 intptr_t system_len; |
355 const char* system_string = | 336 const char* system_string = |
356 StringUtils::Utf8ToConsoleString(utf8, utf8_len, &system_len); | 337 StringUtils::Utf8ToConsoleString(utf8, utf8_len, &system_len); |
357 if (system_string == NULL) { | 338 if (system_string == NULL) { |
358 Dart_ThrowException( | 339 Dart_ThrowException( |
359 DartUtils::NewInternalError("StringToSystemEncoding failed")); | 340 DartUtils::NewInternalError("StringToSystemEncoding failed")); |
360 } | 341 } |
361 uint8_t* buffer = NULL; | 342 uint8_t* buffer = NULL; |
362 Dart_Handle external_array = IOBuffer::Allocate(system_len, &buffer); | 343 Dart_Handle external_array = IOBuffer::Allocate(system_len, &buffer); |
363 if (!Dart_IsError(external_array)) { | 344 if (!Dart_IsError(external_array)) { |
364 memmove(buffer, system_string, system_len); | 345 memmove(buffer, system_string, system_len); |
365 } | 346 } |
366 Dart_SetReturnValue(args, external_array); | 347 Dart_SetReturnValue(args, external_array); |
367 } | 348 } |
368 | 349 |
369 } // namespace bin | 350 } // namespace bin |
370 } // namespace dart | 351 } // namespace dart |
371 | 352 |
372 #endif // !defined(DART_IO_DISABLED) | 353 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |