'_ENV' name permanently stored in global state for easier access

This commit is contained in:
Roberto Ierusalimschy 2010-03-13 12:55:42 -03:00
parent 63a2b62468
commit 22ef84b6c8
5 changed files with 11 additions and 12 deletions

View File

@ -1,5 +1,5 @@
/*
** $Id: ldebug.c,v 2.65 2010/03/05 14:01:29 roberto Exp roberto $
** $Id: ldebug.c,v 2.66 2010/03/12 19:14:06 roberto Exp roberto $
** Debug Interface
** See Copyright Notice in lua.h
*/
@ -304,7 +304,7 @@ static const char *getobjname (lua_State *L, CallInfo *ci, int reg,
? luaF_getlocalname(p, t + 1, pc)
: getstr(p->upvalues[t].name);
kname(p, k, a, what, name);
what = (tabname && strcmp(tabname, "_ENV") == 0) ? "global" : "field";
what = (tabname == getstr(G(L)->envn)) ? "global" : "field";
}
break;
}

3
llex.h
View File

@ -1,5 +1,5 @@
/*
** $Id: llex.h,v 1.62 2009/10/11 20:02:19 roberto Exp roberto $
** $Id: llex.h,v 1.63 2010/03/08 16:55:52 roberto Exp roberto $
** Lexical Analyzer
** See Copyright Notice in lua.h
*/
@ -60,7 +60,6 @@ typedef struct LexState {
Mbuffer *buff; /* buffer for tokens */
struct Varlist *varl; /* list of all active local variables */
TString *source; /* current source name */
TString *envn; /* name of environment variable */
char decpoint; /* locale decimal point */
} LexState;

View File

@ -1,5 +1,5 @@
/*
** $Id: lparser.c,v 2.78 2010/03/08 16:55:52 roberto Exp roberto $
** $Id: lparser.c,v 2.79 2010/03/12 19:14:06 roberto Exp roberto $
** Lua Parser
** See Copyright Notice in lua.h
*/
@ -288,7 +288,7 @@ static void singlevar (LexState *ls, expdesc *var) {
FuncState *fs = ls->fs;
if (singlevaraux(fs, varname, var, 1) == VVOID) { /* global name? */
expdesc key;
singlevaraux(fs, ls->envn, var, 1); /* get _ENV variable */
singlevaraux(fs, G(ls->L)->envn, var, 1); /* get _ENV variable */
lua_assert(var->k == VLOCAL || var->k == VUPVAL);
codestring(ls, &key, varname); /* key is variable name */
luaK_indexed(fs, var, &key); /* env[varname] */
@ -433,11 +433,8 @@ static void open_mainfunc (lua_State *L, LexState *ls, FuncState *fs) {
expdesc v;
open_func(ls, fs);
fs->f->is_vararg = 1; /* main function is always vararg */
ls->envn = luaS_new(L, "_ENV"); /* create '_ENV' string */
setsvalue2s(L, L->top++, ls->envn); /* anchor it */
init_exp(&v, VLOCAL, 0);
newupvalue(fs, ls->envn, &v); /* create '_ENV' upvalue */
L->top--; /* now string is anchored as an upvalue name */
newupvalue(fs, G(L)->envn, &v); /* create '_ENV' upvalue */
}

View File

@ -1,5 +1,5 @@
/*
** $Id: lstate.c,v 2.67 2009/12/17 12:26:09 roberto Exp roberto $
** $Id: lstate.c,v 2.68 2009/12/22 15:32:50 roberto Exp roberto $
** Global State
** See Copyright Notice in lua.h
*/
@ -161,6 +161,8 @@ static void f_luaopen (lua_State *L, void *ud) {
luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
luaT_init(L);
luaX_init(L);
g->envn = luaS_new(L, "_ENV");
luaS_fix(g->envn); /* never collect this name */
luaS_fix(luaS_newliteral(L, MEMERRMSG));
g->GCthreshold = 4*g->totalbytes;
}

View File

@ -1,5 +1,5 @@
/*
** $Id: lstate.h,v 2.52 2009/12/22 15:32:50 roberto Exp roberto $
** $Id: lstate.h,v 2.53 2010/02/09 11:55:37 roberto Exp roberto $
** Global State
** See Copyright Notice in lua.h
*/
@ -144,6 +144,7 @@ typedef struct global_State {
const lua_Number *version; /* pointer to version number */
struct Table *mt[NUM_TAGS]; /* metatables for basic types */
TString *tmname[TM_N]; /* array with tag-method names */
TString *envn; /* environment variable name */
} global_State;