weakling 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
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
- RubyClass weakrefClass = (RubyClass)runtime.getClassFromPath("WeakRef");
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, weakrefClass);
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 = "__getobj__")
121
- public IRubyObject getobj() {
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 RuntimeHelpers.invokeSuper(context, this, obj, Block.NULL_BLOCK);
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 RuntimeHelpers.invokeSuper(context, this, obj, Block.NULL_BLOCK);
148
+ return context.getRuntime().getNil();
163
149
  }
164
150
 
165
151
  @JRubyMethod(name = "weakref_alive?")
Binary file
@@ -1,4 +1,2 @@
1
- require 'weakref'
2
1
  require 'refqueue'
3
-
4
2
  require 'weakling/collections.rb'
@@ -1,41 +1,48 @@
1
- require 'weakref'
2
1
  require 'refqueue'
3
2
 
4
- class WeakRef::IdHash
5
- def initialize
6
- @hash = Hash.new
7
- @queue = WeakRef::RefQueue.new
8
- end
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
- class IdWeakRef < WeakRef
11
- attr_accessor :id
12
- def initialize(obj, queue)
13
- super(obj, queue)
14
- @id = obj.__id__
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
- def [](id)
19
- _cleanup
20
- if wr = @hash[id]
21
- return wr.__getobj__ rescue nil
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
- return nil
25
- end
29
+ def add(object)
30
+ _cleanup
31
+ wr = IdWeakRef.new(object, @queue)
26
32
 
27
- def add(object)
28
- _cleanup
29
- wr = IdWeakRef.new(object, @queue)
33
+ @hash[wr.id] = wr
30
34
 
31
- @hash[wr.id] = wr
35
+ return wr.id
36
+ end
32
37
 
33
- return wr.id
34
- end
38
+ def _cleanup
39
+ while ref = @queue.poll
40
+ @hash.delete(ref.id)
41
+ end
42
+ end
35
43
 
36
- def _cleanup
37
- while ref = @queue.poll
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{weakling}
5
- s.version = "0.0.2"
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
- - 2
9
- version: 0.0.2
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-18 23:20:24.678000 -05:00
17
+ date: 2010-03-22 17:23:45.218000 -05:00
18
18
  default_executable:
19
19
  dependencies: []
20
20