问题
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