3

Is it possible to pass function pointers from C++ (compiled into Javascript using Emscripten) to directly-written JS? I've found ways of creating function pointers of Javascript functions to pass to C++, but not a way of exposing a function pointer, given a value at runtime in C++ code, to Javascript.

Code-wide, what I'm after is to be able to complete the code snippet below in order to call the function passed as cFunctionPointer where I'm doing the console.log

void passToJs(void (*cFunctionPointer)()) {
  EM_ASM_ARGS({
    // Prints out an integer. Would like to be able to
    // call the function it represents.
    console.log($0);
  }, cFunctionPointer);
}

1 Answer 1

1

Found the answer at https://stackoverflow.com/a/25584986/1319998. You can use the Runtime.dynCall function:

void passToJs(void (*cFunctionPointer)()) {
  EM_ASM_ARGS({
    Module.Runtime.dynCall('v', $0, []);
  }, cFunctionPointer);
}

The 'v' is the signature of a void function that doesn't take any arguments.

Apparently it supports other signatures, such as 'vii', which is a void function that takes 2 integer arguments. The integer arguments would then have to passed in the array which is the 3rd argument of Runtime.dynCall.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.