Skip to content

Commit

Permalink
Showing 5 changed files with 46 additions and 29 deletions.
6 changes: 0 additions & 6 deletions spec/truffle/tags/core/module/class_variable_defined_tags.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
fails:Module#class_variable_defined? returns true if a class variable with the given name is defined in self
fails:Module#class_variable_defined? returns true if a class variable with the given name is defined in the metaclass
fails:Module#class_variable_defined? returns true if the class variable is defined in a metaclass
fails:Module#class_variable_defined? returns true if a class variables with the given name is defined in an included module
fails:Module#class_variable_defined? raises a NameError when the given name is not allowed
fails:Module#class_variable_defined? converts a non string/symbol/fixnum name to string using to_str
fails:Module#class_variable_defined? raises a TypeError when the given names can't be converted to strings using to_str
4 changes: 0 additions & 4 deletions spec/truffle/tags/core/module/class_variable_get_tags.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
fails:Module#class_variable_get returns the value of the class variable with the given name
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 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
7 changes: 0 additions & 7 deletions spec/truffle/tags/core/module/class_variable_set_tags.txt

This file was deleted.

1 change: 0 additions & 1 deletion spec/truffle/tags/core/module/class_variables_tags.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
fails:Module#class_variables returns an Array of Symbols of class variable names defined in a metaclass
fails:Module#class_variables returns an Array with names of class variables defined in metaclasses
57 changes: 46 additions & 11 deletions truffle/src/main/java/org/jruby/truffle/nodes/core/ModuleNodes.java
Original file line number Diff line number Diff line change
@@ -688,22 +688,29 @@ public Object classExec(VirtualFrame frame, RubyModule self, Object[] args, NotP
}

@CoreMethod(names = "class_variable_defined?", required = 1)
public abstract static class ClassVariableDefinedNode extends CoreMethodArrayArgumentsNode {
@NodeChildren({
@NodeChild(type = RubyNode.class, value = "module"),
@NodeChild(type = RubyNode.class, value = "name")
})
public abstract static class ClassVariableDefinedNode extends CoreMethodNode {

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

@TruffleBoundary
@Specialization(guards = "isRubyString(name)")
public boolean isClassVariableDefinedString(RubyModule module, RubyBasicObject name) {
return module.getClassVariables().containsKey(name.toString());
@CreateCast("name")
public RubyNode coerceToString(RubyNode name) {
return NameToJavaStringNodeGen.create(getContext(), getSourceSection(), name);
}

@TruffleBoundary
@Specialization(guards = "isRubySymbol(name)")
public boolean isClassVariableDefinedSymbol(RubyModule module, RubyBasicObject name) {
return module.getClassVariables().containsKey(SymbolNodes.getString(name));
@Specialization
public boolean isClassVariableDefinedString(RubyModule module, String name) {
RubyContext.checkClassVariableName(getContext(), name, this);

final Object value = ModuleOperations.lookupClassVariable(module, name);

return value != null;
}

}
@@ -725,11 +732,11 @@ public RubyNode coerceToString(RubyNode name) {
}

@Specialization
@TruffleBoundary
public Object getClassVariable(RubyModule module, String name) {
CompilerDirectives.transferToInterpreter();

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

final Object value = ModuleOperations.lookupClassVariable(module, name);

if (value == null) {
CompilerDirectives.transferToInterpreter();
@@ -741,6 +748,34 @@ public Object getClassVariable(RubyModule module, String name) {

}

@CoreMethod(names = "class_variable_set", required = 2, raiseIfFrozenSelf = true)
@NodeChildren({
@NodeChild(type = RubyNode.class, value = "module"),
@NodeChild(type = RubyNode.class, value = "name"),
@NodeChild(type = RubyNode.class, value = "value")
})
public abstract static class ClassVariableSetNode extends CoreMethodNode {

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

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

@Specialization
@TruffleBoundary
public Object setClassVariable(RubyModule module, String name, Object value) {
RubyContext.checkClassVariableName(getContext(), name, this);

module.getClassVariables().put(name, value);
return value;
}

}

@CoreMethod(names = "class_variables")
public abstract static class ClassVariablesNode extends CoreMethodArrayArgumentsNode {

0 comments on commit 1461bfe

Please sign in to comment.