| 54 | |
| 55 | |
| 56 | static TValue *index2value (lua_State *L, int idx) { |
| 57 | CallInfo *ci = L->ci; |
| 58 | if (idx > 0) { |
| 59 | StkId o = ci->func + idx; |
| 60 | api_check(L, idx <= L->ci->top - (ci->func + 1), "unacceptable index"); |
| 61 | if (o >= L->top) return &G(L)->nilvalue; |
| 62 | else return s2v(o); |
| 63 | } |
| 64 | else if (!ispseudo(idx)) { /* negative index */ |
| 65 | api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); |
| 66 | return s2v(L->top + idx); |
| 67 | } |
| 68 | else if (idx == LUA_REGISTRYINDEX) |
| 69 | return &G(L)->l_registry; |
| 70 | else { /* upvalues */ |
| 71 | idx = LUA_REGISTRYINDEX - idx; |
| 72 | api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); |
| 73 | if (ttislcf(s2v(ci->func))) /* light C function? */ |
| 74 | return &G(L)->nilvalue; /* it has no upvalues */ |
| 75 | else { |
| 76 | CClosure *func = clCvalue(s2v(ci->func)); |
| 77 | return (idx <= func->nupvalues) ? &func->upvalue[idx-1] |
| 78 | : &G(L)->nilvalue; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
| 84 | static StkId index2stack (lua_State *L, int idx) { |
no outgoing calls
no test coverage detected