In a situation quite reminiscent of this query, I am aiming to wrap a function using SWIG that accepts a map of strings to strings:
void foo(std::map<std::string, std::string> const& args);
Creating an alias for the map suffices for Python:
namespace std {
%template(map_string_string) map<string, string>;
}
The code generator will automatically generate and utilize a wrapper function map_string_string
.
my_module.foo({'a': 'b', 'c', 'd'})
This call will be correctly executed, with values not fitting the signature omitted.
How can I achieve this in JavaScript?
I attempted the same (naturally), and while the wrapper is generated, attempting to invoke foo
like so:
my_module.foo({'a':'b', 'c':'d'});
Results in:
/path/to/example.js:3
my_module.foo({'a':'b', 'c':'d'});
^
Error: in method 'foo', argument 1 of type 'std::map< std::string,std::string > const &'
at Object.<anonymous> (/path/to/example.js:8:7)
...
Even trying to call the wrapper function map_string_string
results in this error.
Is there another way to handle "string maps" in JavaScript? Or perhaps an easy method to wrap an associative array in Swig?
Edit: In the interest of completeness, the source files used are included below:
...In both cases - Python and JavaScript - api.foo()
is working as expected. However, api.bar()
runs successfully in Python but throws the mentioned error in JavaScript.