library.getFunction(name, signature)
name<string>signature<Object>- 返回:<Function>
解析一个符号并返回一个可调用的 JavaScript 封装器。
🌐 Resolves a symbol and returns a callable JavaScript wrapper.
返回的函数有一个 .pointer 属性,包含作为 bigint 的原生函数地址。
🌐 The returned function has a .pointer property containing the native function
address as a bigint.
如果相同的符号已经被解析过,再用不同的签名请求它会抛出异常。用相同的签名再次请求它会返回同样的函数,从 library.functions 读取它也是一样。
🌐 If the same symbol has already been resolved, requesting it again with a
different signature throws. Requesting it again with the same signature returns
the same function, as does reading it from library.functions.
const { DynamicLibrary, suffix } = require('node:ffi');
const lib = new DynamicLibrary(`./mylib.${suffix}`);
const add = lib.getFunction('add_i32', {
arguments: ['int32', 'int32'],
return: 'int32',
});
console.log(add(20, 22));
console.log(add.pointer);