aboutsummaryrefslogtreecommitdiff
path: root/lauxlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-06-30 14:40:27 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-06-30 14:40:27 -0300
commita139e2e003e0b62b7d34eeda20dd2354e74885f9 (patch)
tree7893fbccec3852dbf35321d2612074c2259570ef /lauxlib.c
parenta71c5f6f531a2503ff80e579e6c3bb95968645ba (diff)
downloadlua-a139e2e003e0b62b7d34eeda20dd2354e74885f9.tar.gz
lua-a139e2e003e0b62b7d34eeda20dd2354e74885f9.tar.bz2
lua-a139e2e003e0b62b7d34eeda20dd2354e74885f9.zip
old (and complex) luaL_findtable now used only in compatibility code
inside lauxlib.c
Diffstat (limited to 'lauxlib.c')
-rw-r--r--lauxlib.c81
1 files changed, 51 insertions, 30 deletions
diff --git a/lauxlib.c b/lauxlib.c
index abb28624..38e40404 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.214 2010/05/31 16:34:19 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.215 2010/06/09 17:53:59 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*/
@@ -657,6 +657,39 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
657 657
658 658
659/* 659/*
660** {======================================================
661** Compatibility with 5.1 module functions
662** =======================================================
663*/
664
665static const char *luaL_findtablex (lua_State *L, int idx,
666 const char *fname, int szhint) {
667 const char *e;
668 if (idx) lua_pushvalue(L, idx);
669 do {
670 e = strchr(fname, '.');
671 if (e == NULL) e = fname + strlen(fname);
672 lua_pushlstring(L, fname, e - fname);
673 lua_rawget(L, -2);
674 if (lua_isnil(L, -1)) { /* no such field? */
675 lua_pop(L, 1); /* remove this nil */
676 lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
677 lua_pushlstring(L, fname, e - fname);
678 lua_pushvalue(L, -2);
679 lua_settable(L, -4); /* set new table into field */
680 }
681 else if (!lua_istable(L, -1)) { /* field has a non-table value? */
682 lua_pop(L, 2); /* remove table and value */
683 return fname; /* return problematic part of the name */
684 }
685 lua_remove(L, -2); /* remove previous table */
686 fname = e + 1;
687 } while (*e == '.');
688 return NULL;
689}
690
691
692/*
660** Count number of elements in a luaL_Reg list. 693** Count number of elements in a luaL_Reg list.
661*/ 694*/
662static int libsize (const luaL_Reg *l) { 695static int libsize (const luaL_Reg *l) {
@@ -674,13 +707,13 @@ static int libsize (const luaL_Reg *l) {
674*/ 707*/
675LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname, 708LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname,
676 int sizehint) { 709 int sizehint) {
677 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */ 710 luaL_findtablex(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */
678 lua_getfield(L, -1, modname); /* get _LOADED[modname] */ 711 lua_getfield(L, -1, modname); /* get _LOADED[modname] */
679 if (!lua_istable(L, -1)) { /* not found? */ 712 if (!lua_istable(L, -1)) { /* not found? */
680 lua_pop(L, 1); /* remove previous result */ 713 lua_pop(L, 1); /* remove previous result */
681 /* try global variable (and create one if it does not exist) */ 714 /* try global variable (and create one if it does not exist) */
682 lua_pushglobaltable(L); 715 lua_pushglobaltable(L);
683 if (luaL_findtable(L, 0, modname, sizehint) != NULL) 716 if (luaL_findtablex(L, 0, modname, sizehint) != NULL)
684 luaL_error(L, "name conflict for module " LUA_QS, modname); 717 luaL_error(L, "name conflict for module " LUA_QS, modname);
685 lua_pushvalue(L, -1); 718 lua_pushvalue(L, -1);
686 lua_setfield(L, -3, modname); /* _LOADED[modname] = new table */ 719 lua_setfield(L, -3, modname); /* _LOADED[modname] = new table */
@@ -707,6 +740,21 @@ LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
707 lua_pop(L, nup); /* remove upvalues */ 740 lua_pop(L, nup); /* remove upvalues */
708} 741}
709 742
743/* }====================================================== */
744
745
746LUALIB_API void luaL_findtable (lua_State *L, int idx, const char *fname) {
747 lua_getfield(L, idx, fname);
748 if (lua_istable(L, -1)) return; /* table already there */
749 else {
750 idx = lua_absindex(L, idx);
751 lua_pop(L, 1); /* remove previous result */
752 lua_newtable(L);
753 lua_pushvalue(L, -1); /* copy to be left at top */
754 lua_setfield(L, idx, fname); /* assign new table to field */
755 }
756}
757
710 758
711LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, 759LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
712 const char *r) { 760 const char *r) {
@@ -725,33 +773,6 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
725} 773}
726 774
727 775
728LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
729 const char *fname, int szhint) {
730 const char *e;
731 if (idx) lua_pushvalue(L, idx);
732 do {
733 e = strchr(fname, '.');
734 if (e == NULL) e = fname + strlen(fname);
735 lua_pushlstring(L, fname, e - fname);
736 lua_rawget(L, -2);
737 if (lua_isnil(L, -1)) { /* no such field? */
738 lua_pop(L, 1); /* remove this nil */
739 lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
740 lua_pushlstring(L, fname, e - fname);
741 lua_pushvalue(L, -2);
742 lua_settable(L, -4); /* set new table into field */
743 }
744 else if (!lua_istable(L, -1)) { /* field has a non-table value? */
745 lua_pop(L, 2); /* remove table and value */
746 return fname; /* return problematic part of the name */
747 }
748 lua_remove(L, -2); /* remove previous table */
749 fname = e + 1;
750 } while (*e == '.');
751 return NULL;
752}
753
754
755static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { 776static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
756 (void)ud; 777 (void)ud;
757 (void)osize; 778 (void)osize;