Search code examples
c++node.jsv8

Cannot convert C++ string vector to V8 array


I'm trying to do some basic string processing in C++ and send the results as an array to Node.js (V8). While I can pass integers and strings without any problems, passing C++ string vectors and converting them to a Javascript array causes node-gyp to complain a lot.

My knowledge of C++ is quite patchy and I'm just getting started working with V8, so can someone fill me in on what I'm missing?

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <node.h>
#include <v8.h>

using namespace std;
using namespace v8;

Handle<Value> uniques(const Arguments& args) {
    HandleScope scope;
    vector<string> words;

    // open text file with lots of words separated by spaces
    ifstream openFile( "D:\\words" );

    if( openFile.is_open() ) {
        while( !openFile.eof() ) {
            string temp;
            openFile >> temp;
            if( temp != "" ) {
                words.push_back( temp );
            }
        }
    }

    // Filter out duplicates
    sort( words.begin(), words.end() );
    words.erase( unique( words.begin(), words.end() ), words.end() );

    // Convert string Vector to V8 array
    Handle<Array> wordArray = Array::New( words.size() );
    for (unsigned i=0; i < words.size(); i++) {

        // The problem area: this next line is where node-gyp starts moaning.
        wordArray->Set( Number::New(i), String::New(words[i]) );
    }
    return scope.Close( wordArray );
}


// setup Node.js module
void init(Handle<Object> exports) {
    exports->Set(String::NewSymbol("words"), FunctionTemplate::New(uniques)->GetFunction());
}

Output:

gyp info using [email protected]
gyp info using [email protected] | win32 | ia32
gyp info spawn D:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe
gyp info spawn args [ 'build/binding.sln',
gyp info spawn args   '/clp:Verbosity=minimal',
gyp info spawn args   '/nologo',
gyp info spawn args   '/p:Configuration=Release;Platform=Win32' ]
Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
  words.cc
D:\Program Files\Microsoft Visual Studio 11.0\VC\include\xlocale(336): warning C4530: C++ exception handler used, but unwind semantics are
not enabled. Specify /EHsc [D:\words.vcxproj]
..\words.cc(14): error C3203: 'basic_string' : unspecialized class template can't be used as a template argument for template parameter '_T
y', expected a real type [D:\words.vcxproj]
..\words.cc(14): error C2955: 'std::basic_string' : use of class template requires template argument list [D:\words.vcxproj]
          D:\Program Files\Microsoft Visual Studio 11.0\VC\include\xstring(698) : see declaration of 'std::basic_string'
..\words.cc(22): error C2664: 'void std::vector<_Ty>::push_back(int &&)' : cannot convert parameter 1 from 'std::string' to 'int &&' [D:\words.vcxproj]
          with
          [
              _Ty=int
          ]
          Reason: cannot convert from 'std::string' to 'int'
          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
..\words.cc(34): error C2665: 'v8::String::New' : none of the 2 overloads could convert all the argument types [D:\words.vcxproj]
          D:\Users\vijay\.node-gyp\0.10.18\deps\v8\include\v8.h(1225): could be 'v8::Local<T> v8::String::New(const char *,int)'
          with
          [
              T=v8::String
          ]
          D:\Users\vijay\.node-gyp\0.10.18\deps\v8\include\v8.h(1228): or       'v8::Local<T> v8::String::New(const uint16_t *,int)'
          with
          [
              T=v8::String
          ]
          while trying to match the argument list '(int)'
gyp ERR! build error
gyp ERR! stack Error: `D:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onExit (D:\Users\vijay\AppData\Roaming\npm\node_modules\node-gyp\lib\build.js:267:23)
gyp ERR! stack     at ChildProcess.EventEmitter.emit (events.js:98:17)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (child_process.js:789:12)
gyp ERR! System Windows_NT 6.1.7600

Solution

  • v8::String::New has a different signature. See here.

    Try it like this:

    String::New(&words[i][0], words[i].size())