diff options
Diffstat (limited to 'lapi.c')
| -rw-r--r-- | lapi.c | 30 |
1 files changed, 17 insertions, 13 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 2.76 2009/04/17 22:00:01 roberto Exp $ | 2 | ** $Id: lapi.c,v 2.76 2009/04/17 22:00:01 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 | */ |
| @@ -35,20 +35,21 @@ const char lua_ident[] = | |||
| 35 | 35 | ||
| 36 | 36 | ||
| 37 | 37 | ||
| 38 | #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base)) | 38 | #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func)) |
| 39 | 39 | ||
| 40 | #define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject) | 40 | #define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject) |
| 41 | 41 | ||
| 42 | 42 | ||
| 43 | static TValue *index2adr (lua_State *L, int idx) { | 43 | static TValue *index2adr (lua_State *L, int idx) { |
| 44 | CallInfo *ci = L->ci; | ||
| 44 | if (idx > 0) { | 45 | if (idx > 0) { |
| 45 | TValue *o = L->base + (idx - 1); | 46 | TValue *o = ci->func + idx; |
| 46 | api_check(L, idx <= L->ci->top - L->base); | 47 | api_check(L, idx <= ci->top - (ci->func + 1)); |
| 47 | if (o >= L->top) return cast(TValue *, luaO_nilobject); | 48 | if (o >= L->top) return cast(TValue *, luaO_nilobject); |
| 48 | else return o; | 49 | else return o; |
| 49 | } | 50 | } |
| 50 | else if (idx > LUA_REGISTRYINDEX) { | 51 | else if (idx > LUA_REGISTRYINDEX) { |
| 51 | api_check(L, idx != 0 && -idx <= L->top - L->base); | 52 | api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1)); |
| 52 | return L->top + idx; | 53 | return L->top + idx; |
| 53 | } | 54 | } |
| 54 | else switch (idx) { /* pseudo-indices */ | 55 | else switch (idx) { /* pseudo-indices */ |
| @@ -83,13 +84,15 @@ static Table *getcurrenv (lua_State *L) { | |||
| 83 | 84 | ||
| 84 | LUA_API int lua_checkstack (lua_State *L, int size) { | 85 | LUA_API int lua_checkstack (lua_State *L, int size) { |
| 85 | int res = 1; | 86 | int res = 1; |
| 87 | CallInfo *ci = L->ci; | ||
| 86 | lua_lock(L); | 88 | lua_lock(L); |
| 87 | if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) | 89 | if (size > LUAI_MAXCSTACK || |
| 90 | (L->top - (ci->func + 1) + size) > LUAI_MAXCSTACK) | ||
| 88 | res = 0; /* stack overflow */ | 91 | res = 0; /* stack overflow */ |
| 89 | else if (size > 0) { | 92 | else if (size > 0) { |
| 90 | luaD_checkstack(L, size); | 93 | luaD_checkstack(L, size); |
| 91 | if (L->ci->top < L->top + size) | 94 | if (ci->top < L->top + size) |
| 92 | L->ci->top = L->top + size; | 95 | ci->top = L->top + size; |
| 93 | } | 96 | } |
| 94 | lua_unlock(L); | 97 | lua_unlock(L); |
| 95 | return res; | 98 | return res; |
| @@ -138,20 +141,21 @@ LUA_API void lua_checkversion_ (lua_State *L, int version) { | |||
| 138 | 141 | ||
| 139 | 142 | ||
| 140 | LUA_API int lua_gettop (lua_State *L) { | 143 | LUA_API int lua_gettop (lua_State *L) { |
| 141 | return cast_int(L->top - L->base); | 144 | return cast_int(L->top - (L->ci->func + 1)); |
| 142 | } | 145 | } |
| 143 | 146 | ||
| 144 | 147 | ||
| 145 | LUA_API void lua_settop (lua_State *L, int idx) { | 148 | LUA_API void lua_settop (lua_State *L, int idx) { |
| 149 | StkId func = L->ci->func; | ||
| 146 | lua_lock(L); | 150 | lua_lock(L); |
| 147 | if (idx >= 0) { | 151 | if (idx >= 0) { |
| 148 | api_check(L, idx <= L->stack_last - L->base); | 152 | api_check(L, idx <= L->stack_last - (func + 1)); |
| 149 | while (L->top < L->base + idx) | 153 | while (L->top < (func + 1) + idx) |
| 150 | setnilvalue(L->top++); | 154 | setnilvalue(L->top++); |
| 151 | L->top = L->base + idx; | 155 | L->top = (func + 1) + idx; |
| 152 | } | 156 | } |
| 153 | else { | 157 | else { |
| 154 | api_check(L, -(idx+1) <= (L->top - L->base)); | 158 | api_check(L, -(idx+1) <= (L->top - (func + 1))); |
| 155 | L->top += idx+1; /* `subtract' index (index is negative) */ | 159 | L->top += idx+1; /* `subtract' index (index is negative) */ |
| 156 | } | 160 | } |
| 157 | lua_unlock(L); | 161 | lua_unlock(L); |
