[Lua] Add support for Lua 5.2 (patch SF#3514593 from Miles Bader)

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12968 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Olly Betts 2012-04-05 04:29:11 +00:00
commit e3eb54594e
18 changed files with 73 additions and 20 deletions

View file

@ -15,6 +15,24 @@ extern "C" {
#include <stdlib.h> /* for malloc */
#include <assert.h> /* for a few sanity tests */
/* -----------------------------------------------------------------------------
* compatibility defines
* ----------------------------------------------------------------------------- */
/* History of Lua C API length functions: In Lua 5.0 (and before?)
there was "lua_strlen". In Lua 5.1, this was renamed "lua_objlen",
but a compatibility define of "lua_strlen" was added. In Lua 5.2,
this function was again renamed, to "lua_rawlen" (to emphasize that
it doesn't call the "__len" metamethod), and the compatibility
define of lua_strlen was removed. All SWIG uses have been updated
to "lua_rawlen", and we add our own defines of that here for older
versions of Lua. */
#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 501
# define lua_rawlen lua_strlen
#elif LUA_VERSION_NUM == 501
# define lua_rawlen lua_objlen
#endif
/* -----------------------------------------------------------------------------
* global swig types
* ----------------------------------------------------------------------------- */

View file

@ -30,7 +30,11 @@ SWIGEXPORT int SWIG_init(lua_State* L) /* default Lua action */
#if (SWIG_LUA_TARGET != SWIG_LUA_FLAVOR_ELUAC) /* valid for both Lua and eLua */
int i;
/* start with global table */
#ifdef LUA_RIDX_GLOBALS
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
#else
lua_pushvalue(L,LUA_GLOBALSINDEX);
#endif
/* SWIG's internal initalisation */
SWIG_InitializeModule((void*)L);
SWIG_PropagateClientData();

View file

@ -298,7 +298,7 @@ parmeters match which function
// special check for a char (string of length 1)
%typecheck(SWIG_TYPECHECK_CHAR,fragment="SWIG_lua_isnilstring") char, const char& {
$1 = SWIG_lua_isnilstring(L,$input) && (lua_strlen(L,$input)==1);
$1 = SWIG_lua_isnilstring(L,$input) && (lua_rawlen(L,$input)==1);
}
%typecheck(SWIG_TYPECHECK_STRING,fragment="SWIG_lua_isnilstring") char *, char[] {

View file

@ -41,18 +41,18 @@ but
Similarly for getting the string
$1 = (char*)lua_tostring(L, $input);
becomes
$1.assign(lua_tostring(L,$input),lua_strlen(L,$input));
$1.assign(lua_tostring(L,$input),lua_rawlen(L,$input));
Not using: lua_tolstring() as this is only found in Lua 5.1 & not 5.0.2
*/
%typemap(in,checkfn="lua_isstring") std::string
%{$1.assign(lua_tostring(L,$input),lua_strlen(L,$input));%}
%{$1.assign(lua_tostring(L,$input),lua_rawlen(L,$input));%}
%typemap(out) std::string
%{ lua_pushlstring(L,$1.data(),$1.size()); SWIG_arg++;%}
%typemap(in,checkfn="lua_isstring") const std::string& (std::string temp)
%{temp.assign(lua_tostring(L,$input),lua_strlen(L,$input)); $1=&temp;%}
%{temp.assign(lua_tostring(L,$input),lua_rawlen(L,$input)); $1=&temp;%}
%typemap(out) const std::string&
%{ lua_pushlstring(L,$1->data(),$1->size()); SWIG_arg++;%}

View file

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