Javascript 使用Emscripten调用函数指针
使用Emscripten,是否可以从JavaScript调用函数指针(因此是一个数字?Javascript 使用Emscripten调用函数指针,javascript,emscripten,Javascript,Emscripten,使用Emscripten,是否可以从JavaScript调用函数指针(因此是一个数字? 函数的签名是可变的,因此我无法编写帮助程序并完成它 为了举例说明,我有一个如下函数: // Returns a function pointer to call, with the appropiate // arguments, to initialize the demanded feature. void* get_feature_activator(feature_t feat); 您应该按如下方式
函数的签名是可变的,因此我无法编写帮助程序并完成它 为了举例说明,我有一个如下函数:
// Returns a function pointer to call, with the appropiate
// arguments, to initialize the demanded feature.
void* get_feature_activator(feature_t feat);
您应该按如下方式使用它:
// 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);
要对JavaScript执行相同操作,请执行以下操作:
var activator = _get_feature_activator(constants.FEATURE_SPEAKER);
// TODO: Need to call this pointer with one float
我将创建一个C函数:
void call_feature_activator(int activator, float in_val) {
((void(*)(float))activator) (in_val);
}
然后,您可以在JavaScript端调用函数来触发activator调用,它将处理回溯到函数指针并调用它。您可以使用
Runtime.dynCall
从JS调用C函数指针。例如,见
参数是
(类型签名、指针、参数数组)
。例如,类型“vi”表示返回void,接收一个整数参数。这与生成的代码中的函数_TABLE _vi相对应。正如我所说的,我不能使用助手,因为签名是不固定的。在这种情况下,函数接受一个浮点,但对于另一个功能,它可以接受两个整数,等等。有没有一种方法可以调用函数指针,而不必为每个可能的签名创建一个C函数?哇!我以前见过这个函数,我知道你可以用它们来调用指针,但是。。。我以为它们是罗马数字xD