| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class _ProcessStartStatus { | 5 class _ProcessStartStatus { |
| 6 int _errorCode; // Set to OS error code if process start failed. | 6 int _errorCode; // Set to OS error code if process start failed. |
| 7 String _errorMessage; // Set to OS error message if process start failed. | 7 String _errorMessage; // Set to OS error message if process start failed. |
| 8 } | 8 } |
| 9 | 9 |
| 10 | 10 |
| 11 class _Process implements Process { | 11 class _Process implements Process { |
| 12 | 12 |
| 13 _Process.start(String path, List<String> arguments) { | 13 _Process.start(String path, |
| 14 List<String> arguments, |
| 15 [String workingDirectory]) { |
| 14 if (path is !String) { | 16 if (path is !String) { |
| 15 throw new ProcessException("Path is not a String: $path"); | 17 throw new ProcessException("Path is not a String: $path"); |
| 16 } | 18 } |
| 17 _path = path; | 19 _path = path; |
| 18 | 20 |
| 19 if (arguments is !List) { | 21 if (arguments is !List) { |
| 20 throw new ProcessException("Arguments is not a List: $arguments"); | 22 throw new ProcessException("Arguments is not a List: $arguments"); |
| 21 } | 23 } |
| 22 int len = arguments.length; | 24 int len = arguments.length; |
| 23 _arguments = new ObjectArray<String>(len); | 25 _arguments = new ObjectArray<String>(len); |
| 24 for (int i = 0; i < len; i++) { | 26 for (int i = 0; i < len; i++) { |
| 25 var arg = arguments[i]; | 27 var arg = arguments[i]; |
| 26 if (arg is !String) { | 28 if (arg is !String) { |
| 27 throw new ProcessException("Non-string argument: $arg"); | 29 throw new ProcessException("Non-string argument: $arg"); |
| 28 } | 30 } |
| 29 _arguments[i] = arguments[i]; | 31 _arguments[i] = arguments[i]; |
| 30 } | 32 } |
| 31 | 33 |
| 34 if (workingDirectory is !String && workingDirectory !== null) { |
| 35 throw new ProcessException( |
| 36 "WorkingDirectory is not a String: $workingDirectory"); |
| 37 } |
| 38 _workingDirectory = workingDirectory; |
| 39 |
| 32 _in = new _Socket._internalReadOnly(); // stdout coming from process. | 40 _in = new _Socket._internalReadOnly(); // stdout coming from process. |
| 33 _out = new _Socket._internalWriteOnly(); // stdin going to process. | 41 _out = new _Socket._internalWriteOnly(); // stdin going to process. |
| 34 _err = new _Socket._internalReadOnly(); // stderr coming from process. | 42 _err = new _Socket._internalReadOnly(); // stderr coming from process. |
| 35 _exitHandler = new _Socket._internalReadOnly(); | 43 _exitHandler = new _Socket._internalReadOnly(); |
| 36 _closed = false; | 44 _closed = false; |
| 37 _killed = false; | 45 _killed = false; |
| 38 _started = false; | 46 _started = false; |
| 39 _exitHandlerCallback = null; | 47 _exitHandlerCallback = null; |
| 40 // TODO(ager): Make the actual process starting really async instead of | 48 // TODO(ager): Make the actual process starting really async instead of |
| 41 // simulating it with a timer. | 49 // simulating it with a timer. |
| 42 new Timer((Timer ignore) => start(), 0); | 50 new Timer((Timer ignore) => start(), 0); |
| 43 } | 51 } |
| 44 | 52 |
| 45 int _intFromBytes(List<int> bytes, int offset) { | 53 int _intFromBytes(List<int> bytes, int offset) { |
| 46 return (bytes[offset] + | 54 return (bytes[offset] + |
| 47 (bytes[offset + 1] << 8) + | 55 (bytes[offset + 1] << 8) + |
| 48 (bytes[offset + 2] << 16) + | 56 (bytes[offset + 2] << 16) + |
| 49 (bytes[offset + 3] << 24)); | 57 (bytes[offset + 3] << 24)); |
| 50 } | 58 } |
| 51 | 59 |
| 52 void start() { | 60 void start() { |
| 53 var status = new _ProcessStartStatus(); | 61 var status = new _ProcessStartStatus(); |
| 54 bool success = _start( | 62 bool success = _start(_path, |
| 55 _path, _arguments, _in, _out, _err, _exitHandler, status); | 63 _arguments, |
| 64 _workingDirectory, |
| 65 _in, |
| 66 _out, |
| 67 _err, |
| 68 _exitHandler, |
| 69 status); |
| 56 if (!success) { | 70 if (!success) { |
| 57 close(); | 71 close(); |
| 58 if (_errorHandler !== null) { | 72 if (_errorHandler !== null) { |
| 59 _errorHandler(new ProcessException(status._errorMessage, | 73 _errorHandler(new ProcessException(status._errorMessage, |
| 60 status._errorCode)); | 74 status._errorCode)); |
| 61 return; | 75 return; |
| 62 } | 76 } |
| 63 } | 77 } |
| 64 _started = true; | 78 _started = true; |
| 65 | 79 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 if (exitDataRead == EXIT_DATA_SIZE) handleExit(); | 113 if (exitDataRead == EXIT_DATA_SIZE) handleExit(); |
| 100 }; | 114 }; |
| 101 | 115 |
| 102 if (_startHandler !== null) { | 116 if (_startHandler !== null) { |
| 103 _startHandler(); | 117 _startHandler(); |
| 104 } | 118 } |
| 105 } | 119 } |
| 106 | 120 |
| 107 bool _start(String path, | 121 bool _start(String path, |
| 108 List<String> arguments, | 122 List<String> arguments, |
| 123 String workingDirectory, |
| 109 Socket input, | 124 Socket input, |
| 110 Socket output, | 125 Socket output, |
| 111 Socket error, | 126 Socket error, |
| 112 Socket exitHandler, | 127 Socket exitHandler, |
| 113 _ProcessStartStatus status) native "Process_Start"; | 128 _ProcessStartStatus status) native "Process_Start"; |
| 114 | 129 |
| 115 void _processExit(int pid) native "Process_Exit"; | 130 void _processExit(int pid) native "Process_Exit"; |
| 116 | 131 |
| 117 InputStream get stdout() { | 132 InputStream get stdout() { |
| 118 if (_closed) { | 133 if (_closed) { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 void set errorHandler(void callback(ProcessException exception)) { | 195 void set errorHandler(void callback(ProcessException exception)) { |
| 181 _errorHandler = callback; | 196 _errorHandler = callback; |
| 182 } | 197 } |
| 183 | 198 |
| 184 void set startHandler(void callback()) { | 199 void set startHandler(void callback()) { |
| 185 _startHandler = callback; | 200 _startHandler = callback; |
| 186 } | 201 } |
| 187 | 202 |
| 188 String _path; | 203 String _path; |
| 189 ObjectArray<String> _arguments; | 204 ObjectArray<String> _arguments; |
| 205 String _workingDirectory; |
| 190 Socket _in; | 206 Socket _in; |
| 191 Socket _out; | 207 Socket _out; |
| 192 Socket _err; | 208 Socket _err; |
| 193 Socket _exitHandler; | 209 Socket _exitHandler; |
| 194 int _pid; | 210 int _pid; |
| 195 bool _closed; | 211 bool _closed; |
| 196 bool _killed; | 212 bool _killed; |
| 197 bool _started; | 213 bool _started; |
| 198 Function _exitHandlerCallback; | 214 Function _exitHandlerCallback; |
| 199 Function _errorHandler; | 215 Function _errorHandler; |
| 200 Function _startHandler; | 216 Function _startHandler; |
| 201 } | 217 } |
| OLD | NEW |