Functions

Function

(func $add11 (param $p i32) (result i32) 
// name of func, paramter, return value (returns the last value on the stack)
    get_local $p
    i32.const 11
    i32.add)

// call function with param of 4
i32.const 4
call $add11

Function Export Example

(func (export "add11") (param $p i32) (result i32)
    get_local $p
    i32.const 11
    i32.add)
WebAssembly.instantiate(wasmModule.toBinary({}).buffer, importObject).then(function(res) {
    console.log(res.instance.exports.add11(2));
  });

Function Import Example

function consoleLogString(offset, length) {
    var bytes = new Uint8Array(memory.buffer, offset, length);
    var string = new TextDecoder('utf8').decode(bytes);
    console.log(string);
}


var importObject = { console: { log: consoleLogString } };

WebAssembly.instantiate(wasmModule.toBinary({}).buffer, importObject).then(function(res) {
    
  });
// import
(import "console" "log" (func $log (param i32 i32)))

// call
i32.const 0 // param 1
i32.const 1 // param 2
call $log

Last updated