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.



929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
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
# File 'lib/wardite.rb', line 929

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

  import_section = inst.import_section
  @funcs = []
  @modules = inst.import_object

  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]
      target_module = self.modules[desc.module_name.to_sym]
      if target_module.nil?
        raise ::NameError, "module #{desc.module_name} not found"
      end
      target_name = desc.name.to_sym
      
      ext_function = ExternalFunction.new(target_module, target_name, callsig, retsig)
      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]]



925
926
927
# File 'lib/wardite.rb', line 925

def elements
  @elements
end

#funcsObject

: Array



915
916
917
# File 'lib/wardite.rb', line 915

def funcs
  @funcs
end

#globalsObject

: Array



921
922
923
# File 'lib/wardite.rb', line 921

def globals
  @globals
end

#memoriesObject

: Array



919
920
921
# File 'lib/wardite.rb', line 919

def memories
  @memories
end

#modulesObject

: Hash[Symbol, wasmModule]



917
918
919
# File 'lib/wardite.rb', line 917

def modules
  @modules
end

#tablesObject

: Array



923
924
925
# File 'lib/wardite.rb', line 923

def tables
  @tables
end

Instance Method Details

#[](idx) ⇒ Object



1057
1058
1059
# File 'lib/wardite.rb', line 1057

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