Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting from v8::Arguments to C++ Types
    primarykey
    data
    text
    <p>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:</p> <pre><code>Mailer::sendEmail(Array recipients, String sender, String message); </code></pre> <p>Which would be called like this:</p> <pre><code>mailer.sendEmail(["joe@gmail.com", "sally@gmail.com"], "fred@gmail.com", "Hi there"); </code></pre> <p>Now in C++ land, I have a class function with this signature:</p> <pre><code>SendEmail(const v8::Arguments&amp; args) </code></pre> <p>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:</p> <pre><code>send(std::list&lt;std::string&gt; recipients, std::string from, std::string message) </code></pre> <p>And this is where I'm lost. I don't know how to take the values from <em>args</em>, and convert them into regular C++ types, so I can pass the values to my <em>send</em> function. As I understand it, the 3 values passed to Mailer::sendEmail will be available in <em>args[0]</em>, <em>args[1]</em>, and <em>args[2]</em>. I even understand I can do some type checking like <code>if (!args[0]-&gt;IsArray())</code>, but actually converting <em>args[0]</em> to <code>std::list&lt;std::string&gt;</code> is what I don't know how to do.</p> <p><strong>Edit:</strong> 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.</p> <pre><code>static Handle&lt;Value&gt; SendEmail(const Arguments&amp; args) { HandleScope scope; list&lt;string&gt; values; Local&lt;Object&gt; obj = args[0]-&gt;ToObject(); Local&lt;Array&gt; props = obj-&gt;GetPropertyNames(); // Iterate through args[0], adding each element to our list for(unsigned int i = 0; i &lt; props-&gt;Length(); i++) { String::AsciiValue val(obj-&gt;Get(i)-&gt;ToString()); values.push_front(string(*val)); } // Display the values in the list for debugging purposes for (list&lt;string&gt;::iterator it = values.begin(); it != values.end(); it++) { cout &lt;&lt; *it &lt;&lt; endl; } return scope.Close(args.This()); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload