Lua - Fix char pointers, wchar_t pointers and char arrays so that nil can be passed as a valid value

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@11963 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2010-04-02 19:48:20 +00:00
commit 662084aa06
5 changed files with 41 additions and 10 deletions

View file

@ -1,6 +1,10 @@
Version 2.0.0 (in progress)
============================
2010-04-02: wsfulton
[Lua] Fix char pointers, wchar_t pointers and char arrays so that nil can be passed as a
valid value. Bug reported by Gedalia Pasternak.
2010-04-01: wsfulton
Numerous subtle typemap matching rule fixes when using the default type. The typemap
matching rules are to take a type and find the best default typemap (SWIGTYPE, SWIGTYPE* etc),

View file

@ -109,6 +109,12 @@ bool SetConstCharArrayStaticString(const char str[], unsigned int number) {
char *CharPingPong(char *str) {
return str;
}
char *CharArrayPingPong(char abcstr[]) {
return abcstr;
}
char *CharArrayDimsPingPong(char abcstr[16]) {
return abcstr;
}
// variables
char *global_char = NULL;

View file

@ -0,0 +1,12 @@
require("import") -- the import fn
import("char_strings") -- import code
assert (char_strings.CharPingPong("hi there") == "hi there")
assert (char_strings.CharPingPong(nil) == nil)
assert (char_strings.CharArrayPingPong("hi there") == "hi there")
assert (char_strings.CharArrayPingPong(nil) == nil)
assert (char_strings.CharArrayDimsPingPong("hi there") == "hi there")
assert (char_strings.CharArrayDimsPingPong(nil) == nil)

View file

@ -90,10 +90,19 @@ temp=($basetype)lua_tonumber(L,$input); $1=&temp;%}
%{ lua_pushboolean(L,(int)((*$1)!=0)); SWIG_arg++;%}
// strings (char* and char[])
%typemap(in,checkfn="lua_isstring") const char*, char*
%fragment("SWIG_lua_isnilstring", "header") {
int SWIG_lua_isnilstring(lua_State *L, int idx) {
int ret = lua_isstring(L, idx);
if (!ret)
ret = lua_isnil(L, idx);
return ret;
}
}
%typemap(in,checkfn="SWIG_lua_isnilstring",fragment="SWIG_lua_isnilstring") const char*, char*
%{$1 = ($ltype)lua_tostring(L, $input);%}
%typemap(in,checkfn="lua_isstring") const char[ANY], char[ANY]
%typemap(in,checkfn="SWIG_lua_isnilstring",fragment="SWIG_lua_isnilstring") const char[ANY], char[ANY]
%{$1 = ($ltype)lua_tostring(L, $input);%}
%typemap(out) const char*, char*
@ -105,14 +114,14 @@ temp=($basetype)lua_tonumber(L,$input); $1=&temp;%}
// char's
// currently treating chars as small strings, not as numbers
// (however signed & unsigned char's are numbers...)
%typemap(in,checkfn="lua_isstring") char
%typemap(in,checkfn="SWIG_lua_isnilstring",fragment="SWIG_lua_isnilstring") char
%{$1 = (lua_tostring(L, $input))[0];%}
%typemap(out) char
%{ lua_pushfstring(L,"%c",$1); SWIG_arg++;%}
// by const ref
%typemap(in,checkfn="lua_isstring") const char& (char temp)
%typemap(in,checkfn="SWIG_lua_isnilstring",fragment="SWIG_lua_isnilstring") const char& (char temp)
%{temp = (lua_tostring(L, $input))[0]; $1=&temp;%}
%typemap(out) const char&
@ -288,12 +297,12 @@ parmeters match which function
}
// special check for a char (string of length 1)
%typecheck(SWIG_TYPECHECK_CHAR) char, const char& {
$1 = lua_isstring(L,$input) && (lua_strlen(L,$input)==1);
%typecheck(SWIG_TYPECHECK_CHAR,fragment="SWIG_lua_isnilstring") char, const char& {
$1 = SWIG_lua_isnilstring(L,$input) && (lua_strlen(L,$input)==1);
}
%typecheck(SWIG_TYPECHECK_STRING) char *, char[] {
$1 = lua_isstring(L,$input);
%typecheck(SWIG_TYPECHECK_STRING,fragment="SWIG_lua_isnilstring") char *, char[] {
$1 = SWIG_lua_isnilstring(L,$input);
}
%typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE *, SWIGTYPE [] {

View file

@ -28,13 +28,13 @@ wchar_t* str2wstr(const char* str, int len)
}
%}
%typemap( in, checkfn="lua_isstring" ) wchar_t*
%typemap(in, checkfn="SWIG_lua_isnilstring", fragment="SWIG_lua_isnilstring") wchar_t*
%{
$1 = str2wstr(lua_tostring( L, $input ),lua_strlen( L, $input ));
if ($1==0) {lua_pushfstring(L,"Error in converting to wchar (arg %d)",$input);goto fail;}
%}
%typemap( freearg ) wchar_t*
%typemap(freearg) wchar_t*
%{
free($1);
%}