lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 12 August 2015 at 16:23, 云风 Cloud Wu <cloudwu@gmail.com> wrote:
>> > Is it possible to consider an implementation where L->top is strictly
>> > the top of the stack, and not used for a dual purpose? Or am I missing
>> > a point here?
>>
>> Can you suggest how to do such implementation?
>
>

My original problem was that the the behaviour of luaV_execute() is
different from the way a Lua function returns from OP_RETURN. So I
think maybe this is the only case that is interesting from the point
of view of resolving the original issue.

So then narrowing down to the following requirement:

The desired behaviour is that if in OP_CALL invoked a new
luaV_execute() to run the Lua function instead of reentering the
current invocation of luaV_execute() we should get the same result;
this I believe is not the case currently as there is an assumption
that luaV_execute() will always be called by an external C function.

I haven't thought this through yet but following are my initial
thoughts re a potential solution.

For C functions, the function returns a value indicating how many
parameters were returned - and then the caller saves the values they
need.

For Lua functions, the OP_RETURN  instruction actually saves the return values.

So what if the two were harmonised - i.e. the OP_RETURN also behaves
like the C function by returning how many values were returned, and
the adjustment is made by the caller.

There are two cases:

If OP_RETURN is returning to OP_CALL via reentry then perhaps the
number of parameters returned can be set via a local variable in
luaV_execute() (but this will be subject to the problem highlighted by
Roberto - i.e. the behaviour in presence of a hook).

If OP_RETURN is returning to the caller then it will just return the
number of parameters like a C function would - i.e. luaV_execute()
will return an int value.

So the responsibility of copying the return values is always on the caller.

As I said - not fully thought through - so please feel free to tear it down.

Regards
Dibyendu