From 2fef8c772b5d9c175941d4c8a64dedf736567466 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 11 Feb 2003 13:32:31 -0200 Subject: [PATCH] auxiliary function to check userdata with types --- lauxlib.c | 15 ++++++++++++++- lauxlib.h | 3 ++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index adc0f905..8fb01bc7 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.93 2003/01/27 13:46:16 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.94 2003/02/11 09:44:38 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -121,6 +121,19 @@ LUALIB_API void luaL_checkany (lua_State *L, int narg) { } +LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { + if (!lua_getmetatable(L, ud)) return NULL; /* no metatable? */ + lua_pushstring(L, tname); + lua_rawget(L, LUA_REGISTRYINDEX); + if (!lua_rawequal(L, -1, -2)) { + lua_pop(L, 2); + return NULL; + } + lua_pop(L, 2); + return lua_touserdata(L, ud); +} + + LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) { const char *s = lua_tostring(L, narg); if (!s) tag_error(L, narg, LUA_TSTRING); diff --git a/lauxlib.h b/lauxlib.h index fb2b0ab6..cdb313fd 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.56 2003/01/17 15:28:09 roberto Exp roberto $ +** $Id: lauxlib.h,v 1.57 2003/01/27 13:46:16 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -33,6 +33,7 @@ LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *e); LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *e); LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname); LUALIB_API int luaL_argerror (lua_State *L, int numarg, const char *extramsg); +LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname); LUALIB_API const char *luaL_checklstring (lua_State *L, int numArg, size_t *l); LUALIB_API const char *luaL_optlstring (lua_State *L, int numArg, const char *def, size_t *l);