lua-users home
lua-l archive

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


> On 10 Oct 2006, at 13:14, David Burgess wrote:
>
> > > I do have one further question for Mike. What do you
> > > think of the idea of a prototype for lua_sethook() that reads
> > > like:
> > >
> > > typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar, void*
> > > userdata);
>
> I don't know what Mike thinks, but I think it's a good idea.
>
> When I'm creating interfaces in C I almost always regret creating an
> interface where I pass a C function pointer but not an associated
> void * data closure as well.
>
> drj

I agree, especially since when one has such a version, it is
trivial to also provide a version that does _not_ have the userdata.
Given the following:

    typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar, void *userdata);

    void lua_sethook (lua_State *L, lua_Hook func, void *userdata, int mask, int count);

It is easy to add the versions that do not have the userdata by passing
the original function as the userdata, and an impedance matching
function as the hook:

    typedef void (*lua_Hook1) (lua_State *L, lua_Debug *ar);

    static void impedance_matcher(lua_State *L, lua_Debug *ar, void *userdata)
    {
        ((lua_Hook1)userdata)( L, ar );
    }

    void lua_sethook1 (lua_State *L, lua_Hook1 func, int mask, int count)
    {
        lua_sethook( L, impedance_matcher, func, mask, count );
    }

- Eric