Skip to content

Commit 1bd2aa9

Browse files
committed
Fire exit event with exit code on async proc exit.
This gets the most basic case of Proc::Async working on JVM, where no input/output handles are used.
1 parent c9ca5de commit 1bd2aa9

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

src/vm/jvm/runtime/org/perl6/nqp/io/AsyncProcessHandle.java

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public class AsyncProcessHandle implements IIOClosable {
2626

2727
public AsyncProcessHandle(ThreadContext tc, SixModelObject queue, SixModelObject argsObj,
2828
String cwd, SixModelObject envObj, SixModelObject configObj) {
29-
List<String> args = getArgs(tc, argsObj);
30-
Map<String, String> env = getEnv(tc, envObj);
31-
Map<String, SixModelObject> config = getConfig(tc, configObj);
29+
final List<String> args = getArgs(tc, argsObj);
30+
final Map<String, String> env = getEnv(tc, envObj);
31+
final Map<String, SixModelObject> config = getConfig(tc, configObj);
3232

33-
ProcessBuilder pb = new ProcessBuilder(args);
33+
final ProcessBuilder pb = new ProcessBuilder(args);
3434
pb.directory(new File(cwd));
3535
pb.environment().clear();
3636
pb.environment().putAll(env);
@@ -39,27 +39,39 @@ public AsyncProcessHandle(ThreadContext tc, SixModelObject queue, SixModelObject
3939
this.queue = queue;
4040
this.tc = tc;
4141
this.hllConfig = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig;
42-
try {
43-
this.proc = pb.start();
44-
SixModelObject ready = config.get("ready");
45-
if (ready != null)
46-
send(ready);
47-
}
48-
catch (Throwable t) {
49-
SixModelObject message = boxError(t.getMessage());
50-
51-
SixModelObject error = config.get("error");
52-
if (error != null)
53-
send(error, message);
54-
55-
SixModelObject stdoutBytes = config.get("stdout_bytes");
56-
if (stdoutBytes != null)
57-
send(stdoutBytes, this.hllConfig.intBoxType, this.hllConfig.strBoxType, message);
58-
59-
SixModelObject stderrBytes = config.get("stderr_bytes");
60-
if (stderrBytes != null)
61-
send(stderrBytes, this.hllConfig.intBoxType, this.hllConfig.strBoxType, message);
62-
}
42+
new Thread(new Runnable() {
43+
public void run() {
44+
try {
45+
AsyncProcessHandle.this.proc = pb.start();
46+
47+
SixModelObject ready = config.get("ready");
48+
if (ready != null)
49+
send(ready);
50+
51+
int outcome = AsyncProcessHandle.this.proc.waitFor();
52+
SixModelObject done = config.get("done");
53+
if (done != null)
54+
send(done, boxInt(outcome));
55+
}
56+
catch (Throwable t) {
57+
SixModelObject message = boxError(t.getMessage());
58+
59+
SixModelObject error = config.get("error");
60+
if (error != null)
61+
send(error, message);
62+
63+
SixModelObject stdoutBytes = config.get("stdout_bytes");
64+
if (stdoutBytes != null)
65+
send(stdoutBytes, AsyncProcessHandle.this.hllConfig.intBoxType,
66+
AsyncProcessHandle.this.hllConfig.strBoxType, message);
67+
68+
SixModelObject stderrBytes = config.get("stderr_bytes");
69+
if (stderrBytes != null)
70+
send(stderrBytes, AsyncProcessHandle.this.hllConfig.intBoxType,
71+
AsyncProcessHandle.this.hllConfig.strBoxType, message);
72+
}
73+
}
74+
}).start();
6375
}
6476

6577
private List<String> getArgs(ThreadContext tc, SixModelObject argsObj) {
@@ -112,6 +124,10 @@ private SixModelObject boxError(String error) {
112124
return Ops.box_s(error, this.hllConfig.strBoxType, this.tc);
113125
}
114126

127+
private SixModelObject boxInt(int value) {
128+
return Ops.box_i(value, this.hllConfig.intBoxType, this.tc);
129+
}
130+
115131
public void writeBytes(ThreadContext tc, AsyncTaskInstance task, SixModelObject toWrite) {
116132
}
117133

0 commit comments

Comments
 (0)