lua-users home
lua-l archive

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

It was thus said that the Great Marc Balmer once stated:
> Hi
> 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?  Are there other
> approaches?  If you also use callback written in Lua, which you call
> from C, I'd like you to share your opinion (and/or experience).

  For one project I used form #2, as that made more sense for the program
[1], since the C side provides an environment for the Lua code to run in
(much like libc makes an environment for C code to run).  The advantage to
this is it's simple to implement, but the downside is less flexibility.

  For #1, yes, it's more flexible, but now the C code is reponsible for
saving the callback somewhere in the Lua environment.  The implementation
becomes a bit more complex.  

  -spc (I tend to prefer #2 ... )