Skip to content

Commit 057cde9

Browse files
committed
[Truffle] Basic implementation of Kernel#system.
1 parent e45e0e4 commit 057cde9

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

core/src/main/java/org/jruby/truffle/nodes/core/KernelNodes.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@
5050
import java.io.IOException;
5151
import java.io.PrintStream;
5252
import java.math.BigInteger;
53-
import java.util.Arrays;
54-
import java.util.Collection;
55-
import java.util.List;
56-
import java.util.Map;
53+
import java.util.*;
5754

5855
@CoreClass(name = "Kernel")
5956
public abstract class KernelNodes {
@@ -1949,7 +1946,7 @@ public RubyString sprintf(Object[] args) {
19491946
}
19501947
}
19511948

1952-
@CoreMethod(names = "system", isModuleFunction = true, argumentsAsArray = true)
1949+
@CoreMethod(names = "system", isModuleFunction = true, needsSelf = false, required = 1)
19531950
public abstract static class SystemNode extends CoreMethodNode {
19541951

19551952
public SystemNode(RubyContext context, SourceSection sourceSection) {
@@ -1961,10 +1958,28 @@ public SystemNode(SystemNode prev) {
19611958
}
19621959

19631960
@Specialization
1964-
public Object fork(Object[] args) {
1961+
public boolean system(RubyString command) {
19651962
notDesignedForCompilation();
1966-
getContext().getWarnings().warn("Kernel#system not implemented - defined to satisfy some metaprogramming in RubySpec");
1967-
return getContext().getCoreLibrary().getNilObject();
1963+
1964+
// TOOD(CS 5-JAN-15): very simplistic implementation
1965+
1966+
final RubyHash env = getContext().getCoreLibrary().getENV();
1967+
1968+
final List<String> envp = new ArrayList<>();
1969+
1970+
// TODO(CS): cast
1971+
for (KeyValue keyValue : HashOperations.verySlowToKeyValues(env)) {
1972+
envp.add(keyValue.getKey().toString() + "=" + keyValue.getValue().toString());
1973+
}
1974+
1975+
// We need to run via bash to get the variable and other expansion we expect
1976+
try {
1977+
Runtime.getRuntime().exec(new String[]{"bash", "-c", command.toString()}, envp.toArray(new String[envp.size()]));
1978+
} catch (IOException e) {
1979+
throw new RuntimeException(e);
1980+
}
1981+
1982+
return true;
19681983
}
19691984

19701985
}

core/src/main/java/org/jruby/truffle/nodes/core/SystemNode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,5 @@ public Object execute(VirtualFrame frame) {
8181

8282
return context.makeString(resultBuilder.toString());
8383
}
84+
8485
}

0 commit comments

Comments
 (0)