我有一个向量std::vector<std::string> path,我想将它复制到一个v8数组并从我的函数返回它.
我试过创建一个新数组
v8::Handle<v8::Array> result;
Run Code Online (Sandbox Code Playgroud)
并将价值观path投入result但没有运气.我也尝试了几种变体
return scope.Close(v8::Array::New(/* I've tried many things in here */));
Run Code Online (Sandbox Code Playgroud)
没有成功.
这是一个类似的问题,但我似乎无法复制结果.
你如何填充v8阵列?
myt*_*gel 10
直接来自Embedder指南的这个例子似乎非常接近你想要的 - 用新Integer对象替换新String对象.
// 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);
}
Run Code Online (Sandbox Code Playgroud)
我已经阅读了Local和Persistent句柄的语义,因为我认为这是你遇到的问题.
这一行:
v8::Handle<v8::Array> result;
Run Code Online (Sandbox Code Playgroud)
不创建新数组 - 它只创建一个Handle,以后可以用数组填充.