aboutsummaryrefslogtreecommitdiff
path: root/lauxlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lauxlib.c')
-rw-r--r--lauxlib.c18
1 files changed, 13 insertions, 5 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 34f1cc77..43f95e01 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.155 2005/10/20 11:35:25 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.156 2005/10/21 13:47:42 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*/
@@ -226,16 +226,24 @@ LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
226} 226}
227 227
228 228
229static int libsize (const luaL_Reg *l) {
230 int size = 0;
231 for (; l->name; l++) size++;
232 return size;
233}
234
235
229LUALIB_API void luaI_openlib (lua_State *L, const char *libname, 236LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
230 const luaL_Reg *l, int nup) { 237 const luaL_Reg *l, int nup) {
231 if (libname) { 238 if (libname) {
239 int size = libsize(l);
232 /* check whether lib already exists */ 240 /* check whether lib already exists */
233 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED"); 241 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", size);
234 lua_getfield(L, -1, libname); /* get _LOADED[libname] */ 242 lua_getfield(L, -1, libname); /* get _LOADED[libname] */
235 if (!lua_istable(L, -1)) { /* not found? */ 243 if (!lua_istable(L, -1)) { /* not found? */
236 lua_pop(L, 1); /* remove previous result */ 244 lua_pop(L, 1); /* remove previous result */
237 /* try global variable (and create one if it does not exist) */ 245 /* try global variable (and create one if it does not exist) */
238 if (luaL_findtable(L, LUA_GLOBALSINDEX, libname) != NULL) 246 if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL)
239 luaL_error(L, "name conflict for module " LUA_QS, libname); 247 luaL_error(L, "name conflict for module " LUA_QS, libname);
240 lua_pushvalue(L, -1); 248 lua_pushvalue(L, -1);
241 lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ 249 lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */
@@ -331,7 +339,7 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
331 339
332 340
333LUALIB_API const char *luaL_findtable (lua_State *L, int idx, 341LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
334 const char *fname) { 342 const char *fname, int szhint) {
335 const char *e; 343 const char *e;
336 lua_pushvalue(L, idx); 344 lua_pushvalue(L, idx);
337 do { 345 do {
@@ -341,7 +349,7 @@ LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
341 lua_rawget(L, -2); 349 lua_rawget(L, -2);
342 if (lua_isnil(L, -1)) { /* no such field? */ 350 if (lua_isnil(L, -1)) { /* no such field? */
343 lua_pop(L, 1); /* remove this nil */ 351 lua_pop(L, 1); /* remove this nil */
344 lua_newtable(L); /* create a new table for field */ 352 lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
345 lua_pushlstring(L, fname, e - fname); 353 lua_pushlstring(L, fname, e - fname);
346 lua_pushvalue(L, -2); 354 lua_pushvalue(L, -2);
347 lua_settable(L, -4); /* set new table into field */ 355 lua_settable(L, -4); /* set new table into field */