weakling 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +8 -0
- data/ext/org/jruby/ext/RefQueueLibrary.java +9 -23
- data/lib/refqueue.jar +0 -0
- data/lib/weakling.rb +0 -2
- data/lib/weakling/collections.rb +35 -28
- data/weakling.gemspec +1 -1
- metadata +3 -3
data/Rakefile
CHANGED
@@ -2,19 +2,27 @@ require 'ant'
|
|
2
2
|
|
3
3
|
directory "pkg/classes"
|
4
4
|
|
5
|
+
desc "Clean up build artifacts"
|
5
6
|
task :clean do
|
6
7
|
rm_rf "pkg/classes"
|
7
8
|
rm_rf "lib/refqueue.jar"
|
8
9
|
end
|
9
10
|
|
11
|
+
desc "Compile the extension"
|
10
12
|
task :compile => "pkg/classes" do |t|
|
11
13
|
ant.javac :srcdir => "ext", :destdir => t.prerequisites.first,
|
12
14
|
:source => "1.5", :target => "1.5", :debug => true,
|
13
15
|
:classpath => "${java.class.path}:${sun.boot.class.path}"
|
14
16
|
end
|
15
17
|
|
18
|
+
desc "Build the jar"
|
16
19
|
task :jar => :compile do
|
17
20
|
ant.jar :basedir => "pkg/classes", :destfile => "lib/refqueue.jar", :includes => "**/*.class"
|
18
21
|
end
|
19
22
|
|
20
23
|
task :package => :jar
|
24
|
+
|
25
|
+
desc "Run the specs"
|
26
|
+
task :spec => :jar do
|
27
|
+
ruby "-S", "spec", "spec"
|
28
|
+
end
|
@@ -7,11 +7,11 @@ import org.jruby.Ruby;
|
|
7
7
|
import org.jruby.RubyClass;
|
8
8
|
import org.jruby.RubyException;
|
9
9
|
import org.jruby.RubyKernel;
|
10
|
+
import org.jruby.RubyModule;
|
10
11
|
import org.jruby.RubyObject;
|
11
12
|
import org.jruby.anno.JRubyMethod;
|
12
13
|
import org.jruby.anno.JRubyClass;
|
13
14
|
import org.jruby.exceptions.RaiseException;
|
14
|
-
import org.jruby.javasupport.util.RuntimeHelpers;
|
15
15
|
import org.jruby.runtime.Block;
|
16
16
|
import org.jruby.runtime.ObjectAllocator;
|
17
17
|
import org.jruby.runtime.ThreadContext;
|
@@ -28,13 +28,15 @@ import org.jruby.runtime.load.Library;
|
|
28
28
|
*/
|
29
29
|
public class RefQueueLibrary implements Library {
|
30
30
|
public void load(Ruby runtime, boolean wrap) throws IOException {
|
31
|
+
// only used for RefError
|
31
32
|
RubyKernel.require(runtime.getKernel(), runtime.newString("weakref"), Block.NULL_BLOCK);
|
32
33
|
|
33
|
-
|
34
|
+
RubyModule weaklingModule = runtime.getOrCreateModule("Weakling");
|
35
|
+
RubyClass weakrefClass = runtime.defineClassUnder("WeakRef", runtime.getObject(), WEAKREF_ALLOCATOR, weaklingModule);
|
34
36
|
weakrefClass.setAllocator(WEAKREF_ALLOCATOR);
|
35
37
|
weakrefClass.defineAnnotatedMethods(WeakRef.class);
|
36
38
|
|
37
|
-
RubyClass refQueueClass = runtime.defineClassUnder("RefQueue", runtime.getObject(), REFQUEUE_ALLOCATOR,
|
39
|
+
RubyClass refQueueClass = runtime.defineClassUnder("RefQueue", runtime.getObject(), REFQUEUE_ALLOCATOR, weaklingModule);
|
38
40
|
refQueueClass.defineAnnotatedMethods(RefQueue.class);
|
39
41
|
}
|
40
42
|
|
@@ -117,8 +119,8 @@ public class RefQueueLibrary implements Library {
|
|
117
119
|
super(runtime, klazz);
|
118
120
|
}
|
119
121
|
|
120
|
-
@JRubyMethod(name = "
|
121
|
-
public IRubyObject
|
122
|
+
@JRubyMethod(name = "get")
|
123
|
+
public IRubyObject get() {
|
122
124
|
IRubyObject obj = ref.get();
|
123
125
|
|
124
126
|
if (obj == null) {
|
@@ -129,27 +131,11 @@ public class RefQueueLibrary implements Library {
|
|
129
131
|
return obj;
|
130
132
|
}
|
131
133
|
|
132
|
-
@JRubyMethod(name = "__setobj__")
|
133
|
-
public IRubyObject setobj(IRubyObject obj) {
|
134
|
-
return getRuntime().getNil();
|
135
|
-
}
|
136
|
-
|
137
|
-
// This is only here to replace the "new" in JRuby's weakref, which
|
138
|
-
// doesn't really need to be there.
|
139
|
-
@JRubyMethod(name = "new", required = 1, optional = 1, meta = true)
|
140
|
-
public static IRubyObject newInstance(IRubyObject clazz, IRubyObject[] args) {
|
141
|
-
WeakRef weakRef = (WeakRef)((RubyClass)clazz).allocate();
|
142
|
-
|
143
|
-
weakRef.callInit(args, Block.NULL_BLOCK);
|
144
|
-
|
145
|
-
return weakRef;
|
146
|
-
}
|
147
|
-
|
148
134
|
@JRubyMethod(name = "initialize", frame = true, visibility = Visibility.PRIVATE)
|
149
135
|
public IRubyObject initialize(ThreadContext context, IRubyObject obj) {
|
150
136
|
ref = new RubyWeakReference(obj, this);
|
151
137
|
|
152
|
-
return
|
138
|
+
return context.getRuntime().getNil();
|
153
139
|
}
|
154
140
|
|
155
141
|
@JRubyMethod(name = "initialize", frame = true, visibility = Visibility.PRIVATE)
|
@@ -159,7 +145,7 @@ public class RefQueueLibrary implements Library {
|
|
159
145
|
}
|
160
146
|
ref = new RubyWeakReference(obj, this, ((RefQueue)queue).getQueue());
|
161
147
|
|
162
|
-
return
|
148
|
+
return context.getRuntime().getNil();
|
163
149
|
}
|
164
150
|
|
165
151
|
@JRubyMethod(name = "weakref_alive?")
|
data/lib/refqueue.jar
CHANGED
Binary file
|
data/lib/weakling.rb
CHANGED
data/lib/weakling/collections.rb
CHANGED
@@ -1,41 +1,48 @@
|
|
1
|
-
require 'weakref'
|
2
1
|
require 'refqueue'
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
module Weakling
|
4
|
+
class IdHash
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@hash = Hash.new
|
9
|
+
@queue = Weakling::RefQueue.new
|
10
|
+
end
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
class IdWeakRef < Weakling::WeakRef
|
13
|
+
attr_accessor :id
|
14
|
+
def initialize(obj, queue)
|
15
|
+
super(obj, queue)
|
16
|
+
@id = obj.__id__
|
17
|
+
end
|
15
18
|
end
|
16
|
-
end
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
def [](id)
|
21
|
+
_cleanup
|
22
|
+
if wr = @hash[id]
|
23
|
+
return wr.get rescue nil
|
24
|
+
end
|
25
|
+
|
26
|
+
return nil
|
22
27
|
end
|
23
28
|
|
24
|
-
|
25
|
-
|
29
|
+
def add(object)
|
30
|
+
_cleanup
|
31
|
+
wr = IdWeakRef.new(object, @queue)
|
26
32
|
|
27
|
-
|
28
|
-
_cleanup
|
29
|
-
wr = IdWeakRef.new(object, @queue)
|
33
|
+
@hash[wr.id] = wr
|
30
34
|
|
31
|
-
|
35
|
+
return wr.id
|
36
|
+
end
|
32
37
|
|
33
|
-
|
34
|
-
|
38
|
+
def _cleanup
|
39
|
+
while ref = @queue.poll
|
40
|
+
@hash.delete(ref.id)
|
41
|
+
end
|
42
|
+
end
|
35
43
|
|
36
|
-
|
37
|
-
|
38
|
-
@hash.delete(ref.id)
|
44
|
+
def each
|
45
|
+
@hash.each {|id, wr| obj = wr.get rescue nil; yield [id,obj] if obj}
|
39
46
|
end
|
40
47
|
end
|
41
|
-
end
|
48
|
+
end
|
data/weakling.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{weakling}
|
5
|
-
s.version = "0.0.
|
5
|
+
s.version = "0.0.3"
|
6
6
|
s.authors = ["Charles Oliver Nutter"]
|
7
7
|
s.date = Time.now.strftime('YYYY-MM-DD')
|
8
8
|
s.description = "A modified WeakRef impl for JRuby plus some weakref-related tools"
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Charles Oliver Nutter
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-03-
|
17
|
+
date: 2010-03-22 17:23:45.218000 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|