跳转到主要内容
JSCallback 包装 ArkTS 函数,使其可用于期望函数指针的 C 代码。

示例

import { JSCallback, FFIType } from 'arkffi';

const cb = new JSCallback(
  (a: number, b: number): number => a + b,
  { args: [FFIType.int32, FFIType.int32], returns: FFIType.int32 },
);

cb.call(2, 3); // → 5
cb.ptr;        // → 槽位句柄
cb.close();

构造函数

constructor(
  callback: (...args: any[]) => any,
  def: {
    args: string[];
    returns: string;
    threadsafe?: boolean;
  },
);
参数类型默认值说明
callbackFunction要包装的 ArkTS 函数
def.argsstring[]C 函数参数类型编码
def.returnsstringC 函数返回类型编码
def.threadsafebooleanfalse是否可从任意线程调用

属性

ptr

get ptr(): number;
返回可用于原生 C 代码的函数指针地址。
  • 非线程安全:返回槽位句柄(小整数)。
  • 线程安全:返回真实的可执行蹦床函数地址,可传给 C 函数作为回调参数。

getHandle()

getHandle(): number;
返回内部槽位句柄,可用于 ffi.invokeCallback()ffi.callCallbackThreadSafe()

threadsafe

readonly threadsafe: boolean;
回调是否以线程安全模式创建。

方法

call()

call(...args: any[]): any;
调用 ArkTS 回调。
cb.call(7);  // → 70

close()

close(): void;
释放回调资源。
cb.close();
cb.call(1); // 抛出异常

线程安全回调

const cb = new JSCallback(fn, {
  args: [FFIType.int32],
  returns: FFIType.double,
  threadsafe: true,
});
cb.threadsafe; // → true

闭包捕获

let factor = 10;
const cb = new JSCallback(
  (x: number): number => x * factor,
  { args: [FFIType.int32], returns: FFIType.int32 },
);
cb.call(5); // → 50
factor = 20;
cb.call(5); // → 100
cb.close();