lua-users home
lua-l archive

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


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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
garbage collector.

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.

- --
┌── dg@cowlark.com ─── 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

iD8DBQFGxejqf9E0noFvlzgRArXaAJ9QSUGVTItgXDoF63MUztfIyaR9GgCguRY3
4ljyRKfGM7cWkFEECOYlvpc=
=exdd
-----END PGP SIGNATURE-----