用Emscripten调用函数指针 [英] Calling a function pointer with Emscripten

查看:62
本文介绍了用Emscripten调用函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Emscripten,是否可以从JavaScript调用函数指针(因此为数字)?
该函数的签名是可变的,因此我无法编写帮助程序.

With Emscripten, is it possible to call a function pointer (thus, a number) from JavaScript?
The signature of the function is variable, so I can't write a helper and be done.

为了说明一个例子,我有一个像这样的函数:

To illustrate an example, I've got 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're supposed to 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);

要对JavaScript执行相同操作:

To do the same with JavaScript:

var activator = _get_feature_activator(constants.FEATURE_SPEAKER);
// TODO: Need to call this pointer with one float

推荐答案

您可以使用 Runtime.dynCall 从JS调用C函数指针.例如参见

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

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

参数是(类型签名,指针,参数数组).例如,类型"vi"表示返回void,接收一个整数参数.这对应于您可以在生成的代码中看到的FUNCTION_TABLE_vi.

The arguments are (type signature, pointer, array of arguments). For example, the type 'vi' means return void, receive one integer parameter. This corresponds to FUNCTION_TABLE_vi which you can see in the generated code.

这篇关于用Emscripten调用函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆