diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-06-29 12:06:44 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-06-29 12:06:44 -0300 |
commit | f96497397addca22f22a6ba6eeabc906be43f16b (patch) | |
tree | af8d27b9af36dfe0b0b6e0f765ea90b95b110efc /ldo.c | |
parent | 5a1c8d8ef343bf0157851a4832c2c937b812b64f (diff) | |
download | lua-f96497397addca22f22a6ba6eeabc906be43f16b.tar.gz lua-f96497397addca22f22a6ba6eeabc906be43f16b.tar.bz2 lua-f96497397addca22f22a6ba6eeabc906be43f16b.zip |
new type 'StackValue' for stack elements
(we may want to put extra info there in the future)
Diffstat (limited to 'ldo.c')
-rw-r--r-- | ldo.c | 38 |
1 files changed, 20 insertions, 18 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldo.c,v 2.159 2017/05/13 13:54:47 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 2.160 2017/05/23 12:50:11 roberto Exp roberto $ |
3 | ** Stack and Call structure of Lua | 3 | ** Stack and Call structure of Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -116,7 +116,7 @@ l_noret luaD_throw (lua_State *L, int errcode) { | |||
116 | global_State *g = G(L); | 116 | global_State *g = G(L); |
117 | L->status = cast_byte(errcode); /* mark it as dead */ | 117 | L->status = cast_byte(errcode); /* mark it as dead */ |
118 | if (g->mainthread->errorJmp) { /* main thread has a handler? */ | 118 | if (g->mainthread->errorJmp) { /* main thread has a handler? */ |
119 | setobj2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */ | 119 | setobjs2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */ |
120 | luaD_throw(g->mainthread, errcode); /* re-throw in main thread */ | 120 | luaD_throw(g->mainthread, errcode); /* re-throw in main thread */ |
121 | } | 121 | } |
122 | else { /* no handler at all; abort */ | 122 | else { /* no handler at all; abort */ |
@@ -155,12 +155,12 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { | |||
155 | ** Stack reallocation | 155 | ** Stack reallocation |
156 | ** =================================================================== | 156 | ** =================================================================== |
157 | */ | 157 | */ |
158 | static void correctstack (lua_State *L, TValue *oldstack) { | 158 | static void correctstack (lua_State *L, StkId oldstack) { |
159 | CallInfo *ci; | 159 | CallInfo *ci; |
160 | UpVal *up; | 160 | UpVal *up; |
161 | L->top = (L->top - oldstack) + L->stack; | 161 | L->top = (L->top - oldstack) + L->stack; |
162 | for (up = L->openupval; up != NULL; up = up->u.open.next) | 162 | for (up = L->openupval; up != NULL; up = up->u.open.next) |
163 | up->v = (up->v - oldstack) + L->stack; | 163 | up->v = s2v((uplevel(up) - oldstack) + L->stack); |
164 | for (ci = L->ci; ci != NULL; ci = ci->previous) { | 164 | for (ci = L->ci; ci != NULL; ci = ci->previous) { |
165 | ci->top = (ci->top - oldstack) + L->stack; | 165 | ci->top = (ci->top - oldstack) + L->stack; |
166 | ci->func = (ci->func - oldstack) + L->stack; | 166 | ci->func = (ci->func - oldstack) + L->stack; |
@@ -173,13 +173,13 @@ static void correctstack (lua_State *L, TValue *oldstack) { | |||
173 | 173 | ||
174 | 174 | ||
175 | void luaD_reallocstack (lua_State *L, int newsize) { | 175 | void luaD_reallocstack (lua_State *L, int newsize) { |
176 | TValue *oldstack = L->stack; | 176 | StkId oldstack = L->stack; |
177 | int lim = L->stacksize; | 177 | int lim = L->stacksize; |
178 | lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); | 178 | lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); |
179 | lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK); | 179 | lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK); |
180 | luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue); | 180 | luaM_reallocvector(L, L->stack, L->stacksize, newsize, StackValue); |
181 | for (; lim < newsize; lim++) | 181 | for (; lim < newsize; lim++) |
182 | setnilvalue(L->stack + lim); /* erase new segment */ | 182 | setnilvalue(s2v(L->stack + lim)); /* erase new segment */ |
183 | L->stacksize = newsize; | 183 | L->stacksize = newsize; |
184 | L->stack_last = L->stack + newsize - EXTRA_STACK; | 184 | L->stack_last = L->stack + newsize - EXTRA_STACK; |
185 | correctstack(L, oldstack); | 185 | correctstack(L, oldstack); |
@@ -294,10 +294,10 @@ static void callhook (lua_State *L, CallInfo *ci) { | |||
294 | ** it. Raise an error if __call metafield is not a function. | 294 | ** it. Raise an error if __call metafield is not a function. |
295 | */ | 295 | */ |
296 | static void tryfuncTM (lua_State *L, StkId func) { | 296 | static void tryfuncTM (lua_State *L, StkId func) { |
297 | const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL); | 297 | const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); |
298 | StkId p; | 298 | StkId p; |
299 | if (!ttisfunction(tm)) | 299 | if (!ttisfunction(tm)) |
300 | luaG_typeerror(L, func, "call"); | 300 | luaG_typeerror(L, s2v(func), "call"); |
301 | /* Open a hole inside the stack at 'func' */ | 301 | /* Open a hole inside the stack at 'func' */ |
302 | for (p = L->top; p > func; p--) | 302 | for (p = L->top; p > func; p--) |
303 | setobjs2s(L, p, p-1); | 303 | setobjs2s(L, p, p-1); |
@@ -312,14 +312,15 @@ static void tryfuncTM (lua_State *L, StkId func) { | |||
312 | ** expressions, multiple results for tail calls/single parameters) | 312 | ** expressions, multiple results for tail calls/single parameters) |
313 | ** separated. | 313 | ** separated. |
314 | */ | 314 | */ |
315 | static int moveresults (lua_State *L, const TValue *firstResult, StkId res, | 315 | static int moveresults (lua_State *L, StkId firstResult, StkId res, |
316 | int nres, int wanted) { | 316 | int nres, int wanted) { |
317 | switch (wanted) { /* handle typical cases separately */ | 317 | switch (wanted) { /* handle typical cases separately */ |
318 | case 0: break; /* nothing to move */ | 318 | case 0: break; /* nothing to move */ |
319 | case 1: { /* one result needed */ | 319 | case 1: { /* one result needed */ |
320 | if (nres == 0) /* no results? */ | 320 | if (nres == 0) /* no results? */ |
321 | firstResult = luaO_nilobject; /* adjust with nil */ | 321 | setnilvalue(s2v(res)); /* adjust with nil */ |
322 | setobjs2s(L, res, firstResult); /* move it to proper place */ | 322 | else |
323 | setobjs2s(L, res, firstResult); /* move it to proper place */ | ||
323 | break; | 324 | break; |
324 | } | 325 | } |
325 | case LUA_MULTRET: { | 326 | case LUA_MULTRET: { |
@@ -339,7 +340,7 @@ static int moveresults (lua_State *L, const TValue *firstResult, StkId res, | |||
339 | for (i = 0; i < nres; i++) /* move all results to correct place */ | 340 | for (i = 0; i < nres; i++) /* move all results to correct place */ |
340 | setobjs2s(L, res + i, firstResult + i); | 341 | setobjs2s(L, res + i, firstResult + i); |
341 | for (; i < wanted; i++) /* complete wanted number of results */ | 342 | for (; i < wanted; i++) /* complete wanted number of results */ |
342 | setnilvalue(res + i); | 343 | setnilvalue(s2v(res + i)); |
343 | } | 344 | } |
344 | break; | 345 | break; |
345 | } | 346 | } |
@@ -385,13 +386,14 @@ int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { | |||
385 | */ | 386 | */ |
386 | int luaD_precall (lua_State *L, StkId func, int nresults) { | 387 | int luaD_precall (lua_State *L, StkId func, int nresults) { |
387 | lua_CFunction f; | 388 | lua_CFunction f; |
389 | TValue *funcv = s2v(func); | ||
388 | CallInfo *ci; | 390 | CallInfo *ci; |
389 | switch (ttype(func)) { | 391 | switch (ttype(funcv)) { |
390 | case LUA_TCCL: /* C closure */ | 392 | case LUA_TCCL: /* C closure */ |
391 | f = clCvalue(func)->f; | 393 | f = clCvalue(funcv)->f; |
392 | goto Cfunc; | 394 | goto Cfunc; |
393 | case LUA_TLCF: /* light C function */ | 395 | case LUA_TLCF: /* light C function */ |
394 | f = fvalue(func); | 396 | f = fvalue(funcv); |
395 | Cfunc: { | 397 | Cfunc: { |
396 | int n; /* number of returns */ | 398 | int n; /* number of returns */ |
397 | checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ | 399 | checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ |
@@ -411,12 +413,12 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { | |||
411 | return 1; | 413 | return 1; |
412 | } | 414 | } |
413 | case LUA_TLCL: { /* Lua function: prepare its call */ | 415 | case LUA_TLCL: { /* Lua function: prepare its call */ |
414 | Proto *p = clLvalue(func)->p; | 416 | Proto *p = clLvalue(funcv)->p; |
415 | int n = cast_int(L->top - func) - 1; /* number of real arguments */ | 417 | int n = cast_int(L->top - func) - 1; /* number of real arguments */ |
416 | int fsize = p->maxstacksize; /* frame size */ | 418 | int fsize = p->maxstacksize; /* frame size */ |
417 | checkstackp(L, fsize, func); | 419 | checkstackp(L, fsize, func); |
418 | for (; n < p->numparams - p->is_vararg; n++) | 420 | for (; n < p->numparams - p->is_vararg; n++) |
419 | setnilvalue(L->top++); /* complete missing arguments */ | 421 | setnilvalue(s2v(L->top++)); /* complete missing arguments */ |
420 | if (p->is_vararg) | 422 | if (p->is_vararg) |
421 | luaT_adjustvarargs(L, p, n); | 423 | luaT_adjustvarargs(L, p, n); |
422 | ci = next_ci(L); /* now 'enter' new function */ | 424 | ci = next_ci(L); /* now 'enter' new function */ |