fix(lua): make testudata compatible with Lua 5.1

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2013-11-05 11:35:09 -08:00
parent 19c4a3b626
commit 055cc7f957
2 changed files with 12 additions and 7 deletions

View file

@ -30,13 +30,18 @@ void setfuncs(lua_State * L, luaL_Reg const * l, int nup) {
/**
\brief luaL_testudata replacement.
*/
bool testudata(lua_State * L, unsigned idx, char const * mt) {
try {
luaL_checkudata(L, idx, mt);
return true;
} catch (...) {
return false;
bool testudata(lua_State * L, int ud, char const * tname) {
void * p = lua_touserdata(L, ud);
if (p != nullptr) {
if (lua_getmetatable(L, ud)) {
luaL_getmetatable(L, tname);
if (!lua_rawequal(L, -1, -2))
p = nullptr;
lua_pop(L, 2);
return p;
}
}
return nullptr; // value is not a userdata with a metatable
}
int safe_function_wrapper(lua_State * L, lua_CFunction f){

View file

@ -8,7 +8,7 @@ Author: Leonardo de Moura
#include <lua.hpp>
namespace lean {
void setfuncs(lua_State * L, luaL_Reg const * l, int nup);
bool testudata(lua_State * L, unsigned idx, char const * mt);
bool testudata(lua_State * L, int idx, char const * mt);
/**
\brief Wrapper for invoking function f, and catching Lean exceptions.
*/