如何从BigInt对象中获取64位带符号整数的值(使用napi_get_value_bigint_int64接口)?

阅读 601
1 个回答

在C++代码中,获取传入的参数后,调用napi_get_value_bigint_int64接口提取64位带符号整数的值,同时获取一个表示是否无损转换的布尔值。若不是无损转换则抛出异常,若接口调用成功则返回一个表示成功状态的napi_value类型值(转换为布尔类型表示)。示例代码如下:

#include "napi/native_api.h"

static napi_value GetValueBigintInt64t(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);
    int64_t value = 0;
    bool lossLess = false;
    napi_status status = napi_get_value_bigint_int64(env, args[0], &value, &lossLess);
    if (!lossLess) {
        napi_throw_error(env, nullptr, "BigInt values have not been lossless converted");
        return nullptr;
    }
    napi_value returnValue = nullptr;
    napi_get_boolean(env, status == napi_ok, &returnValue);
    return returnValue;
}

ArkTS侧创建一个BigInt对象后传入该接口函数获取值,如let bigInt = BigInt(-5555555555555555); try { hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_value_bigint_int64: %{public}s', JSON.stringify(testNapi.getValueBigintInt64t(bigInt))); } catch (error) { hilog.error(0x0000, 'testTag', 'Test Node-API NapiGetValueBigint: %{public}s', error.message); }

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题