[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Callback Functions
- From: David Given <dg@...>
- Date: Fri, 17 Aug 2007 19:28:58 +0100
-----BEGIN PGP SIGNED MESSAGE-----
W. C. Bubel wrote:
> Is there a specific provision for this, that I overlooked, or would trick need
> to be involved?
Yup. Look at luaL_ref() and luaL_unref(). They allow you to store a Lua object
(such as a function pointer) in a registry, and then access it by integer key.
This allows you to turn the Lua function pointer into a nice safe integer,
which can then be stored by C without running the risk of confusing the
Your setcallback() calls luaL_ref() to store the function; when you want to
call it, you use lua_rawgeti() to retrieve it, and call it with pcall()
normally; once you're done with the callback, you call luaL_unref() to unset it.
You can use LUA_REGISTRYINDEX to specify the global registry, if you don't
want to make your own.
┌── ｄｇ＠ｃｏｗｌａｒｋ．ｃｏｍ ─── http://www.cowlark.com ───────────────────
│ "There does not now, nor will there ever, exist a programming language in
│ which it is the least bit hard to write bad programs." --- Flon's Axiom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
-----END PGP SIGNATURE-----