Accessors in nodejs c++ addons

How to use setter and getter for a global variable 'X' in a C++ extension to a Node.js ?

I am getting undefined 'x' in getter and setter methods.

Trying to execute Accessors program for Static Global Variables from this link .

I have successfully written a tiny module that adds and multiplies the two given numbers.

following is my code

Init(entry point) method contains-

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

and above that I have following setter and 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();

On compilation it says - ‘x’ was not declared in this scope

Problem courtesy of: Pranav


The error message 'x' was not declared in this scope pretty much pins it down. Your code and the example page you are looking at are explicitly titled Accessing Static Global Variables , so I think they assume you already have such a variable x defined, which you want to access/mutate via JS.

So the quick fix is the add int32_t x; in your C++ file somewhere above XGet and XSet .

The node documentation has a little bit of information on this, and the section titled Wrapping C++ objects might be a good place for you to start if you want to work with instances of an object rather than single global variables.

Solution courtesy of: loganfsmyth