Class: Wardite::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/wardite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(import_object, &blk) ⇒ Instance

Returns a new instance of Instance.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/wardite.rb', line 45

def initialize(import_object, &blk)
  blk.call(self)
  @import_object = import_object

  @store = Store.new(self)
  @exports = Exports.new(self.export_section, store)
  @runtime = Runtime.new(self)

  @types = []
  type_section = self.type_section
  if type_section
    type_section.defined_types.each_with_index do |calltype, idx|
      rettype = type_section.defined_results[idx]
      @types << Type.new(calltype, rettype)
    end
  end

  check_data_count
end

Instance Attribute Details

#exportsObject

: Exports



39
40
41
# File 'lib/wardite.rb', line 39

def exports
  @exports
end

#import_objectObject (readonly)

: Hash[Symbol, Hash[Symbol, wasmCallable]]



41
42
43
# File 'lib/wardite.rb', line 41

def import_object
  @import_object
end

#runtimeObject

: Runtime



33
34
35
# File 'lib/wardite.rb', line 33

def runtime
  @runtime
end

#sectionsObject

: Array



31
32
33
# File 'lib/wardite.rb', line 31

def sections
  @sections
end

#storeObject

: Store



37
38
39
# File 'lib/wardite.rb', line 37

def store
  @store
end

#typesObject

: Array



35
36
37
# File 'lib/wardite.rb', line 35

def types
  @types
end

#versionObject

: Integer



29
30
31
# File 'lib/wardite.rb', line 29

def version
  @version
end

Instance Method Details

#check_data_countObject



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/wardite.rb', line 66

def check_data_count
  data_count = self.data_count_section&.count
  if data_count
    actual_count = self.data_section&.segments&.size
    if !actual_count
      raise LoadError, "invalid data segment count"
    end
    if (data_count != actual_count)
      raise LoadError, "invalid data segment count"
    end
  end
end

#code_sectionObject



198
199
200
201
202
203
204
205
206
207
# File 'lib/wardite.rb', line 198

def code_section
  sec = @sections.find{|s| s.code == Const::SectionCode }
  if !sec
    return nil
  end
  if !sec.is_a?(CodeSection)
    raise(GenericError, "instance doesn't have required section")
  end
  sec
end

#data_count_sectionObject



150
151
152
153
154
155
156
157
158
159
# File 'lib/wardite.rb', line 150

def data_count_section
  sec = @sections.find{|s| s.code == Const::SectionDataCount }
  if !sec
    return nil
  end
  if !sec.is_a?(DataCountSection)
    raise(GenericError, "[BUG] found invalid data section")
  end
  sec
end

#data_sectionObject



138
139
140
141
142
143
144
145
146
147
# File 'lib/wardite.rb', line 138

def data_section
  sec = @sections.find{|s| s.code == Const::SectionData }
  if !sec
    return nil
  end
  if !sec.is_a?(DataSection)
    raise(GenericError, "[BUG] found invalid data section")
  end
  sec
end

#elem_sectionObject



186
187
188
189
190
191
192
193
194
195
# File 'lib/wardite.rb', line 186

def elem_section
  sec = @sections.find{|s| s.code == Const::SectionElement }
  if !sec
    return nil
  end
  if !sec.is_a?(ElemSection)
    raise(GenericError, "instance doesn't have required section")
  end
  sec
end

#export_sectionObject



210
211
212
213
214
215
216
217
218
219
# File 'lib/wardite.rb', line 210

def export_section
  sec = @sections.find{|s| s.code == Const::SectionExport }
  if !sec
    return ExportSection.new
  end
  if !sec.is_a?(ExportSection)
    raise(GenericError, "instance doesn't have required section")
  end
  sec
end

#function_sectionObject



162
163
164
165
166
167
168
169
170
171
# File 'lib/wardite.rb', line 162

def function_section
  sec = @sections.find{|s| s.code == Const::SectionFunction }
  if !sec
    return nil
  end
  if !sec.is_a?(FunctionSection)
    raise(GenericError, "instance doesn't have required section")
  end
  sec
end

#global_sectionObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/wardite.rb', line 126

def global_section
  sec = @sections.find{|s| s.code == Const::SectionGlobal }
  if !sec
    return nil
  end
  if !sec.is_a?(GlobalSection)
    raise(GenericError, "instance doesn't have required section")
  end
  sec
end

#import_sectionObject



80
81
82
83
84
85
86
87
# File 'lib/wardite.rb', line 80

def import_section
  sec = @sections.find{|s| s.code == Const::SectionImport }
  if !sec.is_a?(ImportSection)
    # returns dummy empty section
    return ImportSection.new
  end
  sec
end

#memory_sectionObject



102
103
104
105
106
107
108
109
110
111
# File 'lib/wardite.rb', line 102

def memory_section
  sec = @sections.find{|s| s.code == Const::SectionMemory }
  if !sec
    return nil
  end
  if !sec.is_a?(MemorySection)
    raise(GenericError, "[BUG] found invalid memory section")
  end
  sec
end

#start_sectionObject



114
115
116
117
118
119
120
121
122
123
# File 'lib/wardite.rb', line 114

def start_section
  sec = @sections.find{|s| s.code == Const::SectionStart }
  if !sec
    return nil
  end
  if !sec.is_a?(StartSection)
    raise(GenericError, "[BUG] found invalid start section")
  end
  sec
end

#table_sectionObject



174
175
176
177
178
179
180
181
182
183
# File 'lib/wardite.rb', line 174

def table_section
  sec = @sections.find{|s| s.code == Const::SectionTable }
  if !sec
    return nil
  end
  if !sec.is_a?(TableSection)
    raise(GenericError, "instance doesn't have required section")
  end
  sec
end

#type_sectionObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/wardite.rb', line 90

def type_section
  sec = @sections.find{|s| s.code == Wardite::Const::SectionType }
  if !sec
    return nil
  end
  if !sec.is_a?(TypeSection)
    raise(GenericError, "instance doesn't have required section")
  end
  sec
end