Skip to content

Commit 35a42df

Browse files
itaratoandrykonchin
authored andcommitted
Add Module#const_added.
1 parent b6c229a commit 35a42df

File tree

5 files changed

+43
-0
lines changed

5 files changed

+43
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Compatibility:
2929
* Add `Module#refinements` (#3039, @itarato).
3030
* Add `Refinement#refined_class` (#3039, @itarato).
3131
* Add `rb_hash_new_capa` function (#3039, @itarato).
32+
* Add `Module#const_added` (#3039, @itarato).
3233

3334
Performance:
3435

spec/ruby/core/module/const_added_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,36 @@ class SubClass
121121

122122
ScratchPad.recorded.should == [line + 2, line + 4, line + 7, line + 11]
123123
end
124+
125+
it "is called when the constant is ready to be used" do
126+
mod = Module.new do
127+
def self.const_added(name)
128+
ScratchPad.record const_get(name)
129+
end
130+
end
131+
132+
mod.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
133+
TEST = 123
134+
RUBY
135+
136+
ScratchPad.recorded.should == 123
137+
end
138+
139+
it "records re-definition of existing constants" do
140+
ScratchPad.record []
141+
142+
mod = Module.new do
143+
def self.const_added(name)
144+
ScratchPad << const_get(name)
145+
end
146+
end
147+
148+
mod.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
149+
TEST = 123
150+
TEST = 456
151+
RUBY
152+
153+
ScratchPad.recorded.should == [123, 456]
154+
end
124155
end
125156
end

spec/truffleruby.next-specs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ spec/ruby/core/module/refinements_spec.rb
3434
spec/ruby/core/refinement/refined_class_spec.rb
3535
spec/ruby/core/module/used_refinements_spec.rb
3636
spec/ruby/optional/capi/hash_spec.rb
37+
spec/ruby/core/module/const_added_spec.rb

src/main/java/org/truffleruby/core/module/ModuleFields.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.truffleruby.RubyLanguage;
3131
import org.truffleruby.collections.ConcurrentOperations;
3232
import org.truffleruby.collections.ConcurrentWeakSet;
33+
import org.truffleruby.core.CoreLibrary;
3334
import org.truffleruby.core.encoding.Encodings;
3435
import org.truffleruby.core.encoding.TStringUtils;
3536
import org.truffleruby.core.kernel.KernelNodes;
@@ -494,6 +495,12 @@ private RubyConstant setConstantInternal(RubyContext context, Node currentNode,
494495
invalidateConstantIncludedBy(name);
495496
}
496497

498+
final CoreLibrary coreLibrary = context.getCoreLibrary();
499+
if (currentNode != null && coreLibrary != null && coreLibrary.isLoaded()) {
500+
final RubySymbol constSymbol = context.getLanguageSlow().getSymbol(name);
501+
RubyContext.send(currentNode, rubyModule, "const_added", constSymbol);
502+
}
503+
497504
return autoload ? newConstant : previous;
498505
}
499506

src/main/ruby/truffleruby/core/module.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ def prepend(*modules)
105105
self
106106
end
107107

108+
private def const_added(name)
109+
end
110+
108111
def const_defined?(name, inherit = true)
109112
Primitive.module_const_defined?(self, name, inherit, true)
110113
end

0 commit comments

Comments
 (0)