aboutsummaryrefslogtreecommitdiff
path: root/lauxlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lauxlib.c')
-rw-r--r--lauxlib.c58
1 files changed, 16 insertions, 42 deletions
diff --git a/lauxlib.c b/lauxlib.c
index e99f6f60..a19cb079 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.148 2005/08/18 20:36:26 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.149 2005/08/25 15:39:16 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*/
@@ -66,7 +66,7 @@ LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {
66 66
67 67
68static void tag_error (lua_State *L, int narg, int tag) { 68static void tag_error (lua_State *L, int narg, int tag) {
69 luaL_typerror(L, narg, lua_typename(L, tag)); 69 luaL_typerror(L, narg, lua_typename(L, tag));
70} 70}
71 71
72 72
@@ -235,15 +235,9 @@ LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
235 lua_getfield(L, -1, libname); /* get _LOADED[libname] */ 235 lua_getfield(L, -1, libname); /* get _LOADED[libname] */
236 if (!lua_istable(L, -1)) { /* not found? */ 236 if (!lua_istable(L, -1)) { /* not found? */
237 lua_pop(L, 1); /* remove previous result */ 237 lua_pop(L, 1); /* remove previous result */
238 luaL_getfield(L, LUA_GLOBALSINDEX, libname); /* try global variable */ 238 /* try global variable (and create one if it does not exist) */
239 if (!lua_istable(L, -1)) { 239 if (luaL_findtable(L, LUA_GLOBALSINDEX, libname) != NULL)
240 if (!lua_isnil(L, -1)) 240 luaL_error(L, "name conflict for module " LUA_QS, libname);
241 luaL_error(L, "name conflict for module " LUA_QS, libname);
242 lua_pop(L, 1);
243 lua_newtable(L); /* create it */
244 lua_pushvalue(L, -1); /* register it with given name */
245 luaL_setfield(L, LUA_GLOBALSINDEX, libname);
246 }
247 lua_pushvalue(L, -1); 241 lua_pushvalue(L, -1);
248 lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ 242 lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */
249 } 243 }
@@ -337,30 +331,13 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
337} 331}
338 332
339 333
340 334LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
341LUALIB_API const char *luaL_getfield (lua_State *L, int idx, 335 const char *fname) {
342 const char *fname) {
343 const char *e;
344 lua_pushvalue(L, idx);
345 while ((e = strchr(fname, '.')) != NULL) {
346 lua_pushlstring(L, fname, e - fname);
347 lua_rawget(L, -2);
348 lua_remove(L, -2); /* remove previous table */
349 fname = e + 1;
350 if (!lua_istable(L, -1)) return fname;
351 }
352 lua_pushstring(L, fname);
353 lua_rawget(L, -2); /* get last field */
354 lua_remove(L, -2); /* remove previous table */
355 return NULL;
356}
357
358
359LUALIB_API const char *luaL_setfield (lua_State *L, int idx,
360 const char *fname) {
361 const char *e; 336 const char *e;
362 lua_pushvalue(L, idx); 337 lua_pushvalue(L, idx);
363 while ((e = strchr(fname, '.')) != NULL) { 338 do {
339 e = strchr(fname, '.');
340 if (e == NULL) e = fname + strlen(fname);
364 lua_pushlstring(L, fname, e - fname); 341 lua_pushlstring(L, fname, e - fname);
365 lua_rawget(L, -2); 342 lua_rawget(L, -2);
366 if (lua_isnil(L, -1)) { /* no such field? */ 343 if (lua_isnil(L, -1)) { /* no such field? */
@@ -370,16 +347,13 @@ LUALIB_API const char *luaL_setfield (lua_State *L, int idx,
370 lua_pushvalue(L, -2); 347 lua_pushvalue(L, -2);
371 lua_settable(L, -4); /* set new table into field */ 348 lua_settable(L, -4); /* set new table into field */
372 } 349 }
373 lua_remove(L, -2); /* remove previous table */ 350 else if (!lua_istable(L, -1)) { /* field has a non-table value? */
374 fname = e + 1;
375 if (!lua_istable(L, -1)) {
376 lua_pop(L, 2); /* remove table and value */ 351 lua_pop(L, 2); /* remove table and value */
377 return fname; 352 return fname; /* return problematic part of the name */
378 } 353 }
379 } 354 lua_remove(L, -2); /* remove previous table */
380 lua_pushvalue(L, -2); /* move value to the top */ 355 fname = e + 1;
381 lua_setfield(L, -2, fname); /* set last field */ 356 } while (*e == '.');
382 lua_pop(L, 2); /* remove value and table */
383 return NULL; 357 return NULL;
384} 358}
385 359
@@ -635,7 +609,7 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
635 free(ptr); 609 free(ptr);
636 return NULL; 610 return NULL;
637 } 611 }
638 else 612 else
639 return realloc(ptr, nsize); 613 return realloc(ptr, nsize);
640} 614}
641 615