Passing an Array via nan returns undefined when attempting to retrieve
<p>I'm currently writing a native addon for NodeJS using their c++/v8 system, and am attempting to write an array from Javascript, to C++ and later retrieve it. Whenever I attempt to retrieve a value from the array and return it, it returns an empty array - as I'm recently diving into c++ I am unsure if this is my misunderstanding pointers/c++ fundamentals or NodeJS c++ interactions. </p>

<p>My files as follows:</p>

<h1>functions.cc</h1>

<pre><code>#include "functions.h"
#include &lt;node.h&gt;
#include &lt;nan.h&gt;

using namespace std;
using namespace v8;

NAN_METHOD(nothing) {
}

NAN_METHOD(aString) {
    info.GetReturnValue().Set(Nan::New("This is a thing.").ToLocalChecked());
}

NAN_METHOD(aBoolean) {
    info.GetReturnValue().Set(false);
}

NAN_METHOD(aNumber) {
    info.GetReturnValue().Set(1.75);
}

NAN_METHOD(anObject) {
    v8::Local&lt;v8::Object&gt; obj = Nan::New&lt;v8::Object&gt;();
    Nan::Set(obj, Nan::New("key").ToLocalChecked(), Nan::New("value").ToLocalChecked());
    info.GetReturnValue().Set(obj);
}

NAN_METHOD(anArray) {
    v8::Local&lt;v8::Array&gt; arr = Nan::New&lt;v8::Array&gt;(3);
    Nan::Set(arr, 0, Nan::New(1));
    Nan::Set(arr, 1, Nan::New(2));
    Nan::Set(arr, 2, Nan::New(3));
    info.GetReturnValue().Set(arr);
}

NAN_METHOD(callback) {
    v8::Local&lt;v8::Function&gt; callbackHandle = info[0].As&lt;v8::Function&gt;();
    Nan::MakeCallback(Nan::GetCurrentContext()-&gt;Global(), callbackHandle, 0, 0);
}

// Wrapper Impl

Nan::Persistent&lt;v8::Function&gt; MyObject::constructor;

NAN_MODULE_INIT(MyObject::Init) {
  v8::Local&lt;v8::FunctionTemplate&gt; tpl = Nan::New&lt;v8::FunctionTemplate&gt;(New);
  tpl-&gt;SetClassName(Nan::New("MyObject").ToLocalChecked());
  tpl-&gt;InstanceTemplate()-&gt;SetInternalFieldCount(1);

  //Nan::SetPrototypeMethod(tpl, "plusOne", PlusOne);
  Nan::SetPrototypeMethod(tpl, "getArr", arrY);
  Nan::SetPrototypeMethod(tpl, "PassArray", passArray);
  Nan::SetPrototypeMethod(tpl, "setArray", SetArray);

  constructor.Reset(Nan::GetFunction(tpl).ToLocalChecked());
  Nan::Set(target, Nan::New("MyObject").ToLocalChecked(), Nan::GetFunction(tpl).ToLocalChecked());
}

MyObject::MyObject(v8::Local&lt;v8::Array&gt; value) : value_(value) {
}

MyObject::~MyObject() {
}

NAN_METHOD(MyObject::New) {
  if (info.IsConstructCall()) {
    v8::Local&lt;v8::Array&gt; arr = Nan::New&lt;v8::Array&gt;();
    v8::Local&lt;v8::Array&gt; value = v8::Handle&lt;v8::Array&gt;::Cast(arr);
    MyObject *obj = new MyObject(value);
    obj-&gt;Wrap(info.This());
    info.GetReturnValue().Set(info.This());
  } else {
    const int argc = 1; 
    v8::Local&lt;v8::Value&gt; argv[argc] = {info[0]};
    v8::Local&lt;v8::Function&gt; cons = Nan::New(constructor);
    info.GetReturnValue().Set(cons-&gt;NewInstance(argc, argv));
  }
}

// NAN_METHOD(MyObject::PlusOne) {
//   MyObject* obj = Nan::ObjectWrap::Unwrap&lt;MyObject&gt;(info.This());
//   obj-&gt;value_ += 1;
//   info.GetReturnValue().Set(obj-&gt;value_);
// }

NAN_METHOD(MyObject::SetArray){
    Nan::HandleScope scope;
    vector&lt;string&gt; result;
    Handle&lt;Value&gt; val;
    Local&lt;Array&gt; arr = Nan::New&lt;Array&gt;();
    MyObject* obj = ObjectWrap::Unwrap&lt;MyObject&gt;(info.Holder());

    if (info[0]-&gt;IsArray()) {
      Handle&lt;Array&gt; jsArray = Handle&lt;Array&gt;::Cast(info[0]);
      for (unsigned int i = 0; i &lt; jsArray-&gt;Length(); i++) {
        val = jsArray-&gt;Get(i);
        result.push_back(string(*String::Utf8Value(val)));
        Nan::Set(arr, i, val);
      }
    }
    info.GetReturnValue().Set(obj-&gt;value_);
}

NAN_METHOD(MyObject::arrY){
  MyObject* obj = ObjectWrap::Unwrap&lt;MyObject&gt;(info.Holder());
  info.GetReturnValue().Set(Local&lt;Array&gt;::Cast(obj-&gt;value_));
}

NAN_METHOD(MyObject::passArray) {
    Nan::HandleScope scope;
    vector&lt;string&gt; result;
    Handle&lt;Value&gt; val;
    Local&lt;Array&gt; arr = Nan::New&lt;Array&gt;();

    if (info[0]-&gt;IsArray()) {
      Handle&lt;Array&gt; jsArray = Handle&lt;Array&gt;::Cast(info[0]);
      for (unsigned int i = 0; i &lt; jsArray-&gt;Length(); i++) {
        val = jsArray-&gt;Get(i);
        result.push_back(string(*String::Utf8Value(val)));
        Nan::Set(arr, i, val);
      }
    }
    MyObject* obj = Nan::ObjectWrap::Unwrap&lt;MyObject&gt;(info.This());
    obj-&gt;value_ = arr;
    info.GetReturnValue().Set(obj-&gt;value_);
}
</code></pre>

<h1>functions.h</h1>

<pre><code>#ifndef NATIVE_EXTENSION_GRAB_H
#define NATIVE_EXTENSION_GRAB_H

#include &lt;nan.h&gt;

// Example top-level functions. These functions demonstrate how to return various js types.
// Implementations are in functions.cc

NAN_METHOD(nothing);
NAN_METHOD(aString);
NAN_METHOD(aBoolean);
NAN_METHOD(aNumber);
NAN_METHOD(anObject);
NAN_METHOD(anArray);
NAN_METHOD(callback);

// Example with node ObjectWrap
// Based on https://nodejs.org/api/addons.html#addons_wrapping_c_objects but using NAN
class MyObject : public Nan::ObjectWrap {
  public:
    static NAN_MODULE_INIT(Init);

  private:
    explicit MyObject(v8::Local&lt;v8::Array&gt; value);
    ~MyObject();

    static NAN_METHOD(New);
    static NAN_METHOD(PlusOne);
    static NAN_METHOD(SetArray);
    static NAN_METHOD(arrY);
    static NAN_METHOD(passArray);

    static Nan::Persistent&lt;v8::Function&gt; constructor;
    v8::Local&lt;v8::Array&gt; value_;
};

#endif
</code></pre>

<h1>index.js</h1>

<pre><code>var NativeExtension = require('bindings')('NativeExtension');
module.exports = NativeExtension;


console.log(NativeExtension.aString());

var obj = NativeExtension.MyObject([1,2,3]);
console.log(NativeExtension.aString());
console.log(obj.PassArray([1, 1, 3]));
console.log(obj.getArr());
console.log(obj.setArray())
</code></pre>

<p>For testing purposes code is definitely not DRY and it's based upon a non-stripped down version of the nan-boilerplate code which is provided via github <a href="https://github.com/fcanas/node-native-boilerplate" rel="nofollow">which can be found here</a>.</p>

<p>Upon rereading my own code I can definitely tell that my method naming is <strong>bad</strong>, so PassArr should set an array, and getArr should return the array passed by "PassArr". SetArray was a hackish attempt which I left in.</p>

<p>To reiterate, the functionality I'm attempting to achieve is:</p>

<pre><code>Javascript Array -&gt; C++ variable
C++ variable -&gt; Javascript Array
</code></pre>

<p>but the currently functionality does not persist my variable. The outcome of running the index.js is as follows:</p>

<pre><code>This is a thing.
This is a thing.
[ 1, 1, 3 ]
Console {
  log: [Function: bound ],
  info: [Function: bound ],
  warn: [Function: bound ],
  error: [Function: bound ],
  dir: [Function: bound ],
  time: [Function: bound ],
  timeEnd: [Function: bound ],
  trace: [Function: bound trace],
  assert: [Function: bound ],
  Console: [Function: Console] }
[]
</code></pre>