[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Ravi bug related to setting of L->top in OP_RETURN
- From: Roberto Ierusalimschy <roberto@...>
- Date: Wed, 5 Aug 2015 10:49:06 -0300
> I am investigating a particularly difficult bug related to setting of
> L->top, and would appreciate some insight on what L->top should be
> when a function returns via OP_RETURN.
>
> The Lua code does following in OP_RETURN:
>
> int b = GETARG_B(i);
> if (cl->p->sizep > 0) luaF_close(L, base);
> b = luaD_poscall(L, ra, (b != 0 ? b - 1 : L->top - ra));
> if (!(ci->callstatus & CIST_REENTRY)) /* 'ci' still the called one */
> return; /* external invocation: return */
> else { /* invocation via reentry: continue execution */
> ci = L->ci;
> if (b) L->top = ci->top;
> lua_assert(isLua(ci));
> lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL);
> goto newframe; /* restart luaV_execute over new Lua function */
> }
>
> It is not clear to me why in the external invocation case, if
> following code is not executed:
>
> ci = L->ci;
> if (b) L->top = ci->top;
Both statements here prepare 'luaV_execute' to run the function
"OP_RETURN" is returning *to*. (The previous call to 'luaD_poscall'
already updated 'L->ci' to point to this previous entry.) When
the running function (the one returning *from*) was called
externally (e.g., from a C function), 'luaV_execute' returns,
so this preparation would be meaningless.
When 'b' is zero, this return must return all values on the stack
(e.g., 'return ...', and L->top must be kept for the next instruction
(after the OP_CALL to where we are returning) to know how many
results the function have (see next answer).
> A related question is what is the meaning of the return value from
> luaD_poscall() - it seems to be a signal for L-top to be reset to
> ci->top, but I am not sure I understand when this happens.
It happens when the function that called the one being returned needs
multiple returns. As I explained, in that case L->top limits the
number of values being returned, and will be corrected by the
instruction following the OP_CALL (see comments in 'lopcdes.h').
-- Roberto