better support for new libraries

This commit is contained in:
Roberto Ierusalimschy
2002-04-02 17:42:49 -03:00
parent 2cbbf7e95a
commit cd99bbcd0d
2 changed files with 33 additions and 11 deletions

View File

@@ -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 ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** 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) {
for (; l->name; l++) { if (!lua_getmetatable(L, obj)) /* no metatable? */
lua_pushstring(L, l->name); return 0;
lua_pushcfunction(L, l->func); lua_pushstring(L, event);
lua_settable(L, -3); 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);
for (i=0; i<nup; i++) /* copy upvalues to the top */
lua_pushvalue(L, -(nup+1));
lua_pushcclosure(L, l->func, nup);
lua_settable(L, -(nup+3));
}
lua_pop(L, nup);
} }
LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname, 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_pushstring(L, libname);
lua_insert(L, -(nup+1));
lua_newtable(L); lua_newtable(L);
luaL_openlib(L, l); lua_insert(L, -(nup+1));
luaL_openlib(L, l, nup);
lua_settable(L, LUA_GLOBALSINDEX); lua_settable(L, LUA_GLOBALSINDEX);
} }

View File

@@ -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 ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -27,9 +27,10 @@ typedef struct luaL_reg {
} 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, 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_typerror (lua_State *L, int narg, const char *tname);
LUALIB_API void luaL_argerror (lua_State *L, int numarg, LUALIB_API void luaL_argerror (lua_State *L, int numarg,
const char *extramsg); const char *extramsg);