lua-users home
lua-l archive

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


I'm not very experienced yet in this, so there might be a better way, but what I'd do is, presuming that the function is top on the parameter stack of your C function (in 4.0):
  int l_callback = 0;
  ...
  if (!lua_isfunction(L, -1)) lua_error(L, "Not a function");
  if (l_callback) lua_unref(L, l_callback); /* Be sure to release the */
                                            /* prev ref! */
  l_callback = lua_ref(L, true);

  ...

  /* Calling the function */
  lua_getref(L, l_callback);
  /* ... Push Parameters ... */

  lua_call(L, nargs, nresults);
  /* Be sure to pop all the results off the stack! */

If anyone else has a better idea, I'd love to hear it :) Some variation on what I have above is what I use.
--G

At 09:12 PM 3/28/2002 -0700, you wrote:
I need to find a way to pass Lua functions to C. What I want to do is have a
C function that can be called by a Lua script, and have a lua function
passed to it. That way the script can specify which function it wants C to
call, like registering an event handler. But I can't seem to find anything
in the FAQ or manual about how to do this. Any help is appreciated.

Thanks, Philip Bock