Class: Relaton::Un::TokenGenerator::Heap

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeHeap

Returns a new instance of Heap.



127
128
129
130
131
132
# File 'lib/relaton/un/token_generator.rb', line 127

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



138
139
140
141
142
143
144
145
146
# File 'lib/relaton/un/token_generator.rb', line 138

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



148
149
150
151
152
153
# File 'lib/relaton/un/token_generator.rb', line 148

def drop(idx)
  return if idx < BUILTINS

  @slab[idx] = @free_head
  @free_head = idx
end

#get(idx) ⇒ Object



134
135
136
# File 'lib/relaton/un/token_generator.rb', line 134

def get(idx)
  @slab[idx]
end