从v8 :: Arguments转换为C ++类型 [英] Converting from v8::Arguments to C++ Types

查看:51
本文介绍了从v8 :: Arguments转换为C ++类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++创建Node.js模块,但是我迷上了v8 :: Arguments类.可以说我有一个用于发送电子邮件的Javascript类,该类具有带有此签名的方法:

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:

Mailer::sendEmail(Array recipients, String sender, String message);

将这样称呼:

mailer.sendEmail(["joe@gmail.com", "sally@gmail.com"], "fred@gmail.com", "Hi there");

现在在C ++领域,我有一个带有以下签名的类函数:

Now in C++ land, I have a class function with this signature:

SendEmail(const v8::Arguments& args)

这将在Javascript领域支持我的Mailer :: sendEmail方法.SendEmail函数将创建我的Emailer类的新实例,该类本身具有带有以下签名的类函数:

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:

send(std::list<std::string> recipients, std::string from, std::string message)

这就是我迷路的地方.我不知道如何从 args 中获取值,并将其转换为常规C ++类型,因此我可以将这些值传递给我的 send 函数.据我了解,传递给Mailer :: sendEmail的3个值将在 args [0] args [1] args [2]中可用.我什至知道我可以做一些类型检查,例如 if(!args [0]-> IsArray()),但实际上是将 args [0] 转换为std :: list< std :: string> 是我不知道怎么做的.

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 send function. 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.

我发现这样做的方法有点怪异,但我仍然认为V8具有一些内置方法可以更干净地处理此问题.

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.

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());
}

推荐答案

我能找到的与JS类型和C ++类型之间相互转换的最佳方法是使用 v8转换,它是从v8-juice,仅包含转换方法.

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.

将args [0]转换为std :: list只需一行:

Converting args[0] to std::list is one line:

std::list<std::string> values = cvv8::CastFromJS<std::list<std::string> >(args[0]);

这篇关于从v8 :: Arguments转换为C ++类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆