| 47 | |
| 48 | |
| 49 | static TValue *index2adr (lua_State *L, int idx) { |
| 50 | if (idx > 0) { |
| 51 | TValue *o = L->base + (idx - 1); |
| 52 | api_check(L, idx <= L->ci->top - L->base); |
| 53 | if (o >= L->top) return cast(TValue *, luaO_nilobject); |
| 54 | else return o; |
| 55 | } |
| 56 | else if (idx > LUA_REGISTRYINDEX) { |
| 57 | api_check(L, idx != 0 && -idx <= L->top - L->base); |
| 58 | return L->top + idx; |
| 59 | } |
| 60 | else switch (idx) { /* pseudo-indices */ |
| 61 | case LUA_REGISTRYINDEX: return registry(L); |
| 62 | case LUA_ENVIRONINDEX: { |
| 63 | Closure *func = curr_func(L); |
| 64 | sethvalue(L, &L->env, func->c.env); |
| 65 | return &L->env; |
| 66 | } |
| 67 | case LUA_GLOBALSINDEX: return gt(L); |
| 68 | default: { |
| 69 | Closure *func = curr_func(L); |
| 70 | idx = LUA_GLOBALSINDEX - idx; |
| 71 | return (idx <= func->c.nupvalues) |
| 72 | ? &func->c.upvalue[idx-1] |
| 73 | : cast(TValue *, luaO_nilobject); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | static Table *getcurrenv (lua_State *L) { |
no test coverage detected