|
1 | 1 | package org.perl6.nqp.io;
|
2 | 2 |
|
| 3 | +import java.io.File; |
| 4 | +import java.lang.ProcessBuilder; |
| 5 | +import java.lang.ProcessBuilder.Redirect; |
3 | 6 | import java.util.ArrayList;
|
4 | 7 | import java.util.HashMap;
|
5 | 8 | import java.util.List;
|
|
16 | 19 | import org.perl6.nqp.sixmodel.reprs.IOHandleInstance;
|
17 | 20 |
|
18 | 21 | public class AsyncProcessHandle implements IIOClosable {
|
| 22 | + private Process proc; |
| 23 | + private SixModelObject queue; |
| 24 | + private ThreadContext tc; |
| 25 | + private HLLConfig hllConfig; |
| 26 | + |
19 | 27 | public AsyncProcessHandle(ThreadContext tc, SixModelObject queue, SixModelObject argsObj,
|
20 | 28 | String cwd, SixModelObject envObj, SixModelObject configObj) {
|
21 | 29 | List<String> args = getArgs(tc, argsObj);
|
22 | 30 | Map<String, String> env = getEnv(tc, envObj);
|
23 | 31 | Map<String, SixModelObject> config = getConfig(tc, configObj);
|
24 | 32 |
|
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 | + } |
26 | 63 | }
|
27 | 64 |
|
28 | 65 | private List<String> getArgs(ThreadContext tc, SixModelObject argsObj) {
|
@@ -58,6 +95,23 @@ private Map<String, SixModelObject> getConfig(ThreadContext tc, SixModelObject c
|
58 | 95 | return env;
|
59 | 96 | }
|
60 | 97 |
|
| 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 | + |
61 | 115 | public void writeBytes(ThreadContext tc, AsyncTaskInstance task, SixModelObject toWrite) {
|
62 | 116 | }
|
63 | 117 |
|
|
0 commit comments