Class: Wardite::WasmFunction

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

Overview

TODO: common interface btw. WasmFunction and ExternalFunction?

may be _WasmCallable?

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ValueHelper

#F32, #F64, #I32, #I64

Constructor Details

#initialize(callsig, retsig, code_body) ⇒ WasmFunction

Returns a new instance of WasmFunction.



1229
1230
1231
1232
1233
1234
1235
1236
# File 'lib/wardite.rb', line 1229

def initialize(callsig, retsig, code_body)
  @callsig = callsig
  @retsig = retsig

  @code_body = code_body
  @findex = 0 # for debug
  @default_locals = construct_default_locals
end

Instance Attribute Details

#callsigObject

: Array



1215
1216
1217
# File 'lib/wardite.rb', line 1215

def callsig
  @callsig
end

#code_bodyObject

: CodeSection::CodeBody



1219
1220
1221
# File 'lib/wardite.rb', line 1219

def code_body
  @code_body
end

#default_localsObject

: Array



1223
1224
1225
# File 'lib/wardite.rb', line 1223

def default_locals
  @default_locals
end

#findexObject

: Integer



1221
1222
1223
# File 'lib/wardite.rb', line 1221

def findex
  @findex
end

#retsigObject

: Array



1217
1218
1219
# File 'lib/wardite.rb', line 1217

def retsig
  @retsig
end

Instance Method Details

#bodyObject



1239
1240
1241
# File 'lib/wardite.rb', line 1239

def body
  code_body.body
end

#clone(override_type: nil) ⇒ Object



1279
1280
1281
1282
1283
1284
1285
1286
# File 'lib/wardite.rb', line 1279

def clone(override_type: nil)
  if override_type
    # code_body is assumed to be frozen, so we can copy its ref
    WasmFunction.new(override_type.callsig, override_type.retsig, code_body)
  else
    WasmFunction.new(callsig, retsig, code_body)
  end
end

#construct_default_localsObject



1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
# File 'lib/wardite.rb', line 1254

def construct_default_locals
  locals = [] #: Array[wasmValue]
  locals_count.each_with_index do |count, i|
    typ = locals_type[i]
    count.times do
      case typ
      when :i32, :u32
        locals.push I32(0)
      when :i64, :u64
        locals.push I64(0)
      when :f32
        locals.push F32(0.0)
      when :f64
        locals.push F64(0.0)
      else
        $stderr.puts "warning: unknown type #{typ.inspect}. default to I32"
        locals.push I32(0)
      end
    end
  end
  locals
end

#locals_countObject



1249
1250
1251
# File 'lib/wardite.rb', line 1249

def locals_count
  code_body.locals_count
end

#locals_typeObject



1244
1245
1246
# File 'lib/wardite.rb', line 1244

def locals_type
  code_body.locals_type
end