Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(523)

Unified Diff: runtime/bin/process_impl.dart

Issue 10392023: Change dart:io to use Future for one-shot operations. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Minor cleanup Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: runtime/bin/process_impl.dart
diff --git a/runtime/bin/process_impl.dart b/runtime/bin/process_impl.dart
index b0092dc7318a87135b67498bf302c5f16d49cbb3..356f084a8e91f7706db9c29e0b88d87704979f02 100644
--- a/runtime/bin/process_impl.dart
+++ b/runtime/bin/process_impl.dart
@@ -8,33 +8,24 @@ class _ProcessStartStatus {
}
-// Abstract factory class capable of producing interactive and
-// non-interactive processes.
class _Process {
-
- factory Process.start(String path,
- List<String> arguments,
- [ProcessOptions options]) {
+ static InteractiveProcess start(String path,
+ List<String> arguments,
+ [ProcessOptions options]) {
return new _InteractiveProcess.start(path, arguments, options);
}
- factory Process.run(String path,
- List<String> arguments,
- ProcessOptions options,
- void callback(int exitCode,
- String stdout,
- String stderr)) {
- return new _NonInteractiveProcess.start(path,
- arguments,
- options,
- callback);
+ static Future<ProcessResult> run(String path,
+ List<String> arguments,
+ [ProcessOptions options]) {
+ return new _NonInteractiveProcess._start(path, arguments, options)._result;
}
}
// _InteractiveProcess is the actual implementation of all processes
// started from Dart code.
-class _InteractiveProcess implements Process {
+class _InteractiveProcess implements InteractiveProcess {
_InteractiveProcess.start(String path,
List<String> arguments,
@@ -97,7 +88,7 @@ class _InteractiveProcess implements Process {
_onExit = null;
// TODO(ager): Make the actual process starting really async instead of
// simulating it with a timer.
- new Timer(0, (Timer ignore) => start());
+ new Timer(0, (Timer ignore) => _start());
}
String _windowsArgumentEscape(String argument) {
@@ -153,17 +144,17 @@ class _InteractiveProcess implements Process {
(bytes[offset + 3] << 24));
}
- void start() {
+ void _start() {
var status = new _ProcessStartStatus();
- bool success = _start(_path,
- _arguments,
- _workingDirectory,
- _environment,
- _in,
- _out,
- _err,
- _exitHandler,
- status);
+ bool success = _startNative(_path,
+ _arguments,
+ _workingDirectory,
+ _environment,
+ _in,
+ _out,
+ _err,
+ _exitHandler,
+ status);
if (!success) {
close();
_reportError(new ProcessException(status._errorMessage, status._errorCode));
@@ -207,15 +198,15 @@ class _InteractiveProcess implements Process {
}
}
- bool _start(String path,
- List<String> arguments,
- String workingDirectory,
- List<String> environment,
- Socket input,
- Socket output,
- Socket error,
- Socket exitHandler,
- _ProcessStartStatus status) native "Process_Start";
+ bool _startNative(String path,
+ List<String> arguments,
+ String workingDirectory,
+ List<String> environment,
+ Socket input,
+ Socket output,
+ Socket error,
+ Socket exitHandler,
+ _ProcessStartStatus status) native "Process_Start";
InputStream get stdout() {
if (_closed) {
@@ -318,10 +309,10 @@ class _InteractiveProcess implements Process {
// buffers output so it can be delivered to the callback when the
// process exits.
class _NonInteractiveProcess implements Process {
- _NonInteractiveProcess.start(String path,
- List<String> arguments,
- ProcessOptions options,
- Function this._callback) {
+ _NonInteractiveProcess._start(String path,
+ List<String> arguments,
+ ProcessOptions options) {
+ _completer = new Completer<ProcessResult>();
// Extract output encoding options and verify arguments.
var stdoutEncoding = Encoding.UTF_8;
var stderrEncoding = Encoding.UTF_8;
@@ -345,6 +336,9 @@ class _NonInteractiveProcess implements Process {
// Start the underlying process.
_process = new _InteractiveProcess.start(path, arguments, options);
+ // Setup process error handling.
+ _process.onError = (e) => _completer.completeException(e);
+
// Setup process exit handling.
_process.onExit = (exitCode) {
_exitCode = exitCode;
@@ -378,52 +372,32 @@ class _NonInteractiveProcess implements Process {
void _checkDone() {
if (_exitCode != null && _stderrClosed && _stdoutClosed) {
- _callback(_exitCode, _stdoutBuffer.toString(), _stderrBuffer.toString());
+ _completer.complete(new _ProcessResult(_exitCode,
+ _stdoutBuffer.toString(),
+ _stderrBuffer.toString()));
}
}
- InputStream get stdout() {
- throw new UnsupportedOperationException(
- 'Cannot get stdout stream for process started with '
- 'the run constructor. The entire stdout '
- 'will be supplied in the callback on completion.');
- }
-
- InputStream get stderr() {
- throw new UnsupportedOperationException(
- 'Cannot get stderr stream for process started with '
- 'the run constructor. The entire stderr '
- 'will be supplied in the callback on completion.');
- }
+ Future<ProcessResult> get _result() => _completer.future;
- OutputStream get stdin() {
- throw new UnsupportedOperationException(
- 'Cannot communicate via stdin with process started with '
- 'the run constructor');
- }
-
- void set onStart(void callback()) => _process.onStart = callback;
-
- void set onExit(void callback(int exitCode)) {
- throw new UnsupportedOperationException(
- 'Cannot set exit handler on process started with '
- 'the run constructor. The exit code will '
- 'be supplied in the callback on completion.');
- }
-
- void set onError(void callback(e)) {
- _process.onError = callback;
- }
-
- void kill() => _process.kill();
-
- void close() => _process.close();
-
- Process _process;
- Function _callback;
+ Completer<ProcessResult> _completer;
+ InteractiveProcess _process;
StringBuffer _stdoutBuffer;
StringBuffer _stderrBuffer;
int _exitCode;
bool _stdoutClosed = false;
bool _stderrClosed = false;
}
+
+
+class _ProcessResult implements ProcessResult {
+ _ProcessResult(int this._exitCode, String this._stdout, String this._stderr);
Søren Gjesse 2012/05/10 11:24:45 Can't this be a const constructor? Then the fields
Mads Ager (google) 2012/05/10 12:42:38 Good call. Yes it can.
+
+ int get exitCode() => _exitCode;
+ String get stdout() => _stdout;
+ String get stderr() => _stderr;
+
+ int _exitCode;
+ String _stdout;
+ String _stderr;
+}

Powered by Google App Engine
This is Rietveld 408576698