lua-users home
lua-l archive

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


2015-03-13 19:31 GMT+02:00 Tim Hill <drtimhill@gmail.com>:
>> On Mar 13, 2015, at 1:12 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
>> Is this a correct summary of the problem you need to solve?
>>
>> 1. LuaState 1, to which you have full access, dumps a function,
>>    the source of which you write, to a string.
>> 2. LuaState 2,  to which you do not have full access, somehow
>>   gets that string, loads it and calls it.
>> 3. You wish to write your code in such a way that the running
>>   function can detect whether it has been called with first upvalue
>>   equal to the table in which it looks up global variables.
>
> Yes on 1 and 2, if “full access” means I control the underlying C code that runs the state. I do NOT have full control of the corpus of Lua code running on the state.
>
> For #3, it’s not the *running* function that I’m serializing, it’s an arbitrary function (well, closure), passed as an argument to a “transfer()” API that does the serialization.

Revised summary:

1. Lua State 1 contains a closure `func`, which may be the result of
loading stripped bytecode and therefore cannot be relied on to
know the names of its upvalues.
2. Enough information must be passed via some communication
channel to allow Lua State 2 to reconstruct the closure complete
with its upvalues.
3. The planned method is to string.dump the bytecode and
serialize all upvalues except _ENV. The assumption may be
made that _ENV is either the first upvalue or absent.
4. Lua State 2 can tell whether _ENV is the first upvalue or absent
by comparing the number of serialized upvalues with
debug.getinfo(func).nups after the function has been loaded.
5. The difficulty is that Lua State 1 cannot tell with certainty
whether the value in the first upvalue is in fact _ENV.

Well, it seems to me that the question is not so much whether
the upvalue is _ENV as whether it ought to be set to the _ENV
that Lua State 2 can supply. Thus if you can provide a table with
the same keys that Lua State 2 has in its _ENV, and all the keys
of the first upvalue of `func` are found to represented in that
table, it would be reasonable to decide that the answer is yes.