Fix std::string support for v8.

This commit is contained in:
Oliver Buchtala 2013-01-08 21:48:22 +01:00
commit 9d630ab930
3 changed files with 60 additions and 59 deletions

View file

@ -1,12 +1,10 @@
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* std_string.i * std_string.i
* *
* Typemaps for std::string and const std::string& * Typemaps for 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:
*
* To use non-const std::string references use the following %apply. Note
* that they are passed by value.
* %apply const std::string & {std::string &}; * %apply const std::string & {std::string &};
*
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
%{ %{

View file

@ -47,9 +47,6 @@ SWIG_FromCharPtrAndSize(const char* carray, size_t size)
if (carray) { if (carray) {
if (size > INT_MAX) { if (size > INT_MAX) {
// TODO: handle extra long strings // TODO: handle extra long strings
//swig_type_info* pchar_descriptor = SWIG_pchar_descriptor();
//return pchar_descriptor ?
// SWIG_InternalNewPointerObj(%const_cast(carray,char *), pchar_descriptor, 0) : SWIG_Py_Void();
return v8::Undefined(); return v8::Undefined();
} else { } else {
v8::Handle<v8::String> js_str = v8::String::New(carray, size); v8::Handle<v8::String> js_str = v8::String::New(carray, size);

View file

@ -1,68 +1,74 @@
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* std_string.i * std_string.i
* *
* Typemaps for std::string and const std::string& * 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 * To use non-const std::string references use the following %apply:
* that they are passed by value.
* %apply const std::string & {std::string &}; * %apply const std::string & {std::string &};
*
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
%{ %{
#include <string> #include <string>
%} %}
namespace std { %fragment("SWIGV8_valueToString", "header", fragment="SWIG_AsCharPtrAndSize") {
std::string* SWIGV8_valueToStringPtr(v8::Handle<v8::Value> val) {
int alloc;
size_t size;
char* chars;
int res = SWIG_AsCharPtrAndSize(val, &chars, &size, &alloc);
if(res != SWIG_OK) {
v8::ThrowException(v8::Exception::TypeError(v8::String::New("Could not convert to string.")));
return 0;
}
// copies the data (again)
std::string *str = new std::string(chars);
if (alloc) delete[] chars;
return str;
}
}
%fragment("SWIGV8_stringToValue", "header", fragment="SWIG_FromCharPtrAndSize") {
v8::Handle<v8::Value> SWIGV8_stringToValue(const std::string &str) {
return SWIG_FromCharPtrAndSize(str.c_str(), str.length());
}
}
namespace std {
%naturalvar string; %naturalvar string;
class string; class string;
// string %typemap(in, fragment="SWIGV8_valueToString") string (std::string* tmp)
%typemap(in) string
%{ %{
if(!$input->IsString()) { tmp = SWIGV8_valueToStringPtr($input);
// TODO: Throw exception? $1 = *tmp;
return NULL; delete tmp;
}
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 %{ %typemap(in, fragment="SWIGV8_valueToString") const string &
$result = v8::String::New($1.c_str(), $1.size());
%}
%typemap(freearg) string
%{%}
// const string &
%typemap(in) const string &
%{ %{
$1 = SWIGV8_valueToStringPtr($input);
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 & %{ %typemap(freearg) const string &
$result = v8::String::New($1.c_str(), $1.size()); %{
delete $1;
%} %}
%typemap(freearg) const string & //TODO: Not working: A memory leak %typemap(out, fragment="SWIGV8_stringToValue") string
%{ free($1_cstr); %} %{
$result = SWIGV8_stringToValue($1);
%}
//%typemap(typecheck) const string & = char *; %typemap(out, fragment="SWIGV8_stringToValue") const string &
%{
$result = SWIGV8_stringToValue($1);
%}
} }