javascript - 如何使用 N-API 将 BigInt 类型从 C++ 返回到 javascript?

标签 javascript node.js n-api node-addon-api

我正在使用这个 https://github.com/nodejs/node-addon-api/blob/master/doc/bigint.md文档作为从 C++ 返回 bigint 的引用,但我收到以下错误:

error: ‘BigInt’ in namespace ‘Napi’ does not name a type
     Napi::BigInt HelloWrapped(const Napi::CallbackInfo& info);

这是我的源代码:

#include <napi.h>
#include "bigintexample.h"

std::int64_t bigintexample::hello() {
    return 1234;
}

Napi::BigInt bigintexample::HelloWrapped(const Napi::CallbackInfo& info) {
    Napi::Env env = info.Env();
    Napi::BigInt returnValue = Napi::BigInt::New(env, bigintexample::hello());

    return returnValue;
}


Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
    exports.Set("hello", Napi::Function::New(env, bigintexample::HelloWrapped));
    return exports;
}


NODE_API_MODULE(NODE_GYP_MODULE_NAME, InitAll)

在 napi.h 中,只有当 NAPI_VERSION 定义大于 2147483646 时,BigInt 功能才可用。当我将 NAPI_VERSION 定义设置为高于 2147483646 的数字时,我收到以下错误消息

/home/user/bigint-napi/node_modules/node-addon-api/napi-inl.h:573:24: error: ‘napi_create_bigint_int64’ was not declared in this scope
   napi_status status = napi_create_bigint_int64(env, val, &value);

最佳答案

通过添加定义解决它:NAPI_EXPERIMENTAL 并删除 NAPI_VERSION 定义

关于javascript - 如何使用 N-API 将 BigInt 类型从 C++ 返回到 javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54879033/

相关文章:

javascript - 禁用 IE 11 输入清除 'X' 按钮

javascript - Angular 服务不作为exp工作

node.js - 使用 Node.js 客户端在 gcloud 存储中创建和共享存储桶

node.js - 如何使用 node-addon-api 保存回调供以后使用?

vue.js - 错误 "Cannot read property ' indexOf' of undefined"绑定(bind) n-api 模块

c++ - 如何解决 N-Api Addon C 中的 Node.js Promise

javascript - OrionJS 样式表适用于所有模板

javascript - 使用 Angular 的解析 : {}? 时链接 promise 的最佳方式是什么

javascript - 如何在javascript中解析字符串以返回某些内容

node.js - 如何在nodejs上使用RabbitMQ的消息