commit b95e46621873cfb460e1d11dcd153914d5d69f86
parent d406d3d05ffe8bc01d6416437963ce092cfc9772
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Fri, 15 Jun 2018 16:30:55 -0300
new field 'nilvalue' in struct 'global_State' to avoid the use of
addresses of static variables
Diffstat:
5 files changed, 17 insertions(+), 22 deletions(-)
diff --git a/lapi.c b/lapi.c
@@ -1,5 +1,5 @@
/*
-** $Id: lapi.c,v 2.292 2018/06/01 17:40:38 roberto Exp roberto $
+** $Id: lapi.c,v 2.293 2018/06/15 17:30:52 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
@@ -37,13 +37,10 @@ const char lua_ident[] =
"$LuaAuthors: " LUA_AUTHORS " $";
-/* value at a non-valid index */
-static const TValue nonvalidvaluep = {NILCONSTANT};
-#define NONVALIDVALUE cast(TValue *, &nonvalidvaluep)
+/* test for a valid index */
+#define isvalid(L, o) (!ttisnil(o) || o != &G(L)->nilvalue)
-/* corresponding test */
-#define isvalid(o) ((o) != &nonvalidvaluep)
/* test for pseudo index */
#define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
@@ -57,7 +54,7 @@ static TValue *index2value (lua_State *L, int idx) {
if (idx > 0) {
StkId o = ci->func + idx;
api_check(L, idx <= L->ci->top - (ci->func + 1), "unacceptable index");
- if (o >= L->top) return NONVALIDVALUE;
+ if (o >= L->top) return &G(L)->nilvalue;
else return s2v(o);
}
else if (!ispseudo(idx)) { /* negative index */
@@ -70,10 +67,10 @@ static TValue *index2value (lua_State *L, int idx) {
idx = LUA_REGISTRYINDEX - idx;
api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
if (ttislcf(s2v(ci->func))) /* light C function? */
- return NONVALIDVALUE; /* it has no upvalues */
+ return &G(L)->nilvalue; /* it has no upvalues */
else {
CClosure *func = clCvalue(s2v(ci->func));
- return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE;
+ return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : &G(L)->nilvalue;
}
}
}
@@ -225,7 +222,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
lua_lock(L);
fr = index2value(L, fromidx);
to = index2value(L, toidx);
- api_check(l, isvalid(to), "invalid index");
+ api_check(l, isvalid(L, to), "invalid index");
setobj(L, to, fr);
if (isupvalue(toidx)) /* function upvalue? */
luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr);
@@ -251,7 +248,7 @@ LUA_API void lua_pushvalue (lua_State *L, int idx) {
LUA_API int lua_type (lua_State *L, int idx) {
const TValue *o = index2value(L, idx);
- return (isvalid(o) ? ttype(o) : LUA_TNONE);
+ return (isvalid(L, o) ? ttype(o) : LUA_TNONE);
}
@@ -296,7 +293,7 @@ LUA_API int lua_isuserdata (lua_State *L, int idx) {
LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
const TValue *o1 = index2value(L, index1);
const TValue *o2 = index2value(L, index2);
- return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0;
+ return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0;
}
@@ -323,7 +320,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
lua_lock(L); /* may call tag method */
o1 = index2value(L, index1);
o2 = index2value(L, index2);
- if (isvalid(o1) && isvalid(o2)) {
+ if (isvalid(L, o1) && isvalid(L, o2)) {
switch (op) {
case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
diff --git a/lobject.h b/lobject.h
@@ -1,5 +1,5 @@
/*
-** $Id: lobject.h,v 2.144 2018/06/01 17:40:38 roberto Exp roberto $
+** $Id: lobject.h,v 2.145 2018/06/15 14:14:20 roberto Exp roberto $
** Type definitions for Lua objects
** See Copyright Notice in lua.h
*/
@@ -144,9 +144,6 @@ typedef StackValue *StkId; /* index to stack elements */
#define ttisstrictnil(o) checktag((o), LUA_TNIL)
-/* macro defining a nil value */
-#define NILCONSTANT {NULL}, LUA_TNIL
-
#define setnilvalue(obj) settt_(obj, LUA_TNIL)
diff --git a/lstate.c b/lstate.c
@@ -1,5 +1,5 @@
/*
-** $Id: lstate.c,v 2.152 2018/05/29 18:02:51 roberto Exp roberto $
+** $Id: lstate.c,v 2.153 2018/06/01 17:40:38 roberto Exp roberto $
** Global State
** See Copyright Notice in lua.h
*/
@@ -345,6 +345,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
g->twups = NULL;
g->totalbytes = sizeof(LG);
g->GCdebt = 0;
+ setnilvalue(&g->nilvalue);
setgcparam(g->gcpause, LUAI_GCPAUSE);
setgcparam(g->gcstepmul, LUAI_GCMUL);
g->gcstepsize = LUAI_GCSTEPSIZE;
diff --git a/lstate.h b/lstate.h
@@ -1,5 +1,5 @@
/*
-** $Id: lstate.h,v 2.157 2018/02/25 12:43:52 roberto Exp roberto $
+** $Id: lstate.h,v 2.158 2018/03/16 15:33:34 roberto Exp roberto $
** Global State
** See Copyright Notice in lua.h
*/
@@ -148,6 +148,7 @@ typedef struct global_State {
lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
stringtable strt; /* hash table for strings */
TValue l_registry;
+ TValue nilvalue; /* a nil value */
unsigned int seed; /* randomized seed for hashes */
lu_byte currentwhite;
lu_byte gcstate; /* state of garbage collector */
diff --git a/ltm.c b/ltm.c
@@ -1,5 +1,5 @@
/*
-** $Id: ltm.c,v 2.68 2018/06/01 17:40:38 roberto Exp roberto $
+** $Id: ltm.c,v 2.69 2018/06/08 19:06:59 roberto Exp roberto $
** Tag methods
** See Copyright Notice in lua.h
*/
@@ -69,7 +69,6 @@ const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
- static const TValue nilobject = {NILCONSTANT};
Table *mt;
switch (ttype(o)) {
case LUA_TTABLE:
@@ -81,7 +80,7 @@ const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
default:
mt = G(L)->mt[ttype(o)];
}
- return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : &nilobject);
+ return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue);
}