Class: Relaton::Un::TokenGenerator::Heap
- Inherits:
-
Object
- Object
- Relaton::Un::TokenGenerator::Heap
- Defined in:
- lib/relaton/un/token_generator.rb
Overview
Manages a slab-allocated heap of Ruby objects indexed by i32, mirroring wasm-bindgen’s JS object heap.
Constant Summary collapse
- BUILTINS =
36
Instance Method Summary collapse
- #alloc(obj) ⇒ Object
- #drop(idx) ⇒ Object
- #get(idx) ⇒ Object
-
#initialize ⇒ Heap
constructor
A new instance of Heap.
Constructor Details
#initialize ⇒ Heap
Returns a new instance of Heap.
73 74 75 76 77 78 |
# File 'lib/relaton/un/token_generator.rb', line 73 def initialize # Indices 0-3 mirror wasm-bindgen's builtin slots: # 0 = undefined, 1 = null, 2 = true, 3 = false @slab = [:undefined, nil, true, false] @free_head = @slab.length end |
Instance Method Details
#alloc(obj) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/relaton/un/token_generator.rb', line 84 def alloc(obj) if @free_head >= @slab.length @slab << (@slab.length + 1) end idx = @free_head @free_head = @slab[idx] @slab[idx] = obj idx end |
#drop(idx) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/relaton/un/token_generator.rb', line 94 def drop(idx) return if idx < BUILTINS @slab[idx] = @free_head @free_head = idx end |
#get(idx) ⇒ Object
80 81 82 |
# File 'lib/relaton/un/token_generator.rb', line 80 def get(idx) @slab[idx] end |