Wasm Reference
  • Welcome
  • Nodejs WAT Compiler
  • Types
    • Functions
    • Globals
    • Linear Memory
    • Tables
    • Numbers
    • Text
  • Structures
    • IF
    • LOOP
  • Algorithms
    • Hello World
    • Estimate Pi
  • Maths Helper Algorithms
    • Pseudorandom Number Generator
    • Factorial
    • Dist
    • Map
    • Fract
    • RemF or ModF
    • Round
    • Clamp
    • Mix
    • Pow
    • Approx Sin
    • Approx Cos
    • Approx Tan
    • ToRad and ToDeg
Powered by GitBook
On this page
  • Function
  • Function Export Example
  • Function Import Example

Was this helpful?

  1. Types

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
PreviousNodejs WAT CompilerNextGlobals

Last updated 5 years ago

Was this helpful?