diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-06-15 16:31:22 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-06-15 16:31:22 -0300 |
| commit | b95e46621873cfb460e1d11dcd153914d5d69f86 (patch) | |
| tree | aeb1db66a8254b5f997bcfd0a8ac606eea2f621c /lapi.c | |
| parent | d406d3d05ffe8bc01d6416437963ce092cfc9772 (diff) | |
| download | lua-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
Diffstat (limited to 'lapi.c')
| -rw-r--r-- | lapi.c | 23 |
1 files changed, 10 insertions, 13 deletions
| @@ -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 | ||
| 42 | static 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 | ||
| 252 | LUA_API int lua_type (lua_State *L, int idx) { | 249 | LUA_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) { | |||
| 296 | LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { | 293 | LUA_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; |
