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: 245cad97f107
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: a275c6acb556
Choose a head ref
  • 2 commits
  • 9 files changed
  • 1 contributor

Commits on Apr 16, 2015

  1. Copy the full SHA
    cfc3f68 View commit details
  2. Copy the full SHA
    a275c6a View commit details
9 changes: 0 additions & 9 deletions spec/truffle/tags/core/module/constants_tags.txt

This file was deleted.

3 changes: 0 additions & 3 deletions spec/truffle/tags/core/module/method_defined_tags.txt

This file was deleted.

10 changes: 0 additions & 10 deletions spec/truffle/tags/core/module/private_method_defined_tags.txt

This file was deleted.

10 changes: 0 additions & 10 deletions spec/truffle/tags/core/module/protected_method_defined_tags.txt

This file was deleted.

10 changes: 0 additions & 10 deletions spec/truffle/tags/core/module/public_method_defined_tags.txt

This file was deleted.

147 changes: 127 additions & 20 deletions truffle/src/main/java/org/jruby/truffle/nodes/core/ModuleNodes.java
Original file line number Diff line number Diff line change
@@ -52,6 +52,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

@CoreClass(name = "Module")
public abstract class ModuleNodes {
@@ -787,6 +788,8 @@ public RubyArray getClassVariables(RubyModule module) {
@CoreMethod(names = "constants", optional = 1)
public abstract static class ConstantsNode extends CoreMethodNode {

@Child BooleanCastNode booleanCastNode;

public ConstantsNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
@@ -795,24 +798,46 @@ public ConstantsNode(ConstantsNode prev) {
super(prev);
}

private boolean booleanCast(VirtualFrame frame, Object value) {
if (booleanCastNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
booleanCastNode = insert(BooleanCastNodeFactory.create(getContext(), getSourceSection(), null));
}
return booleanCastNode.executeBoolean(frame, value);
}

@Specialization
public RubyArray constants(RubyModule module, UndefinedPlaceholder unused) {
public RubyArray constants(RubyModule module, UndefinedPlaceholder inherit) {
return constants(module, true);
}

@Specialization
public RubyArray constants(RubyModule module, boolean inherit) {
notDesignedForCompilation();

final RubyArray array = new RubyArray(getContext().getCoreLibrary().getArrayClass());
final List<RubySymbol> constantsArray = new ArrayList<>();

// TODO(cs): handle inherit
for (String constant : module.getConstants().keySet()) {
array.slowPush(getContext().newSymbol(constant));
final Map<String, RubyConstant> constants;
if (inherit) {
constants = ModuleOperations.getAllConstants(module);
} else {
constants = module.getConstants();
}

return array;
for (Entry<String, RubyConstant> constant : constants.entrySet()) {
if (!constant.getValue().isPrivate()) {
constantsArray.add(getContext().newSymbol(constant.getKey()));
}
}

return RubyArray.fromObjects(getContext().getCoreLibrary().getArrayClass(), constantsArray.toArray(new Object[constantsArray.size()]));
}

@Specialization(guards = "!isUndefinedPlaceholder(arguments[1])")
public RubyArray constants(VirtualFrame frame, RubyModule module, Object inherit) {
return constants(module, booleanCast(frame, inherit));
}

}

@CoreMethod(names = "const_defined?", required = 1, optional = 1)
@@ -1253,7 +1278,8 @@ RubyArray includedModules(RubyModule module) {
}

@CoreMethod(names = "method_defined?", required = 1, optional = 1)
public abstract static class MethodDefinedNode extends CoreMethodNode {
@NodeChildren({ @NodeChild("module"), @NodeChild("name"), @NodeChild("inherit") })
public abstract static class MethodDefinedNode extends RubyNode {

public MethodDefinedNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
@@ -1263,30 +1289,30 @@ public MethodDefinedNode(MethodDefinedNode prev) {
super(prev);
}

@Specialization
public boolean isMethodDefined(RubyModule module, RubyString name, UndefinedPlaceholder inherit) {
notDesignedForCompilation();
@CreateCast("name")
public RubyNode coerceToString(RubyNode name) {
return SymbolOrToStrNodeFactory.create(getContext(), getSourceSection(), name);
}

return ModuleOperations.lookupMethod(module, name.toString()) != null;
@Specialization
public boolean isMethodDefined(RubyModule module, String name, UndefinedPlaceholder inherit) {
return isMethodDefined(module, name, true);
}

@Specialization
public boolean isMethodDefined(RubyModule module, RubyString name, boolean inherit) {
public boolean isMethodDefined(RubyModule module, String name, boolean inherit) {
notDesignedForCompilation();

final InternalMethod method;
if (inherit) {
return ModuleOperations.lookupMethod(module, name.toString()) != null;
method = ModuleOperations.lookupMethod(module, name);
} else {
return module.getMethods().containsKey(name.toString());
method = module.getMethods().get(name);
}
}

@Specialization
public boolean isMethodDefined(RubyModule module, RubySymbol name, UndefinedPlaceholder inherit) {
notDesignedForCompilation();

return ModuleOperations.lookupMethod(module, name.toString()) != null;
return method != null && !method.getVisibility().isPrivate();
}

}

@CoreMethod(names = "module_function", argumentsAsArray = true)
@@ -1487,6 +1513,33 @@ public RubyModule privateClassMethod(RubyModule module, Object... args) {
}
}

@CoreMethod(names = "private_method_defined?", required = 1)
@NodeChildren({ @NodeChild("module"), @NodeChild("name") })
public abstract static class PrivateMethodDefinedNode extends RubyNode {

public PrivateMethodDefinedNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public PrivateMethodDefinedNode(PrivateMethodDefinedNode prev) {
super(prev);
}

@CreateCast("name")
public RubyNode coerceToString(RubyNode name) {
return SymbolOrToStrNodeFactory.create(getContext(), getSourceSection(), name);
}

@Specialization
public boolean isPrivateMethodDefined(RubyModule module, String name) {
notDesignedForCompilation();

InternalMethod method = ModuleOperations.lookupMethod(module, name);
return method != null && method.getVisibility().isPrivate();
}

}

@CoreMethod(names = "protected_instance_methods", optional = 1)
public abstract static class ProtectedInstanceMethodsNode extends CoreMethodNode {

@@ -1520,6 +1573,33 @@ public boolean filter(InternalMethod method) {
}
}

@CoreMethod(names = "protected_method_defined?", required = 1)
@NodeChildren({ @NodeChild("module"), @NodeChild("name") })
public abstract static class ProtectedMethodDefinedNode extends RubyNode {

public ProtectedMethodDefinedNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public ProtectedMethodDefinedNode(ProtectedMethodDefinedNode prev) {
super(prev);
}

@CreateCast("name")
public RubyNode coerceToString(RubyNode name) {
return SymbolOrToStrNodeFactory.create(getContext(), getSourceSection(), name);
}

@Specialization
public boolean isProtectedMethodDefined(RubyModule module, String name) {
notDesignedForCompilation();

InternalMethod method = ModuleOperations.lookupMethod(module, name);
return method != null && method.getVisibility().isProtected();
}

}

@CoreMethod(names = "private_instance_methods", optional = 1)
public abstract static class PrivateInstanceMethodsNode extends CoreMethodNode {

@@ -1584,6 +1664,33 @@ public boolean filter(InternalMethod method) {
}
}

@CoreMethod(names = "public_method_defined?", required = 1)
@NodeChildren({ @NodeChild("module"), @NodeChild("name") })
public abstract static class PublicMethodDefinedNode extends RubyNode {

public PublicMethodDefinedNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public PublicMethodDefinedNode(PublicMethodDefinedNode prev) {
super(prev);
}

@CreateCast("name")
public RubyNode coerceToString(RubyNode name) {
return SymbolOrToStrNodeFactory.create(getContext(), getSourceSection(), name);
}

@Specialization
public boolean isPublicMethodDefined(RubyModule module, String name) {
notDesignedForCompilation();

InternalMethod method = ModuleOperations.lookupMethod(module, name);
return method != null && method.getVisibility() == Visibility.PUBLIC;
}

}

@CoreMethod(names = "instance_methods", optional = 1)
public abstract static class InstanceMethodsNode extends CoreMethodNode {

Original file line number Diff line number Diff line change
@@ -56,10 +56,8 @@ public static Map<String, RubyConstant> getAllConstants(RubyModule module) {
// Look in the current module
constants.putAll(module.getConstants());

// TODO(eregon): Look in lexical scope?

// Look in ancestors
for (RubyModule ancestor : module.parentAncestors()) {
for (RubyModule ancestor : module.includedModules()) {
for (Map.Entry<String, RubyConstant> constant : ancestor.getConstants().entrySet()) {
if (!constants.containsKey(constant.getKey())) {
constants.put(constant.getKey(), constant.getValue());
1 change: 1 addition & 0 deletions truffle/src/main/ruby/core.rb
Original file line number Diff line number Diff line change
@@ -221,6 +221,7 @@
require_relative 'core/kernel'
require_relative 'core/math'
require_relative 'core/method'
require_relative 'core/module'
require_relative 'core/signal'
require_relative 'core/string'
require_relative 'core/thread'
19 changes: 19 additions & 0 deletions truffle/src/main/ruby/core/module.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved. This
# code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
# Eclipse Public License version 1.0
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1

class Module

def Module.constants(inherited = undefined)
if undefined.equal?(inherited)
Object.constants
else
super(inherited)
end
end

end