diff options
-rw-r--r-- | lbaselib.c | 11 | ||||
-rw-r--r-- | ltablib.c | 20 | ||||
-rw-r--r-- | lua.c | 10 | ||||
-rw-r--r-- | lua.h | 10 |
4 files changed, 28 insertions, 23 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.235 2009/12/28 16:30:31 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.236 2010/03/12 19:14:06 roberto Exp roberto $ |
3 | ** Basic library | 3 | ** Basic library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -23,7 +23,7 @@ | |||
23 | static int luaB_print (lua_State *L) { | 23 | static int luaB_print (lua_State *L) { |
24 | int n = lua_gettop(L); /* number of arguments */ | 24 | int n = lua_gettop(L); /* number of arguments */ |
25 | int i; | 25 | int i; |
26 | lua_getfield(L, LUA_ENVIRONINDEX, "tostring"); | 26 | lua_getglobal(L, "tostring"); |
27 | for (i=1; i<=n; i++) { | 27 | for (i=1; i<=n; i++) { |
28 | const char *s; | 28 | const char *s; |
29 | size_t l; | 29 | size_t l; |
@@ -679,11 +679,12 @@ static void auxopen (lua_State *L, const char *name, | |||
679 | static void base_open (lua_State *L) { | 679 | static void base_open (lua_State *L) { |
680 | /* set global _G */ | 680 | /* set global _G */ |
681 | lua_pushglobaltable(L); | 681 | lua_pushglobaltable(L); |
682 | lua_setfield(L, LUA_ENVIRONINDEX, "_G"); | 682 | lua_pushglobaltable(L); |
683 | lua_setfield(L, -2, "_G"); | ||
683 | /* open lib into global table */ | 684 | /* open lib into global table */ |
684 | luaL_register(L, "_G", base_funcs); | 685 | luaL_register(L, "_G", base_funcs); |
685 | lua_pushliteral(L, LUA_VERSION); | 686 | lua_pushliteral(L, LUA_VERSION); |
686 | lua_setfield(L, LUA_ENVIRONINDEX, "_VERSION"); /* set global _VERSION */ | 687 | lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */ |
687 | /* `ipairs' and `pairs' need auxiliary functions as upvalues */ | 688 | /* `ipairs' and `pairs' need auxiliary functions as upvalues */ |
688 | auxopen(L, "ipairs", luaB_ipairs, ipairsaux); | 689 | auxopen(L, "ipairs", luaB_ipairs, ipairsaux); |
689 | auxopen(L, "pairs", luaB_pairs, luaB_next); | 690 | auxopen(L, "pairs", luaB_pairs, luaB_next); |
@@ -694,7 +695,7 @@ static void base_open (lua_State *L) { | |||
694 | lua_pushliteral(L, "kv"); | 695 | lua_pushliteral(L, "kv"); |
695 | lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */ | 696 | lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */ |
696 | lua_pushcclosure(L, luaB_newproxy, 1); | 697 | lua_pushcclosure(L, luaB_newproxy, 1); |
697 | lua_setfield(L, LUA_ENVIRONINDEX, "newproxy"); /* set global `newproxy' */ | 698 | lua_setfield(L, -2, "newproxy"); /* set global `newproxy' */ |
698 | } | 699 | } |
699 | 700 | ||
700 | 701 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltablib.c,v 1.53 2009/12/28 16:30:31 roberto Exp roberto $ | 2 | ** $Id: ltablib.c,v 1.54 2010/01/13 19:59:10 roberto Exp roberto $ |
3 | ** Library for Table Manipulation | 3 | ** Library for Table Manipulation |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -171,15 +171,15 @@ static int tconcat (lua_State *L) { | |||
171 | static int pack (lua_State *L) { | 171 | static int pack (lua_State *L) { |
172 | int top = lua_gettop(L); | 172 | int top = lua_gettop(L); |
173 | lua_createtable(L, top, 1); /* create result table */ | 173 | lua_createtable(L, top, 1); /* create result table */ |
174 | /* use function environment as a temporary place to keep new table */ | ||
175 | lua_replace(L, LUA_ENVIRONINDEX); | ||
176 | lua_pushinteger(L, top); /* number of elements */ | 174 | lua_pushinteger(L, top); /* number of elements */ |
177 | lua_setfield(L, LUA_ENVIRONINDEX, "n"); /* t.n = number of elements */ | 175 | lua_setfield(L, -2, "n"); /* t.n = number of elements */ |
178 | for (; top >= 1; top--) /* assign elements */ | 176 | if (top > 0) { /* at least one element? */ |
179 | lua_rawseti(L, LUA_ENVIRONINDEX, top); | 177 | lua_pushvalue(L, 1); |
180 | lua_pushvalue(L, LUA_ENVIRONINDEX); /* return new table */ | 178 | lua_rawseti(L, -2, 1); /* insert first element */ |
181 | /* remove new table from environment to allow its later collection */ | 179 | lua_replace(L, 1); /* move table into its position (index 1) */ |
182 | lua_copy(L, LUA_REGISTRYINDEX, LUA_ENVIRONINDEX); | 180 | for (; top >= 2; top--) /* assign other elements */ |
181 | lua_rawseti(L, 1, top); | ||
182 | } | ||
183 | return 1; | 183 | return 1; |
184 | } | 184 | } |
185 | 185 | ||
@@ -328,7 +328,7 @@ LUAMOD_API int luaopen_table (lua_State *L) { | |||
328 | #if defined(LUA_COMPAT_UNPACK) | 328 | #if defined(LUA_COMPAT_UNPACK) |
329 | /* _G.unpack = table.unpack */ | 329 | /* _G.unpack = table.unpack */ |
330 | lua_getfield(L, -1, "unpack"); | 330 | lua_getfield(L, -1, "unpack"); |
331 | lua_setfield(L, LUA_ENVIRONINDEX, "unpack"); | 331 | lua_setglobal(L, "unpack"); |
332 | #endif | 332 | #endif |
333 | return 1; | 333 | return 1; |
334 | } | 334 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.c,v 1.187 2010/02/18 19:18:41 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.188 2010/02/27 21:15:36 roberto Exp roberto $ |
3 | ** Lua stand-alone interpreter | 3 | ** Lua stand-alone interpreter |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -219,7 +219,7 @@ static int dostring (lua_State *L, const char *s, const char *name) { | |||
219 | 219 | ||
220 | 220 | ||
221 | static int dolibrary (lua_State *L, const char *name) { | 221 | static int dolibrary (lua_State *L, const char *name) { |
222 | lua_getfield(L, LUA_ENVIRONINDEX, "require"); | 222 | lua_getglobal(L, "require"); |
223 | lua_pushstring(L, name); | 223 | lua_pushstring(L, name); |
224 | return report(L, docall(L, 1, 1)); | 224 | return report(L, docall(L, 1, 1)); |
225 | } | 225 | } |
@@ -227,7 +227,7 @@ static int dolibrary (lua_State *L, const char *name) { | |||
227 | 227 | ||
228 | static const char *get_prompt (lua_State *L, int firstline) { | 228 | static const char *get_prompt (lua_State *L, int firstline) { |
229 | const char *p; | 229 | const char *p; |
230 | lua_getfield(L, LUA_ENVIRONINDEX, firstline ? "_PROMPT" : "_PROMPT2"); | 230 | lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2"); |
231 | p = lua_tostring(L, -1); | 231 | p = lua_tostring(L, -1); |
232 | if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2); | 232 | if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2); |
233 | lua_pop(L, 1); /* remove global */ | 233 | lua_pop(L, 1); /* remove global */ |
@@ -301,7 +301,7 @@ static void dotty (lua_State *L) { | |||
301 | report(L, status); | 301 | report(L, status); |
302 | if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */ | 302 | if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */ |
303 | luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); | 303 | luaL_checkstack(L, LUA_MINSTACK, "too many results to print"); |
304 | lua_getfield(L, LUA_ENVIRONINDEX, "print"); | 304 | lua_getglobal(L, "print"); |
305 | lua_insert(L, 1); | 305 | lua_insert(L, 1); |
306 | if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != LUA_OK) | 306 | if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != LUA_OK) |
307 | l_message(progname, lua_pushfstring(L, | 307 | l_message(progname, lua_pushfstring(L, |
@@ -319,7 +319,7 @@ static int handle_script (lua_State *L, char **argv, int n) { | |||
319 | int status; | 319 | int status; |
320 | const char *fname; | 320 | const char *fname; |
321 | int narg = getargs(L, argv, n); /* collect arguments */ | 321 | int narg = getargs(L, argv, n); /* collect arguments */ |
322 | lua_setfield(L, LUA_ENVIRONINDEX, "arg"); | 322 | lua_setglobal(L, "arg"); |
323 | fname = argv[n]; | 323 | fname = argv[n]; |
324 | if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0) | 324 | if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0) |
325 | fname = NULL; /* stdin */ | 325 | fname = NULL; /* stdin */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.h,v 1.260 2010/01/06 15:08:00 roberto Exp roberto $ | 2 | ** $Id: lua.h,v 1.261 2010/01/11 17:15:11 roberto Exp roberto $ |
3 | ** Lua - A Scripting Language | 3 | ** Lua - A Scripting Language |
4 | ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) | 4 | ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) |
5 | ** See Copyright Notice at the end of this file | 5 | ** 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); | |||
297 | 297 | ||
298 | #define lua_newtable(L) lua_createtable(L, 0, 0) | 298 | #define lua_newtable(L) lua_createtable(L, 0, 0) |
299 | 299 | ||
300 | #define lua_setglobal(L,s) lua_setfield(L, LUA_ENVIRONINDEX, (s)) | 300 | #define lua_setglobal(L,s) \ |
301 | #define lua_getglobal(L,s) lua_getfield(L, LUA_ENVIRONINDEX, (s)) | 301 | (lua_pushglobaltable(L), lua_pushvalue(L, -2), \ |
302 | lua_setfield(L, -2, (s)), lua_pop(L, 2)) | ||
303 | |||
304 | #define lua_getglobal(L,s) \ | ||
305 | (lua_pushglobaltable(L), lua_getfield(L, -1, (s)), lua_remove(L, -2)) | ||
302 | 306 | ||
303 | #define lua_register(L,n,f) \ | 307 | #define lua_register(L,n,f) \ |
304 | (lua_pushcfunction(L, (f)), lua_setfield(L, LUA_ENVIRONINDEX, (n))) | 308 | (lua_pushcfunction(L, (f)), lua_setfield(L, LUA_ENVIRONINDEX, (n))) |