Skip to content

Commit

Permalink
[Truffle] Implement Module#const_get.
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Nov 3, 2014
1 parent 9f827b9 commit 271942a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
47 changes: 47 additions & 0 deletions core/src/main/java/org/jruby/truffle/nodes/core/ModuleNodes.java
Expand Up @@ -16,6 +16,7 @@
import org.jruby.runtime.Visibility;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.RubyRootNode;
import org.jruby.truffle.nodes.dispatch.Dispatch;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNode;
import org.jruby.truffle.nodes.cast.BooleanCastNode;
import org.jruby.truffle.nodes.cast.BooleanCastNodeFactory;
Expand Down Expand Up @@ -487,6 +488,52 @@ public boolean isConstDefined(RubyModule module, RubySymbol name, @SuppressWarni

}

@CoreMethod(names = "const_get", required = 1)
public abstract static class ConstGetNode extends CoreMethodNode {

@Child protected DispatchHeadNode dispatch;

public ConstGetNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
dispatch = new DispatchHeadNode(context, Dispatch.MissingBehavior.CALL_CONST_MISSING);
}

public ConstGetNode(ConstGetNode prev) {
super(prev);
dispatch = prev.dispatch;
}

@Specialization
public Object getConstant(VirtualFrame frame, RubyModule module, RubyString name) {
notDesignedForCompilation();

return dispatch.dispatch(
frame,
getContext().getCoreLibrary().getNilObject(),
null,
module,
name,
null,
new Object[]{},
Dispatch.DispatchAction.READ_CONSTANT);
}

@Specialization
public Object getConstant(VirtualFrame frame, RubyModule module, RubySymbol name) {
notDesignedForCompilation();

return dispatch.dispatch(
frame,
getContext().getCoreLibrary().getNilObject(),
null,
module,
name,
null,
new Object[]{},
Dispatch.DispatchAction.READ_CONSTANT);
}
}

@CoreMethod(names = "const_missing", needsSelf = false, required = 1)
public abstract static class ConstMissingNode extends CoreMethodNode {

Expand Down
22 changes: 3 additions & 19 deletions spec/truffle/tags/core/module/const_get_tags.txt
@@ -1,25 +1,9 @@
fails:Module#const_get accepts a String or Symbol name
fails:Module#const_get raises a NameError with the not found constant symbol
fails:Module#const_get raises a NameError if the name contains non-alphabetic characters except '_'
fails:Module#const_get calls #to_str to convert the given name to a String
fails:Module#const_get raises a TypeError if conversion to a String by calling #to_str fails
fails:Module#const_get calls #const_missing on the receiver if unable to locate the constant
fails:Module#const_get does not search the containing scope
fails:Module#const_get searches into the receiver superclasses if the inherit flag is true
fails:Module#const_get accepts a toplevel scope qualifier
fails:Module#const_get accepts a scoped constant name
fails:Module#const_get with statically assigned constants searches the immediate class or module first
fails:Module#const_get with statically assigned constants searches a module included in the immediate class before the superclass
fails:Module#const_get with statically assigned constants searches the superclass before a module included in the superclass
fails:Module#const_get with statically assigned constants searches a module included in the superclass
fails:Module#const_get with statically assigned constants searches the superclass chain
fails:Module#const_get with statically assigned constants returns a toplevel constant when the receiver is a Class
fails:Module#const_get with statically assigned constants returns a toplevel constant when the receiver is a Module
fails:Module#const_get with dynamically assigned constants searches the immediate class or module first
fails:Module#const_get with dynamically assigned constants searches a module included in the immediate class before the superclass
fails:Module#const_get with dynamically assigned constants searches the superclass before a module included in the superclass
fails:Module#const_get with dynamically assigned constants searches a module included in the superclass
fails:Module#const_get with dynamically assigned constants searches the superclass chain
fails:Module#const_get with dynamically assigned constants returns a toplevel constant when the receiver is a Class
fails:Module#const_get with dynamically assigned constants returns a toplevel constant when the receiver is a Module
fails:Module#const_get with dynamically assigned constants returns the updated value of a constant
fails:Module#const_get raises a NameError if the constant is defined in the receiver's supperclass and the inherit flag is false
fails:Module#const_get raises a NameError when the receiver is a Module, the constant is defined at toplevel and the inherit flag is false
fails:Module#const_get raises a NameError when the receiver is a Class, the constant is defined at toplevel and the inherit flag is false

0 comments on commit 271942a

Please sign in to comment.