Last active
November 27, 2022 06:25
-
-
Save codefromthecrypt/2810a6684f3b2c0377b88a637f7a93b1 to your computer and use it in GitHub Desktop.
emscripten code which uses a wasm function table
This file contains hidden or 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
#include <stdlib.h> | |
/* define a dynamic function */ | |
int (*dynamicInt)(); | |
/* import a function from the host */ | |
extern int hostInt(); | |
/* assign it to the next table offset (0) */ | |
int (*hostIntPtr)() = &hostInt; | |
/* define a function in wasm */ | |
int guestInt() { | |
return 42; | |
} | |
/* assign it to the next table offset (1) */ | |
int (*guestIntPtr)() = &guestInt; | |
/* decide which function to use */ | |
int main() { | |
dynamicInt = hostIntPtr; | |
int num = dynamicInt(); // call indirect table offset 0 | |
dynamicInt = guestIntPtr; | |
num += dynamicInt(); // call indirect table offset 1 | |
return num; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment