Skip to content

Commit

Permalink
Add Method#const_addeed.
Browse files Browse the repository at this point in the history
  • Loading branch information
itarato committed Jun 12, 2023
1 parent c7760e6 commit 8928843
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Compatibility:
* 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 optional `timeout` argument to `Thread::Queue#pop` (#3039, @itarato).
* Add `Module#const_added` (#3039, @itarato).

Performance:

Expand Down
31 changes: 31 additions & 0 deletions spec/ruby/core/module/const_added_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,36 @@ class SubClass

ScratchPad.recorded.should == [line + 2, line + 4, line + 7, line + 11]
end

it "is called when the constant is ready to be used" do
mod = Module.new do
def self.const_added(name)
ScratchPad.record const_get(name)
end
end

mod.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
TEST = 123
RUBY

ScratchPad.recorded.should == 123
end

it "records re-definition of existing constants" do
ScratchPad.record []

mod = Module.new do
def self.const_added(name)
ScratchPad << const_get(name)
end
end

mod.module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
TEST = 123
TEST = 456
RUBY

ScratchPad.recorded.should == [123, 456]
end
end
end
2 changes: 2 additions & 0 deletions spec/truffleruby.next-specs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ spec/ruby/core/string/byterindex_spec.rb
spec/ruby/core/queue/deq_spec.rb
spec/ruby/core/queue/pop_spec.rb
spec/ruby/core/queue/shift_spec.rb

spec/ruby/core/module/const_added_spec.rb
7 changes: 7 additions & 0 deletions src/main/java/org/truffleruby/core/module/ModuleFields.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.truffleruby.RubyLanguage;
import org.truffleruby.collections.ConcurrentOperations;
import org.truffleruby.collections.ConcurrentWeakSet;
import org.truffleruby.core.CoreLibrary;
import org.truffleruby.core.encoding.Encodings;
import org.truffleruby.core.encoding.TStringUtils;
import org.truffleruby.core.kernel.KernelNodes;
Expand Down Expand Up @@ -494,6 +495,12 @@ private RubyConstant setConstantInternal(RubyContext context, Node currentNode,
invalidateConstantIncludedBy(name);
}

final CoreLibrary coreLibrary = context.getCoreLibrary();
if (currentNode != null && coreLibrary != null && coreLibrary.isLoaded()) {
final RubySymbol constSymbol = context.getLanguageSlow().getSymbol(name);
RubyContext.send(currentNode, rubyModule, "const_added", constSymbol);
}

return autoload ? newConstant : previous;
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/ruby/truffleruby/core/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ def prepend(*modules)
self
end

private def const_added(name)
end

def const_defined?(name, inherit = true)
Primitive.module_const_defined?(self, name, inherit, true)
end
Expand Down

0 comments on commit 8928843

Please sign in to comment.