Skip to content

Instantly share code, notes, and snippets.

@mirkosertic
Created November 23, 2017 11:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mirkosertic/7c1a0b8a98db90982b995219c26750fa to your computer and use it in GitHub Desktop.
Save mirkosertic/7c1a0b8a98db90982b995219c26750fa to your computer and use it in GitHub Desktop.
WebAssembly call_indirect example
(module
(type $_type (func (param f32) (param f32) (result f32))) ;; we need a signature for the indirect call
(memory 256 256)
(table 128 anyfunc) ;; Table with function pointers
(elem (i32.const 0) $div) ;; function pointer with index 0 points to $div function
(func $div (param $p1 f32) (param $p2 f32) (result f32)
(f32.div
(get_local $p1)
(get_local $p2)
)
)
(func $div2 (param $p1 f32) (param $p2 f32) (result f32)
(call $div
(get_local $p1)
(get_local $p2)
)
)
(func $div2 (param $p1 f32) (param $p2 f32) (result f32)
(call_indirect $_type ;; we need the signature of the function for validation
(get_local $p1)
(get_local $p2)
(i32.const 0) ;; this is the index into the table!!
)
)
(global $STACKTOP (mut i32) (i32.const 0))
(export "testdiv" (func $div))
(export "testdivB" (func $div2))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment