lua-users home
lua-l archive

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

On 31/03/2011 14:17, Tony Finch whispered from the shadows...:
> Anthony Howe <> wrote:
>> I have a related problem with function environments in 5.1. How to get
>> function environments to inherit the environment of the caller and NOT
>> that of where it was defined.
> Your problems with 5.1 and 5.2 are basically the same. The non-locals that
> a function has access to are determined when the closure is created, and
> are not related to the call chain.
> It sounds to me that you would be better off using an OO style, defining
> methods that act on a context object. Each thread is associated with a
> particular object. This will save you the effort of re-creating all the
> closures when spawning a thread, at the cost of an extra argument in each
> function call.

Not sure that would work in the project's current design. The master
state is created and populated with libraries and functions, some within
a table "namespace" eg.

function hook.connect(ip, ptr)
function hook.process(input)
function hook.disconnect()

When a new connection arrives a new thread is created from the master
state, the thread given its own environment that allows access to master
state global variables, but is populated from C with tables confined to
the thread like "client" connection details. There might be several
table. The C code then invokes the hook related to the input state, like
hook.connect(), etc.

So far this all works well, EXCEPT for when a hook.function() starts a
call chain and some where deep down a function refers to a table in the
thread's scope like "client.ip", but the function has its own
environment from when it was defined, not that of the caller, so it
throws an error like "attempt to index global 'client' (a nil value)",
which is not what is expected.

Not sure how your OO model would work here.

Anthony C Howe            Skype: SirWumpus                  SnertSoft
                        Twitter: SirWumpus      BarricadeMX & Milters