Skip to content

Instantly share code, notes, and snippets.

@codefromthecrypt
Last active November 27, 2022 06:25
Show Gist options
  • Save codefromthecrypt/2810a6684f3b2c0377b88a637f7a93b1 to your computer and use it in GitHub Desktop.
Save codefromthecrypt/2810a6684f3b2c0377b88a637f7a93b1 to your computer and use it in GitHub Desktop.
emscripten code which uses a wasm function table
#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