Skip to content
This repository has been archived by the owner on Mar 15, 2022. It is now read-only.

Commit

Permalink
Merge 612ae6f into 9f16e93
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasallan committed Mar 18, 2015
2 parents 9f16e93 + 612ae6f commit 1d7c0d4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 51 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ gemspec

group :development do
gem 'rake', '~> 10.3.2'
gem 'rake-compiler', '0.9.5'
end

group :testing do
Expand Down
88 changes: 42 additions & 46 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,69 +16,65 @@ Rake::TestTask.new do |t|
t.verbose = true
end

spec = eval(File.read(File.expand_path('../ref.gemspec', __FILE__)))

Gem::PackageTask.new(spec) do |p|
p.gem_spec = spec
end
Rake.application["package"].prerequisites.unshift("java:build")
Rake.application["package"].prerequisites.unshift("rbx:delete_rbc_files")
GEM_NAME = 'ref'
EXTENSION_NAME = 'extension'
JAVA_EXT_NAME = 'ref_ext'
CORE_GEMSPEC = Gem::Specification.load('ref.gemspec')

desc "Release to rubygems.org"
task :release => [:package, "gem:push"]
if Ref.jruby?
CORE_GEM = "#{GEM_NAME}-#{Ref::VERSION}-java.gem"

namespace :java do
desc "Build the jar files for Jruby support. You must set your JRUBY_HOME environment variable to the root of your jruby install."
task :build do
base_dir = File.dirname(__FILE__)
tmp_dir = File.join(base_dir, "tmp")
classes_dir = File.join(tmp_dir, "classes")
jar_dir = File.join(base_dir, "lib", "org", "jruby", "ext", "ref")
FileUtils.rm_rf(classes_dir)
ext_dir = File.join(base_dir, "ext", "java")
source_files = FileList["#{base_dir}/ext/**/*.java"]
jar_file = File.join(jar_dir, 'references.jar')
# Only build if any of the source files have changed
up_to_date = File.exist?(jar_file) && source_files.all?{|f| File.mtime(f) <= File.mtime(jar_file)}
unless up_to_date
FileUtils.mkdir_p(classes_dir)
puts "#{ENV['JAVA_HOME']}/bin/javac -target 1.5 -classpath '#{"#{ENV['JRUBY_HOME']}/lib/jruby.jar"}' -d '#{classes_dir}' -sourcepath '#{ext_dir}' '#{source_files.join("' '")}'"
`#{ENV['JAVA_HOME']}/bin/javac -target 1.5 -classpath '#{"#{ENV['JRUBY_HOME']}/lib/jruby.jar"}' -d '#{classes_dir}' -sourcepath '#{ext_dir}' '#{source_files.join("' '")}'`
if $? == 0
FileUtils.rm_rf(jar_dir) if File.exist?(jar_dir)
FileUtils.mkdir_p(jar_dir)
`#{ENV['JAVA_HOME']}/bin/jar cf '#{jar_file}' -C '#{classes_dir}' org`
end
FileUtils.rm_rf(classes_dir)
end
require 'rake/javaextensiontask'
Rake::JavaExtensionTask.new(JAVA_EXT_NAME, CORE_GEMSPEC) do |ext|
ext.ext_dir = 'ext'
end
else
CORE_GEM = "#{GEM_NAME}-#{Ref::VERSION}.gem"
end

namespace :rbx do
desc "Cleanup *.rbc files in lib directory"
task :delete_rbc_files do
FileList["lib/**/*.rbc"].each do |rbc_file|
File.delete(rbc_file)
end
nil
task :clean do
rm_f Dir.glob('./**/*.so')
rm_f Dir.glob('./**/*.bundle')
rm_f Dir.glob('./lib/*.jar')
mkdir_p 'pkg'
end


namespace :build do
build_deps = [:clean]
build_deps << :compile if Ref.jruby?
desc "Build #{CORE_GEM} into the pkg directory"
task :core => build_deps do
sh "gem build #{CORE_GEMSPEC.name}.gemspec"
sh 'mv *.gem pkg/'
end
end

if Ref.jruby?
desc 'Build JRuby-specific core gem (alias for `build:core`)'
task :build => ['build:core']
end

namespace :test do
namespace :performance do
desc "Run a speed test on how long it takes to create 100000 weak references"
task :weak_reference do
puts "Testing performance of weak references..."
t = Time.now
Process.fork do
100000.times do
Ref::WeakReference.new(Object.new)
unless Ref.jruby?
t = Time.now
Process.fork do
100000.times do
Ref::WeakReference.new(Object.new)
end
end
Process.wait
puts "Creating 100,000 weak references took #{Time.now - t} seconds"
else
puts 'Cannot run weak_reference performance test on JRuby - Fork is not available on this platform.'
end
Process.wait
puts "Creating 100,000 weak references took #{Time.now - t} seconds"
end

desc "Run a speed test on how long it takes to create 100000 soft references"
task :soft_reference do
puts "Testing performance of soft references..."
Expand Down
Binary file removed lib/org/jruby/ext/ref/references.jar
Binary file not shown.
15 changes: 10 additions & 5 deletions lib/ref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ module Ref
require 'ref/reference'
require 'ref/reference_queue'

# Set the best implementation for weak references based on the runtime.
if defined?(RUBY_PLATFORM) && RUBY_PLATFORM == 'java'
# Use native Java references
if defined?(Java)
begin
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'ref_ext'
require 'org/jruby/ext/ref/references'
ensure
$LOAD_PATH.shift if $LOAD_PATH.first == File.dirname(__FILE__)
rescue LoadError
require 'ref/soft_reference'
require 'ref/weak_reference'
warn 'Error loading Rspec rake tasks, probably building the gem...'
end
else
require 'ref/soft_reference'
Expand All @@ -35,4 +36,8 @@ module Ref
require 'ref/strong_reference'
require 'ref/weak_key_map'
require 'ref/weak_value_map'

def self.jruby?
defined?(Java)
end
end
Binary file added lib/ref_ext.jar
Binary file not shown.

0 comments on commit 1d7c0d4

Please sign in to comment.