Browse Source

avoid non-raw accesses to globals when variable may not exist

v5-2
Roberto Ierusalimschy 22 years ago
parent
commit
76de732745
  1. 8
      lbaselib.c
  2. 11
      lua.c

8
lbaselib.c

@ -1,5 +1,5 @@
/* /*
** $Id: lbaselib.c,v 1.111 2002/11/26 08:45:36 roberto Exp roberto $ ** $Id: lbaselib.c,v 1.112 2002/11/26 12:53:29 roberto Exp roberto $
** Basic library ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -213,7 +213,8 @@ static int luaB_next (lua_State *L) {
static int luaB_pairs (lua_State *L) { static int luaB_pairs (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 1, LUA_TTABLE);
lua_getglobal(L, "next"); /* return generator, */ lua_pushliteral(L, "next");
lua_rawget(L, LUA_GLOBALSINDEX); /* return generator, */
lua_pushvalue(L, 1); /* state, */ lua_pushvalue(L, 1); /* state, */
lua_pushnil(L); /* and initial value */ lua_pushnil(L); /* and initial value */
return 3; return 3;
@ -224,7 +225,8 @@ static int luaB_ipairs (lua_State *L) {
lua_Number i = lua_tonumber(L, 2); lua_Number i = lua_tonumber(L, 2);
luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 1, LUA_TTABLE);
if (i == 0 && lua_isnone(L, 2)) { /* `for' start? */ if (i == 0 && lua_isnone(L, 2)) { /* `for' start? */
lua_getglobal(L, "ipairs"); /* return generator, */ lua_pushliteral(L, "ipairs");
lua_rawget(L, LUA_GLOBALSINDEX); /* return generator, */
lua_pushvalue(L, 1); /* state, */ lua_pushvalue(L, 1); /* state, */
lua_pushnumber(L, 0); /* and initial value */ lua_pushnumber(L, 0); /* and initial value */
return 3; return 3;

11
lua.c

@ -1,5 +1,5 @@
/* /*
** $Id: lua.c,v 1.109 2002/11/19 13:49:43 roberto Exp roberto $ ** $Id: lua.c,v 1.110 2002/11/25 17:47:13 roberto Exp roberto $
** Lua stand-alone interpreter ** Lua stand-alone interpreter
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -121,7 +121,8 @@ static int report (int status) {
static int lcall (int narg, int clear) { static int lcall (int narg, int clear) {
int status; int status;
int base = lua_gettop(L) - narg; /* function index */ int base = lua_gettop(L) - narg; /* function index */
lua_getglobal(L, "_TRACEBACK"); /* get traceback function */ lua_pushliteral(L, "_TRACEBACK");
lua_rawget(L, LUA_GLOBALSINDEX); /* get traceback function */
lua_insert(L, base); /* put it under chunk and args */ lua_insert(L, base); /* put it under chunk and args */
signal(SIGINT, laction); signal(SIGINT, laction);
status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base); status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
@ -175,7 +176,8 @@ static int dostring (const char *s, const char *name) {
static int load_file (const char *name) { static int load_file (const char *name) {
lua_getglobal(L, "require"); lua_pushliteral(L, "require");
lua_rawget(L, LUA_GLOBALSINDEX);
if (!lua_isfunction(L, -1)) { /* no `require' defined? */ if (!lua_isfunction(L, -1)) { /* no `require' defined? */
lua_pop(L, 1); lua_pop(L, 1);
return file_input(name); return file_input(name);
@ -228,7 +230,8 @@ static int readline (lua_State *l, const char *prompt) {
static const char *get_prompt (int firstline) { static const char *get_prompt (int firstline) {
const char *p = NULL; const char *p = NULL;
lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2"); lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2");
lua_rawget(L, LUA_GLOBALSINDEX);
p = lua_tostring(L, -1); p = lua_tostring(L, -1);
if (p == NULL) p = (firstline ? PROMPT : PROMPT2); if (p == NULL) p = (firstline ? PROMPT : PROMPT2);
lua_pop(L, 1); /* remove global */ lua_pop(L, 1); /* remove global */

Loading…
Cancel
Save