Skip to content

Commit c9ca5de

Browse files
committed
Start async process, conveying any errors.
1 parent 8849e9a commit c9ca5de

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.perl6.nqp.io;
22

3+
import java.io.File;
4+
import java.lang.ProcessBuilder;
5+
import java.lang.ProcessBuilder.Redirect;
36
import java.util.ArrayList;
47
import java.util.HashMap;
58
import java.util.List;
@@ -16,13 +19,47 @@
1619
import org.perl6.nqp.sixmodel.reprs.IOHandleInstance;
1720

1821
public class AsyncProcessHandle implements IIOClosable {
22+
private Process proc;
23+
private SixModelObject queue;
24+
private ThreadContext tc;
25+
private HLLConfig hllConfig;
26+
1927
public AsyncProcessHandle(ThreadContext tc, SixModelObject queue, SixModelObject argsObj,
2028
String cwd, SixModelObject envObj, SixModelObject configObj) {
2129
List<String> args = getArgs(tc, argsObj);
2230
Map<String, String> env = getEnv(tc, envObj);
2331
Map<String, SixModelObject> config = getConfig(tc, configObj);
2432

25-
throw ExceptionHandling.dieInternal(tc, "NYI");
33+
ProcessBuilder pb = new ProcessBuilder(args);
34+
pb.directory(new File(cwd));
35+
pb.environment().clear();
36+
pb.environment().putAll(env);
37+
setupInputOutput(tc, pb, config);
38+
39+
this.queue = queue;
40+
this.tc = tc;
41+
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+
}
2663
}
2764

2865
private List<String> getArgs(ThreadContext tc, SixModelObject argsObj) {
@@ -58,6 +95,23 @@ private Map<String, SixModelObject> getConfig(ThreadContext tc, SixModelObject c
5895
return env;
5996
}
6097

98+
private void setupInputOutput(ThreadContext tc, ProcessBuilder pb,
99+
Map<String, SixModelObject> config) {
100+
// TODO Implement this
101+
}
102+
103+
private void send(SixModelObject... args) {
104+
final SixModelObject Array = this.hllConfig.listType;
105+
SixModelObject result = Array.st.REPR.allocate(this.tc, Array.st);
106+
for (SixModelObject arg : args)
107+
result.push_boxed(this.tc, arg);
108+
((ConcBlockingQueueInstance)this.queue).push_boxed(this.tc, result);
109+
}
110+
111+
private SixModelObject boxError(String error) {
112+
return Ops.box_s(error, this.hllConfig.strBoxType, this.tc);
113+
}
114+
61115
public void writeBytes(ThreadContext tc, AsyncTaskInstance task, SixModelObject toWrite) {
62116
}
63117

0 commit comments

Comments
 (0)