Search code examples
javascriptnode.jsnode.js-addon

Using Nan to create array in Node.js add-on code


I'm writing Node add-on and using the nan library as much as I can for writing the code. It's recommended by the Node project because it lets you write code that's compatible with different version of v8 and node.

However after looking through their documentation many times, I haven't found any guidance on handling of arrays in the nan API. For basic tasks like processing arrays passed as arguments by the Javascript code, or instantiating new array objects in the add-on and return it to Javascript code. Are we supposed to directly work with v8::Array API. I wished the Nan::New part of the API would have handled this better.

Have I missed something?


Solution

  • If you look at the V8 documentation, you'll see that v8::Array is a subset of v8::Object. Given this, you can create an array with

    Nan::New<v8::Array>();
    

    You can reference the v8::Array documentation for more details.