Преглед изворни кода

better support for new libraries

Roberto Ierusalimschy пре 23 година
родитељ
комит
cd99bbcd0d
2 измењених фајлова са 31 додато и 9 уклоњено
  1. 27 6
      lauxlib.c
  2. 4 3
      lauxlib.h

+ 27 - 6
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; 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,
-                                   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);
 }
 

+ 4 - 3
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);