Class: Array32

Inherits:
Object show all
Defined in:
lib/rlang/lib/array/array32.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(count) ⇒ Array32

count: number of elements in Array Array elements are native types or pointers to objects Arrays are fixed size for now



21
22
23
24
25
26
27
28
29
# File 'lib/rlang/lib/array/array32.rb', line 21

def initialize(count)
  if count == 0
    @ptr = 0
  else
    # Memory size is count * 4 bytes
    @ptr = Object.allocate(count << 2)
  end
  @count = count
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



13
14
15
# File 'lib/rlang/lib/array/array32.rb', line 13

def count
  @count
end

#ptrObject (readonly)

Returns the value of attribute ptr.



13
14
15
# File 'lib/rlang/lib/array/array32.rb', line 13

def ptr
  @ptr
end

Instance Method Details

#[](idx) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/rlang/lib/array/array32.rb', line 35

def [](idx)
  result :I32
  raise "Index out of bound" if idx >= @count || idx < -@count
  idx = @count + idx if idx <0
  # offset in memory for elt #idx is idx * 4
  Memory.load32(@ptr + (idx << 2))
end

#[]=(idx, value) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/rlang/lib/array/array32.rb', line 43

def []=(idx, value)
  arg value: :I32
  result :I32
  raise "Index out of bound" if idx >= @count || idx < -@count
  idx = @count + idx if idx <0
  # offset in memory for elt #idx is idx * 4
  Memory.store32(@ptr + (idx << 2), value)
  value
end

#empty?Boolean

Returns:

  • (Boolean)


33
# File 'lib/rlang/lib/array/array32.rb', line 33

def empty?; @count == 0; end

#freeObject



53
54
55
56
57
# File 'lib/rlang/lib/array/array32.rb', line 53

def free
  result :none
  Object.free(@ptr)
  Object.free(self)
end

#lengthObject



32
# File 'lib/rlang/lib/array/array32.rb', line 32

def length; @count; end

#sizeObject



31
# File 'lib/rlang/lib/array/array32.rb', line 31

def size;   @count; end