Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Compatibility] Add Module#const_added #3099

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
andrykonchin marked this conversation as resolved.
Show resolved Hide resolved
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