Permalink
Browse files

Move everything to Weakling namespace, don't extend Delegator, make I…

…dHash include Enumerable.
  • Loading branch information...
1 parent 08cf3e6 commit 178ec9e4a31fac347645a8adffdf99954d319714 @headius committed Mar 22, 2010
Showing with 44 additions and 52 deletions.
  1. +9 −22 ext/org/jruby/ext/RefQueueLibrary.java
  2. +0 −2 lib/weakling.rb
  3. +35 −28 lib/weakling/collections.rb
@@ -7,6 +7,7 @@
import org.jruby.RubyClass;
import org.jruby.RubyException;
import org.jruby.RubyKernel;
+import org.jruby.RubyModule;
import org.jruby.RubyObject;
import org.jruby.anno.JRubyMethod;
import org.jruby.anno.JRubyClass;
@@ -28,13 +29,15 @@
*/
public class RefQueueLibrary implements Library {
public void load(Ruby runtime, boolean wrap) throws IOException {
+ // only used for RefError
RubyKernel.require(runtime.getKernel(), runtime.newString("weakref"), Block.NULL_BLOCK);
- RubyClass weakrefClass = (RubyClass)runtime.getClassFromPath("WeakRef");
+ RubyModule weaklingModule = runtime.getOrCreateModule("Weakling");
+ RubyClass weakrefClass = runtime.defineClassUnder("WeakRef", runtime.getObject(), WEAKREF_ALLOCATOR, weaklingModule);
weakrefClass.setAllocator(WEAKREF_ALLOCATOR);
weakrefClass.defineAnnotatedMethods(WeakRef.class);
- RubyClass refQueueClass = runtime.defineClassUnder("RefQueue", runtime.getObject(), REFQUEUE_ALLOCATOR, weakrefClass);
+ RubyClass refQueueClass = runtime.defineClassUnder("RefQueue", runtime.getObject(), REFQUEUE_ALLOCATOR, weaklingModule);
refQueueClass.defineAnnotatedMethods(RefQueue.class);
}
@@ -117,8 +120,8 @@ public WeakRef(Ruby runtime, RubyClass klazz) {
super(runtime, klazz);
}
- @JRubyMethod(name = "__getobj__")
- public IRubyObject getobj() {
+ @JRubyMethod(name = "get")
+ public IRubyObject get() {
IRubyObject obj = ref.get();
if (obj == null) {
@@ -129,27 +132,11 @@ public IRubyObject getobj() {
return obj;
}
- @JRubyMethod(name = "__setobj__")
- public IRubyObject setobj(IRubyObject obj) {
- return getRuntime().getNil();
- }
-
- // This is only here to replace the "new" in JRuby's weakref, which
- // doesn't really need to be there.
- @JRubyMethod(name = "new", required = 1, optional = 1, meta = true)
- public static IRubyObject newInstance(IRubyObject clazz, IRubyObject[] args) {
- WeakRef weakRef = (WeakRef)((RubyClass)clazz).allocate();
-
- weakRef.callInit(args, Block.NULL_BLOCK);
-
- return weakRef;
- }
-
@JRubyMethod(name = "initialize", frame = true, visibility = Visibility.PRIVATE)
public IRubyObject initialize(ThreadContext context, IRubyObject obj) {
ref = new RubyWeakReference(obj, this);
- return RuntimeHelpers.invokeSuper(context, this, obj, Block.NULL_BLOCK);
+ return context.getRuntime().getNil();
}
@JRubyMethod(name = "initialize", frame = true, visibility = Visibility.PRIVATE)
@@ -159,7 +146,7 @@ public IRubyObject initialize(ThreadContext context, IRubyObject obj, IRubyObjec
}
ref = new RubyWeakReference(obj, this, ((RefQueue)queue).getQueue());
- return RuntimeHelpers.invokeSuper(context, this, obj, Block.NULL_BLOCK);
+ return context.getRuntime().getNil();
}
@JRubyMethod(name = "weakref_alive?")
View
@@ -1,4 +1,2 @@
-require 'weakref'
require 'refqueue'
-
require 'weakling/collections.rb'
@@ -1,41 +1,48 @@
-require 'weakref'
require 'refqueue'
-class WeakRef::IdHash
- def initialize
- @hash = Hash.new
- @queue = WeakRef::RefQueue.new
- end
+module Weakling
+ class IdHash
+ include Enumerable
+
+ def initialize
+ @hash = Hash.new
+ @queue = Weakling::RefQueue.new
+ end
- class IdWeakRef < WeakRef
- attr_accessor :id
- def initialize(obj, queue)
- super(obj, queue)
- @id = obj.__id__
+ class IdWeakRef < Weakling::WeakRef
+ attr_accessor :id
+ def initialize(obj, queue)
+ super(obj, queue)
+ @id = obj.__id__
+ end
end
- end
- def [](id)
- _cleanup
- if wr = @hash[id]
- return wr.__getobj__ rescue nil
+ def [](id)
+ _cleanup
+ if wr = @hash[id]
+ return wr.__getobj__ rescue nil
+ end
+
+ return nil
end
- return nil
- end
+ def add(object)
+ _cleanup
+ wr = IdWeakRef.new(object, @queue)
- def add(object)
- _cleanup
- wr = IdWeakRef.new(object, @queue)
+ @hash[wr.id] = wr
- @hash[wr.id] = wr
+ return wr.id
+ end
- return wr.id
- end
+ def _cleanup
+ while ref = @queue.poll
+ @hash.delete(ref.id)
+ end
+ end
- def _cleanup
- while ref = @queue.poll
- @hash.delete(ref.id)
+ def each
+ @hash.each {|id, wr| obj = wr.get rescue nil; yield [id,obj] if obj}
end
end
-end
+end

0 comments on commit 178ec9e

Please sign in to comment.