Documentation IndexFetch the complete documentation index at: /llms.txtUse this file to discover all available pages before exploring further.
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
将 ArkTS 函数包装为 C 回调
JSCallback
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; }, );
callback
Function
def.args
string[]
def.returns
string
def.threadsafe
boolean
false
ptr
get ptr(): number;
getHandle()
getHandle(): number;
ffi.invokeCallback()
ffi.callCallbackThreadSafe()
threadsafe
readonly threadsafe: boolean;
call()
call(...args: any[]): any;
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();
此页面对您有帮助吗?