I'm writing a Node.js native module with a function accepting arbitrary length arguments, and it's similar with this in JS:
我正在编写一个带有接受任意长度参数的函数的Node.js本机模块,它与JS中的类似:
cb = function( )
{
// Receive arguments and do something...
}
foo = function( )
{
cb.apply({}, arguments)
}
foo([1,2,3])
foo([4])
Here, foo
apply the cb
with arbitrary arguments.
这里,foo使用任意参数应用cb。
And the C++ version, according to most Node.js articles about callbacks, would like this:
根据大多数关于回调的Node.js文章,C ++版本都是这样的:
Handle<Value> Foo(const Arguments& args)
{
HandleScope scope;
// Assume that we can get callback from somewhere else.
Local<Function> cb = getExistingCallback();
// Now call it with arbitrary arguments.
cb->Call(Object::New(), args.Length(), args.Data());
return scope.Close(Undefined());
}
But Arguments::Data
can only provide v8::Local<v8::Value>&
, not v8::Handle<v8::Value>*
, so compiler will throw errors.
但Arguments :: Data只能提供v8 :: Local
Because Local
derived from Handle
, it's not the problem. I just don't if there is any solution I can use to avoid copy all member from the Data
to a new array then pass it in.
因为Local来自Handle,所以不是问题。我只是不知道是否有任何解决方案我可以用来避免将所有成员从Data复制到新数组然后传入。
1 个解决方案
#1
1
Arguments::Data
is not what you want. That data is totally unrelated to the values passed to the function itself, if you look at the source. Data
reads from implicit_args_
while the data you want is in values_
.
参数::数据不是你想要的。如果你查看源代码,那么这些数据与传递给函数本身的值完全无关。数据从implicit_args_读取,而您想要的数据在values_中。
I don't think there is an easy way to get at that information without using operator[]
so maybe your best bet is the construct the list dynamically? You can use a std::vector
since its data values are contiguous.
我不认为有一种简单的方法可以在不使用operator []的情况下获取该信息,所以也许你最好的选择是动态构建列表?您可以使用std :: vector,因为它的数据值是连续的。
int argc = args.Length();
std::vector<Local<Value>> argv;
for (int i = 0; i < argc; i++){
argv.push_back(args[i]);
}
cb->Call(Object::New(), argc, &argv[0]);
#1
1
Arguments::Data
is not what you want. That data is totally unrelated to the values passed to the function itself, if you look at the source. Data
reads from implicit_args_
while the data you want is in values_
.
参数::数据不是你想要的。如果你查看源代码,那么这些数据与传递给函数本身的值完全无关。数据从implicit_args_读取,而您想要的数据在values_中。
I don't think there is an easy way to get at that information without using operator[]
so maybe your best bet is the construct the list dynamically? You can use a std::vector
since its data values are contiguous.
我不认为有一种简单的方法可以在不使用operator []的情况下获取该信息,所以也许你最好的选择是动态构建列表?您可以使用std :: vector,因为它的数据值是连续的。
int argc = args.Length();
std::vector<Local<Value>> argv;
for (int i = 0; i < argc; i++){
argv.push_back(args[i]);
}
cb->Call(Object::New(), argc, &argv[0]);