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.



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
# File 'lib/wardite.rb', line 972

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
end

Instance Attribute Details

#funcsObject

: Array



964
965
966
# File 'lib/wardite.rb', line 964

def funcs
  @funcs
end

#memoriesObject

FIXME: attr_accessor :modules



968
969
970
# File 'lib/wardite.rb', line 968

def memories
  @memories
end

Instance Method Details

#[](idx) ⇒ Object



1034
1035
1036
# File 'lib/wardite.rb', line 1034

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