import ffi from 'liblibrary.so';
import { dlopen, FFIType, CString, CFunction, JSCallback } from 'arkffi';
const LIB = 'libffi_target.so';
// ── dlopen ──
const lib = dlopen(LIB, {
add: { args: [FFIType.double, FFIType.double], returns: FFIType.double },
gcd: { args: [FFIType.int32, FFIType.int32], returns: FFIType.int32 },
factorial: { args: [FFIType.int32], returns: FFIType.int64 },
compute: { args: [FFIType.int32, FFIType.double, FFIType.CString], returns: FFIType.double },
getVersion: { args: [], returns: FFIType.int64 },
});
const sum = lib.symbols.add(2.0, 3.0); // 5.0
const g = lib.symbols.gcd(12, 18); // 6
const sq = lib.symbols.compute(0, 4.0, 'square'); // 16.0
const ptr = lib.symbols.getVersion();
new CString(ptr).toString(); // "1.0.0"
// ── CFunction ──
const addPtr = ffi.getSymbolPtr(handle, 'add');
const addFn = CFunction({
args: [FFIType.double, FFIType.double],
returns: FFIType.double, ptr: addPtr,
});
addFn(7.0, 8.0); // 15.0
addFn.close();
// ── JSCallback ──
const cb = new JSCallback(
(a: number, b: number): number => a + b,
{ args: [FFIType.int32, FFIType.int32], returns: FFIType.int32 },
);
cb.call(100, 200); // 300
cb.close();
// ── 原始桥接 ──
const h = ffi.load(LIB);
ffi.defineFunction(h, 'divide', 'dd', 'd');
ffi.callBySig(h, 'divide', [10.0, 3.0], []);
ffi.close(h);
lib.close();