Class: Dommy::Js::HandleTable
- Inherits:
-
Object
- Object
- Dommy::Js::HandleTable
- Defined in:
- lib/dommy/js/handle_table.rb
Overview
Maps JS-facing integer handles to live Ruby objects. Engine-agnostic.
Handles are monotonic — never reused — so a handle can never refer to two different objects over the table’s lifetime. That invariant is what makes GC-driven #release race-free: a finalizer releasing handle N can’t clobber a different object that happened to get the same id.
The same Ruby object reuses its handle while still registered (keyed by object_id), so it maps to a single JS proxy (stable identity). A registry entry also keeps the object reachable while JS holds a proxy for it.
Instance Method Summary collapse
- #fetch(handle) ⇒ Object
-
#initialize ⇒ HandleTable
constructor
A new instance of HandleTable.
- #register(obj) ⇒ Object
-
#release(handle) ⇒ Object
Forget a handle (called when its JS proxy is garbage-collected).
- #size ⇒ Object
Constructor Details
#initialize ⇒ HandleTable
Returns a new instance of HandleTable.
16 17 18 19 20 |
# File 'lib/dommy/js/handle_table.rb', line 16 def initialize @by_handle = {} # handle (Integer) -> object @handle_by_oid = {} # object_id -> handle (for identity reuse) @next_handle = 0 end |
Instance Method Details
#fetch(handle) ⇒ Object
33 34 35 |
# File 'lib/dommy/js/handle_table.rb', line 33 def fetch(handle) @by_handle.fetch(handle.to_i) end |
#register(obj) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/dommy/js/handle_table.rb', line 22 def register(obj) oid = obj.object_id existing = @handle_by_oid[oid] return existing if existing && @by_handle[existing].equal?(obj) handle = (@next_handle += 1) @by_handle[handle] = obj @handle_by_oid[oid] = handle handle end |
#release(handle) ⇒ Object
Forget a handle (called when its JS proxy is garbage-collected). Only trims the mapping; the object itself lives on via its other references.
39 40 41 42 43 44 45 |
# File 'lib/dommy/js/handle_table.rb', line 39 def release(handle) obj = @by_handle.delete(handle.to_i) return unless obj oid = obj.object_id @handle_by_oid.delete(oid) if @handle_by_oid[oid] == handle.to_i end |
#size ⇒ Object
47 48 49 |
# File 'lib/dommy/js/handle_table.rb', line 47 def size @by_handle.size end |