diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-04-02 17:42:49 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-04-02 17:42:49 -0300 |
commit | cd99bbcd0d88e7b5864ea9c9634ce8abd9bdedda (patch) | |
tree | 8d432e309e82d871e21121a98b0d5505ebf4f785 | |
parent | 2cbbf7e95a229cce3033f68807cf3e82021879f8 (diff) | |
download | lua-cd99bbcd0d88e7b5864ea9c9634ce8abd9bdedda.tar.gz lua-cd99bbcd0d88e7b5864ea9c9634ce8abd9bdedda.tar.bz2 lua-cd99bbcd0d88e7b5864ea9c9634ce8abd9bdedda.zip |
better support for new libraries
-rw-r--r-- | lauxlib.c | 33 | ||||
-rw-r--r-- | lauxlib.h | 7 |
2 files changed, 31 insertions, 9 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.62 2002/03/20 12:54:08 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.63 2002/03/27 15:30:41 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -116,20 +116,41 @@ LUALIB_API lua_Number luaL_opt_number (lua_State *L, int narg, lua_Number def) { | |||
116 | } | 116 | } |
117 | 117 | ||
118 | 118 | ||
119 | LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l) { | 119 | LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { |
120 | if (!lua_getmetatable(L, obj)) /* no metatable? */ | ||
121 | return 0; | ||
122 | lua_pushstring(L, event); | ||
123 | lua_rawget(L, -2); | ||
124 | if (lua_isnil(L, -1)) { | ||
125 | lua_pop(L, 2); /* remove metatable and metafield */ | ||
126 | return 0; | ||
127 | } | ||
128 | lua_pushvalue(L, obj); | ||
129 | lua_rawcall(L, 1, 1); | ||
130 | return 1; | ||
131 | } | ||
132 | |||
133 | |||
134 | LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int nup) { | ||
120 | for (; l->name; l++) { | 135 | for (; l->name; l++) { |
136 | int i; | ||
121 | lua_pushstring(L, l->name); | 137 | lua_pushstring(L, l->name); |
122 | lua_pushcfunction(L, l->func); | 138 | for (i=0; i<nup; i++) /* copy upvalues to the top */ |
123 | lua_settable(L, -3); | 139 | lua_pushvalue(L, -(nup+1)); |
140 | lua_pushcclosure(L, l->func, nup); | ||
141 | lua_settable(L, -(nup+3)); | ||
124 | } | 142 | } |
143 | lua_pop(L, nup); | ||
125 | } | 144 | } |
126 | 145 | ||
127 | 146 | ||
128 | LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname, | 147 | LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname, |
129 | const luaL_reg *l) { | 148 | const luaL_reg *l, int nup) { |
130 | lua_pushstring(L, libname); | 149 | lua_pushstring(L, libname); |
150 | lua_insert(L, -(nup+1)); | ||
131 | lua_newtable(L); | 151 | lua_newtable(L); |
132 | luaL_openlib(L, l); | 152 | lua_insert(L, -(nup+1)); |
153 | luaL_openlib(L, l, nup); | ||
133 | lua_settable(L, LUA_GLOBALSINDEX); | 154 | lua_settable(L, LUA_GLOBALSINDEX); |
134 | } | 155 | } |
135 | 156 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.h,v 1.42 2002/02/05 22:36:52 roberto Exp roberto $ | 2 | ** $Id: lauxlib.h,v 1.43 2002/03/20 12:54:08 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -27,9 +27,10 @@ typedef struct luaL_reg { | |||
27 | } luaL_reg; | 27 | } luaL_reg; |
28 | 28 | ||
29 | 29 | ||
30 | LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l); | 30 | LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int nup); |
31 | LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname, | 31 | LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname, |
32 | const luaL_reg *l); | 32 | const luaL_reg *l, int nup); |
33 | LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event); | ||
33 | LUALIB_API void luaL_typerror (lua_State *L, int narg, const char *tname); | 34 | LUALIB_API void luaL_typerror (lua_State *L, int narg, const char *tname); |
34 | LUALIB_API void luaL_argerror (lua_State *L, int numarg, | 35 | LUALIB_API void luaL_argerror (lua_State *L, int numarg, |
35 | const char *extramsg); | 36 | const char *extramsg); |