Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 8d34a507cc0b
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 76165450d6fc
Choose a head ref
  • 2 commits
  • 15 files changed
  • 1 contributor

Commits on Oct 13, 2014

  1. [Truffle] Add the possibility to make singleton methods.

    * rb_define_singleton_method() in MRI, (meta = true) in JRuby.
    * Refactor addMethod().
    eregon committed Oct 13, 2014
    Copy the full SHA
    1642e85 View commit details
  2. [Truffle] Use our shiny new singleton methods.

    * Now all @coremethod should be correctly classified.
    * Usage of self is rather rare, so use the same strategy as for isModuleFunction.
    eregon committed Oct 13, 2014
    Copy the full SHA
    7616545 View commit details
Original file line number Diff line number Diff line change
@@ -22,6 +22,8 @@

Visibility visibility() default Visibility.PUBLIC;

boolean onSingleton() default false;

boolean isModuleFunction() default false;

boolean needsSelf() default true;
Original file line number Diff line number Diff line change
@@ -143,33 +143,40 @@ private static void addMethod(RubyClass rubyObjectClass, MethodDetails methodDet

final Visibility visibility = anno.visibility();

if (anno.isModuleFunction() && visibility != Visibility.PUBLIC) {
System.err.println("WARNING: visibility ignored when isModuleFunction in " + methodDetails.getIndicativeName());
if (anno.isModuleFunction()) {
if (visibility != Visibility.PUBLIC) {
System.err.println("WARNING: visibility ignored when isModuleFunction in " + methodDetails.getIndicativeName());
}
if (anno.onSingleton()) {
System.err.println("WARNING: Either onSingleton or isModuleFunction for " + methodDetails.getIndicativeName());
}
}

// Do not use needsSelf=true in module functions, it is either the module/class or the instance.
final boolean needsSelf = !anno.isModuleFunction() && anno.needsSelf();
// Usage of needsSelf is quite rare for singleton methods (except constructors).
final boolean needsSelf = !anno.isModuleFunction() && !anno.onSingleton() && anno.needsSelf();

final RubyRootNode rootNode = makeGenericMethod(context, methodDetails, needsSelf);

final RubyMethod method = new RubyMethod(rootNode.getSharedMethodInfo(), canonicalName, module, visibility, false,
Truffle.getRuntime().createCallTarget(rootNode), null);

if (anno.isModuleFunction()) {
module.addMethod(null, method.withNewVisibility(Visibility.PRIVATE));
module.getSingletonClass(null).addMethod(null, method.withNewVisibility(Visibility.PUBLIC));
addMethod(module, method, aliases, Visibility.PRIVATE);
addMethod(module.getSingletonClass(null), method, aliases, Visibility.PUBLIC);
} else if (anno.onSingleton()) {
addMethod(module.getSingletonClass(null), method, aliases, visibility);
} else {
module.addMethod(null, method);
addMethod(module, method, aliases, visibility);
}
}

for (String alias : aliases) {
final RubyMethod withAlias = method.withNewName(alias);

module.addMethod(null, withAlias);
private static void addMethod(RubyModule module, RubyMethod method, List<String> aliases, Visibility visibility) {
method = method.withNewVisibility(visibility);

if (anno.isModuleFunction()) {
module.getSingletonClass(null).addMethod(null, withAlias.withNewVisibility(Visibility.PUBLIC));
}
module.addMethod(null, method);
for (String alias : aliases) {
module.addMethod(null, method.withNewName(alias));
}
}

9 changes: 4 additions & 5 deletions core/src/main/java/org/jruby/truffle/nodes/core/DirNodes.java
Original file line number Diff line number Diff line change
@@ -23,8 +23,7 @@

@CoreClass(name = "Dir")
public abstract class DirNodes {

@CoreMethod(names = "[]", isModuleFunction = true, minArgs = 1, maxArgs = 1)
@CoreMethod(names = "[]", onSingleton = true, minArgs = 1, maxArgs = 1)
public abstract static class GlobNode extends CoreMethodNode {

public GlobNode(RubyContext context, SourceSection sourceSection) {
@@ -96,7 +95,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO

}

@CoreMethod(names = "chdir", isModuleFunction = true, needsBlock = true, minArgs = 1, maxArgs = 1)
@CoreMethod(names = "chdir", onSingleton = true, needsBlock = true, minArgs = 1, maxArgs = 1)
public abstract static class ChdirNode extends YieldingCoreMethodNode {

public ChdirNode(RubyContext context, SourceSection sourceSection) {
@@ -129,7 +128,7 @@ public Object chdir(VirtualFrame frame, RubyString path, RubyProc block) {

}

@CoreMethod(names = {"exist?", "exists?"}, isModuleFunction = true, maxArgs = 1)
@CoreMethod(names = {"exist?", "exists?"}, onSingleton = true, maxArgs = 1)
public abstract static class ExistsNode extends CoreMethodNode {

public ExistsNode(RubyContext context, SourceSection sourceSection) {
@@ -149,7 +148,7 @@ public boolean exists(RubyString path) {

}

@CoreMethod(names = "pwd", isModuleFunction = true, maxArgs = 0)
@CoreMethod(names = {"pwd", "getwd"}, onSingleton = true, maxArgs = 0)
public abstract static class PwdNode extends CoreMethodNode {

public PwdNode(RubyContext context, SourceSection sourceSection) {
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ public boolean equal(RubyEncoding a, RubyEncoding b) {

}

@CoreMethod(names = "default_external", isModuleFunction = true, maxArgs = 0)
@CoreMethod(names = "default_external", onSingleton = true, maxArgs = 0)
public abstract static class DefaultExternalNode extends CoreMethodNode {

public DefaultExternalNode(RubyContext context, SourceSection sourceSection) {
@@ -65,7 +65,7 @@ public RubyEncoding defaultExternal() {

}

@CoreMethod(names = "default_internal", isModuleFunction = true, maxArgs = 0)
@CoreMethod(names = "default_internal", onSingleton = true, maxArgs = 0)
public abstract static class DefaultInternalNode extends CoreMethodNode {

public DefaultInternalNode(RubyContext context, SourceSection sourceSection) {
@@ -91,7 +91,7 @@ public RubyEncoding defaultInternal() {

}

@CoreMethod(names = "find", isModuleFunction = true, maxArgs = 1, minArgs = 1)
@CoreMethod(names = "find", onSingleton = true, maxArgs = 1, minArgs = 1)
public abstract static class FindNode extends CoreMethodNode {

public FindNode(RubyContext context, SourceSection sourceSection) {
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@ public NilPlaceholder initialize(RubyFiber fiber, RubyProc block) {

}

@CoreMethod(names = "yield", isModuleFunction = true, isSplatted = true)
@CoreMethod(names = "yield", onSingleton = true, isSplatted = true)
public abstract static class YieldNode extends CoreMethodNode {

public YieldNode(RubyContext context, SourceSection sourceSection) {
22 changes: 11 additions & 11 deletions core/src/main/java/org/jruby/truffle/nodes/core/FileNodes.java
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@

@CoreClass(name = "File")
public abstract class FileNodes {
@CoreMethod(names = "absolute_path", isModuleFunction = true, minArgs = 1, maxArgs = 1)
@CoreMethod(names = "absolute_path", onSingleton = true, minArgs = 1, maxArgs = 1)
public abstract static class AbsolutePathNode extends CoreMethodNode {

public AbsolutePathNode(RubyContext context, SourceSection sourceSection) {
@@ -62,7 +62,7 @@ public NilPlaceholder close(RubyFile file) {

}

@CoreMethod(names = "delete", isModuleFunction = true, minArgs = 1, maxArgs = 1)
@CoreMethod(names = "delete", onSingleton = true, minArgs = 1, maxArgs = 1)
public abstract static class DeleteNode extends CoreMethodNode {

public DeleteNode(RubyContext context, SourceSection sourceSection) {
@@ -84,7 +84,7 @@ public int delete(RubyString file) {

}

@CoreMethod(names = "directory?", isModuleFunction = true, maxArgs = 1)
@CoreMethod(names = "directory?", onSingleton = true, maxArgs = 1)
public abstract static class DirectoryNode extends CoreMethodNode {

public DirectoryNode(RubyContext context, SourceSection sourceSection) {
@@ -104,7 +104,7 @@ public boolean directory(RubyString path) {

}

@CoreMethod(names = "dirname", isModuleFunction = true, minArgs = 1, maxArgs = 1)
@CoreMethod(names = "dirname", onSingleton = true, minArgs = 1, maxArgs = 1)
public abstract static class DirnameNode extends CoreMethodNode {

public DirnameNode(RubyContext context, SourceSection sourceSection) {
@@ -172,7 +172,7 @@ public NilPlaceholder eachLine(VirtualFrame frame, RubyFile file, RubyProc block

}

@CoreMethod(names = {"exist?", "exists?"}, isModuleFunction = true, minArgs = 1, maxArgs = 1)
@CoreMethod(names = {"exist?", "exists?"}, onSingleton = true, minArgs = 1, maxArgs = 1)
public abstract static class ExistsNode extends CoreMethodNode {

public ExistsNode(RubyContext context, SourceSection sourceSection) {
@@ -192,7 +192,7 @@ public boolean exists(RubyString path) {

}

@CoreMethod(names = "executable?", isModuleFunction = true, minArgs = 1, maxArgs = 1)
@CoreMethod(names = "executable?", onSingleton = true, minArgs = 1, maxArgs = 1)
public abstract static class ExecutableNode extends CoreMethodNode {

public ExecutableNode(RubyContext context, SourceSection sourceSection) {
@@ -212,7 +212,7 @@ public boolean executable(RubyString path) {

}

@CoreMethod(names = "expand_path", isModuleFunction = true, minArgs = 1, maxArgs = 2)
@CoreMethod(names = "expand_path", onSingleton = true, minArgs = 1, maxArgs = 2)
public abstract static class ExpandPathNode extends CoreMethodNode {

public ExpandPathNode(RubyContext context, SourceSection sourceSection) {
@@ -237,7 +237,7 @@ public RubyString expandPath(RubyString path, RubyString dir) {

}

@CoreMethod(names = "file?", isModuleFunction = true, minArgs = 1, maxArgs = 1)
@CoreMethod(names = "file?", onSingleton = true, minArgs = 1, maxArgs = 1)
public abstract static class FileNode extends CoreMethodNode {

public FileNode(RubyContext context, SourceSection sourceSection) {
@@ -257,7 +257,7 @@ public boolean file(RubyString path) {

}

@CoreMethod(names = "join", isModuleFunction = true, isSplatted = true)
@CoreMethod(names = "join", onSingleton = true, isSplatted = true)
public abstract static class JoinNode extends CoreMethodNode {

public JoinNode(RubyContext context, SourceSection sourceSection) {
@@ -295,7 +295,7 @@ public static void join(StringBuilder builder, Object[] parts) {
}
}

@CoreMethod(names = "open", isModuleFunction = true, needsBlock = true, minArgs = 2, maxArgs = 2)
@CoreMethod(names = "open", onSingleton = true, needsBlock = true, minArgs = 2, maxArgs = 2)
public abstract static class OpenNode extends YieldingCoreMethodNode {

public OpenNode(RubyContext context, SourceSection sourceSection) {
@@ -392,7 +392,7 @@ public RubyString read(RubyFile file) {

}

@CoreMethod(names = "size?", minArgs = 1, maxArgs = 1, isModuleFunction = true)
@CoreMethod(names = "size?", onSingleton = true, minArgs = 1, maxArgs = 1)
public abstract static class SizeNode extends CoreMethodNode {

public SizeNode(RubyContext context, SourceSection sourceSection) {
Original file line number Diff line number Diff line change
@@ -90,7 +90,7 @@ public boolean equal(RubyHash a, RubySymbol b) {

}

@CoreMethod(names = "[]", isModuleFunction = true, isSplatted = true)
@CoreMethod(names = "[]", onSingleton = true, isSplatted = true)
public abstract static class ConstructNode extends HashCoreMethodNode {

private final BranchProfile singleObject = new BranchProfile();
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@
@CoreClass(name = "IO")
public abstract class IONodes {

@CoreMethod(names = "readlines", isModuleFunction = true, minArgs = 1, maxArgs = 1)
@CoreMethod(names = "readlines", onSingleton = true, minArgs = 1, maxArgs = 1)
public abstract static class ReadLinesNode extends CoreMethodNode {

public ReadLinesNode(RubyContext context, SourceSection sourceSection) {
Original file line number Diff line number Diff line change
@@ -802,7 +802,7 @@ public RubyString name(RubyModule module) {
}
}

@CoreMethod(names = "nesting", isModuleFunction = true, maxArgs = 0)
@CoreMethod(names = "nesting", onSingleton = true, maxArgs = 0)
public abstract static class NestingNode extends CoreMethodNode {

public NestingNode(RubyContext context, SourceSection sourceSection) {
Original file line number Diff line number Diff line change
@@ -111,7 +111,7 @@ public Object match(RubyRegexp regexp, RubyString string) {

}

@CoreMethod(names = "escape", isModuleFunction = true, minArgs = 1, maxArgs = 1)
@CoreMethod(names = "escape", onSingleton = true, minArgs = 1, maxArgs = 1)
public abstract static class EscapeNode extends CoreMethodNode {

public EscapeNode(RubyContext context, SourceSection sourceSection) {
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@ protected boolean notSymbol(RubySymbol a, Object b) {

}

@CoreMethod(names = "all_symbols", isModuleFunction = true, maxArgs = 0)
@CoreMethod(names = "all_symbols", onSingleton = true, maxArgs = 0)
public abstract static class AllSymbolsNode extends CoreMethodNode {

public AllSymbolsNode(RubyContext context, SourceSection sourceSection) {
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ public double sub(RubyTime a, RubyTime b) {

}

@CoreMethod(names = "now", isModuleFunction = true, maxArgs = 0)
@CoreMethod(names = "now", onSingleton = true, maxArgs = 0)
public abstract static class NowNode extends CoreMethodNode {

public NowNode(RubyContext context, SourceSection sourceSection) {
@@ -57,7 +57,7 @@ public RubyTime now() {

}

@CoreMethod(names = {"from_array", "time_s_from_array"}, isModuleFunction = true, maxArgs = 0)
@CoreMethod(names = {"from_array", "time_s_from_array"}, onSingleton = true, maxArgs = 0)
public abstract static class FromArrayNode extends CoreMethodNode {

public FromArrayNode(RubyContext context, SourceSection sourceSection) {
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@
@CoreClass(name = "TruffleDebug")
public abstract class TruffleDebugNodes {

@CoreMethod(names = "array_storage_info", isModuleFunction = true, minArgs = 1, maxArgs = 1)
@CoreMethod(names = "array_storage_info", onSingleton = true, minArgs = 1, maxArgs = 1)
public abstract static class ArrayStorageInfoNode extends CoreMethodNode {

public ArrayStorageInfoNode(RubyContext context, SourceSection sourceSection) {
@@ -39,7 +39,7 @@ public RubyString javaClassOf(RubyArray array) {

}

@CoreMethod(names = "dump_call_stack", isModuleFunction = true, maxArgs = 0)
@CoreMethod(names = "dump_call_stack", onSingleton = true, maxArgs = 0)
public abstract static class DumpCallStackNode extends CoreMethodNode {

public DumpCallStackNode(RubyContext context, SourceSection sourceSection) {
@@ -63,7 +63,7 @@ public NilPlaceholder dumpCallStack() {

}

@CoreMethod(names = "flush_stdout", isModuleFunction = true, maxArgs = 0)
@CoreMethod(names = "flush_stdout", onSingleton = true, maxArgs = 0)
public abstract static class FlushStdoutNode extends CoreMethodNode {

public FlushStdoutNode(RubyContext context, SourceSection sourceSection) {
@@ -82,7 +82,7 @@ public NilPlaceholder flush() {

}

@CoreMethod(names = "full_tree", isModuleFunction = true, maxArgs = 0)
@CoreMethod(names = "full_tree", onSingleton = true, maxArgs = 0)
public abstract static class FullTreeNode extends CoreMethodNode {

public FullTreeNode(RubyContext context, SourceSection sourceSection) {
@@ -102,7 +102,7 @@ public RubyString fullTree() {

}

@CoreMethod(names = "java_class_of", isModuleFunction = true, minArgs = 1, maxArgs = 1)
@CoreMethod(names = "java_class_of", onSingleton = true, minArgs = 1, maxArgs = 1)
public abstract static class JavaClassOfNode extends CoreMethodNode {

public JavaClassOfNode(RubyContext context, SourceSection sourceSection) {
@@ -122,7 +122,7 @@ public RubyString javaClassOf(Object value) {

}

@CoreMethod(names = "panic", isModuleFunction = true, maxArgs = 0)
@CoreMethod(names = "panic", onSingleton = true, maxArgs = 0)
public abstract static class PanicNode extends CoreMethodNode {

public PanicNode(RubyContext context, SourceSection sourceSection) {
@@ -141,7 +141,7 @@ public NilPlaceholder doPanic() {

}

@CoreMethod(names = "parse_tree", isModuleFunction = true, maxArgs = 0)
@CoreMethod(names = "parse_tree", onSingleton = true, maxArgs = 0)
public abstract static class ParseTreeNode extends CoreMethodNode {

public ParseTreeNode(RubyContext context, SourceSection sourceSection) {
@@ -167,7 +167,7 @@ public Object parseTree() {

}

@CoreMethod(names = "slow_path", isModuleFunction = true, minArgs = 1, maxArgs = 1)
@CoreMethod(names = "slow_path", onSingleton = true, minArgs = 1, maxArgs = 1)
public abstract static class SlowPathNode extends CoreMethodNode {

public SlowPathNode(RubyContext context, SourceSection sourceSection) {
@@ -186,7 +186,7 @@ public Object slowPath(Object value) {

}

@CoreMethod(names = "tree", isModuleFunction = true, maxArgs = 0)
@CoreMethod(names = "tree", onSingleton = true, maxArgs = 0)
public abstract static class TreeNode extends CoreMethodNode {

public TreeNode(RubyContext context, SourceSection sourceSection) {
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@

@CoreClass(name = "ByteArray")
public abstract class ByteArrayNodes {
@CoreMethod(names = "allocate", isModuleFunction = true)
@CoreMethod(names = "allocate", onSingleton = true)
public abstract static class AllocateNode extends CoreMethodNode {
public AllocateNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
@@ -35,8 +35,7 @@ public RubyObject allocate(RubyObject baClass, RubyObject size) {
}
}

// FIXME(eregon): this should only be defined on the singleton class, not as an instance method.
@CoreMethod(names = {"new", "allocate_sized"}, isModuleFunction = true, minArgs = 1, maxArgs = 1)
@CoreMethod(names = {"new", "allocate_sized"}, onSingleton = true, minArgs = 1, maxArgs = 1)
public abstract static class AllocateSizedNode extends CoreMethodNode {
@Child
protected DispatchHeadNode bytesToIntNode;
Loading