Skip to content

Commit

Permalink
Truffle's autoload works like Ruby's
Browse files Browse the repository at this point in the history
But Truffle has its own binding_of_caller equivalent
  • Loading branch information
MaxLap committed Dec 6, 2018
1 parent fb65dd7 commit 59d7e31
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core_gem/lib/deep_cover/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def running?

def start
return if running?
if RUBY_VERSION >= '2.3.0' && RUBY_PLATFORM != 'java'
if RUBY_VERSION >= '2.3.0' && on_mri?
require_relative 'core_ext/instruction_sequence_load_iseq'
else
require_relative 'core_ext/autoload_overrides'
Expand Down
8 changes: 7 additions & 1 deletion core_gem/lib/deep_cover/core_ext/autoload_overrides.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ module DeepCover
load_all

module KernelAutoloadOverride
if RUBY_PLATFORM == 'java'
if DeepCover.on_jruby?
# JRuby has different semantics for Kernel.autoload and Kernel#autoload
def autoload(name, path)
if Kernel.equal?(self)
Expand All @@ -99,6 +99,12 @@ def autoload(name, path)
autoload_path = DeepCover.autoload_tracker.autoload_path_for(mod, name, path)
mod.autoload_without_deep_cover(name, autoload_path)
end
elsif DeepCover.on_truffleruby?
def autoload(name, path)
mod = Truffle.invoke_primitive(:caller_binding).eval('Module.nesting').first || Object
autoload_path = DeepCover.autoload_tracker.autoload_path_for(mod, name, path)
mod.autoload_without_deep_cover(name, autoload_path)
end
else
def autoload(name, path)
mod = binding.of_caller(1).eval('Module.nesting').first || Object
Expand Down
8 changes: 4 additions & 4 deletions core_gem/spec/deep_cover_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
RSpec.describe DeepCover do
describe 'cover' do
after { DeepCover.reset }
it 'temporarily overrides (or not in 2.3 +) `require`, `require_relative` and `autoload`' do
it 'temporarily overrides (or not in MRI 2.3+) `require`, `require_relative` and `autoload`' do
methods = %i[require require_relative]
methods << :autoload unless RUBY_PLATFORM == 'java'
original = methods.map { |m| method(m).source_location }
Expand All @@ -15,11 +15,11 @@
sources = methods.map { |m| method(m).source_location }
end
sources.zip(original).each do |now, before|
if RUBY_VERSION < '2.3.0' || RUBY_PLATFORM == 'java'
now.should_not == before
else
if RUBY_VERSION >= '2.3.0' && DeepCover.on_mri?
# We use load_iseq in 2.3+, so no override in that case
now.should == before
else
now.should_not == before
end
end
methods.map { |m| method(m).source_location }.should == original
Expand Down

0 comments on commit 59d7e31

Please sign in to comment.