Add std::string support to v8 typemaps.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/oliverb-javascript-v8@13808 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Oliver Buchtala 2012-09-08 01:10:57 +00:00
commit 84e5476147
2 changed files with 69 additions and 2 deletions

68
Lib/javascript/v8/std_string.i Executable file
View file

@ -0,0 +1,68 @@
/* -----------------------------------------------------------------------------
* std_string.i
*
* Typemaps for std::string and const std::string&
* These are mapped to a JSCore String and are passed around by value.
*
* To use non-const std::string references use the following %apply. Note
* that they are passed by value.
* %apply const std::string & {std::string &};
* ----------------------------------------------------------------------------- */
%{
#include <string>
%}
namespace std {
%naturalvar string;
class string;
// string
%typemap(in) string
%{
if(!$input->IsString()) {
// TODO: Throw exception?
return NULL;
}
size_t $1_strsize = js_str->Utf8Length();
char* 1_cstr = new char[1_strsize];
js_str->WriteUtf8(1_cstr, 1_strsize);
$1 = std::string($1_cstr);
%}
%typemap(out) string %{
$result = v8::String::New($1.c_str(), $1.size());
%}
%typemap(freearg) string
%{%}
// const string &
%typemap(in) const string &
%{
if(!$input->IsString()) {
// TODO: Throw exception?
return NULL;
}
size_t $1_strsize = js_str->Utf8Length();
char* 1_cstr = new char[1_strsize];
js_str->WriteUtf8(1_cstr, 1_strsize);
$1 = newstd::string($1_cstr);
%}
%typemap(out) const string & %{
$result = v8::String::New($1.c_str(), $1.size());
%}
%typemap(freearg) const string & //TODO: Not working: A memory leak
%{ free($1_cstr); %}
//%typemap(typecheck) const string & = char *;
}