From 0df2238063f3e5b1a12c5272484f2afd45a2a2f1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 7 Sep 2010 16:21:39 -0300 Subject: [PATCH] name "_ENV" configurable through 'luaconf.h' --- lapi.c | 4 ++-- lbaselib.c | 4 ++-- ldebug.c | 4 ++-- llex.c | 4 ++-- loadlib.c | 4 ++-- lparser.c | 8 ++++---- luaconf.h | 10 +++++++++- 7 files changed, 23 insertions(+), 15 deletions(-) diff --git a/lapi.c b/lapi.c index b19ac922..aacfd93c 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.134 2010/08/04 18:40:28 roberto Exp roberto $ +** $Id: lapi.c,v 2.135 2010/09/03 14:14:01 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -891,7 +891,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, /* get global table from registry */ Table *reg = hvalue(&G(L)->l_registry); const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); - /* set global table as 1st upvalue of 'f' (may be _ENV) */ + /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ setobj(L, f->l.upvals[0]->v, gt); luaC_barrier(L, f->l.upvals[0], gt); } diff --git a/lbaselib.c b/lbaselib.c index b8dc6b50..e3fc8d8b 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.247 2010/08/23 18:03:11 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.248 2010/09/03 14:14:01 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -336,7 +336,7 @@ static int luaB_loadin (lua_State *L) { const char *name; lua_pushvalue(L, 1); /* environment for loaded function */ name = lua_setupvalue(L, -2, 1); - if (name == NULL || strcmp(name, "_ENV") != 0) + if (name == NULL || strcmp(name, LUA_ENV) != 0) luaL_error(L, "loaded chunk does not have environment upvalue"); } return n; diff --git a/ldebug.c b/ldebug.c index fcead7ce..01aab131 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.71 2010/06/16 13:44:36 roberto Exp roberto $ +** $Id: ldebug.c,v 2.72 2010/06/21 16:30:12 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -315,7 +315,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 = (vn && strcmp(vn, "_ENV") == 0) ? "global" : "field"; + what = (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field"; } break; } diff --git a/llex.c b/llex.c index 8ee4a7da..83ce856e 100644 --- a/llex.c +++ b/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 2.36 2010/04/05 16:35:37 roberto Exp roberto $ +** $Id: llex.c,v 2.37 2010/04/16 12:31:07 roberto Exp roberto $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -161,7 +161,7 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) { ls->linenumber = 1; ls->lastline = 1; ls->source = source; - ls->envn = luaS_new(L, "_ENV"); /* create env name */ + ls->envn = luaS_new(L, LUA_ENV); /* create env name */ luaS_fix(ls->envn); /* never collect this name */ luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ next(ls); /* read first char */ diff --git a/loadlib.c b/loadlib.c index d46f4acd..c9d7625d 100644 --- a/loadlib.c +++ b/loadlib.c @@ -1,5 +1,5 @@ /* -** $Id: loadlib.c,v 1.89 2010/07/28 15:51:59 roberto Exp roberto $ +** $Id: loadlib.c,v 1.90 2010/08/02 17:14:48 roberto Exp roberto $ ** Dynamic library loader for Lua ** See Copyright Notice in lua.h ** @@ -496,7 +496,7 @@ static int ll_require (lua_State *L) { #if defined(LUA_COMPAT_MODULE) /* -** changes the _ENV variable of calling function +** changes the environment variable of calling function */ static void set_env (lua_State *L) { lua_Debug ar; diff --git a/lparser.c b/lparser.c index c042ddc8..27919b6a 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.90 2010/07/07 16:27:29 roberto Exp roberto $ +** $Id: lparser.c,v 2.91 2010/08/23 17:32:34 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -289,7 +289,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, ls->envn, var, 1); /* get environment 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,14 +433,14 @@ static void close_func (LexState *ls) { /* ** opens the main function, which is a regular vararg function with an -** upvalue named '_ENV' +** upvalue named LUA_ENV */ static void open_mainfunc (LexState *ls, FuncState *fs) { expdesc v; open_func(ls, fs); fs->f->is_vararg = 1; /* main function is always vararg */ init_exp(&v, VLOCAL, 0); - newupvalue(fs, ls->envn, &v); /* create '_ENV' upvalue */ + newupvalue(fs, ls->envn, &v); /* create environment upvalue */ } diff --git a/luaconf.h b/luaconf.h index ad4e65f9..1b881475 100644 --- a/luaconf.h +++ b/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.141 2010/07/25 15:02:41 roberto Exp roberto $ +** $Id: luaconf.h,v 1.142 2010/07/28 15:51:59 roberto Exp roberto $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -115,6 +115,14 @@ #endif +/* +@@ LUA_ENV is the name of the variable that holds the current +@@ environment, used to access global names. +** CHANGE it if you do not like this name. +*/ +#define LUA_ENV "_ENV" + + /* @@ LUA_API is a mark for all core API functions. @@ LUALIB_API is a mark for all auxiliary library functions.