Newer
Older
art_of_webassembly / ch03 / function_table / table_export.wat
@clewis clewis on 20 Mar 2024 852 bytes initial commit
(module

    ;; js increment function
    (import "js" "increment" (func $js_increment  (result i32)))

    ;; js decrement function
    (import "js" "decrement" (func $js_decrement (result i32)))

    ;; exported table with 4 functions
    (table $tbl (export "tbl") 4 anyfunc)

    (global $i (mut i32) (i32.const 0))

    (func $increment (export "increment") (result i32)
        (global.set $i (i32.add (global.get $i) (i32.const 1)))
        global.get $i               ;; $i = $i + 1
    )

    (func $decrement (export "decrement") (result i32)
        (global.set $i (i32.sub (global.get $i) (i32.const 1)))
        global.get $i               ;; $i = $i - 1
    )

    ;; populate the table
    ;; (i32.const 0) is the index of the first element.
    (elem (i32.const 0) $js_increment $js_decrement $increment $decrement)
    
) ;; end module