Discussion:
[Swig-devel] Passing string from Javascript for C++ library
Nirav Vora
2015-03-20 20:18:31 UTC
Permalink
Hi,
I have created a C++ library that is exposing few public methods:
1) void getStringMessagePacket(std::string & stringMsg);
2) void getCharMessagePacket(char** charMsg);
3) std::string getRetStringMessagePacket();

I have successfully created node module from above c++ library using SWIG:
swig -lstd_string.i -c++ -javascript -v8 -DV8_VERSION=0x032873 msgLib.i

I am able to successfully get and print the string values from javascript
application for (3) method above.

Following is snapshot of javascript syntax used to invoke (1) API:
var inputData = "hello";
instanceTP.getStringMessagePacket(inputData);

However i am getting following error for (1):
Error: in method 'TPLayerClass_getStringMessagePacket', argument 2 of type
'std::string &'
at Error (native)

Here is the signature created in wrapper file :
static SwigV8ReturnValue
_wrap_transportLayerClass_getStringMessagePacket(const SwigV8Arguments
&args) {
.
.
}

I have included following in ".i" file in very beginning:

%include "std_string.i"

Any inputs/suggestions are highly appreciated.


Thanks & Regards,
NV
William S Fulton
2015-05-10 16:32:16 UTC
Permalink
Post by Nirav Vora
Hi,
1) void getStringMessagePacket(std::string & stringMsg);
2) void getCharMessagePacket(char** charMsg);
3) std::string getRetStringMessagePacket();
swig -lstd_string.i -c++ -javascript -v8 -DV8_VERSION=0x032873 msgLib.i
I am able to successfully get and print the string values from javascript
application for (3) method above.
var inputData = "hello";
instanceTP.getStringMessagePacket(inputData);
Error: in method 'TPLayerClass_getStringMessagePacket', argument 2 of type
'std::string &'
at Error (native)
static SwigV8ReturnValue
_wrap_transportLayerClass_getStringMessagePacket(const SwigV8Arguments
&args) {
.
.
}
%include "std_string.i"
Any inputs/suggestions are highly appreciated.
I don't know Javascript particularly well, but in most of the SWIG
target languages, strings are immutable and std::string& will be
wrapped as a pointer to std::string. You would need to provide an
instance of a std::string class (not a JS string) in order to use it.
In order to use 1) in the way you expect, you'd need to pass in some
sort of Javascript string type that is not immutable and provide the
typemaps to marshal this type to the C++ std::string and back again.
There is an example doing this for Java's StringBuffer class in
Examples/java/typemap, but Java and Javascript typemaps work quite
differently, so not much is of relevance there apart from the general
idea. If any of the SWIG Javascript developers see this, they could
probably suggest further help. Similarly for output string types like
char** parameters in 2).

Workarounds are to write a simple C++ function which effectively
provides 3), such as:

std::string getStringMessagePacketWrapper()
{
std::string s;
getStringMessagePacket(s);
return s;
}

William

Continue reading on narkive:
Loading...