wref 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +2 -0
- data/VERSION +1 -1
- data/lib/wref.rb +27 -20
- data/spec/wref_spec.rb +23 -15
- data/wref.gemspec +2 -2
- metadata +3 -3
data/Gemfile.lock
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.1
|
data/lib/wref.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
require "java" if RUBY_ENGINE == "jruby"
|
2
|
+
|
1
3
|
#A simple weak-reference framework with mapping. Only handles the referencing of objects.
|
2
4
|
#===Examples
|
3
5
|
# user_obj = ob.get(:User, 1)
|
4
|
-
# weak_ref = Wref(user_obj)
|
6
|
+
# weak_ref = Wref.new(user_obj)
|
5
7
|
# user_obj = nil
|
6
8
|
# sleep 0.5
|
7
9
|
# GC.start
|
@@ -21,20 +23,27 @@ class Wref
|
|
21
23
|
|
22
24
|
#Initializes various variables.
|
23
25
|
def initialize(obj)
|
24
|
-
@id = obj.__id__
|
25
|
-
|
26
26
|
if RUBY_ENGINE == "jruby"
|
27
|
-
require "java"
|
28
27
|
@weakref = java.lang.ref.WeakReference.new(obj)
|
29
28
|
else
|
29
|
+
@id = obj.__id__
|
30
30
|
@class_name = obj.class.name.to_sym
|
31
|
+
ObjectSpace.define_finalizer(obj, self.method(:destroy))
|
31
32
|
|
32
|
-
if obj.respond_to?(
|
33
|
+
if obj.respond_to?(:__object_unique_id__)
|
33
34
|
@unique_id = obj.__object_unique_id__
|
34
35
|
end
|
35
36
|
end
|
36
37
|
end
|
37
38
|
|
39
|
+
#Destroyes most variables on the object, releasing memory and returning 'Wref::Recycled' all the time.
|
40
|
+
def destroy
|
41
|
+
@id = nil
|
42
|
+
@class_name = nil
|
43
|
+
@unique_id = nil
|
44
|
+
@weakref = nil
|
45
|
+
end
|
46
|
+
|
38
47
|
#Returns the object that this weak reference holds or raises Wref::Recycled.
|
39
48
|
# begin
|
40
49
|
# obj = wref.get
|
@@ -44,9 +53,8 @@ class Wref
|
|
44
53
|
# end
|
45
54
|
def get
|
46
55
|
begin
|
47
|
-
raise Wref::Recycled if !@class_name or !@id
|
48
|
-
|
49
56
|
if RUBY_ENGINE == "jruby"
|
57
|
+
raise Wref::Recycled if !@weakref
|
50
58
|
obj = @weakref.get
|
51
59
|
|
52
60
|
if obj == nil
|
@@ -55,23 +63,22 @@ class Wref
|
|
55
63
|
return obj
|
56
64
|
end
|
57
65
|
else
|
66
|
+
raise Wref::Recycled if !@class_name or !@id
|
58
67
|
obj = ObjectSpace._id2ref(@id)
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
if !obj_class_name or @class_name != obj_class_name.to_sym or @id != obj.__id__
|
65
|
-
raise Wref::Recycled
|
66
|
-
end
|
67
|
-
|
68
|
-
if @unique_id
|
69
|
-
if !obj.respond_to?("__object_unique_id__") or obj.__object_unique_id__ != @unique_id
|
68
|
+
|
69
|
+
#Some times this class-name will be nil for some reason - knj
|
70
|
+
obj_class_name = obj.class.name
|
71
|
+
|
72
|
+
if !obj_class_name or @class_name != obj_class_name.to_sym or @id != obj.__id__
|
70
73
|
raise Wref::Recycled
|
71
74
|
end
|
75
|
+
|
76
|
+
if @unique_id
|
77
|
+
raise Wref::Recycled if !obj.respond_to?(:__object_unique_id__) or obj.__object_unique_id__ != @unique_id
|
78
|
+
end
|
79
|
+
|
80
|
+
return obj
|
72
81
|
end
|
73
|
-
|
74
|
-
return obj
|
75
82
|
rescue RangeError, TypeError
|
76
83
|
raise Wref::Recycled
|
77
84
|
end
|
data/spec/wref_spec.rb
CHANGED
@@ -2,20 +2,28 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe "Wref" do
|
4
4
|
it "should not fail" do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
5
|
+
#This test does not work under JRuby.
|
6
|
+
if RUBY_ENGINE != "jruby"
|
7
|
+
str = "Test"
|
8
|
+
ref = Wref.new(str)
|
9
|
+
raise "Should have been alive but wasnt." if !ref.alive?
|
10
|
+
str = nil
|
11
|
+
|
12
|
+
#In MRI we have to define another object finalizer, before the last will be finalized.
|
13
|
+
str2 = "Test 2"
|
14
|
+
ref2 = Wref.new(str2)
|
15
|
+
ref2 = nil
|
16
|
+
GC.start
|
17
|
+
raise "Should have been GCed but wasnt." if ref.alive?
|
18
|
+
|
19
|
+
|
20
|
+
str = "Test"
|
21
|
+
map = Wref_map.new
|
22
|
+
map[5] = str
|
23
|
+
raise "Should have been valid but wasnt." if !map.valid?(5)
|
24
|
+
str = nil
|
25
|
+
GC.start
|
26
|
+
raise "Should habe been garbage collected but wasnt." if !map.valid?(5)
|
27
|
+
end
|
20
28
|
end
|
21
29
|
end
|
data/wref.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{wref}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Kasper Johansen"]
|
12
|
-
s.date = %q{2012-05-
|
12
|
+
s.date = %q{2012-05-12}
|
13
13
|
s.description = %q{Lightweight weak reference and weak hash that works in 1.9 and JRuby.}
|
14
14
|
s.email = %q{k@spernj.org}
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: wref
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Kasper Johansen
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-05-
|
13
|
+
date: 2012-05-12 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -93,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
93
93
|
requirements:
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
hash:
|
96
|
+
hash: 3404293009183367169
|
97
97
|
segments:
|
98
98
|
- 0
|
99
99
|
version: "0"
|