How to export array data through node addon

旧街凉风 提交于 2021-01-28 20:45:58

问题


I'm using node 0.12.x, I want to return some array data from node addon written by c++

Isolate* isolate = args.GetIsolate();
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
obj->value_ += 1;
args.GetReturnValue().Set(Number::New(isolate, obj->value_));

This is a sample for returning Number data.


回答1:


using namespace v8;

Create an array:

Local<Array> myArray = Array::New(isolate);

You can then create objects with properties (or just integers) and push them into the array:

 for (int i = 0; i < n; i++) {
    Local<Object> obj = Object::New(isolate);
    obj->Set(String::NewFromUtf8(isolate, "tag1"), "test");
    myArray->Set(i, obj);
 }

 args.GetReturnValue().Set(myArray);

If you're writing native code for node.js I highly recommend using nan: https://github.com/nodejs/nan



来源:https://stackoverflow.com/questions/33162289/how-to-export-array-data-through-node-addon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!