diff options
| -rw-r--r-- | lapi.c | 25 |
1 files changed, 13 insertions, 12 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 2.150 2011/08/09 20:58:29 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.151 2011/08/17 20:26:47 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 | */ |
| @@ -34,9 +34,13 @@ const char lua_ident[] = | |||
| 34 | "$LuaAuthors: " LUA_AUTHORS " $"; | 34 | "$LuaAuthors: " LUA_AUTHORS " $"; |
| 35 | 35 | ||
| 36 | 36 | ||
| 37 | /* value at a non-valid index */ | ||
| 38 | #define NONVALIDVALUE cast(TValue *, luaO_nilobject) | ||
| 37 | 39 | ||
| 38 | #define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject, \ | 40 | /* corresponding test */ |
| 39 | "invalid index") | 41 | #define isvalid(o) ((o) != luaO_nilobject) |
| 42 | |||
| 43 | #define api_checkvalidindex(L, i) api_check(L, isvalid(i), "invalid index") | ||
| 40 | 44 | ||
| 41 | 45 | ||
| 42 | static TValue *index2addr (lua_State *L, int idx) { | 46 | static TValue *index2addr (lua_State *L, int idx) { |
| @@ -44,7 +48,7 @@ static TValue *index2addr (lua_State *L, int idx) { | |||
| 44 | if (idx > 0) { | 48 | if (idx > 0) { |
| 45 | TValue *o = ci->func + idx; | 49 | TValue *o = ci->func + idx; |
| 46 | api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index"); | 50 | api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index"); |
| 47 | if (o >= L->top) return cast(TValue *, luaO_nilobject); | 51 | if (o >= L->top) return NONVALIDVALUE; |
| 48 | else return o; | 52 | else return o; |
| 49 | } | 53 | } |
| 50 | else if (idx > LUA_REGISTRYINDEX) { | 54 | else if (idx > LUA_REGISTRYINDEX) { |
| @@ -57,12 +61,10 @@ static TValue *index2addr (lua_State *L, int idx) { | |||
| 57 | idx = LUA_REGISTRYINDEX - idx; | 61 | idx = LUA_REGISTRYINDEX - idx; |
| 58 | api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); | 62 | api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); |
| 59 | if (ttislcf(ci->func)) /* light C function? */ | 63 | if (ttislcf(ci->func)) /* light C function? */ |
| 60 | return cast(TValue *, luaO_nilobject); /* it has no upvalues */ | 64 | return NONVALIDVALUE; /* it has no upvalues */ |
| 61 | else { | 65 | else { |
| 62 | CClosure *func = clCvalue(ci->func); | 66 | CClosure *func = clCvalue(ci->func); |
| 63 | return (idx <= func->nupvalues) | 67 | return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE; |
| 64 | ? &func->upvalue[idx-1] | ||
| 65 | : cast(TValue *, luaO_nilobject); | ||
| 66 | } | 68 | } |
| 67 | } | 69 | } |
| 68 | } | 70 | } |
| @@ -237,7 +239,7 @@ LUA_API void lua_pushvalue (lua_State *L, int idx) { | |||
| 237 | 239 | ||
| 238 | LUA_API int lua_type (lua_State *L, int idx) { | 240 | LUA_API int lua_type (lua_State *L, int idx) { |
| 239 | StkId o = index2addr(L, idx); | 241 | StkId o = index2addr(L, idx); |
| 240 | return (o == luaO_nilobject) ? LUA_TNONE : ttypenv(o); | 242 | return (isvalid(o) ? ttypenv(o) : LUA_TNONE); |
| 241 | } | 243 | } |
| 242 | 244 | ||
| 243 | 245 | ||
| @@ -275,8 +277,7 @@ LUA_API int lua_isuserdata (lua_State *L, int idx) { | |||
| 275 | LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { | 277 | LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { |
| 276 | StkId o1 = index2addr(L, index1); | 278 | StkId o1 = index2addr(L, index1); |
| 277 | StkId o2 = index2addr(L, index2); | 279 | StkId o2 = index2addr(L, index2); |
| 278 | return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 | 280 | return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0; |
| 279 | : luaV_rawequalobj(o1, o2); | ||
| 280 | } | 281 | } |
| 281 | 282 | ||
| 282 | 283 | ||
| @@ -309,7 +310,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { | |||
| 309 | lua_lock(L); /* may call tag method */ | 310 | lua_lock(L); /* may call tag method */ |
| 310 | o1 = index2addr(L, index1); | 311 | o1 = index2addr(L, index1); |
| 311 | o2 = index2addr(L, index2); | 312 | o2 = index2addr(L, index2); |
| 312 | if (o1 == luaO_nilobject || o2 == luaO_nilobject) | 313 | if (!isvalid(o1) || !isvalid(o2)) |
| 313 | i = 0; | 314 | i = 0; |
| 314 | else switch (op) { | 315 | else switch (op) { |
| 315 | case LUA_OPEQ: i = equalobj(L, o1, o2); break; | 316 | case LUA_OPEQ: i = equalobj(L, o1, o2); break; |
