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.



1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
# File 'lib/wardite.rb', line 1145

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



1137
1138
1139
# File 'lib/wardite.rb', line 1137

def funcs
  @funcs
end

#memoriesObject

FIXME: attr_accessor :modules



1141
1142
1143
# File 'lib/wardite.rb', line 1141

def memories
  @memories
end

Instance Method Details

#[](idx) ⇒ Object



1207
1208
1209
# File 'lib/wardite.rb', line 1207

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