Class: WAG::Function

Inherits:
Object
  • Object
show all
Includes:
Encodable, Instructable
Defined in:
lib/wag/function.rb

Direct Known Subclasses

FunctionType

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Encodable

#to_wasm, #to_wat

Methods included from Instructable

#f32, #f64, #i32, #i64, #local, #memory

Constructor Details

#initialize(label = nil) ⇒ Function

Returns a new instance of Function.



10
11
12
13
# File 'lib/wag/function.rb', line 10

def initialize(label = nil)
  @label = WAG::Label.from(label) if label
  @params = []
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



8
9
10
# File 'lib/wag/function.rb', line 8

def label
  @label
end

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'lib/wag/function.rb', line 8

def params
  @params
end

Instance Method Details

#param(label = nil, type) ⇒ Object



15
16
17
# File 'lib/wag/function.rb', line 15

def param(label = nil, type)
  @params.push(WAG::Param.new(type, label))
end

#result(*types) ⇒ Object



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

def result(*types)
  return @result if types.empty?

  @result = WAG::Result.new(*types)
end

#to_sexprObject



25
26
27
28
29
30
31
# File 'lib/wag/function.rb', line 25

def to_sexpr
  [:func].tap do |func|
    func.push(@label.to_sexpr) if @label
    func.concat(@params.map(&:to_sexpr)) unless @params.empty?
    func.push(@result.to_sexpr) if @result
  end
end