Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node js STL vector of strings support broken #1205

Open
0-issue opened this issue Mar 2, 2018 · 4 comments
Open

Node js STL vector of strings support broken #1205

0-issue opened this issue Mar 2, 2018 · 4 comments

Comments

@0-issue
Copy link

0-issue commented Mar 2, 2018

Hi,

The support for STL vector of strings is broken. Here's an example:

Works fine: function reverse_str. It takes one std string as input, and returns another std string.
Doesn't work fine: function reverse_each_str. It takes STL vector of strings as input, and returns another STL vector of strings.

File example.i:

%module example
 %{
 /* Includes the header in the wrapper code */
 #include <chrono>
 #include <iostream>
 #include <cstdlib>
 #include <cstdio>
 #include <unistd.h>
 using namespace std;
 using namespace std::chrono;
 %}

 %include "cpointer.i"
 %include "std_string.i"
 %include "std_vector.i"
 %include "typemaps.i"

 %template(VectInt) std::vector<int>;
 %template(VectString) std::vector<std::string>;
 %template(VectVoidPtr) std::vector<void*>;

 %inline %{
std::string reverse_str (std::string arg) {
    std::string reverse_arg;
    reverse_arg = std::string(arg.rbegin(),arg.rend());
    return reverse_arg;
}

std::vector<std::string> reverse_each_str(std::vector<std::string> v) {
    std::vector<std::string> w(v);
    for (unsigned int i=0; i<w.size(); i++)
        w[i] = std::string(w[i].rbegin(),w[i].rend());
    return w;
}

%}

File binding.gyp:

{
        'targets': [
                {
                        'target_name': 'example',
                        'sources': [ 'example_nodejs.cxx' ],
                        'cflags': ['-std=c++11'],
                        'cflags!': [ '-fno-exceptions' ],
                        'cflags_cc!': [ '-fno-exceptions' ],
                }
        ]
}

Example test.js:

#!<path_to_your_node_install_dir>/bin/node
var example = require("<path_to_example_dir>/example/build/Release/example")

var arguments = ['hello', 'world'];

console.log(example.reverse_str(arguments[0]));
console.log(example.reverse_each_str(arguments));

Shell commands:

swig -javascript -node -c++ -outdir example -o example/example_nodejs.cxx example.i
cd example
node-gyp --python <path_to_python2.7> --nodedir <path_to_your_node_install_dir> configure build
cd <path_to_test.js>
./test.js

I think --nodedir <path_to_your_node_install_dir> is optional, I had to add a path to point to correct node version (I tried multiple: 6.x, 8.x...). Also the --python <path_to_python2.7> is optional if you are using not using python 3 or above.

Also, I applied patch from #968 as I had node 8 installed, that got me going with node 8, and function reverse_str works, but reverse_each_str doesn't.

Thanks for this tool & help in advance. Have used SWIG with other languages, its a great tool.

Best,

Aman

@ojwb ojwb added the Javascript label Mar 2, 2018
@frans-fuerst
Copy link

I have the same issue. I just tried v4.0.0 from master @ 67f5ade7ad03f15a2811190df899196dbf5a12fe. When I print the result of a function returning a vector of string in JavaScript I just get
_exports_StringVector {}

@microcai
Copy link

confirms

@kbobrowski
Copy link

kbobrowski commented Sep 13, 2019

it works when converting to and from swig object:

var example = require("./build/Release/example")

var args = ['hello', 'world'];

console.log(example.reverse_str(args[0]));

var cArgs = new example.VectString(args.length)
for (var i=0; i<args.length; i++) {
  cArgs.set(i, args[i]);
}

var cVectString = example.reverse_each_str(cArgs);

var jsVectString = []
for (var i=0; i<cVectString.size(); i++) {
  jsVectString.push(cVectString.get(i))
}
console.log(jsVectString);

@ojwb
Copy link
Member

ojwb commented Jun 1, 2023

When I print the result of a function returning a vector of string in JavaScript I just get
_exports_StringVector {}

That sounds like #653.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants