[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: A Question of Style
- From: Marc Balmer <marc@...>
- Date: Mon, 28 Nov 2011 11:38:27 +0100
Am 26.11.11 15:00, schrieb Roberto Ierusalimschy:
>> I have a stylistic question wrt/ calling Lua "callbacks" from a C
>> program. I see two obvious approaches:
>> 1) Lua code registers callbacks explicitely using a RegisterCallback
>> function that is provided by the C program; the C program later calls
>> the callback function when one is registered.
>> 2) Lua code does not register callbacks, but the callbacks must be
>> functions with a certain name, e.g. "MouseMovedCallback"; C code will
>> then see if a function with the correct name is available in the Lua
>> state, and if so, call it.
>> Are there advantages of one approach over the other?
> (2) is simpler, but (1) seems more clean, as it avoids global names.
> I see some alternative approaches as combinations of (1) and (2).
> For instance, Lua code can register one single callback table,
> wherein callbacks must be functions with fixed names. It is simple
> to set and get callbacks (just table operations), but it avoids
> using the global table. Or you can use a table with a fixed name
> (so it creates only one global name).
Actually version 1 has a nice advantage: I can create a list of
handlers in the C part for each event and call them in a chain, with the
event handler indicating with the return value whether or not other
handlers should run.