从 v8::Arguments 转换为 C++ 类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7476145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting from v8::Arguments to C++ Types
提问by mellowsoon
I'm playing with creating Node.js modules in C++, but I'm stumped on the v8::Arguments class. Lets say I have a Javascript class for sending emails, which has a method with this signature:
我正在尝试用 C++ 创建 Node.js 模块,但我对 v8::Arguments 类感到困惑。假设我有一个用于发送电子邮件的 Javascript 类,它有一个带有此签名的方法:
Mailer::sendEmail(Array recipients, String sender, String message);
Which would be called like this:
这将被称为这样:
mailer.sendEmail(["[email protected]", "[email protected]"], "[email protected]", "Hi there");
Now in C++ land, I have a class function with this signature:
现在在 C++ 领域,我有一个带有这个签名的类函数:
SendEmail(const v8::Arguments& args)
Which is backing my Mailer::sendEmail method in Javascript land. The SendEmail function will create a new instance of my Emailer class, which itself has a class function with this signature:
这是在 Javascript 领域支持我的 Mailer::sendEmail 方法。SendEmail 函数将创建我的 Emailer 类的一个新实例,它本身有一个具有以下签名的类函数:
send(std::list<std::string> recipients, std::string from, std::string message)
And this is where I'm lost. I don't know how to take the values from args, and convert them into regular C++ types, so I can pass the values to my sendfunction. As I understand it, the 3 values passed to Mailer::sendEmail will be available in args[0], args[1], and args[2]. I even understand I can do some type checking like if (!args[0]->IsArray())
, but actually converting args[0]to std::list<std::string>
is what I don't know how to do.
这就是我迷失的地方。我不知道如何从args获取值,并将它们转换为常规的 C++ 类型,因此我可以将这些值传递给我的send函数。据我了解,传递给 Mailer::sendEmail 的 3 个值将在args[0]、args[1]和args[2] 中可用。我什至明白我可以做一些类型检查if (!args[0]->IsArray())
,但实际上将args[0]转换std::list<std::string>
为我不知道该怎么做。
Edit:I found a hackish way of doing this, but I still think V8 has some built in methods to handle this in a cleaner way.
编辑:我发现了一种骇人听闻的方法,但我仍然认为 V8 有一些内置方法可以以更简洁的方式处理这个问题。
static Handle<Value> SendEmail(const Arguments& args)
{
HandleScope scope;
list<string> values;
Local<Object> obj = args[0]->ToObject();
Local<Array> props = obj->GetPropertyNames();
// Iterate through args[0], adding each element to our list
for(unsigned int i = 0; i < props->Length(); i++) {
String::AsciiValue val(obj->Get(i)->ToString());
values.push_front(string(*val));
}
// Display the values in the list for debugging purposes
for (list<string>::iterator it = values.begin(); it != values.end(); it++) {
cout << *it << endl;
}
return scope.Close(args.This());
}
采纳答案by mellowsoon
The best way I can find to convert to and from JS types and C++ types, is using v8-juice, which provides type casting methods. Specifically I'm using v8-convert, which is a spin off of v8-juice, that includes only the conversion methods.
我能找到的在 JS 类型和 C++ 类型之间进行转换的最好方法是使用v8-juice,它提供了类型转换方法。具体来说,我使用的是v8-convert,它是 v8-juice 的衍生产品,仅包含转换方法。
Converting args[0] to std::list is one line:
将 args[0] 转换为 std::list 是一行:
std::list<std::string> values = cvv8::CastFromJS<std::list<std::string> >(args[0]);
回答by Ryan
I know this is an older topic, but the way I tend to do this is as follows:
我知道这是一个较旧的话题,但我倾向于这样做的方式如下:
Handle<Value> MethodName (const Arguments& args) {
// get the param
v8::String::Utf8Value param1(args[0]->ToString());
// convert it to string
std::string foo = std::string(*param1);
}
回答by Segfault
If you are using NaN (native abstractions for node) then try this code:
如果您使用 NaN (node 的本机抽象),请尝试以下代码:
std::string myString(*NanAsciiString(args[0]));