Skip to content

Commit

Permalink
[Truffle] Proper delegation to Cext's require.
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed May 29, 2015
1 parent 3956fba commit a6f927d
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions truffle/src/main/ruby/core/truffle/cext/require.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,45 @@
# GNU Lesser General Public License version 2.1

if Truffle::CExt.supported?
module Kernel
# Monkey patch #require, similar to how RubyGems does,
# in order to load C extensions.
alias_method :truffle_cext_original_require, :require

# Normally a Ruby method in core is not allowed to replace one defined in Java.
# But here we really want it.
remove_method :require

def require(name)
# We're getting quite hacky here. A lot of C extensions are required
# using the format foo/foo, so we need to guess that the real name is
# foo from that.

if name =~ /(.+?)\/\1/
cext_name = $1
else
cext_name = name
end

# Monkey patch #require, similar to how RubyGems does, in order to load
# C extensions.

def require(name)
# We're getting quite hacky here. A lot of C extensions are required
# using the format foo/foo, so we need to guess that the real name is
# foo from that.

if name =~ /(.+?)\/\1/
cext_name = $1
else
cext_name = name
end

# Look in each $JRUBY_TRUFFLE_CEXT_PATH directory for
# cext_name/ext/cext_name/extconf.rb
# Look in each $JRUBY_TRUFFLE_CEXT_PATH directory for
# cext_name/ext/cext_name/extconf.rb

if ENV.include? 'JRUBY_TRUFFLE_CEXT_PATH'
cext_path = ENV['JRUBY_TRUFFLE_CEXT_PATH'].split(':')
else
cext_path = [Dir.pwd]
end
if ENV.include? 'JRUBY_TRUFFLE_CEXT_PATH'
cext_path = ENV['JRUBY_TRUFFLE_CEXT_PATH'].split(':')
else
cext_path = [Dir.pwd]
end

cext_path.each do |dir|
extconf = File.join(dir, cext_name, 'ext', cext_name, 'extconf.rb')
cext_path.each do |dir|
extconf = File.join(dir, cext_name, 'ext', cext_name, 'extconf.rb')

if File.exist? extconf
return Truffle::CExt::load_extconf(extconf)
if File.exist? extconf
return Truffle::CExt::load_extconf(extconf)
end
end
end

Kernel.require name
truffle_cext_original_require name
end
module_function :require
end

end

1 comment on commit a6f927d

@chrisseaton
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

Please sign in to comment.