Skip to content

Commit

Permalink
[Truffle] Basic implementation of Kernel#system.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Jan 6, 2015
1 parent e45e0e4 commit 057cde9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
31 changes: 23 additions & 8 deletions core/src/main/java/org/jruby/truffle/nodes/core/KernelNodes.java
Expand Up @@ -50,10 +50,7 @@
import java.io.IOException;
import java.io.PrintStream;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.*;

@CoreClass(name = "Kernel")
public abstract class KernelNodes {
Expand Down Expand Up @@ -1949,7 +1946,7 @@ public RubyString sprintf(Object[] args) {
}
}

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

public SystemNode(RubyContext context, SourceSection sourceSection) {
Expand All @@ -1961,10 +1958,28 @@ public SystemNode(SystemNode prev) {
}

@Specialization
public Object fork(Object[] args) {
public boolean system(RubyString command) {
notDesignedForCompilation();
getContext().getWarnings().warn("Kernel#system not implemented - defined to satisfy some metaprogramming in RubySpec");
return getContext().getCoreLibrary().getNilObject();

// TOOD(CS 5-JAN-15): very simplistic implementation

This comment has been minimized.

Copy link
@nirvdrum

nirvdrum Jan 6, 2015

Contributor

TOOD -> TODO. If you're grepping or using the TODO view in IDEA, you'll end up missing this.


final RubyHash env = getContext().getCoreLibrary().getENV();

final List<String> envp = new ArrayList<>();

// TODO(CS): cast
for (KeyValue keyValue : HashOperations.verySlowToKeyValues(env)) {
envp.add(keyValue.getKey().toString() + "=" + keyValue.getValue().toString());
}

// We need to run via bash to get the variable and other expansion we expect
try {
Runtime.getRuntime().exec(new String[]{"bash", "-c", command.toString()}, envp.toArray(new String[envp.size()]));
} catch (IOException e) {
throw new RuntimeException(e);
}

return true;
}

}
Expand Down
Expand Up @@ -81,4 +81,5 @@ public Object execute(VirtualFrame frame) {

return context.makeString(resultBuilder.toString());
}

}

0 comments on commit 057cde9

Please sign in to comment.