Skip to content

Commit

Permalink
Add Module#refinements.
Browse files Browse the repository at this point in the history
  • Loading branch information
itarato committed Jun 5, 2023
1 parent e0c429e commit 7fb546c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 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 `Module#refinements` (#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/module/refinements_spec.rb
17 changes: 12 additions & 5 deletions src/main/java/org/truffleruby/core/module/ModuleNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package org.truffleruby.core.module;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -37,7 +38,6 @@
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
import org.truffleruby.builtins.CoreMethodNode;
import org.truffleruby.annotations.CoreModule;
import org.truffleruby.builtins.NonStandard;
import org.truffleruby.annotations.Primitive;
import org.truffleruby.builtins.PrimitiveArrayArgumentsNode;
import org.truffleruby.builtins.PrimitiveNode;
Expand Down Expand Up @@ -2324,7 +2324,6 @@ protected RubyArray usedModules() {

}

@NonStandard
@CoreMethod(names = "used_refinements", onSingleton = true)
public abstract static class UsedRefinementsNode extends CoreMethodArrayArgumentsNode {

Expand All @@ -2335,15 +2334,23 @@ protected RubyArray usedRefinements() {
final DeclarationContext declarationContext = RubyArguments.getDeclarationContext(frame);
final Set<RubyModule> refinements = new HashSet<>();
for (RubyModule[] refinementModules : declarationContext.getRefinements().values()) {
for (RubyModule refinementModule : refinementModules) {
refinements.add(refinementModule);
}
Collections.addAll(refinements, refinementModules);
}
return createArray(refinements.toArray());
}

}

@CoreMethod(names = "refinements")
public abstract static class RefinementsNode extends CoreMethodArrayArgumentsNode {

@Specialization
protected RubyArray refinements(RubyModule self) {
return createArray(self.fields.getRefinements().values().toArray());
}

}

@GenerateUncached
@ImportStatic(ArrayGuards.class)
public abstract static class SetMethodVisibilityNode extends RubyBaseNode {
Expand Down
15 changes: 15 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,21 @@ def test_used_modules
assert_equal [ref::RefB, ref::RefA], ref::Combined::USED_MODS
end

def test_refinements
int_refinement = nil
str_refinement = nil
m = Module.new {
refine Integer do
int_refinement = self
end

refine String do
str_refinement = self
end
}
assert_equal([int_refinement, str_refinement], m.refinements)
end

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

0 comments on commit 7fb546c

Please sign in to comment.