Skip to content

Commit 413a393

Browse files
committed
Stack indices changed to union's
That will allow to change pointers to offsets while reallocating the stack.
1 parent ba089bc commit 413a393

File tree

19 files changed

+384
-370
lines changed

19 files changed

+384
-370
lines changed

lapi.c

Lines changed: 119 additions & 116 deletions
Large diffs are not rendered by default.

lapi.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,26 @@
1212
#include "lstate.h"
1313

1414

15-
/* Increments 'L->top', checking for stack overflows */
16-
#define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \
17-
"stack overflow");}
15+
/* Increments 'L->top.p', checking for stack overflows */
16+
#define api_incr_top(L) {L->top.p++; \
17+
api_check(L, L->top.p <= L->ci->top.p, \
18+
"stack overflow");}
1819

1920

2021
/*
2122
** If a call returns too many multiple returns, the callee may not have
2223
** stack space to accommodate all results. In this case, this macro
23-
** increases its stack space ('L->ci->top').
24+
** increases its stack space ('L->ci->top.p').
2425
*/
2526
#define adjustresults(L,nres) \
26-
{ if ((nres) <= LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; }
27+
{ if ((nres) <= LUA_MULTRET && L->ci->top.p < L->top.p) \
28+
L->ci->top.p = L->top.p; }
2729

2830

2931
/* Ensure the stack has at least 'n' elements */
30-
#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \
31-
"not enough elements in the stack")
32+
#define api_checknelems(L,n) \
33+
api_check(L, (n) < (L->top.p - L->ci->func.p), \
34+
"not enough elements in the stack")
3235

3336

3437
/*

ldebug.c

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ static const char *upvalname (const Proto *p, int uv) {
182182

183183

184184
static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
185-
if (clLvalue(s2v(ci->func))->p->is_vararg) {
185+
if (clLvalue(s2v(ci->func.p))->p->is_vararg) {
186186
int nextra = ci->u.l.nextraargs;
187187
if (n >= -nextra) { /* 'n' is negative */
188-
*pos = ci->func - nextra - (n + 1);
188+
*pos = ci->func.p - nextra - (n + 1);
189189
return "(vararg)"; /* generic name for any vararg */
190190
}
191191
}
@@ -194,7 +194,7 @@ static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
194194

195195

196196
const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) {
197-
StkId base = ci->func + 1;
197+
StkId base = ci->func.p + 1;
198198
const char *name = NULL;
199199
if (isLua(ci)) {
200200
if (n < 0) /* access to vararg values? */
@@ -203,7 +203,7 @@ const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) {
203203
name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
204204
}
205205
if (name == NULL) { /* no 'standard' name? */
206-
StkId limit = (ci == L->ci) ? L->top : ci->next->func;
206+
StkId limit = (ci == L->ci) ? L->top.p : ci->next->func.p;
207207
if (limit - base >= n && n > 0) { /* is 'n' inside 'ci' stack? */
208208
/* generic name for any valid slot */
209209
name = isLua(ci) ? "(temporary)" : "(C temporary)";
@@ -221,16 +221,16 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
221221
const char *name;
222222
lua_lock(L);
223223
if (ar == NULL) { /* information about non-active function? */
224-
if (!isLfunction(s2v(L->top - 1))) /* not a Lua function? */
224+
if (!isLfunction(s2v(L->top.p - 1))) /* not a Lua function? */
225225
name = NULL;
226226
else /* consider live variables at function start (parameters) */
227-
name = luaF_getlocalname(clLvalue(s2v(L->top - 1))->p, n, 0);
227+
name = luaF_getlocalname(clLvalue(s2v(L->top.p - 1))->p, n, 0);
228228
}
229229
else { /* active function; get information through 'ar' */
230230
StkId pos = NULL; /* to avoid warnings */
231231
name = luaG_findlocal(L, ar->i_ci, n, &pos);
232232
if (name) {
233-
setobjs2s(L, L->top, pos);
233+
setobjs2s(L, L->top.p, pos);
234234
api_incr_top(L);
235235
}
236236
}
@@ -245,8 +245,8 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
245245
lua_lock(L);
246246
name = luaG_findlocal(L, ar->i_ci, n, &pos);
247247
if (name) {
248-
setobjs2s(L, pos, L->top - 1);
249-
L->top--; /* pop value */
248+
setobjs2s(L, pos, L->top.p - 1);
249+
L->top.p--; /* pop value */
250250
}
251251
lua_unlock(L);
252252
return name;
@@ -289,7 +289,7 @@ static int nextline (const Proto *p, int currentline, int pc) {
289289

290290
static void collectvalidlines (lua_State *L, Closure *f) {
291291
if (noLuaClosure(f)) {
292-
setnilvalue(s2v(L->top));
292+
setnilvalue(s2v(L->top.p));
293293
api_incr_top(L);
294294
}
295295
else {
@@ -298,7 +298,7 @@ static void collectvalidlines (lua_State *L, Closure *f) {
298298
const Proto *p = f->l.p;
299299
int currentline = p->linedefined;
300300
Table *t = luaH_new(L); /* new table to store active lines */
301-
sethvalue2s(L, L->top, t); /* push it on stack */
301+
sethvalue2s(L, L->top.p, t); /* push it on stack */
302302
api_incr_top(L);
303303
setbtvalue(&v); /* boolean 'true' to be the value of all indices */
304304
if (!p->is_vararg) /* regular function? */
@@ -388,20 +388,20 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
388388
lua_lock(L);
389389
if (*what == '>') {
390390
ci = NULL;
391-
func = s2v(L->top - 1);
391+
func = s2v(L->top.p - 1);
392392
api_check(L, ttisfunction(func), "function expected");
393393
what++; /* skip the '>' */
394-
L->top--; /* pop function */
394+
L->top.p--; /* pop function */
395395
}
396396
else {
397397
ci = ar->i_ci;
398-
func = s2v(ci->func);
398+
func = s2v(ci->func.p);
399399
lua_assert(ttisfunction(func));
400400
}
401401
cl = ttisclosure(func) ? clvalue(func) : NULL;
402402
status = auxgetinfo(L, what, ar, cl, ci);
403403
if (strchr(what, 'f')) {
404-
setobj2s(L, L->top, func);
404+
setobj2s(L, L->top.p, func);
405405
api_incr_top(L);
406406
}
407407
if (strchr(what, 'L'))
@@ -663,7 +663,7 @@ static const char *funcnamefromcall (lua_State *L, CallInfo *ci,
663663
*/
664664
static int isinstack (CallInfo *ci, const TValue *o) {
665665
StkId pos;
666-
for (pos = ci->func + 1; pos < ci->top; pos++) {
666+
for (pos = ci->func.p + 1; pos < ci->top.p; pos++) {
667667
if (o == s2v(pos))
668668
return 1;
669669
}
@@ -681,7 +681,7 @@ static const char *getupvalname (CallInfo *ci, const TValue *o,
681681
LClosure *c = ci_func(ci);
682682
int i;
683683
for (i = 0; i < c->nupvalues; i++) {
684-
if (c->upvals[i]->v == o) {
684+
if (c->upvals[i]->v.p == o) {
685685
*name = upvalname(c->p, i);
686686
return "upvalue";
687687
}
@@ -710,7 +710,7 @@ static const char *varinfo (lua_State *L, const TValue *o) {
710710
kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
711711
if (!kind && isinstack(ci, o)) /* no? try a register */
712712
kind = getobjname(ci_func(ci)->p, currentpc(ci),
713-
cast_int(cast(StkId, o) - (ci->func + 1)), &name);
713+
cast_int(cast(StkId, o) - (ci->func.p + 1)), &name);
714714
}
715715
return formatvarinfo(L, kind, name);
716716
}
@@ -807,10 +807,10 @@ l_noret luaG_errormsg (lua_State *L) {
807807
if (L->errfunc != 0) { /* is there an error handling function? */
808808
StkId errfunc = restorestack(L, L->errfunc);
809809
lua_assert(ttisfunction(s2v(errfunc)));
810-
setobjs2s(L, L->top, L->top - 1); /* move argument */
811-
setobjs2s(L, L->top - 1, errfunc); /* push function */
812-
L->top++; /* assume EXTRA_STACK */
813-
luaD_callnoyield(L, L->top - 2, 1); /* call it */
810+
setobjs2s(L, L->top.p, L->top.p - 1); /* move argument */
811+
setobjs2s(L, L->top.p - 1, errfunc); /* push function */
812+
L->top.p++; /* assume EXTRA_STACK */
813+
luaD_callnoyield(L, L->top.p - 2, 1); /* call it */
814814
}
815815
luaD_throw(L, LUA_ERRRUN);
816816
}
@@ -826,8 +826,8 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
826826
va_end(argp);
827827
if (isLua(ci)) { /* if Lua function, add source:line information */
828828
luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci));
829-
setobjs2s(L, L->top - 2, L->top - 1); /* remove 'msg' from the stack */
830-
L->top--;
829+
setobjs2s(L, L->top.p - 2, L->top.p - 1); /* remove 'msg' */
830+
L->top.p--;
831831
}
832832
luaG_errormsg(L);
833833
}
@@ -872,7 +872,7 @@ static int changedline (const Proto *p, int oldpc, int newpc) {
872872
** invalid; if so, use zero as a valid value. (A wrong but valid 'oldpc'
873873
** at most causes an extra call to a line hook.)
874874
** This function is not "Protected" when called, so it should correct
875-
** 'L->top' before calling anything that can run the GC.
875+
** 'L->top.p' before calling anything that can run the GC.
876876
*/
877877
int luaG_traceexec (lua_State *L, const Instruction *pc) {
878878
CallInfo *ci = L->ci;
@@ -895,7 +895,7 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) {
895895
return 1; /* do not call hook again (VM yielded, so it did not move) */
896896
}
897897
if (!isIT(*(ci->u.l.savedpc - 1))) /* top not being used? */
898-
L->top = ci->top; /* correct top */
898+
L->top.p = ci->top.p; /* correct top */
899899
if (counthook)
900900
luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */
901901
if (mask & LUA_MASKLINE) {

ldebug.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
/* Active Lua function (given call info) */
18-
#define ci_func(ci) (clLvalue(s2v((ci)->func)))
18+
#define ci_func(ci) (clLvalue(s2v((ci)->func.p)))
1919

2020

2121
#define resethookcount(L) (L->hookcount = L->basehookcount)

0 commit comments

Comments
 (0)