nodejs c ++插件中的访问器

如何在一个Node.js的C ++扩展中使用全局variables“X”的setter和getter?

我正在getter和setter方法中获取未定义的“x”。

尝试从此链接执行Static Global Variables程序。

我已经成功地写了一个微小的模块,添加和乘以两个给定的数字。

以下是我的代码

Init(入口点)方法包含 –

exports->SetAccessor(String::New("x"), XGet, XSet);

在此之上,我有下面的设置和getter。

 Handle<Value> XGet(Local<String> property, const AccessorInfo& info){ return Integer::New(x); } void XSet(Local<String> property, Local<Value> value, const AccessorInfo& info){ x = value->Int32Value(); } 

在编译时说 – 'x' was not declared in this scope

错误消息'x' was not declared in this scope几乎'x' was not declared in this scope它。 你的代码和你正在看的示例页面明确标题为Accessing Static Global Variables ,所以我认为他们假设你已经有了一个variablesx定义,你想通过JS访问/ mutate。

所以快速修复是add int32_t x; 在你的C ++文件上面的XGetXSet上面。

节点文档中有一些关于这方面的信息,如果你想使用对象的实例而不是单个全局variables,标题为“ Wrapping C++ objects ”的部分可能是一个很好的开始。