Class: WAG::Module

Inherits:
Object
  • Object
show all
Includes:
Encodable
Defined in:
lib/wag/module.rb

Instance Method Summary collapse

Methods included from Encodable

#to_wasm, #to_wat

Constructor Details

#initializeModule

Returns a new instance of Module.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/wag/module.rb', line 7

def initialize
  @data = []
  @elements = []
  @exports = []
  @functions = []
  @globals = []
  @imports = []
  @memories = []
  @tables = []
  @types = []
end

Instance Method Details

#buildObject



19
20
21
22
# File 'lib/wag/module.rb', line 19

def build(&)
  instance_exec(&)
  self
end

#data(offset, value) ⇒ Object



66
67
68
69
70
# File 'lib/wag/module.rb', line 66

def data(offset, value)
  data = Data.new(offset, value)
  @data << data
  data
end

#elem(table_id, *labels, &block) ⇒ Object



59
60
61
62
63
64
# File 'lib/wag/module.rb', line 59

def elem(table_id, *labels, &block)
  elem = Element.new(table_id, *labels)
  @elements << elem
  elem.instance_exec(&block) if block
  elem
end

#export(name, &block) ⇒ Object



31
32
33
34
35
36
# File 'lib/wag/module.rb', line 31

def export(name, &block)
  export = Export.new(name)
  @exports << export
  export.instance_exec(&block) if block
  export
end

#func(label = nil, &block) ⇒ Object



72
73
74
75
76
77
# File 'lib/wag/module.rb', line 72

def func(label = nil, &block)
  func = Function.new(label)
  @functions << func
  func.instance_exec(&block) if block
  func
end

#global(label, type, &block) ⇒ Object



45
46
47
48
49
50
# File 'lib/wag/module.rb', line 45

def global(label, type, &block)
  global = Global.new(label, type)
  @globals << global
  global.instance_exec(&block) if block
  global
end

#import(module_name, name, &block) ⇒ Object



24
25
26
27
28
29
# File 'lib/wag/module.rb', line 24

def import(module_name, name, &block)
  import = Import.new(module_name, name)
  @imports << import
  import.instance_exec(&block) if block
  import
end

#memory(number, min = nil, max = nil, &block) ⇒ Object



38
39
40
41
42
43
# File 'lib/wag/module.rb', line 38

def memory(number, min = nil, max = nil, &block)
  mem = Memory.new(number, min, max)
  @memories << mem
  mem.instance_exec(&block) if block
  mem
end

#table(elements, type = :funcref, &block) ⇒ Object



79
80
81
82
83
84
# File 'lib/wag/module.rb', line 79

def table(elements, type = :funcref, &block)
  table = Table.new(elements, type)
  @tables << table
  table.instance_exec(&block) if block
  table
end

#to_sexprObject

rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/wag/module.rb', line 86

def to_sexpr # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  mod = [:module]
  mod.concat(@imports.map(&:to_sexpr))
  mod.concat(@exports.map(&:to_sexpr))
  mod.concat(@memories.map(&:to_sexpr))
  mod.concat(@globals.map(&:to_sexpr))
  mod.concat(@types.map(&:to_sexpr))
  mod.concat(@tables.map(&:to_sexpr))
  mod.concat(@elements.map(&:to_sexpr))
  mod.concat(@data.map(&:to_sexpr))
  mod.concat(@functions.map(&:to_sexpr))
  mod
end

#type(label, &block) ⇒ Object



52
53
54
55
56
57
# File 'lib/wag/module.rb', line 52

def type(label, &block)
  type = FunctionType.new(label)
  @types << type
  type.instance_exec(&block) if block
  type
end