Skip to content

Commit

Permalink
[Truffle] Fix a couple cases of Module#class_variable_get.
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Apr 20, 2015
1 parent ff7a1d0 commit c0a8a73
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
3 changes: 0 additions & 3 deletions spec/truffle/tags/core/module/class_variable_get_tags.txt
@@ -1,9 +1,6 @@
fails:Module#class_variable_get returns the value of the class variable with the given name
fails:Module#class_variable_get raises a NameError for a class variable named '@@'
fails:Module#class_variable_get raises a NameError for a class variables with the given name defined in an extended module
fails:Module#class_variable_get returns class variables defined in the metaclass and accessed by class methods
fails:Module#class_variable_get returns class variables defined in the metaclass and accessed by instance methods
fails:Module#class_variable_get returns a class variable defined in a metaclass
fails:Module#class_variable_get raises a NameError when an uninitialized class variable is accessed
fails:Module#class_variable_get converts a non string/symbol/fixnum name to string using to_str
fails:Module#class_variable_get raises a TypeError when the given names can't be converted to strings using to_str
Expand Up @@ -737,7 +737,8 @@ public boolean isClassVariableDefined(RubyModule module, RubySymbol name) {
}

@CoreMethod(names = "class_variable_get", required = 1)
public abstract static class ClassVariableGetNode extends CoreMethodNode {
@NodeChildren({ @NodeChild("module"), @NodeChild("name") })
public abstract static class ClassVariableGetNode extends RubyNode {

public ClassVariableGetNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
Expand All @@ -747,16 +748,24 @@ public ClassVariableGetNode(ClassVariableGetNode prev) {
super(prev);
}

@Specialization
public Object getClassVariable(RubyModule module, RubyString name) {
notDesignedForCompilation();
return ModuleOperations.lookupClassVariable(module, RubyContext.checkClassVariableName(getContext(), name.toString(), this));
@CreateCast("name")
public RubyNode coerceToString(RubyNode name) {
return SymbolOrToStrNodeFactory.create(getContext(), getSourceSection(), name);
}

@Specialization
public Object getClassVariable(RubyModule module, RubySymbol name) {
public Object getClassVariable(RubyModule module, String name) {
notDesignedForCompilation();
return ModuleOperations.lookupClassVariable(module, RubyContext.checkClassVariableName(getContext(), name.toString(), this));

RubyContext.checkClassVariableName(getContext(), name, this);
Object value = ModuleOperations.lookupClassVariable(module, name);

if (value == null) {
CompilerDirectives.transferToInterpreter();
throw new RaiseException(getContext().getCoreLibrary().nameErrorUninitializedClassVariable(module, name, this));
} else {
return value;
}
}

}
Expand Down
Expand Up @@ -867,6 +867,11 @@ public RubyException nameErrorUninitializedConstant(RubyModule module, String na
return nameError(String.format("uninitialized constant %s::%s", module.getName(), name), name, currentNode);
}

public RubyException nameErrorUninitializedClassVariable(RubyModule module, String name, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return nameError(String.format("uninitialized class variable %s in %s", name, module.getName()), name, currentNode);
}

public RubyException nameErrorPrivateConstant(RubyModule module, String name, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return nameError(String.format("private constant %s::%s referenced", module.getName(), name), name, currentNode);
Expand Down

0 comments on commit c0a8a73

Please sign in to comment.