我有一个围绕 C++ 类的简单 N-API 包装器。类被实例化和解构,但类中的实例变量没有被声明,即标准输出显示导出不包含“App.m_variable”。
我已经阅读了所有文档https://nodejs.org/api/n-api.html#n_api_napi_define_class,我已经浏览了所有提到“napi_define_class”的谷歌网站。
没有喜悦。
任何帮助将不胜感激!
node -v
v12.19.0
lsb_release -a
Description: Ubuntu 20.04.1 LTS
// file: app.h
class App
{
public:
double m_variable = 3.0;
void hello();
};
// file: index.cpp
// handle App() destructor
void n_app_dtor(napi_env env, void* instance, void* /* hint */)
{
std::cout << "jax: " << "n_app_dtor instance=" << instance << std::endl;
delete ((App*) instance);
}
// handle new App()
napi_value n_app_new(napi_env env, napi_callback_info info)
{
<snip>
napi_status status;
napi_value n_result;
status = napi_create_object(env, &n_result);
if (status != napi_ok) <snip...>
App* instance = new App();
std::cout << "jax: " << "n_app_new instance=" << instance << std::endl;
status = napi_wrap(env, n_result, (void*) instance, n_app_dtor, nullptr, nullptr);
if (status != napi_ok) <snip...>
return n_result;
}
// handle get of App::m_variable
napi_value n_app_get_m_variable(napi_env env, napi_callback_info info)
{
std::cout << "jax: " << "n_app_get_m_variable " << std::endl;
<snip>
App* instance;
napi_status status;
status = napi_unwrap(env, argv[0], (void**) &instance);
if (status != napi_ok) <snip...>
std::cout << "jax: " << "n_app_get_m_variable instance=" << instance << std::endl;
napi_value n_result;
status = napi_create_double(env, instance->m_variable, &n_result);
if (status != napi_ok) <snip...>
return n_result;
}
napi_value Init(napi_env env, napi_value exports)
{
napi_status status;
napi_property_descriptor properties[] = {
{"m_variable", nullptr, nullptr, n_app_get_m_variable, nullptr, nullptr, napi_default, nullptr},
};
napi_value n_result;
status = napi_define_class(env, "App", NAPI_AUTO_LENGTH, n_app_new, nullptr, 1, properties, &n_result);
if (status != napi_ok) <...>
status = napi_set_named_property(env, exports, "App", n_result);
if (status != napi_ok) <...>
return exports;
}
NAPI_MODULE(example, Init);
# test.js
console.log("jax: js App proto:", example.App.prototype)
app = new example.App();
console.log("jax: js app:", app)
console.log("jax: js app.m_variable :", app.m_variable)
# output
$ node test.js
jax: js App proto: App {}
jax: n_app_new instance=0x4a4caf0
jax: js app: {}
jax: js app.m_variable : undefined
jax: n_app_dtor instance=0x4a4caf0
您可以看到有充分的证据表明 napi_define_class() 工作正常:
- 正在调用和创建 App() 实例
- 析构函数被调用并工作
- 该类正在导出
但是属性,特别是 m_variable,应该显示在导出中。所以 napi_property_descriptor 定义可能有问题?
另请注意,对 n_app_get_m_variable 和标准输出的调用应该有一个日志行显示它已被调用。但这不会发生。
注意属性参数我尝试了一堆不同的值:
- napi_default
- napi_configurable
- napi_static
- napi_enumerable