最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

I'm writing Node add-on and using the nan library as much as I can for writing the code. It's remended by the Node project because it lets you write code that's patible 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?

I'm writing Node add-on and using the nan library as much as I can for writing the code. It's remended by the Node project because it lets you write code that's patible 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?

Share Improve this question asked Oct 13, 2016 at 19:34 JayeshJayesh 53.4k23 gold badges80 silver badges101 bronze badges 2
  • 1 If you look at the documentation they point to, v8:Array is an object. So for creating a new array, I'm fairly certain (I haven't messed around with Node addons in a while) you just do Nan::New<v8:Array>(). As for iterating over an array, I'm not sure. – Mike Cluck Commented Oct 13, 2016 at 19:40
  • Indeed. That worked. Makes the code better by not needing reference to isolate (as required when v8::Array is used directly). Thanks for the tip. If you post it as answer, i'll accept it. – Jayesh Commented Oct 13, 2016 at 20:11
Add a ment  | 

2 Answers 2

Reset to default 7

Searching for solutions to some related problems I have found this repository which has some very good working examples.

I'm just pointing out the Array related conversions here for quick reference.

Receive array in arguments:

Local<Array> array = Local<Array>::Cast(args[0]); //args[0] holds the first argument

for (unsigned int i = 0; i < array->Length(); i++ ) {
  if (Nan::Has(array, i).FromJust()) {
    //assuming the argument is an array of 'double' values, for any other type the following line will be changed to do the conversion
    double value = Nan::Get(array, i).ToLocalChecked()->NumberValue();

    Nan::Set(array, i, Nan::New<Number>(value + 1));
  }
}

Return an array:

//Assuming arr is an 'array' of 'double' values
Local<Array> a = New<v8::Array>(3);
Nan::Set(a, 0, Nan::New(arr[0]));
Nan::Set(a, 1, Nan::New(arr[1]));
Nan::Set(a, 2, Nan::New(arr[2]));

info.GetReturnValue().Set(a); //here 'info' is 'const Nan::FunctionCallbackInfo<v8::Value>& info' received in Nan Method defintion parameter

The specific solution can be found here.

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.

发布评论

评论列表(0)

  1. 暂无评论