Class: Wardite::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/wardite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inst) ⇒ Store

Returns a new instance of Store.



962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
# File 'lib/wardite.rb', line 962

def initialize(inst)
  type_section = inst.type_section
  func_section = inst.function_section
  code_section = inst.code_section

  import_section = inst.import_section
  @funcs = []

  if type_section && func_section && code_section
    import_section.imports.each do |desc|
      callsig = type_section.defined_types[desc.sig_index]
      retsig = type_section.defined_results[desc.sig_index]
      imported_module = inst.import_object[desc.module_name.to_sym]
      if !imported_module
        raise ::NameError, "module #{desc.module_name} not found"
      end
      imported_proc = imported_module[desc.name.to_sym]
      if !imported_proc
        raise ::NameError, "function #{desc.module_name}.#{desc.name} not found"
      end
      
      ext_function = ExternalFunction.new(callsig, retsig, imported_proc)
      self.funcs << ext_function
    end

    func_section.func_indices.each_with_index do |sigindex, findex|
      callsig = type_section.defined_types[sigindex]
      retsig = type_section.defined_results[sigindex]
      codes = code_section.func_codes[findex]
      wasm_function = WasmFunction.new(callsig, retsig, codes)
      idx = self.funcs.size
      wasm_function.findex = idx
      self.funcs << wasm_function
    end
  end

  @memories = []
  memory_section = inst.memory_section
  if memory_section
    memory_section.limits.each do |(min, max)|
      self.memories << Memory.new(min, max)
    end

    data_section = inst.data_section
    if data_section
      data_section.segments.each do |segment|
        if segment.mode != :active
          next
        end
        memory = self.memories[segment.mem_index]
        if !memory
          raise GenericError, "invalid memory index: #{segment.mem_index}"
        end

        data_start = segment.offset
        data_end = segment.offset + segment.data.size
        if data_end > memory.data.size
          raise GenericError, "data too large for memory"
        end

        memory.data[data_start...data_end] = segment.data
      end
    end
  end

  @globals = []
  global_section = inst.global_section
  if global_section
    global_section.globals.each do |g|
      @globals << Global.new do |g2|
        g2.type = g.type
        g2.mutable = g.mutable
        g2.shared = g.shared
        g2.value = g.value
      end
    end
  end

  @tables = []
  @elements = []
  table_section = inst.table_section
  if table_section
    table_section.table_types.each_with_index do |typ, idx|
      init, max = *table_section.table_limits[idx]
      if !init
        raise LoadError, "empty limits"
      end
      table = Table.new(typ, init, max)
      @tables << table
    end
  end

  elem_section = inst.elem_section
  if elem_section
    elem_section.table_indices.each_with_index do |tidx, idx|
      table = @tables[tidx]
      if !table
        raise LoadError, "invalid table index #{tidx}"
      end
      typ = table.type
      offset = elem_section.table_offsets[idx]
      if !offset
        raise LoadError, "invalid element index #{idx}"
      end
      indices = elem_section.element_indices[idx]
      if !indices
        raise LoadError, "invalid element index #{idx}"
      end
      elms = [typ, offset, indices] #: [Symbol, Integer, Array[Integer]]
      @elements << elms
    end
  end

  @elements.each_with_index do |(typ, offset, indices), idx|
    table = @tables[idx]
    if !table
      raise LoadError, "invalid table index #{idx}"          
    end
    indices.each_with_index do |eidx, tidx|
      case typ
      when :funcref
        table.set(offset + tidx, @funcs[eidx])
      when :externref
        raise NotImplementedError, "no support :externref"
      end
    end
  end
end

Instance Attribute Details

#elementsObject

: Array[[Symbol, Integer, Array]]



958
959
960
# File 'lib/wardite.rb', line 958

def elements
  @elements
end

#funcsObject

: Array



948
949
950
# File 'lib/wardite.rb', line 948

def funcs
  @funcs
end

#globalsObject

: Array



954
955
956
# File 'lib/wardite.rb', line 954

def globals
  @globals
end

#memoriesObject

FIXME: attr_accessor :modules



952
953
954
# File 'lib/wardite.rb', line 952

def memories
  @memories
end

#tablesObject

: Array



956
957
958
# File 'lib/wardite.rb', line 956

def tables
  @tables
end

Instance Method Details

#[](idx) ⇒ Object



1092
1093
1094
# File 'lib/wardite.rb', line 1092

def [](idx)
  @funcs[idx]
end