Class: V8::Weak::WeakValueMap

Inherits:
Object
  • Object
show all
Defined in:
lib/v8/weak.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWeakValueMap

Returns a new instance of WeakValueMap.



31
32
33
# File 'lib/v8/weak.rb', line 31

def initialize
  @values = {}
end

Class Method Details

.ensure_cleanup(values, key, ref) ⇒ Object



48
49
50
51
52
# File 'lib/v8/weak.rb', line 48

def self.ensure_cleanup(values,key,ref)
  proc {
    values.delete(key) if values[key] == ref
  }
end

Instance Method Details

#[](key) ⇒ Object



35
36
37
38
39
# File 'lib/v8/weak.rb', line 35

def [](key)
  if ref = @values[key]
    ref.object
  end
end

#[]=(key, value) ⇒ Object



41
42
43
44
45
46
# File 'lib/v8/weak.rb', line 41

def []=(key, value)
  ref = V8::Weak::Ref.new(value)
  ObjectSpace.define_finalizer(value, self.class.ensure_cleanup(@values, key, ref))

  @values[key] = ref
end