从JavaScript调用C ++函数指针 [英] Call C++ function pointer from Javascript
问题描述
是否可以将函数指针从C ++(使用Emscripten编译为Javascript)传递到直接编写的JS?我已经找到了创建函数指针的JavaScript函数传递给C ++的方法,但不是一个方法暴露一个函数指针,在C ++代码的运行时,到Javascript。
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.
代码范围,我接下来是能够完成下面的代码片段,以调用作为 cFunctionPointer
传递的函数 console.log
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);
}
推荐答案
在 http://stackoverflow.com/a/25584986/1319998 。您可以使用 Runtime.dynCall
函数:
void passToJs(void (*cFunctionPointer)()) {
EM_ASM_ARGS({
Module.Runtime.dynCall('v', $0, []);
}, cFunctionPointer);
}
'v'
显然,它支持其他签名,例如'vii'
,这是一个void函数,它接受2个整数参数。整数参数然后必须在 Runtime.dynCall
的第三个参数的数组中传递。
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
.
这篇关于从JavaScript调用C ++函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!