From 489253d7530e372da8807f1ad083928c3545f772 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 13 Mar 2010 00:57:46 -0300 Subject: better definitions for lua_[gs]etglobal + less uses of ENVIRONINDEX --- lbaselib.c | 11 ++++++----- ltablib.c | 20 ++++++++++---------- lua.c | 10 +++++----- lua.h | 10 +++++++--- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/lbaselib.c b/lbaselib.c index a14ce31d..1b2c687c 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.235 2009/12/28 16:30:31 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.236 2010/03/12 19:14:06 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -23,7 +23,7 @@ static int luaB_print (lua_State *L) { int n = lua_gettop(L); /* number of arguments */ int i; - lua_getfield(L, LUA_ENVIRONINDEX, "tostring"); + lua_getglobal(L, "tostring"); for (i=1; i<=n; i++) { const char *s; size_t l; @@ -679,11 +679,12 @@ static void auxopen (lua_State *L, const char *name, static void base_open (lua_State *L) { /* set global _G */ lua_pushglobaltable(L); - lua_setfield(L, LUA_ENVIRONINDEX, "_G"); + lua_pushglobaltable(L); + lua_setfield(L, -2, "_G"); /* open lib into global table */ luaL_register(L, "_G", base_funcs); lua_pushliteral(L, LUA_VERSION); - lua_setfield(L, LUA_ENVIRONINDEX, "_VERSION"); /* set global _VERSION */ + lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */ /* `ipairs' and `pairs' need auxiliary functions as upvalues */ auxopen(L, "ipairs", luaB_ipairs, ipairsaux); auxopen(L, "pairs", luaB_pairs, luaB_next); @@ -694,7 +695,7 @@ static void base_open (lua_State *L) { lua_pushliteral(L, "kv"); lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */ lua_pushcclosure(L, luaB_newproxy, 1); - lua_setfield(L, LUA_ENVIRONINDEX, "newproxy"); /* set global `newproxy' */ + lua_setfield(L, -2, "newproxy"); /* set global `newproxy' */ } diff --git a/ltablib.c b/ltablib.c index e2da6081..533f9ac9 100644 --- a/ltablib.c +++ b/ltablib.c @@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.53 2009/12/28 16:30:31 roberto Exp roberto $ +** $Id: ltablib.c,v 1.54 2010/01/13 19:59:10 roberto Exp roberto $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ @@ -171,15 +171,15 @@ static int tconcat (lua_State *L) { static int pack (lua_State *L) { int top = lua_gettop(L); lua_createtable(L, top, 1); /* create result table */ - /* use function environment as a temporary place to keep new table */ - lua_replace(L, LUA_ENVIRONINDEX); lua_pushinteger(L, top); /* number of elements */ - lua_setfield(L, LUA_ENVIRONINDEX, "n"); /* t.n = number of elements */ - for (; top >= 1; top--) /* assign elements */ - lua_rawseti(L, LUA_ENVIRONINDEX, top); - lua_pushvalue(L, LUA_ENVIRONINDEX); /* return new table */ - /* remove new table from environment to allow its later collection */ - lua_copy(L, LUA_REGISTRYINDEX, LUA_ENVIRONINDEX); + lua_setfield(L, -2, "n"); /* t.n = number of elements */ + if (top > 0) { /* at least one element? */ + lua_pushvalue(L, 1); + lua_rawseti(L, -2, 1); /* insert first element */ + lua_replace(L, 1); /* move table into its position (index 1) */ + for (; top >= 2; top--) /* assign other elements */ + lua_rawseti(L, 1, top); + } return 1; } @@ -328,7 +328,7 @@ LUAMOD_API int luaopen_table (lua_State *L) { #if defined(LUA_COMPAT_UNPACK) /* _G.unpack = table.unpack */ lua_getfield(L, -1, "unpack"); - lua_setfield(L, LUA_ENVIRONINDEX, "unpack"); + lua_setglobal(L, "unpack"); #endif return 1; } diff --git a/lua.c b/lua.c index 3f7093f7..ad4e8c28 100644 --- a/lua.c +++ b/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.187 2010/02/18 19:18:41 roberto Exp roberto $ +** $Id: lua.c,v 1.188 2010/02/27 21:15:36 roberto Exp roberto $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -219,7 +219,7 @@ static int dostring (lua_State *L, const char *s, const char *name) { static int dolibrary (lua_State *L, const char *name) { - lua_getfield(L, LUA_ENVIRONINDEX, "require"); + lua_getglobal(L, "require"); lua_pushstring(L, name); return report(L, docall(L, 1, 1)); } @@ -227,7 +227,7 @@ static int dolibrary (lua_State *L, const char *name) { static const char *get_prompt (lua_State *L, int firstline) { const char *p; - lua_getfield(L, LUA_ENVIRONINDEX, firstline ? "_PROMPT" : "_PROMPT2"); + lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2"); p = lua_tostring(L, -1); if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2); lua_pop(L, 1); /* remove global */ @@ -301,7 +301,7 @@ static void dotty (lua_State *L) { report(L, status); if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */ luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); - lua_getfield(L, LUA_ENVIRONINDEX, "print"); + lua_getglobal(L, "print"); lua_insert(L, 1); if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != LUA_OK) l_message(progname, lua_pushfstring(L, @@ -319,7 +319,7 @@ static int handle_script (lua_State *L, char **argv, int n) { int status; const char *fname; int narg = getargs(L, argv, n); /* collect arguments */ - lua_setfield(L, LUA_ENVIRONINDEX, "arg"); + lua_setglobal(L, "arg"); fname = argv[n]; if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0) fname = NULL; /* stdin */ diff --git a/lua.h b/lua.h index 055a8687..43bb8aec 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.260 2010/01/06 15:08:00 roberto Exp roberto $ +** $Id: lua.h,v 1.261 2010/01/11 17:15:11 roberto Exp roberto $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -297,8 +297,12 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); #define lua_newtable(L) lua_createtable(L, 0, 0) -#define lua_setglobal(L,s) lua_setfield(L, LUA_ENVIRONINDEX, (s)) -#define lua_getglobal(L,s) lua_getfield(L, LUA_ENVIRONINDEX, (s)) +#define lua_setglobal(L,s) \ + (lua_pushglobaltable(L), lua_pushvalue(L, -2), \ + lua_setfield(L, -2, (s)), lua_pop(L, 2)) + +#define lua_getglobal(L,s) \ + (lua_pushglobaltable(L), lua_getfield(L, -1, (s)), lua_remove(L, -2)) #define lua_register(L,n,f) \ (lua_pushcfunction(L, (f)), lua_setfield(L, LUA_ENVIRONINDEX, (n))) -- cgit v1.2.3-55-g6feb