From e3eb54594ef57f15edce268f5d27d673db6861a9 Mon Sep 17 00:00:00 2001 From: Olly Betts Date: Thu, 5 Apr 2012 04:29:11 +0000 Subject: [PATCH] [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 --- CHANGES.current | 3 +++ .../test-suite/lua/abstract_access_runme.lua | 4 +++- Examples/test-suite/lua/cpp_basic_runme.lua | 4 +++- Examples/test-suite/lua/disown_runme.lua | 4 +++- Examples/test-suite/lua/enums_runme.lua | 4 +++- .../test-suite/lua/exception_order_runme.lua | 4 +++- .../test-suite/lua/import_nomodule_runme.lua | 4 +++- Examples/test-suite/lua/li_carrays_runme.lua | 4 +++- .../test-suite/lua/li_std_string_runme.lua | 4 +++- Examples/test-suite/lua/li_typemaps_runme.lua | 4 +++- .../test-suite/lua/operator_overload_runme.lua | 4 +++- Lib/lua/luarun.swg | 18 ++++++++++++++++++ Lib/lua/luaruntime.swg | 4 ++++ Lib/lua/luatypemaps.swg | 2 +- Lib/lua/std_string.i | 6 +++--- Lib/lua/wchar.i | 2 +- Source/Modules/lua.cxx | 2 +- configure.in | 16 ++++++++++++---- 18 files changed, 73 insertions(+), 20 deletions(-) diff --git a/CHANGES.current b/CHANGES.current index 5d6d46bb1..aaadbad7c 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -5,6 +5,9 @@ See the RELEASENOTES file for a summary of changes in each release. Version 2.0.5 (in progress) =========================== +2012-04-05: olly + [Lua] Add support for Lua 5.2 (patch SF#3514593 from Miles Bader) + 2012-03-26: xavier98 [octave] Apply patch #3425993 from jgillis: add extra logic to the octave_swig_type::dims(void) method: it checks if the user has defined a __dims__ method and uses this in stead of returning (1,1) [octave] Apply patch #3424833 from jgillis: make is_object return true for swig types diff --git a/Examples/test-suite/lua/abstract_access_runme.lua b/Examples/test-suite/lua/abstract_access_runme.lua index b9f44cf5a..c1d836136 100644 --- a/Examples/test-suite/lua/abstract_access_runme.lua +++ b/Examples/test-suite/lua/abstract_access_runme.lua @@ -2,7 +2,9 @@ require("import") -- the import fn import("abstract_access") -- import code -- catch "undefined" global variables -setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) +local env = _ENV -- Lua 5.2 +if not env then env = getfenv () end -- Lua 5.1 +setmetatable(env, {__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) -- trying to instantiate pure virual classes -- should fail diff --git a/Examples/test-suite/lua/cpp_basic_runme.lua b/Examples/test-suite/lua/cpp_basic_runme.lua index b63b89cdc..02f88479b 100644 --- a/Examples/test-suite/lua/cpp_basic_runme.lua +++ b/Examples/test-suite/lua/cpp_basic_runme.lua @@ -3,7 +3,9 @@ import("cpp_basic") -- import code cb=cpp_basic -- renaming import -- catch "undefined" global variables -setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) +local env = _ENV -- Lua 5.2 +if not env then env = getfenv () end -- Lua 5.1 +setmetatable(env, {__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) f=cb.Foo(4) assert(f.num==4) diff --git a/Examples/test-suite/lua/disown_runme.lua b/Examples/test-suite/lua/disown_runme.lua index 270758990..72115bbc7 100644 --- a/Examples/test-suite/lua/disown_runme.lua +++ b/Examples/test-suite/lua/disown_runme.lua @@ -2,7 +2,9 @@ require("import") -- the import fn import("disown") -- import code -- catch "undefined" global variables -setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) +local env = _ENV -- Lua 5.2 +if not env then env = getfenv () end -- Lua 5.1 +setmetatable(env, {__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) for x=0,100 do a=disown.A() diff --git a/Examples/test-suite/lua/enums_runme.lua b/Examples/test-suite/lua/enums_runme.lua index f96331c9d..6211581fe 100644 --- a/Examples/test-suite/lua/enums_runme.lua +++ b/Examples/test-suite/lua/enums_runme.lua @@ -2,7 +2,9 @@ require("import") -- the import fn import("enums") -- import lib -- catch "undefined" global variables -setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) +local env = _ENV -- Lua 5.2 +if not env then env = getfenv () end -- Lua 5.1 +setmetatable(env, {__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) -- check values assert(enums.CSP_ITERATION_FWD==0) diff --git a/Examples/test-suite/lua/exception_order_runme.lua b/Examples/test-suite/lua/exception_order_runme.lua index e5caa838d..d554ae40d 100644 --- a/Examples/test-suite/lua/exception_order_runme.lua +++ b/Examples/test-suite/lua/exception_order_runme.lua @@ -4,7 +4,9 @@ import("exception_order") -- import lib into global eo=exception_order --alias -- catching undefined variables -setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) +local env = _ENV -- Lua 5.2 +if not env then env = getfenv () end -- Lua 5.1 +setmetatable(env, {__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) a = eo.A() diff --git a/Examples/test-suite/lua/import_nomodule_runme.lua b/Examples/test-suite/lua/import_nomodule_runme.lua index 947acebf5..cd359a62e 100644 --- a/Examples/test-suite/lua/import_nomodule_runme.lua +++ b/Examples/test-suite/lua/import_nomodule_runme.lua @@ -2,7 +2,9 @@ require("import") -- the import fn import("import_nomodule") -- import code -- catch "undefined" global variables -setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) +local env = _ENV -- Lua 5.2 +if not env then env = getfenv () end -- Lua 5.1 +setmetatable(env, {__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) f = import_nomodule.create_Foo() import_nomodule.test1(f,42) diff --git a/Examples/test-suite/lua/li_carrays_runme.lua b/Examples/test-suite/lua/li_carrays_runme.lua index c54e36acc..285d7b32a 100644 --- a/Examples/test-suite/lua/li_carrays_runme.lua +++ b/Examples/test-suite/lua/li_carrays_runme.lua @@ -5,7 +5,9 @@ import("li_carrays") -- import code for k,v in pairs(li_carrays) do _G[k]=v end -- catch "undefined" global variables -setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) +local env = _ENV -- Lua 5.2 +if not env then env = getfenv () end -- Lua 5.1 +setmetatable(env, {__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) -- Testing for %array_functions(int,intArray) ary = new_intArray(2) diff --git a/Examples/test-suite/lua/li_std_string_runme.lua b/Examples/test-suite/lua/li_std_string_runme.lua index 70461f7d1..956bea0fc 100644 --- a/Examples/test-suite/lua/li_std_string_runme.lua +++ b/Examples/test-suite/lua/li_std_string_runme.lua @@ -4,7 +4,9 @@ import("li_std_string") -- import lib for k,v in pairs(li_std_string) do _G[k]=v end -- move to global -- catch "undefined" global variables -setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) +local env = _ENV -- Lua 5.2 +if not env then env = getfenv () end -- Lua 5.1 +setmetatable(env, {__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) -- helper to check type function is_std_string(s) diff --git a/Examples/test-suite/lua/li_typemaps_runme.lua b/Examples/test-suite/lua/li_typemaps_runme.lua index 77aeb54e4..fd7764cf3 100644 --- a/Examples/test-suite/lua/li_typemaps_runme.lua +++ b/Examples/test-suite/lua/li_typemaps_runme.lua @@ -2,7 +2,9 @@ require("import") -- the import fn import("li_typemaps") -- import code -- catch "undefined" global variables -setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) +local env = _ENV -- Lua 5.2 +if not env then env = getfenv () end -- Lua 5.1 +setmetatable(env, {__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) -- Check double INPUT typemaps assert(li_typemaps.in_double(22.22) == 22.22) diff --git a/Examples/test-suite/lua/operator_overload_runme.lua b/Examples/test-suite/lua/operator_overload_runme.lua index 1610c1705..983daa1e5 100644 --- a/Examples/test-suite/lua/operator_overload_runme.lua +++ b/Examples/test-suite/lua/operator_overload_runme.lua @@ -8,7 +8,9 @@ for k,v in pairs(operator_overload) do _G[k]=v end -- move to global Op_sanity_check() -- catching undefined variables -setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) +local env = _ENV -- Lua 5.2 +if not env then env = getfenv () end -- Lua 5.1 +setmetatable(env, {__index=function (t,i) error("undefined global variable `"..i.."'",2) end}) -- test routine: a=Op() diff --git a/Lib/lua/luarun.swg b/Lib/lua/luarun.swg index 2baa9666d..6fe0bccce 100644 --- a/Lib/lua/luarun.swg +++ b/Lib/lua/luarun.swg @@ -15,6 +15,24 @@ extern "C" { #include /* for malloc */ #include /* 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 * ----------------------------------------------------------------------------- */ diff --git a/Lib/lua/luaruntime.swg b/Lib/lua/luaruntime.swg index f96f3d19f..9cf6a14a4 100644 --- a/Lib/lua/luaruntime.swg +++ b/Lib/lua/luaruntime.swg @@ -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(); diff --git a/Lib/lua/luatypemaps.swg b/Lib/lua/luatypemaps.swg index 13cd66f4c..f3ea6f14c 100644 --- a/Lib/lua/luatypemaps.swg +++ b/Lib/lua/luatypemaps.swg @@ -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[] { diff --git a/Lib/lua/std_string.i b/Lib/lua/std_string.i index 92f27d738..64c0957b1 100644 --- a/Lib/lua/std_string.i +++ b/Lib/lua/std_string.i @@ -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++;%} diff --git a/Lib/lua/wchar.i b/Lib/lua/wchar.i index 92f7ed0e2..14a94257f 100644 --- a/Lib/lua/wchar.i +++ b/Lib/lua/wchar.i @@ -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;} %} diff --git a/Source/Modules/lua.cxx b/Source/Modules/lua.cxx index 3623b80ff..ba121d28c 100644 --- a/Source/Modules/lua.cxx +++ b/Source/Modules/lua.cxx @@ -331,7 +331,7 @@ public: Printf(s_dot_set, "\nconst LUA_REG_TYPE dot_set[] = {\n"); } } else { - Printf(s_cmd_tab, "\nstatic const struct luaL_reg swig_commands[] = {\n"); + Printf(s_cmd_tab, "\nstatic const struct luaL_Reg swig_commands[] = {\n"); Printf(s_var_tab, "\nstatic swig_lua_var_info swig_variables[] = {\n"); Printf(s_const_tab, "\nstatic swig_lua_const_info swig_constants[] = {\n"); Printf(f_wrappers, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"); diff --git a/configure.in b/configure.in index 9e88e7eda..67c3bc982 100644 --- a/configure.in +++ b/configure.in @@ -1890,7 +1890,12 @@ else # can we find lua? if test "x$LUABIN" = xyes; then - AC_PATH_PROG(LUABIN, lua) + # We look for a versioned Lua binary first, as there can be + # multiple versions of Lua installed on some systems (like Debian). + # The search order should match the include-file and library search + # orders below (a Lua shared library built for one version may not + # work with a Lua binary of a different version). + AC_PATH_PROGS(LUABIN, [lua5.2 lua5.1 lua]) fi # check version: we need Lua 5.x @@ -1938,8 +1943,11 @@ else # if we didn't get it, going to have to look elsewhere (the hard way) if test -z "$LUA_OK"; then AC_MSG_CHECKING(for lua.h in other locations) - # note: ubuntu seems to like /usr/include/lua5.1/lua.h - dirs="/usr/include/lua* /usr/local/include" + # note: Debian/Ubuntu seem to like /usr/include/lua5.1/lua.h + # The ordering of the include directories to search should match + # the ordering of libraries to search in the library test below. + inc=/usr/include + dirs="$inc/lua5.2 $inc/lua5.1 $inc/lua51 $inc/lua5.0 $inc/lua50 /usr/local/include" for i in $dirs; do #echo "$i" if test -r $i/lua.h; then @@ -1962,7 +1970,7 @@ lua_save_LIBS=$LIBS # the code seems to disrupt LIBS, so saving if test -n "$LUALIB"; then AC_CHECK_FILE($LUALIB/liblua.a,[LUALINK="-L$LUALIB -llua"],[LUABIN=]) else - AC_SEARCH_LIBS(lua_close, [lua lua51 lua5.1 lua50 lua5.0], [LUALINK="-l$ac_lib"],[LUABIN=]) + AC_SEARCH_LIBS(lua_close, [lua lua5.2 lua5.1 lua51 lua5.0 lua50], [LUALINK="-l$ac_lib"],[LUABIN=]) fi # adding lualib for lua 5.0