QuickJS的LibFFI模块,使用JavaScript调用任意C动态库
https://github.com/shajunxing/quickjs-ffi
例如以下三个C函数,包含在test-lib.so动态库中。
void test1();
double test2(float, double, const char *);
void test3(struct {long; double; struct {int; float} });
在QuickJS下使用该模块的调用范例如下:
import { CFunction } from './quickjs-ffi.js'
let test1 = new CFunction('test-lib.so', 'test1', 'void').invoke;
console.log(test1());
let test2 = new CFunction('test-lib.so', 'test2', 'double', 'float', 'double', 'string').invoke;
console.log(test2(3.141592654, 2.718281829, 'How do you do?'));
let test3 = new CFunction('test-lib.so', 'test3', 'void', ['long', 'double', ['int', 'float']]).invoke;
console.log(test3([123456789, 3.141592654, 54321, 2.718281829]));