From 055cc7f9576aed98e84455357dc1a43535b3bdc8 Mon Sep 17 00:00:00 2001 From: Leonardo de Moura Date: Tue, 5 Nov 2013 11:35:09 -0800 Subject: [PATCH] fix(lua): make testudata compatible with Lua 5.1 Signed-off-by: Leonardo de Moura --- src/bindings/lua/util.cpp | 17 +++++++++++------ src/bindings/lua/util.h | 2 +- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/bindings/lua/util.cpp b/src/bindings/lua/util.cpp index f25feedbd4..ebbc448567 100644 --- a/src/bindings/lua/util.cpp +++ b/src/bindings/lua/util.cpp @@ -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){ diff --git a/src/bindings/lua/util.h b/src/bindings/lua/util.h index d401c088d1..6c96ab3fdb 100644 --- a/src/bindings/lua/util.h +++ b/src/bindings/lua/util.h @@ -8,7 +8,7 @@ Author: Leonardo de Moura #include 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. */