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

Revert "Replaces compiled Java with JRuby interop" #20

Merged
merged 1 commit into from
Feb 9, 2015
Merged
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
28 changes: 28 additions & 0 deletions ext/java/org/jruby/ext/ref/ReferencesService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.jruby.ext.ref;

import java.io.IOException;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.load.BasicLibraryService;

/**
* This library adds native Java support for weak and soft references.
*
* @author Brian Durand
*/
public class ReferencesService implements BasicLibraryService {
public boolean basicLoad(Ruby runtime) throws IOException {
RubyModule refModule = runtime.getModule("Ref");
RubyClass referenceClass = refModule.getClass("Reference");

RubyClass rubyWeakReferenceClass = runtime.defineClassUnder("WeakReference", referenceClass, RubyWeakReference.ALLOCATOR, refModule);
rubyWeakReferenceClass.defineAnnotatedMethods(RubyWeakReference.class);

RubyClass rubySoftReferenceClass = runtime.defineClassUnder("SoftReference", referenceClass, RubySoftReference.ALLOCATOR, refModule);
rubySoftReferenceClass.defineAnnotatedMethods(RubySoftReference.class);

return true;
}
}
43 changes: 43 additions & 0 deletions ext/java/org/jruby/ext/ref/RubySoftReference.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.jruby.ext.ref;

import java.lang.ref.SoftReference;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyObject;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;

public class RubySoftReference extends RubyObject {
private SoftReference _ref;
private static final String REFERENCED_OBJECT_ID_VARIABLE = "@referenced_object_id".intern();

public RubySoftReference(Ruby runtime, RubyClass klass) {
super(runtime, klass);
}

public static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
return new RubySoftReference(runtime, klass);
}
};

@JRubyMethod(name = "initialize", frame = true, visibility = Visibility.PRIVATE)
public IRubyObject initialize(ThreadContext context, IRubyObject obj) {
_ref = new SoftReference<IRubyObject>(obj);
fastSetInstanceVariable(REFERENCED_OBJECT_ID_VARIABLE, obj.id());
return context.getRuntime().getNil();
}

@JRubyMethod(name = "object")
public IRubyObject object() {
IRubyObject obj = (IRubyObject)_ref.get();
if (obj != null) {
return obj;
} else {
return getRuntime().getNil();
}
}
}
43 changes: 43 additions & 0 deletions ext/java/org/jruby/ext/ref/RubyWeakReference.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.jruby.ext.ref;

import java.lang.ref.WeakReference;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyObject;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;

public class RubyWeakReference extends RubyObject {
private WeakReference _ref;
private static final String REFERENCED_OBJECT_ID_VARIABLE = "@referenced_object_id".intern();

public RubyWeakReference(Ruby runtime, RubyClass klass) {
super(runtime, klass);
}

public static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
return new RubyWeakReference(runtime, klass);
}
};

@JRubyMethod(name = "initialize", frame = true, visibility = Visibility.PRIVATE)
public IRubyObject initialize(ThreadContext context, IRubyObject obj) {
_ref = new WeakReference<IRubyObject>(obj);
fastSetInstanceVariable(REFERENCED_OBJECT_ID_VARIABLE, obj.id());
return context.getRuntime().getNil();
}

@JRubyMethod(name = "object")
public IRubyObject object() {
IRubyObject obj = (IRubyObject)_ref.get();
if (obj != null) {
return obj;
} else {
return getRuntime().getNil();
}
}
}
Binary file added lib/org/jruby/ext/ref/references.jar
Binary file not shown.
12 changes: 7 additions & 5 deletions lib/ref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ module Ref

# Set the best implementation for weak references based on the runtime.
if defined?(RUBY_PLATFORM) && RUBY_PLATFORM == 'java'
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'ref/jruby/weak_reference'
require 'ref/jruby/soft_reference'

$LOAD_PATH.shift if $LOAD_PATH.first == File.dirname(__FILE__)
# Use native Java references
begin
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'org/jruby/ext/ref/references'
ensure
$LOAD_PATH.shift if $LOAD_PATH.first == File.dirname(__FILE__)
end
else
require 'ref/soft_reference'
if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
Expand Down
13 changes: 0 additions & 13 deletions lib/ref/jruby/soft_reference.rb

This file was deleted.

15 changes: 0 additions & 15 deletions lib/ref/jruby/weak_reference.rb

This file was deleted.