Calling a function pointer using Emscripten

With Emscripten, is it possible to call a function pointer (thus a number) from JavaScript?
The signature of a function is a variable, so I cannot write an helper and do.

To illustrate the example, I have a function like this:

// Returns a function pointer to call, with the appropiate
// arguments, to initialize the demanded feature.
void* get_feature_activator(feature_t feat);

You should use it as follows:

// Initialize the speaker
void* activator = get_feature_activator(FEATURE_SPEAKER);
// We know this function needs one float, so we cast and call it
((void(*)(float))activator) (3.0);

To do the same with JavaScript:

var activator = _get_feature_activator(constants.FEATURE_SPEAKER);
// TODO: Need to call this pointer with one float
+4
source share
2 answers

You can call the function pointer C from JS using Runtime.dynCall. See for example

https://github.com/kripken/emscripten/blob/ee17f05c0a45cad728ce0f215f2d2ffcdd75434b/src/library_browser.js#L715

(type signature, pointer, array of arguments). , "vi" return void, . FUNCTION_TABLE_vi, .

+6

C:

void call_feature_activator(int activator, float in_val) {
  ((void(*)(float))activator) (in_val);
}

JavaScript, , .

0

All Articles