Skip to content

Commit

Permalink
[Truffle] Define Kernel#p in Ruby.
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Oct 27, 2014
1 parent 3776633 commit 5b57945
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 38 deletions.
34 changes: 0 additions & 34 deletions core/src/main/java/org/jruby/truffle/nodes/core/KernelNodes.java
Expand Up @@ -1233,40 +1233,6 @@ public long objectID(RubyBasicObject object) {

}

@CoreMethod(names = "p", isModuleFunction = true, argumentsAsArray = true)
public abstract static class PNode extends CoreMethodNode {

@Child protected DispatchHeadNode inspect;

public PNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
inspect = new DispatchHeadNode(context);
}

public PNode(PNode prev) {
super(prev);
inspect = prev.inspect;
}

@Specialization
public RubyNilClass p(final VirtualFrame frame, final Object[] args) {
notDesignedForCompilation();

getContext().outsideGlobalLock(new Runnable() {

@Override
public void run() {
for (Object arg : args) {
getContext().getRuntime().getInstanceConfig().getOutput().println(inspect.call(frame, arg, "inspect", null));
}
}

});

return getContext().getCoreLibrary().getNilObject();
}
}

@CoreMethod(names = "print", isModuleFunction = true, argumentsAsArray = true)
public abstract static class PrintNode extends CoreMethodNode {

Expand Down
12 changes: 11 additions & 1 deletion core/src/main/ruby/jruby/truffle/core/kernel.rb
Expand Up @@ -8,6 +8,17 @@

module Kernel

module_function

def p(*args)
args.each do |arg|
print arg.inspect
print "\n"
end

args.size <= 1 ? args.first : args
end

def puts(*args)
args.each do |arg|
if arg.is_a?(Array)
Expand All @@ -20,6 +31,5 @@ def puts(*args)
end
end
end
private :puts

end
2 changes: 0 additions & 2 deletions spec/truffle/tags/core/kernel/p_tags.txt
@@ -1,7 +1,5 @@
fails:Kernel#p is a private method
fails:Kernel#p flushes output if receiver is a File
fails:Kernel#p prints obj.inspect followed by system record separator for each argument given
fails:Kernel#p is not affected by setting $\, $/ or $,
fails:Kernel#p prints nothing if no argument is given
fails:Kernel#p prints nothing if called splatting an empty Array
fails:Kernel.p needs to be reviewed for spec completeness
1 change: 0 additions & 1 deletion spec/truffle/tags/core/kernel/puts_tags.txt
Expand Up @@ -8,4 +8,3 @@ fails:Kernel#puts writes [...] for a recursive array arg
fails:Kernel#puts writes a newline after objects that do not end in newlines
fails:Kernel#puts does not write a newline after objects that end in newlines
fails:Kernel#puts ignores the $/ separator global
fails:Kernel.puts needs to be reviewed for spec completeness

0 comments on commit 5b57945

Please sign in to comment.