Skip to content

Commit

Permalink
Add Refinement#refined_class
Browse files Browse the repository at this point in the history
  • Loading branch information
itarato committed Jun 5, 2023
1 parent e0c429e commit 77e1363
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Compatibility:
* Add `String#bytesplice` (#3039, @itarato).
* Add `String#byteindex` and `String#byterindex` (#3039, @itarato).
* Add implementations of `rb_proc_call_with_block`, `rb_proc_call_kw`, `rb_proc_call_with_block_kw` and `rb_funcall_with_block_kw` (#3068, @andrykonchin).
* Add `Refinement#refined_class` (#3039, @itarato).

Performance:

Expand Down
2 changes: 2 additions & 0 deletions spec/truffleruby.next-specs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ spec/ruby/core/string/bytesplice_spec.rb

spec/ruby/core/string/byteindex_spec.rb
spec/ruby/core/string/byterindex_spec.rb

spec/ruby/core/refinement/refined_class_spec.rb
9 changes: 9 additions & 0 deletions src/main/java/org/truffleruby/core/module/ModuleNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -2584,4 +2584,13 @@ protected Object doClass(RubyClass rubyClass) {
return rubyClass.isSingleton;
}
}

@Primitive(name = "refined_class")
public abstract static class RefinedClassNode extends PrimitiveArrayArgumentsNode {

@Specialization
protected RubyModule refinedClass(RubyModule refinement) {
return refinement.fields.getRefinedModule();
}
}
}
4 changes: 4 additions & 0 deletions src/main/ruby/truffleruby/core/refinement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ def import_methods(*modules)
end
self
end

def refined_class
Primitive.refined_class(self)
end
end
14 changes: 14 additions & 0 deletions test/mri/tests/ruby/test_refinement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,20 @@ def test_used_modules
assert_equal [ref::RefB, ref::RefA], ref::Combined::USED_MODS
end

def test_refined_class
refinements = Module.new {
refine Integer do
int_refinement = self
end

refine String do
str_refinement = self
end
}.refinements
assert_equal(Integer, refinements[0].refined_class)
assert_equal(String, refinements[1].refined_class)
end

def test_warn_setconst_in_refinmenet
bug10103 = '[ruby-core:64143] [Bug #10103]'
warnings = [
Expand Down

0 comments on commit 77e1363

Please sign in to comment.