如何将v8值转换为数组 [英] How to convert v8 Value to Array
问题描述
我正在为v8编写一个c ++扩展,并希望将一个Array对象传递给它。我看到传入的参数可以通过IsArray()测试,但没有ToArray()。
I'm writing a c++ extension to v8, and want to pass an Array object into it. I see the incoming argument can be tested by IsArray(), but there isn't a ToArray().
如何访问它的长度,
Handle<Value> MyExtension(const Arguments& args)
{
Handle<Value> v = args[0];
if(v->IsArray())
{
// convert to array, find its length, and access its members by index... ?
}
...
}
明显这里。对象可以返回它的所有属性,但这不是我所希望的。
Must be missing something obvious here. Object can return all its properties, but that's not quite what I was hoping for. Is there a way to get it as an Arrray?
感谢您阅读。
推荐答案
我找不到一种方法来转换或转换为数组。也许有一种方法。但我发现通过做 object-> IsArray()
, object-> get(length) - > Uint32Value code>和
object-> get(int)
,我只需要遍历数组。
I wasn't able to find a way to convert or cast to Array. Maybe there's a way. But I found by doing object->IsArray()
, object->get("length")->Uint32Value()
, and object->get(int)
, I could just walk the array.
v8::Handle<v8::Object> obj;
// ... init obj from arguments or wherever ...
int length = 0;
if(obj->IsArray())
{
length = obj->Get(v8::String::New("length"))->ToObject()->Uint32Value();
}
for(int i = 0; i < length; i++)
{
v8::Local<v8::Value> element = obj->Get(i);
// do something with element
}
这篇关于如何将v8值转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!