Browse Source

changed Process.exitCode specification, allow non blocking query (default=false for backward compatibility)

Nicolas Cannasse 8 years ago
parent
commit
4bdbc88b0a

+ 2 - 1
std/cpp/_std/sys/io/Process.hx

@@ -104,7 +104,8 @@ class Process {
 		return NativeProcess.process_pid(p);
 		return NativeProcess.process_pid(p);
 	}
 	}
 
 
-	public function exitCode() : Int {
+	public function exitCode( ?block : Bool ) : Null<Int> {	
+		if( block == false ) throw "Non blocking exitCode() not supported on this platform";
 		return NativeProcess.process_exit(p);
 		return NativeProcess.process_exit(p);
 	}
 	}
 
 

+ 3 - 1
std/cs/_std/sys/io/Process.hx

@@ -109,8 +109,10 @@ class Process {
 		return native.Id;
 		return native.Id;
 	}
 	}
 
 
-	public function exitCode() : Int
+	public function exitCode( ?block : Bool ) : Null<Int>
 	{
 	{
+		if( block == false && !native.HasExited )
+			return null;
 		native.WaitForExit();
 		native.WaitForExit();
 		return native.ExitCode;
 		return native.ExitCode;
 	}
 	}

+ 7 - 3
std/hl/_std/sys/io/Process.hx

@@ -157,8 +157,12 @@ private class Stdout extends haxe.io.Input {
 		return _pid(p);
 		return _pid(p);
 	}
 	}
 
 
-	public function exitCode() : Int {
-		return _exit(p);
+	public function exitCode( ?block : Bool ) : Null<Int> {
+		var running = false;
+		var code = _exit(p, block == false ? new hl.Ref(running) : null);
+		if( block == false )
+			return running ? null : code;
+		return code;
 	}
 	}
 
 
 	public function close() : Void {
 	public function close() : Void {
@@ -170,7 +174,7 @@ private class Stdout extends haxe.io.Input {
 	}
 	}
 
 
 	@:hlNative("std","process_run")	static function _run( cmd : hl.Bytes, args : hl.NativeArray<hl.Bytes> ) : ProcessHandle { return null; }
 	@:hlNative("std","process_run")	static function _run( cmd : hl.Bytes, args : hl.NativeArray<hl.Bytes> ) : ProcessHandle { return null; }
-	@:hlNative("std", "process_exit") static function _exit( p : ProcessHandle ) : Int { return 0; }
+	@:hlNative("std", "process_exit") static function _exit( p : ProcessHandle, running : hl.Ref<Bool> ) : Int { return 0; }
 	@:hlNative("std", "process_pid") static function _pid( p : ProcessHandle ) : Int { return 0; }
 	@:hlNative("std", "process_pid") static function _pid( p : ProcessHandle ) : Int { return 0; }
 	@:hlNative("std","process_close") static function _close( p : ProcessHandle ) : Void { }
 	@:hlNative("std","process_close") static function _close( p : ProcessHandle ) : Void { }
 	@:hlNative("std","process_kill") static function _kill( p : ProcessHandle ) : Void { }
 	@:hlNative("std","process_kill") static function _kill( p : ProcessHandle ) : Void { }

+ 9 - 1
std/java/_std/sys/io/Process.hx

@@ -92,8 +92,16 @@ class Process {
 		return -1;
 		return -1;
 	}
 	}
 
 
-	public function exitCode() : Int
+	public function exitCode( ?block : Bool ) : Null<Int>
 	{
 	{
+		if( block == false ) {
+			try {
+				return proc.exitValue();
+			} catch( e : Dynamic ) {
+				return null;
+			}
+		}
+		
 		cast(stdout, ProcessInput).bufferContents();
 		cast(stdout, ProcessInput).bufferContents();
 		cast(stderr, ProcessInput).bufferContents();
 		cast(stderr, ProcessInput).bufferContents();
 		try
 		try

+ 1 - 1
std/lua/_std/sys/io/Process.hx

@@ -51,7 +51,7 @@ class Process {
 
 
 	public function close() : Void { }
 	public function close() : Void { }
 
 
-	public function exitCode() : Int { return 0; }
+	public function exitCode( ?block : Bool ) : Null<Int> { return 0; }
 
 
 	public function kill() : Void { }
 	public function kill() : Void { }
 }
 }

+ 2 - 1
std/neko/_std/sys/io/Process.hx

@@ -106,7 +106,8 @@ private class Stdout extends haxe.io.Input {
 		return _pid(p);
 		return _pid(p);
 	}
 	}
 
 
-	public function exitCode() : Int {
+	public function exitCode( ?block : Bool ) : Null<Int> {
+		if( block == false ) throw "Non blocking exitCode() not supported on this platform";
 		return _exit(p);
 		return _exit(p);
 	}
 	}
 
 

+ 2 - 1
std/php/_std/sys/io/Process.hx

@@ -139,11 +139,12 @@ class Process {
 		untyped input.p = fp;
 		untyped input.p = fp;
 	}
 	}
 
 
-	public function exitCode() : Int {
+	public function exitCode( ?block : Bool ) : Null<Int> {
 		if (null == cl)
 		if (null == cl)
 		{
 		{
 			st = untyped __call__('proc_get_status', p);
 			st = untyped __call__('proc_get_status', p);
 			while(st[untyped 'running']) {
 			while(st[untyped 'running']) {
+				if( block == false ) return null;
 				Sys.sleep(0.01);
 				Sys.sleep(0.01);
 				st = untyped __call__('proc_get_status', p);
 				st = untyped __call__('proc_get_status', p);
 			}
 			}

+ 3 - 1
std/python/_std/sys/io/Process.hx

@@ -46,7 +46,9 @@ class Process {
 	public function getPid() : Int {
 	public function getPid() : Int {
 		return p.pid;
 		return p.pid;
 	}
 	}
-	public function exitCode() : Int {
+	public function exitCode( ?block : Bool ) : Null<Int> {
+		if( block == false )
+			return p.poll();
 		return p.wait();
 		return p.wait();
 	}
 	}
 	public function close() : Void {
 	public function close() : Void {

+ 4 - 2
std/sys/io/Process.hx

@@ -58,10 +58,12 @@ extern class Process {
 	function getPid() : Int;
 	function getPid() : Int;
 
 
 	/**
 	/**
-		Block until the process exits and return the exit code of the process.
+		Query the exit code of the process.
+		If `block` is true or not specified, it will block until the process terminates.
+		If `block` is false, it will return either the process exit code if it's already terminated or null if it's still running.
 		If the process has already exited, return the exit code immediately.
 		If the process has already exited, return the exit code immediately.
 	*/
 	*/
-	function exitCode() : Int;
+	function exitCode( ?block : Bool ) : Null<Int>;
 
 
 	/**
 	/**
 		Close the process handle and release the associated resources.
 		Close the process handle and release the associated resources.