diff options
| -rw-r--r-- | lauxlib.c | 81 | ||||
| -rw-r--r-- | lauxlib.h | 5 | ||||
| -rw-r--r-- | linit.c | 4 | ||||
| -rw-r--r-- | loadlib.c | 8 |
4 files changed, 59 insertions, 39 deletions
| @@ -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 | |||
| 665 | static 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 | */ |
| 662 | static int libsize (const luaL_Reg *l) { | 695 | static int libsize (const luaL_Reg *l) { |
| @@ -674,13 +707,13 @@ static int libsize (const luaL_Reg *l) { | |||
| 674 | */ | 707 | */ |
| 675 | LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname, | 708 | LUALIB_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 | |||
| 746 | LUALIB_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 | ||
| 711 | LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, | 759 | LUALIB_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 | ||
| 728 | LUALIB_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 | |||
| 755 | static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { | 776 | static 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; |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.h,v 1.105 2010/05/04 17:21:08 roberto Exp roberto $ | 2 | ** $Id: lauxlib.h,v 1.106 2010/05/31 16:34:19 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 | */ |
| @@ -78,8 +78,7 @@ LUALIB_API int (luaL_len) (lua_State *L, int idx); | |||
| 78 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, | 78 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, |
| 79 | const char *r); | 79 | const char *r); |
| 80 | 80 | ||
| 81 | LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, | 81 | LUALIB_API void (luaL_findtable) (lua_State *L, int idx, const char *fname); |
| 82 | const char *fname, int szhint); | ||
| 83 | 82 | ||
| 84 | LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, | 83 | LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1, |
| 85 | const char *msg, int level); | 84 | const char *msg, int level); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: linit.c,v 1.25 2010/05/20 12:57:59 roberto Exp roberto $ | 2 | ** $Id: linit.c,v 1.26 2010/06/10 21:29:47 roberto Exp roberto $ |
| 3 | ** Initialization of libraries for lua.c and other clients | 3 | ** Initialization of libraries for lua.c and other clients |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -59,7 +59,7 @@ LUALIB_API void luaL_openlibs (lua_State *L) { | |||
| 59 | } | 59 | } |
| 60 | /* add open functions from 'preloadedlibs' into 'package.preload' table */ | 60 | /* add open functions from 'preloadedlibs' into 'package.preload' table */ |
| 61 | lua_pushglobaltable(L); | 61 | lua_pushglobaltable(L); |
| 62 | luaL_findtable(L, 0, "package.preload", 0); | 62 | luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); |
| 63 | for (lib = preloadedlibs; lib->func; lib++) { | 63 | for (lib = preloadedlibs; lib->func; lib++) { |
| 64 | lua_pushcfunction(L, lib->func); | 64 | lua_pushcfunction(L, lib->func); |
| 65 | lua_setfield(L, -2, lib->name); | 65 | lua_setfield(L, -2, lib->name); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: loadlib.c,v 1.84 2010/06/13 19:36:17 roberto Exp roberto $ | 2 | ** $Id: loadlib.c,v 1.85 2010/06/18 17:23:02 roberto Exp roberto $ |
| 3 | ** Dynamic library loader for Lua | 3 | ** Dynamic library loader for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | ** | 5 | ** |
| @@ -423,7 +423,7 @@ static int loader_Croot (lua_State *L) { | |||
| 423 | 423 | ||
| 424 | static int loader_preload (lua_State *L) { | 424 | static int loader_preload (lua_State *L) { |
| 425 | const char *name = luaL_checkstring(L, 1); | 425 | const char *name = luaL_checkstring(L, 1); |
| 426 | lua_getfield(L, lua_upvalueindex(1), "preload"); | 426 | lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); |
| 427 | if (!lua_istable(L, -1)) | 427 | if (!lua_istable(L, -1)) |
| 428 | luaL_error(L, LUA_QL("package.preload") " must be a table"); | 428 | luaL_error(L, LUA_QL("package.preload") " must be a table"); |
| 429 | lua_getfield(L, -1, name); | 429 | lua_getfield(L, -1, name); |
| @@ -633,10 +633,10 @@ LUAMOD_API int luaopen_package (lua_State *L) { | |||
| 633 | LUA_EXEC_DIR "\n" LUA_IGMARK "\n"); | 633 | LUA_EXEC_DIR "\n" LUA_IGMARK "\n"); |
| 634 | lua_setfield(L, -2, "config"); | 634 | lua_setfield(L, -2, "config"); |
| 635 | /* set field `loaded' */ | 635 | /* set field `loaded' */ |
| 636 | luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2); | 636 | luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED"); |
| 637 | lua_setfield(L, -2, "loaded"); | 637 | lua_setfield(L, -2, "loaded"); |
| 638 | /* set field `preload' */ | 638 | /* set field `preload' */ |
| 639 | lua_newtable(L); | 639 | luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); |
| 640 | lua_setfield(L, -2, "preload"); | 640 | lua_setfield(L, -2, "preload"); |
| 641 | lua_pushglobaltable(L); | 641 | lua_pushglobaltable(L); |
| 642 | lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */ | 642 | lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */ |
