From cd99bbcd0d88e7b5864ea9c9634ce8abd9bdedda Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 2 Apr 2002 17:42:49 -0300 Subject: better support for new libraries --- lauxlib.c | 33 +++++++++++++++++++++++++++------ lauxlib.h | 7 ++++--- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index efa313f7..f0345be5 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.62 2002/03/20 12:54:08 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.63 2002/03/27 15:30:41 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -116,20 +116,41 @@ LUALIB_API lua_Number luaL_opt_number (lua_State *L, int narg, lua_Number def) { } -LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l) { +LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { + if (!lua_getmetatable(L, obj)) /* no metatable? */ + return 0; + lua_pushstring(L, event); + lua_rawget(L, -2); + if (lua_isnil(L, -1)) { + lua_pop(L, 2); /* remove metatable and metafield */ + return 0; + } + lua_pushvalue(L, obj); + lua_rawcall(L, 1, 1); + return 1; +} + + +LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int nup) { for (; l->name; l++) { + int i; lua_pushstring(L, l->name); - lua_pushcfunction(L, l->func); - lua_settable(L, -3); + for (i=0; ifunc, nup); + lua_settable(L, -(nup+3)); } + lua_pop(L, nup); } LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname, - const luaL_reg *l) { + const luaL_reg *l, int nup) { lua_pushstring(L, libname); + lua_insert(L, -(nup+1)); lua_newtable(L); - luaL_openlib(L, l); + lua_insert(L, -(nup+1)); + luaL_openlib(L, l, nup); lua_settable(L, LUA_GLOBALSINDEX); } diff --git a/lauxlib.h b/lauxlib.h index 250c7da9..6af70d44 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.42 2002/02/05 22:36:52 roberto Exp roberto $ +** $Id: lauxlib.h,v 1.43 2002/03/20 12:54:08 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -27,9 +27,10 @@ typedef struct luaL_reg { } luaL_reg; -LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l); +LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int nup); LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname, - const luaL_reg *l); + const luaL_reg *l, int nup); +LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event); LUALIB_API void luaL_typerror (lua_State *L, int narg, const char *tname); LUALIB_API void luaL_argerror (lua_State *L, int numarg, const char *extramsg); -- cgit v1.2.3-55-g6feb