HarmonyOS鸿蒙Next中FFI C++调用ArkTS定义的一个方法
HarmonyOS鸿蒙Next中FFI C++调用ArkTS定义的一个方法
鸿蒙提供了FFI,目标是C++调用JS arkTS方法,通过napi接口napi_call_function调用arkts 的方法,本地试了get_named_property拿不到对应的property,是哪有问题么? 附执行代码块: testAdd() 定义在ArkTS内一个方法 C++ 部分:
static napi_value runFunction(napi_env env, napi_callback_info info)
{
napi_status napi_status;
napi_valuetype valuetype;
napi_value global;
napi_status = napi_get_global(env, &global);
napi_value addFunc;
// 获取add函数
napi_status = napi_get_named_property(env, global, "testAdd", &addFunc);
napi_status = napi_typeof(env, addFunc, &valuetype);
if (valuetype != napi_function) {
napi_value sum;
napi_create_double(env, 404, &sum);
return sum;
}
napi_value result;
napi_status = napi_call_function(env, global, addFunc, 0, nullptr, &result);
return result;
}
更多关于HarmonyOS鸿蒙Next中FFI C++调用ArkTS定义的一个方法的实战教程也可以访问 https://www.itying.com/category-93-b0.html
解决措施 现提供两种方式调用artTS内函数
//User.ts export default class User { name: string = ‘username’
location: object = {
country: 'china',
city: 'shanghai'
}
oncAll() {
console.log("testTag user oncAll")
}
}
function oncAllGlobal() { console.log(“testTag user oncAll Global”) } globalThis.oncAllGlobal = oncAllGlobal; // hello.cpp // 传入实例对象,在C++侧调用对象中的函数 static napi_value CallFunction(napi_env env, napi_callback_info info) { size_t argc = 1; napi_value args[1];
napi_get_cb_info(env, info, &argc, args, NULL, NULL);
napi_valuetype valuetype0;
napi_typeof(env, args[0], &valuetype0);
napi_value oncAll;
napi_status status = napi_get_named_property(env, args[0], "oncAll", &oncAll);
if (status != napi_ok)
return oncAll;
napi_value return_val;
status = napi_call_function(env, args[0], oncAll, 0, NULL, &return_val);
return oncAll;
} // 使用全局对象,调用artTS内函数 static napi_value CallFunction_global(napi_env env, napi_callback_info info) { size_t argc = 1; napi_value args[1];
napi_get_cb_info(env, info, &argc, args, NULL, NULL);
napi_value global;
napi_status status = napi_get_global(env, &global);
napi_value oncAll;
status = napi_get_named_property(env, global, "oncAllGlobal", &oncAll);
if (status != napi_ok)
return global;
napi_value return_val;
status = napi_call_function(env, global, oncAll, 0, NULL, &return_val);
return oncAll;
} // index.ets build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => { let user = new User(); hilog.info(0x0000, ‘testTag’, ‘Test NAPI call function: %{public}s’, testNapi.callFunction(user)); hilog.info(0x0000, ‘testTag’, ‘Test NAPI call function global : %{public}s’, testNapi.callFunctionGlobal()); }) } .width(‘100%’) } .height(‘100%’) }
napi使用方式可参考文档:
[https://nodejs.cn/api/n-api.html#napi_call_function](https://nodejs.cn/api/n-api.html#napi_call_function)
napi_get_global函数使用可参考:
[https://nodejs.cn/api/n-api.html#napi_get_global](https://nodejs.cn/api/n-api.html#napi_get_global)
其中napi_value 代表 JavaScript global 对象
更多关于HarmonyOS鸿蒙Next中FFI C++调用ArkTS定义的一个方法的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,通过FFI(Foreign Function Interface)机制,可以从C++代码调用ArkTS定义的方法。首先,在ArkTS中使用@FFI
注解导出方法,然后在C++中使用napi_get_named_property
和napi_call_function
等NAPI接口调用该方法。确保在C++代码中正确加载ArkTS模块,并通过NAPI上下文进行跨语言调用。