I have a vector std::vector<std::string> path
and I would like to copy it to a v8 array and return it from my function.
我有一个矢量std :: vector
I have tried creating a new array
我试过创建一个新数组
v8::Handle<v8::Array> result;
and putting the values from path
into result
but with no luck. I've also tried several variations of
并将路径中的值放入结果但没有运气。我也尝试了几种变体
return scope.Close(v8::Array::New(/* I've tried many things in here */));
without success.
没有成功。
This is a similar question but I cant seem to duplicate the results.
这是一个类似的问题,但我似乎无法复制结果。
How do you populate v8 arrays?
你如何填充v8阵列?
2 个解决方案
#1
9
This example directly from the Embedder's Guide seems very close to what you want - replace new Integer
objects with new String
objects.
直接来自Embedder指南的这个例子似乎非常接近你想要的 - 用新的String对象替换新的Integer对象。
// This function returns a new array with three elements, x, y, and z.
Handle<Array> NewPointArray(int x, int y, int z) {
// We will be creating temporary handles so we use a handle scope.
HandleScope handle_scope;
// Create a new empty array.
Handle<Array> array = Array::New(3);
// Return an empty result if there was an error creating the array.
if (array.IsEmpty())
return Handle<Array>();
// Fill out the values
array->Set(0, Integer::New(x));
array->Set(1, Integer::New(y));
array->Set(2, Integer::New(z));
// Return the value through Close.
return handle_scope.Close(array);
}
I'd read up on the semantics of Local and Persistent handles because I think that is where you have got stuck.
我已经阅读了Local和Persistent句柄的语义,因为我认为这是你遇到的问题。
This line:
这一行:
v8::Handle<v8::Array> result;
Doesn't create a new array - It only creates a Handle that can later be filled in with an array.
不创建新数组 - 它只创建一个Handle,以后可以用数组填充。
#2
1
To create a new array
创建新数组
Handle<Array>postOrder = Array::New(isolate,5);
//New takes two argument 1st one should be isolate and second one should
//be the number
To Set the element in v8::array
在v8 :: array中设置元素
int elem = 101; // this could be a premitive data type, array or vector or list
for(int i=0;i<10;i++) {
postOrder->Set(i++,Number::New(isolate,elem));
}
To Get the element from v8::array
从v8 :: array获取元素
for(int i=0; i<postOrder->Length();i++){
double val = postOrder->Get(i)->NumberValue()
}
//Type conversion is important in v8 to c++ back and forth; there is good library for data structure conversion; **V8pp Header only Librabry**
Thanks!!
谢谢!!
#1
9
This example directly from the Embedder's Guide seems very close to what you want - replace new Integer
objects with new String
objects.
直接来自Embedder指南的这个例子似乎非常接近你想要的 - 用新的String对象替换新的Integer对象。
// This function returns a new array with three elements, x, y, and z.
Handle<Array> NewPointArray(int x, int y, int z) {
// We will be creating temporary handles so we use a handle scope.
HandleScope handle_scope;
// Create a new empty array.
Handle<Array> array = Array::New(3);
// Return an empty result if there was an error creating the array.
if (array.IsEmpty())
return Handle<Array>();
// Fill out the values
array->Set(0, Integer::New(x));
array->Set(1, Integer::New(y));
array->Set(2, Integer::New(z));
// Return the value through Close.
return handle_scope.Close(array);
}
I'd read up on the semantics of Local and Persistent handles because I think that is where you have got stuck.
我已经阅读了Local和Persistent句柄的语义,因为我认为这是你遇到的问题。
This line:
这一行:
v8::Handle<v8::Array> result;
Doesn't create a new array - It only creates a Handle that can later be filled in with an array.
不创建新数组 - 它只创建一个Handle,以后可以用数组填充。
#2
1
To create a new array
创建新数组
Handle<Array>postOrder = Array::New(isolate,5);
//New takes two argument 1st one should be isolate and second one should
//be the number
To Set the element in v8::array
在v8 :: array中设置元素
int elem = 101; // this could be a premitive data type, array or vector or list
for(int i=0;i<10;i++) {
postOrder->Set(i++,Number::New(isolate,elem));
}
To Get the element from v8::array
从v8 :: array获取元素
for(int i=0; i<postOrder->Length();i++){
double val = postOrder->Get(i)->NumberValue()
}
//Type conversion is important in v8 to c++ back and forth; there is good library for data structure conversion; **V8pp Header only Librabry**
Thanks!!
谢谢!!