[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: A Question of Style
- From: Brian Maher <brian@...>
- Date: Tue, 6 Dec 2011 22:16:16 -0800
On the issue of calling Lua functions from C, you might want to avoid
doing that since it prevents the default Lua from working properly
with coroutines. To get around this, you can have your C code
"collect a bunch of events", then have a lua wrapper function call
those events. Here is an example in the lua-http-parser that Robert
Jakabosky and I wrote:
https://github.com/brimworks/lua-http-parser/blob/master/lua-http-parser.c#L474
...of course, if your Lua code never yields this isn't an issue (but
in the case of an HTTP parser, it is much more likely that you would
want to use yield in your user-callbacks).
I should also point out that adding references to the global table is
a bad idea in general since it is easy to create unintentional memory
leaks. It would be better to have your "watcher" object (or whatever
object you register your callbacks with) to keep a proper reference to
the callback. A good example of how I found that out the "hard way"
was when I wrote my libev binding which had two callbacks reference
each other.
Cheers,
-Brian
On Tue, Nov 29, 2011 at 11:56 PM, HyperHacker <hyperhacker@gmail.com> wrote:
> On Tue, Nov 29, 2011 at 20:43, jiang yu <yu.jiang.163@gmail.com> wrote:
>> That's right, but, not simple.
>>
>> 2011/11/30 HyperHacker <hyperhacker@gmail.com>:
>>> On Tue, Nov 29, 2011 at 10:18, jiang yu <yu.jiang.163@gmail.com> wrote:
>>>> I prefer 2)
>>>> 1. It is simpler than 1)
>>>> 2. If there is bug in 2), you can correct it and just dofile() —— no
>>>> need restart the program.
>>>>
>>>> 2011/11/26 Marc Balmer <marc@msys.ch>:
>>>>> 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).
>>>>>
>>>>> I experienced with both forms, I am unsure for which form to go...
>>>>>
>>>>> Thanks,
>>>>> Marc
>>>>>
>>>>
>>>
>>> You can do that with method 1 too, by just having the script
>>> unregister and reregister its handlers at startup.
>>>
>>> --
>>> Sent from my toaster.
>>>
>>
>
> Seems simple enough to me. Just add a couple lines at the end:
> UnregisterCallback('OnOpen', my_on_open_callback)
> RegisterCallback('OnOpen', my_on_open_callback)
>
> UnregisterCallback just needs to fail gracefully if given a callback
> that isn't registered.
>
> --
> Sent from my toaster.
>
--
Brian Maher >> Glory to God <<