Class: Evoasm::Kernel::IO

Inherits:
FFI::AutoPointer
  • Object
show all
Includes:
Enumerable
Defined in:
lib/evoasm/kernel/io.rb

Overview

Represents one or multiple input/output tuples.

Direct Known Subclasses

Input, Output

Constant Summary

MAX_ARITY =

Maximum arity for tuples

8

Instance Method Summary collapse

Constructor Details

#initialize(tuples, types = nil) ⇒ IO

Returns a new instance of IO

Parameters:

  • tuples (Array)

    array of input or output tuples



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/evoasm/kernel/io.rb', line 19

def initialize(tuples, types = nil)
  if tuples.is_a?(FFI::Pointer)
    super(tuples)
  else
    tuples = canonicalize_tuples tuples, types
    types = check_types tuples, types
    arity = types.size

    if arity > MAX_ARITY
      raise ArgumentError, "maximum arity exceeded (#{arity} > #{MAX_ARITY})"
    end

    kernel_io_val_type_enum_type = Libevoasm.enum_type :kernel_io_val_type
    types_array = FFI::MemoryPointer.new :int, arity
    ffi_types = types.map {|t| kernel_io_val_type_enum_type[t]}
    types_array.write_array_of :int, ffi_types

    ptr = Libevoasm.kernel_io_alloc
    Libevoasm.kernel_io_init ptr, arity, tuples.size, types_array

    super(ptr)

    load! tuples, arity, types
  end
end

Instance Method Details

#[](tuple_index) ⇒ Array

Returns the tuple at tuple_index

Parameters:

  • tuple_index (Integer)

Returns:

  • (Array)

    returns the tuple at tuple_index



77
78
79
80
81
82
83
84
85
86
# File 'lib/evoasm/kernel/io.rb', line 77

def [](tuple_index)
  if arity > 1
    Array.new(arity) do |value_index|
      read_write_value(tuple_index, value_index)
    end
  else
    read_write_value(tuple_index, 0)
  end

end

#arityInteger

Returns arity of tuples

Returns:

  • (Integer)

    arity of tuples



46
47
48
# File 'lib/evoasm/kernel/io.rb', line 46

def arity
  Libevoasm.kernel_io_get_arity self
end

#each {|Array| ... } ⇒ Object

Yields:

  • (Array)

    tuple



51
52
53
54
55
56
# File 'lib/evoasm/kernel/io.rb', line 51

def each
  return enum_for(:each) unless block_given?
  size.times do |tuple_index|
    yield self[tuple_index]
  end
end

#lengthInteger

Returns the number of input/output values

Returns:

  • (Integer)

    the number of input/output values



66
67
68
# File 'lib/evoasm/kernel/io.rb', line 66

def length
  Libevoasm.kernel_io_get_n_vals self
end

#sizeInteger

Returns the number of input/output tuples

Returns:

  • (Integer)

    the number of input/output tuples



71
72
73
# File 'lib/evoasm/kernel/io.rb', line 71

def size
  Libevoasm.kernel_io_get_n_tuples self
end

#to_aObject

Converts to array of tuples



59
60
61
62
63
# File 'lib/evoasm/kernel/io.rb', line 59

def to_a
  Array.new(size) do |tuple_index|
    self[tuple_index]
  end
end