call_indirect
Calls a function by its reference. You will need a table that contains a list of function references. These reference
point to functions. You can select the function you want to call using an index to the function reference in the table.
You will need to push any parameters that function needs onto the stack first, followed by the index of the function reference.
Only then can the call_indirect
instruction be used. Any results the function returns will be placed on the stack
when the instruction is finished.
Syntax
call_indirect $table (type $functionType)
Parameters
$table Optional
The table the function reference is located. If not given then the default table is used.
If used then this points to the (table...)
object.
$functionType
Points to a predefinded function type. This is the prototype of the function, detailing the parameters and
return data types. It is stating what the function looks like.
Stack In
i32 |
The index of the function reference in the table. |
Example
(type $functionType
(func (param i32) (result i32))
)
(table $testTable 4 funcref)
(func $add1 (type $functionType)
local.get 0
i32.const 1
i32.add
)
(elem declare func $add1)
(func $test (result i32)
i32.const 42
i32.const 0
call_indirect $testTable (type $functionType)
)