-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRefQueueLibrary.java
More file actions
164 lines (138 loc) · 5.64 KB
/
RefQueueLibrary.java
File metadata and controls
164 lines (138 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package org.jruby.ext;
import java.io.IOException;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import org.jruby.Ruby;
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;
import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.Block;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.load.Library;
/**
* This library adds reference queue support to JRuby's weakrefs by adding a
* RefQueue class that wraps a Java ReferenceQueue and replacing the built-in
* WeakRef impl with a new one that's aware of RefQueue.
*
* @author headius
*/
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);
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, weaklingModule);
refQueueClass.defineAnnotatedMethods(RefQueue.class);
}
private static final ObjectAllocator WEAKREF_ALLOCATOR = new ObjectAllocator() {
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
return new WeakRef(runtime, klazz);
}
};
private static final ObjectAllocator REFQUEUE_ALLOCATOR = new ObjectAllocator() {
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
return new RefQueue(runtime, klazz);
}
};
@JRubyClass(name="RefQueue", parent="Object")
public static class RefQueue extends RubyObject {
private final ReferenceQueue queue;
public RefQueue(Ruby runtime, RubyClass klass) {
super(runtime, klass);
queue = new ReferenceQueue();
}
public ReferenceQueue getQueue() {
return queue;
}
@JRubyMethod
public IRubyObject poll() {
return returnable(queue.poll());
}
@JRubyMethod
public IRubyObject remove() {
try {
return returnable(queue.remove());
} catch (InterruptedException ie) {
// ignore
return getRuntime().getNil();
}
}
@JRubyMethod
public IRubyObject remove(IRubyObject timeout) {
try {
return returnable(queue.remove(timeout.convertToInteger().getLongValue()));
} catch (InterruptedException ie) {
// ignore
return getRuntime().getNil();
}
}
private IRubyObject returnable(Object result) {
RubyWeakReference ref = (RubyWeakReference)result;
if (ref == null) return getRuntime().getNil();
return ref.getWeakRef();
}
}
public static class RubyWeakReference extends WeakReference<IRubyObject> {
private final WeakRef ref;
public RubyWeakReference(IRubyObject obj, WeakRef ref) {
super(obj);
this.ref = ref;
}
public RubyWeakReference(IRubyObject obj, WeakRef ref, ReferenceQueue queue) {
super(obj, queue);
this.ref = ref;
}
public WeakRef getWeakRef() {
return ref;
}
}
public static class WeakRef extends RubyObject {
private RubyWeakReference ref;
public WeakRef(Ruby runtime, RubyClass klazz) {
super(runtime, klazz);
}
@JRubyMethod(name = "get")
public IRubyObject get() {
IRubyObject obj = ref.get();
if (obj == null) {
// FIXME weakref.rb also does caller(2) here for the backtrace
throw newRefError("Illegal Reference - probably recycled");
}
return obj;
}
@JRubyMethod(name = "initialize", frame = true, visibility = Visibility.PRIVATE)
public IRubyObject initialize(ThreadContext context, IRubyObject obj) {
ref = new RubyWeakReference(obj, this);
return context.getRuntime().getNil();
}
@JRubyMethod(name = "initialize", frame = true, visibility = Visibility.PRIVATE)
public IRubyObject initialize(ThreadContext context, IRubyObject obj, IRubyObject queue) {
if (!(queue instanceof RefQueue)) {
throw getRuntime().newTypeError("WeakRef can only queue into a RefQueue");
}
ref = new RubyWeakReference(obj, this, ((RefQueue)queue).getQueue());
return context.getRuntime().getNil();
}
@JRubyMethod(name = "weakref_alive?")
public IRubyObject weakref_alive_p() {
return ref.get() != null ? getRuntime().getTrue() : getRuntime().getFalse();
}
private RaiseException newRefError(String message) {
RubyException exception =
(RubyException)getRuntime().getClass("RefError").newInstance(getRuntime().getCurrentContext(),
new IRubyObject[] {getRuntime().newString(message)}, Block.NULL_BLOCK);
return new RaiseException(exception);
}
}
}