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.



600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
# File 'lib/wardite.rb', line 600

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
end

Instance Attribute Details

#funcsObject

: Array



590
591
592
# File 'lib/wardite.rb', line 590

def funcs
  @funcs
end

#globalsObject

: Array



596
597
598
# File 'lib/wardite.rb', line 596

def globals
  @globals
end

#memoriesObject

FIXME: attr_accessor :modules



594
595
596
# File 'lib/wardite.rb', line 594

def memories
  @memories
end

Instance Method Details

#[](idx) ⇒ Object



675
676
677
# File 'lib/wardite.rb', line 675

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