Created
November 23, 2017 11:24
-
-
Save mirkosertic/7c1a0b8a98db90982b995219c26750fa to your computer and use it in GitHub Desktop.
WebAssembly call_indirect example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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