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.



864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
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
# File 'lib/wardite.rb', line 864

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)
      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|
        memory = self.memories[segment.flags]
        if !memory
          raise GenericError, "invalid memory index: #{segment.flags}"
        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]]



860
861
862
# File 'lib/wardite.rb', line 860

def elements
  @elements
end

#funcsObject

: Array



850
851
852
# File 'lib/wardite.rb', line 850

def funcs
  @funcs
end

#globalsObject

: Array



856
857
858
# File 'lib/wardite.rb', line 856

def globals
  @globals
end

#memoriesObject

FIXME: attr_accessor :modules



854
855
856
# File 'lib/wardite.rb', line 854

def memories
  @memories
end

#tablesObject

: Array



858
859
860
# File 'lib/wardite.rb', line 858

def tables
  @tables
end

Instance Method Details

#[](idx) ⇒ Object



989
990
991
# File 'lib/wardite.rb', line 989

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