aboutsummaryrefslogtreecommitdiff
path: root/lauxlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lauxlib.c')
-rw-r--r--lauxlib.c21
1 files changed, 13 insertions, 8 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 46c7e4f9..c0d7fb5c 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.201 2010/02/18 19:37:57 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.202 2010/03/12 18:59:32 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*/
@@ -670,13 +670,13 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
670 670
671static int libsize (const luaL_Reg *l) { 671static int libsize (const luaL_Reg *l) {
672 int size = 0; 672 int size = 0;
673 for (; l->name; l++) size++; 673 for (; l && l->name; l++) size++;
674 return size; 674 return size;
675} 675}
676 676
677 677
678LUALIB_API void luaL_register (lua_State *L, const char *libname, 678LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
679 const luaL_Reg *l) { 679 const luaL_Reg *l, int nup) {
680 luaL_checkversion(L); 680 luaL_checkversion(L);
681 if (libname) { 681 if (libname) {
682 /* check whether lib already exists */ 682 /* check whether lib already exists */
@@ -692,12 +692,17 @@ LUALIB_API void luaL_register (lua_State *L, const char *libname,
692 lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ 692 lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */
693 } 693 }
694 lua_remove(L, -2); /* remove _LOADED table */ 694 lua_remove(L, -2); /* remove _LOADED table */
695 lua_insert(L, -(nup + 1)); /* move library table to below upvalues */
695 } 696 }
696 if (l == NULL) return; /* nothing to register? */ 697 luaL_checkstack(L, nup, "too many upvalues");
697 for (; l->name; l++) { /* else fill the table with given functions */ 698 for (; l && l->name; l++) { /* else fill the table with given functions */
698 lua_pushcfunction(L, l->func); 699 int i;
699 lua_setfield(L, -2, l->name); 700 for (i = 0; i < nup; i++) /* copy upvalues to the top */
701 lua_pushvalue(L, -nup);
702 lua_pushcclosure(L, l->func, nup);
703 lua_setfield(L, -(nup + 2), l->name);
700 } 704 }
705 lua_pop(L, nup); /* remove upvalues */
701} 706}
702 707
703 708