From c3ec8fdce3fa20a496ec50e5da4d69bfcc6faca6 Mon Sep 17 00:00:00 2001 From: Mark Gossage Date: Thu, 5 Jul 2007 01:31:51 +0000 Subject: [PATCH] [lua] Fix a bug in SWIG_ALLOC_ARRAY() improved the error messages for incorrect arguments. Changed the output of swig_type() to use the human readable form of the type, rather than the raw swig type. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9863 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- CHANGES.current | 6 ++++ Doc/Manual/Lua.html | 4 +-- .../test-suite/lua/li_std_string_runme.lua | 2 +- .../test-suite/lua/li_std_vector_runme.lua | 2 +- Lib/lua/luarun.swg | 36 ++++++++++++++----- Lib/lua/luatypemaps.swg | 18 ++++++++++ Lib/lua/typemaps.i | 2 +- Source/Modules/lua.cxx | 2 +- 8 files changed, 58 insertions(+), 14 deletions(-) diff --git a/CHANGES.current b/CHANGES.current index fdc1a3d2b..2b57da736 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -1,6 +1,12 @@ Version 1.3.32 (in progress) ============================ +07/05/2007: mgossage + [lua] Fix a bug in SWIG_ALLOC_ARRAY() + improved the error messages for incorrect arguments. + Changed the output of swig_type() to use the human readable form of the type, + rather than the raw swig type. + 07/03/2007: wsfulton [C#] Fix directors for some overloaded methods where the imtype resulted in identical methods being generated in the C# director class, eg void foo(int *) and void foo(double *) diff --git a/Doc/Manual/Lua.html b/Doc/Manual/Lua.html index 5abe6502a..1ecf058d7 100644 --- a/Doc/Manual/Lua.html +++ b/Doc/Manual/Lua.html @@ -399,7 +399,7 @@ Unlike many scripting languages, Lua has had support for pointers to C/C++ objec > print(f) userdata: 003FDA80 > print(swig_type(f)) -_p_FILE -- its a FILE* +FILE * -- its a FILE*

Lua enforces the integrity of its userdata, so it is virtually impossible to corrupt the data. But as the user of the pointer, you are responsible for freeing it, or closing any resources associated with it (just as you would in a C program). This does not apply so strictly to classes & structs (see below). One final note: if a function returns a NULL pointer, this is not encoded as a userdata, but as a Lua nil. @@ -1087,7 +1087,7 @@ __index function: 003FB698 .fn table: 003FB528

-The '.type' attribute is the string which is returned from a call to swig_type(). The '.get' and '.set' tables work in a similar manner to the modules, the main difference is the '.fn' table which also holds all the member functions. (The '__gc' function is the classes destructor function) +The '.type' attribute is the name of the class. The '.get' and '.set' tables work in a similar manner to the modules, the main difference is the '.fn' table which also holds all the member functions. (The '__gc' function is the classes destructor function)

The Lua equivalent of the code for enabling functions looks a little like this diff --git a/Examples/test-suite/lua/li_std_string_runme.lua b/Examples/test-suite/lua/li_std_string_runme.lua index c6a52fc2e..70461f7d1 100644 --- a/Examples/test-suite/lua/li_std_string_runme.lua +++ b/Examples/test-suite/lua/li_std_string_runme.lua @@ -8,7 +8,7 @@ setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable -- helper to check type function is_std_string(s) - return type(s)=='userdata' and swig_type(s)=='_p_std__string' + return type(s)=='userdata' and swig_type(s)=='std::string *' end -- std::string by value is just a Lua string diff --git a/Examples/test-suite/lua/li_std_vector_runme.lua b/Examples/test-suite/lua/li_std_vector_runme.lua index 37e268cd9..81994b92f 100644 --- a/Examples/test-suite/lua/li_std_vector_runme.lua +++ b/Examples/test-suite/lua/li_std_vector_runme.lua @@ -43,7 +43,7 @@ for i=0,3 do end for i=0,3 do - assert( swig_type(sv[i]) =='_p_Struct' and sv[i].num==i) + assert( swig_type(sv[i]) =='Struct *' and sv[i].num==i) end -- range checking diff --git a/Lib/lua/luarun.swg b/Lib/lua/luarun.swg index 0c1910ab0..f602fb235 100644 --- a/Lib/lua/luarun.swg +++ b/Lib/lua/luarun.swg @@ -106,11 +106,13 @@ typedef struct { /* helper #defines */ #define SWIG_fail {goto fail;} -#define SWIG_fail_arg(I) {lua_pushfstring(L,"argument %d incorrect/missing",I);goto fail;} -#define SWIG_fail_ptr(func_name,argnum,type) \ - {lua_pushfstring(L,"Error in %s, expected a %s at argument number %d\n",\ - func_name,(type && type->str)?type->str:"void*",argnum);\ +#define SWIG_fail_arg(func_name,argnum,type) \ + {lua_pushfstring(L,"Error in %s (arg %d), expected '%s' got '%s'",\ + func_name,argnum,type,SWIG_Lua_typename(L,argnum));\ goto fail;} +#define SWIG_fail_ptr(func_name,argnum,type) \ + SWIG_fail_arg(func_name,argnum,(type && type->str)?type->str:"void*") + #define SWIG_Lua_get_table(L,n) \ (lua_pushstring(L, n), lua_rawget(L,-2)) @@ -639,14 +641,30 @@ SWIGRUNTIME int SWIG_Lua_ConvertPacked(lua_State* L,int index,void* ptr,size_t return SWIG_ERROR; /* error */ } +/* a function to get the typestring of a piece of data */ +SWIGRUNTIME const char *SWIG_Lua_typename(lua_State *L, int tp) +{ + swig_lua_userdata* usr; + if (lua_isuserdata(L,tp)) + { + usr=(swig_lua_userdata*)lua_touserdata(L,1); /* get data */ + if (usr && usr->type && usr->type->str) + return usr->type->str; + return "userdata (unknown type)"; + } + return lua_typename(L,lua_type(L,tp)); +} + /* lua callable function to get the userdata's type */ SWIGRUNTIME int SWIG_Lua_type(lua_State* L) { - swig_lua_userdata* usr; +/* swig_lua_userdata* usr; if (!lua_isuserdata(L,1)) /* just in case */ - return 0; /* nil reply */ - usr=(swig_lua_userdata*)lua_touserdata(L,1); /* get data */ - lua_pushstring(L,usr->type->name); + /* return 0; /* nil reply */ + /*usr=(swig_lua_userdata*)lua_touserdata(L,1); /* get data */ + /*lua_pushstring(L,usr->type->name); + return 1;*/ + lua_pushstring(L,SWIG_Lua_typename(L,1)); return 1; } @@ -668,6 +686,8 @@ SWIGRUNTIME int SWIG_Lua_equal(lua_State* L) } + + /* ----------------------------------------------------------------------------- * global variable support code: class/struct typemap functions * ----------------------------------------------------------------------------- */ diff --git a/Lib/lua/luatypemaps.swg b/Lib/lua/luatypemaps.swg index 6e7838f4f..5efef21e7 100644 --- a/Lib/lua/luatypemaps.swg +++ b/Lib/lua/luatypemaps.swg @@ -348,3 +348,21 @@ $1=&temp;%} } +/* ----------------------------------------------------------------------------- + * Specials + * ----------------------------------------------------------------------------- */ +// swig::LANGUAGE_OBJ was added to allow containers of native objects +// however its rather difficult to do this in lua, as you cannot hold pointers +// to native objects (they are held in the interpreter) +// therefore for now: just ignoring this feature +#ifdef __cplusplus +%ignore swig::LANGUAGE_OBJ; + +%inline %{ +namespace swig { +typedef struct{} LANGUAGE_OBJ; +//typedef void* LANGUAGE_OBJ; +} +%} + +#endif // __cplusplus \ No newline at end of file diff --git a/Lib/lua/typemaps.i b/Lib/lua/typemaps.i index 4619c6eb6..b49bceadb 100644 --- a/Lib/lua/typemaps.i +++ b/Lib/lua/typemaps.i @@ -175,7 +175,7 @@ int SWIG_read_NAME_num_array(lua_State* L,int index,TYPE *array,int size); %{ #ifdef __cplusplus /* generic alloc/dealloc fns*/ -#define SWIG_ALLOC_ARRAY(TYPE,LEN) new (TYPE)[LEN] +#define SWIG_ALLOC_ARRAY(TYPE,LEN) new TYPE[LEN] #define SWIG_FREE_ARRAY(PTR) if(PTR){delete[] PTR;} #else #define SWIG_ALLOC_ARRAY(TYPE,LEN) (TYPE *)malloc(LEN*sizeof(TYPE)) diff --git a/Source/Modules/lua.cxx b/Source/Modules/lua.cxx index 096904ab6..e0a62d8f8 100644 --- a/Source/Modules/lua.cxx +++ b/Source/Modules/lua.cxx @@ -447,7 +447,7 @@ NEW LANGUAGE NOTE:END ************************************************/ } else { Printf(argument_check, "if(lua_gettop(L)>=%s && !%s(L,%s))", source, checkfn, source); } - Printf(argument_check, " SWIG_fail_arg(%s);\n", source); + Printf(argument_check, " SWIG_fail_arg(\"%s\",%s,\"%s\");\n", name, source, SwigType_str(pt, 0)); } /* NEW LANGUAGE NOTE:*********************************************** lua states the number of arguments passed to a function using the fn