lua-users home
lua-l archive

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


Based on another question and a members suggestion I'm trying to use luaL_ref
to get a reference to a LUA_TFUNCTION for usage later. My config (in Lua) looks like

	application = {
		exec = '/bin/ls',
		can_restart = function(exit_code)
			return exit_code ~= 0

After loading the config I find the application table and loop over it's
values like so:

	while (lua_next(priv->L, -2) != 0) { // key = -2 || value -1
		GValue gvalue;
		memset(&gvalue, 0, sizeof(GValue));

		if (LUA_TSTRING == lua_type(priv->L, -1)) {
			g_value_init(&gvalue, G_TYPE_STRING);
			g_value_set_static_string(&gvalue, lua_tostring(priv->L, -1));
		} else if (LUA_TNUMBER == lua_type(priv->L, -1)) {
			g_value_init(&gvalue, G_TYPE_INT);
			g_value_set_int(&gvalue, (gint)lua_tointeger(priv->L, -1));
		} else if (LUA_TFUNCTION == lua_type(priv->L, -1)) {
			g_value_init(&gvalue, G_TYPE_INT);
			g_value_set_int(&gvalue, luaL_ref(priv->L, -1));

		if (G_IS_VALUE(&gvalue)) {
			g_object_set_property(G_OBJECT(app), lua_tostring(priv->L, -2),

		lua_pop(priv->L, 1);

I would like to be able to call 'can_restart(exit_code)' much later (well
after calling luaL_ref). Everything else in the while loop works perfectly,
but I'm unsure how to get a reference for the function so I can call it later.
Also, I'm not sure what I would pass to luaL_unref(lua_State, int t, int ref)
for the second argument (t)?

Thoughts? Clearly I'm missing some fundamental piece of how all of this fits
together, but I'm not quite sure what. 

Any and all assistance would be greatly appreciated. Thanks a lot!

~ Matthew 

Attachment: signature.asc
Description: Digital signature