aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-06-15 16:31:22 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-06-15 16:31:22 -0300
commitb95e46621873cfb460e1d11dcd153914d5d69f86 (patch)
treeaeb1db66a8254b5f997bcfd0a8ac606eea2f621c
parentd406d3d05ffe8bc01d6416437963ce092cfc9772 (diff)
downloadlua-b95e46621873cfb460e1d11dcd153914d5d69f86.tar.gz
lua-b95e46621873cfb460e1d11dcd153914d5d69f86.tar.bz2
lua-b95e46621873cfb460e1d11dcd153914d5d69f86.zip
new field 'nilvalue' in struct 'global_State' to avoid the use of
addresses of static variables
-rw-r--r--lapi.c23
-rw-r--r--lobject.h5
-rw-r--r--lstate.c3
-rw-r--r--lstate.h3
-rw-r--r--ltm.c5
5 files changed, 17 insertions, 22 deletions
diff --git a/lapi.c b/lapi.c
index 157c22b2..5d106076 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 2.292 2018/06/01 17:40:38 roberto Exp roberto $ 2** $Id: lapi.c,v 2.293 2018/06/15 17:30:52 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -37,13 +37,10 @@ const char lua_ident[] =
37 "$LuaAuthors: " LUA_AUTHORS " $"; 37 "$LuaAuthors: " LUA_AUTHORS " $";
38 38
39 39
40/* value at a non-valid index */
41 40
42static const TValue nonvalidvaluep = {NILCONSTANT}; 41/* test for a valid index */
43#define NONVALIDVALUE cast(TValue *, &nonvalidvaluep) 42#define isvalid(L, o) (!ttisnil(o) || o != &G(L)->nilvalue)
44 43
45/* corresponding test */
46#define isvalid(o) ((o) != &nonvalidvaluep)
47 44
48/* test for pseudo index */ 45/* test for pseudo index */
49#define ispseudo(i) ((i) <= LUA_REGISTRYINDEX) 46#define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
@@ -57,7 +54,7 @@ static TValue *index2value (lua_State *L, int idx) {
57 if (idx > 0) { 54 if (idx > 0) {
58 StkId o = ci->func + idx; 55 StkId o = ci->func + idx;
59 api_check(L, idx <= L->ci->top - (ci->func + 1), "unacceptable index"); 56 api_check(L, idx <= L->ci->top - (ci->func + 1), "unacceptable index");
60 if (o >= L->top) return NONVALIDVALUE; 57 if (o >= L->top) return &G(L)->nilvalue;
61 else return s2v(o); 58 else return s2v(o);
62 } 59 }
63 else if (!ispseudo(idx)) { /* negative index */ 60 else if (!ispseudo(idx)) { /* negative index */
@@ -70,10 +67,10 @@ static TValue *index2value (lua_State *L, int idx) {
70 idx = LUA_REGISTRYINDEX - idx; 67 idx = LUA_REGISTRYINDEX - idx;
71 api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); 68 api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
72 if (ttislcf(s2v(ci->func))) /* light C function? */ 69 if (ttislcf(s2v(ci->func))) /* light C function? */
73 return NONVALIDVALUE; /* it has no upvalues */ 70 return &G(L)->nilvalue; /* it has no upvalues */
74 else { 71 else {
75 CClosure *func = clCvalue(s2v(ci->func)); 72 CClosure *func = clCvalue(s2v(ci->func));
76 return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE; 73 return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : &G(L)->nilvalue;
77 } 74 }
78 } 75 }
79} 76}
@@ -225,7 +222,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
225 lua_lock(L); 222 lua_lock(L);
226 fr = index2value(L, fromidx); 223 fr = index2value(L, fromidx);
227 to = index2value(L, toidx); 224 to = index2value(L, toidx);
228 api_check(l, isvalid(to), "invalid index"); 225 api_check(l, isvalid(L, to), "invalid index");
229 setobj(L, to, fr); 226 setobj(L, to, fr);
230 if (isupvalue(toidx)) /* function upvalue? */ 227 if (isupvalue(toidx)) /* function upvalue? */
231 luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr); 228 luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr);
@@ -251,7 +248,7 @@ LUA_API void lua_pushvalue (lua_State *L, int idx) {
251 248
252LUA_API int lua_type (lua_State *L, int idx) { 249LUA_API int lua_type (lua_State *L, int idx) {
253 const TValue *o = index2value(L, idx); 250 const TValue *o = index2value(L, idx);
254 return (isvalid(o) ? ttype(o) : LUA_TNONE); 251 return (isvalid(L, o) ? ttype(o) : LUA_TNONE);
255} 252}
256 253
257 254
@@ -296,7 +293,7 @@ LUA_API int lua_isuserdata (lua_State *L, int idx) {
296LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { 293LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
297 const TValue *o1 = index2value(L, index1); 294 const TValue *o1 = index2value(L, index1);
298 const TValue *o2 = index2value(L, index2); 295 const TValue *o2 = index2value(L, index2);
299 return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0; 296 return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0;
300} 297}
301 298
302 299
@@ -323,7 +320,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
323 lua_lock(L); /* may call tag method */ 320 lua_lock(L); /* may call tag method */
324 o1 = index2value(L, index1); 321 o1 = index2value(L, index1);
325 o2 = index2value(L, index2); 322 o2 = index2value(L, index2);
326 if (isvalid(o1) && isvalid(o2)) { 323 if (isvalid(L, o1) && isvalid(L, o2)) {
327 switch (op) { 324 switch (op) {
328 case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break; 325 case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
329 case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break; 326 case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
diff --git a/lobject.h b/lobject.h
index 7c2b3f84..dbb9982d 100644
--- a/lobject.h
+++ b/lobject.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.h,v 2.144 2018/06/01 17:40:38 roberto Exp roberto $ 2** $Id: lobject.h,v 2.145 2018/06/15 14:14:20 roberto Exp roberto $
3** Type definitions for Lua objects 3** Type definitions for Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -144,9 +144,6 @@ typedef StackValue *StkId; /* index to stack elements */
144#define ttisstrictnil(o) checktag((o), LUA_TNIL) 144#define ttisstrictnil(o) checktag((o), LUA_TNIL)
145 145
146 146
147/* macro defining a nil value */
148#define NILCONSTANT {NULL}, LUA_TNIL
149
150#define setnilvalue(obj) settt_(obj, LUA_TNIL) 147#define setnilvalue(obj) settt_(obj, LUA_TNIL)
151 148
152 149
diff --git a/lstate.c b/lstate.c
index 2622a1d9..e369683e 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.c,v 2.152 2018/05/29 18:02:51 roberto Exp roberto $ 2** $Id: lstate.c,v 2.153 2018/06/01 17:40:38 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -345,6 +345,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
345 g->twups = NULL; 345 g->twups = NULL;
346 g->totalbytes = sizeof(LG); 346 g->totalbytes = sizeof(LG);
347 g->GCdebt = 0; 347 g->GCdebt = 0;
348 setnilvalue(&g->nilvalue);
348 setgcparam(g->gcpause, LUAI_GCPAUSE); 349 setgcparam(g->gcpause, LUAI_GCPAUSE);
349 setgcparam(g->gcstepmul, LUAI_GCMUL); 350 setgcparam(g->gcstepmul, LUAI_GCMUL);
350 g->gcstepsize = LUAI_GCSTEPSIZE; 351 g->gcstepsize = LUAI_GCSTEPSIZE;
diff --git a/lstate.h b/lstate.h
index cd8b4db7..037b2be6 100644
--- a/lstate.h
+++ b/lstate.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.h,v 2.157 2018/02/25 12:43:52 roberto Exp roberto $ 2** $Id: lstate.h,v 2.158 2018/03/16 15:33:34 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -148,6 +148,7 @@ typedef struct global_State {
148 lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ 148 lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
149 stringtable strt; /* hash table for strings */ 149 stringtable strt; /* hash table for strings */
150 TValue l_registry; 150 TValue l_registry;
151 TValue nilvalue; /* a nil value */
151 unsigned int seed; /* randomized seed for hashes */ 152 unsigned int seed; /* randomized seed for hashes */
152 lu_byte currentwhite; 153 lu_byte currentwhite;
153 lu_byte gcstate; /* state of garbage collector */ 154 lu_byte gcstate; /* state of garbage collector */
diff --git a/ltm.c b/ltm.c
index 551a2b97..d4e2b447 100644
--- a/ltm.c
+++ b/ltm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.c,v 2.68 2018/06/01 17:40:38 roberto Exp roberto $ 2** $Id: ltm.c,v 2.69 2018/06/08 19:06:59 roberto Exp roberto $
3** Tag methods 3** Tag methods
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -69,7 +69,6 @@ const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
69 69
70 70
71const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { 71const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
72 static const TValue nilobject = {NILCONSTANT};
73 Table *mt; 72 Table *mt;
74 switch (ttype(o)) { 73 switch (ttype(o)) {
75 case LUA_TTABLE: 74 case LUA_TTABLE:
@@ -81,7 +80,7 @@ const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
81 default: 80 default:
82 mt = G(L)->mt[ttype(o)]; 81 mt = G(L)->mt[ttype(o)];
83 } 82 }
84 return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : &nilobject); 83 return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue);
85} 84}
86 85
87 86