Fix std_string.i which generated compile errors in certain cases.
This commit is contained in:
parent
fb9c4955fb
commit
7fffd801e4
1 changed files with 30 additions and 43 deletions
|
|
@ -11,6 +11,30 @@
|
|||
|
||||
%{
|
||||
#include <string>
|
||||
|
||||
std::string SWIGJSC_valueToString(JSContextRef context, JSValueRef value) {
|
||||
JSStringRef jsstring = JSValueToStringCopy(context, value, /* JSValueRef *exception */ 0);
|
||||
unsigned int length = JSStringGetLength(jsstring);
|
||||
char *cstr = new char[length+1];
|
||||
JSStringGetUTF8CString(jsstring, cstr, length);
|
||||
|
||||
// create a copy
|
||||
std::string result(cstr);
|
||||
|
||||
JSStringRelease(jsstring);
|
||||
delete[] cstr;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
JSValueRef SWIGJSC_stringToValue(JSContextRef context, const std::string& s)
|
||||
{
|
||||
JSValueRef result;
|
||||
JSStringRef jsstring = JSStringCreateWithUTF8CString(s.c_str());
|
||||
result = JSValueMakeString(context, jsstring);
|
||||
JSStringRelease(jsstring);
|
||||
return result;
|
||||
}
|
||||
%}
|
||||
|
||||
namespace std {
|
||||
|
|
@ -21,51 +45,14 @@ class string;
|
|||
|
||||
// string
|
||||
|
||||
%typemap(in) string
|
||||
%{ if (!$input) {
|
||||
// TODO: Throw exception?
|
||||
return NULL;
|
||||
}
|
||||
JSStringRef $1_str = JSValueToStringCopy(context, $input, NULL);
|
||||
size_t $1_strsize = JSStringGetMaximumUTF8CStringSize($1_str);
|
||||
char* $1_cstr = (char *)malloc($1_strsize * sizeof(char));
|
||||
JSStringGetUTF8CString($1_str, $1_cstr, $1_strsize);
|
||||
$1 = std::string($1_cstr);
|
||||
%typemap(in) string, const string&
|
||||
%{
|
||||
$1 = SWIGJSC_valueToString(context, $input);
|
||||
%}
|
||||
|
||||
%typemap(out) string %{
|
||||
JSStringRef jsstring = JSStringCreateWithUTF8CString($1.c_str());
|
||||
$result = JSValueMakeString(context, jsstring);
|
||||
JSStringRelease(jsstring);
|
||||
%typemap(out) string, const string&
|
||||
%{
|
||||
$result = SWIGJSC_stringToValue(context, $1);
|
||||
%}
|
||||
|
||||
%typemap(freearg) string //TODO: Not working: A memory leak
|
||||
%{ free($1_cstr); %}
|
||||
|
||||
//%typemap(typecheck) string = char *;
|
||||
|
||||
// const string &
|
||||
%typemap(in) const string &
|
||||
%{ if (!$input) {
|
||||
// TODO: Throw exception?
|
||||
return NULL;
|
||||
}
|
||||
JSStringRef $1_str = JSValueToStringCopy(context, $input, NULL);
|
||||
size_t $1_strsize = JSStringGetMaximumUTF8CStringSize($1_str);
|
||||
char* $1_cstr = (char *)malloc($1_strsize * sizeof(char));
|
||||
JSStringGetUTF8CString($1_str, $1_cstr, $1_strsize);
|
||||
$1 = new std::string($1_cstr);
|
||||
%}
|
||||
|
||||
%typemap(out) const string & %{
|
||||
JSStringRef jsstring = JSStringCreateWithUTF8CString($1.c_str());
|
||||
$result = JSValueMakeString(context, jsstring);
|
||||
JSStringRelease(jsstring);
|
||||
%}
|
||||
|
||||
%typemap(freearg) const string & //TODO: Not working: A memory leak
|
||||
%{ free($1_cstr); %}
|
||||
|
||||
//%typemap(typecheck) const string & = char *;
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue