在C++代码中,通过napi_get_cb_info接口可以获取ArkTS侧入参的参数信息、参数个数和this参数。获取参数信息时,先定义参数数组(如napi_value args[1] = {nullptr};),调用接口传入参数数组,接口会将参数填充到数组中;获取参数个数时,定义变量存储个数,调用接口传入nullptr获取个数并创建napi_value类型返回;获取this参数时,定义变量存储this参数,调用接口传入nullptr获取this参数并返回。示例代码如下:#include "napi/native_api.h" // 获取ArkTS侧入参的的参数信息 static napi_value GetCbArgs(napi_env env, napi_callback_info info) { size_t argc = 1; napi_value args[1] = {nullptr}; napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); return args[0]; } // 获取ArkTS侧入参的参数个数 static napi_value GetCbArgQuantity(napi_env env, napi_callback_info info) { size_t argc = 0; napi_value result = nullptr; napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr); napi_create_int32(env, argc, &result); return result; } // 获取ArkTS侧this参数 static napi_value GetCbContext(napi_env env, napi_callback_info info) { napi_value thisArg = nullptr; napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, nullptr); return thisArg; }ArkTS侧传入不同类型参数(如字符串、数组、数字、对象、函数等)调用相应函数获取信息并通过日志输出(如hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_cb_info get string arg:%{public}s', testNapi.getCbArgs(str));等)。本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。
在C++代码中,通过
napi_get_cb_info
接口可以获取ArkTS侧入参的参数信息、参数个数和this
参数。获取参数信息时,先定义参数数组(如napi_value args[1] = {nullptr};
),调用接口传入参数数组,接口会将参数填充到数组中;获取参数个数时,定义变量存储个数,调用接口传入nullptr
获取个数并创建napi_value
类型返回;获取this
参数时,定义变量存储this
参数,调用接口传入nullptr
获取this
参数并返回。示例代码如下:ArkTS侧传入不同类型参数(如字符串、数组、数字、对象、函数等)调用相应函数获取信息并通过日志输出(如
hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_cb_info get string arg:%{public}s', testNapi.getCbArgs(str));
等)。本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。