lua-users home
lua-l archive

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


Hi,

I'm trying to make my C library Lua aware but the problem is that it
relies on lots of global variables to change all its internal state. I
diged the list and found some code that was supposed to help me out,
but now that i compiled it it fails because I think it was old Lua 4.x
code and it isn't compatible with Lua 5.x

here it is:

int I = 100;

static int getGInt(lua_State* L)
{
	int * var = lua_touserdata(L, 2);
	lua_pushinteger(L, *var);
	return 1;
}

static int setGInt(lua_State* L)
{
	int * var = lua_touserdata(L, 2);
	int val = lua_tonumber(L, 3);
	*var = val;
	return 0;
}

static void exportGlobals(lua_State* L)
{
	int _i = lua_newtag(L);
	lua_pushusertag(L, &I, _i); 	lua_setglobal(L, "I");
	lua_pushcfunction(L, getGInt); 	lua_settagmethod(L, _i, "getglobal");
	lua_pushcfunction(L, setGInt); 	lua_settagmethod(L, _i, "setglobal");
}

and my main

int main(int argc, char **argv)
{
	lua_State *L = lua_open();
	luaL_openlibs(L);
	
	luaL_openlib(L, "my.lib", mylib, 0);
	exportGlobals(L);

    if(luaL_dofile(L, "test.lua") != 0)
	{
		printf("%s\n", lua_tostring(L, -1));
		lua_pop(L, 1);
	}
    lua_close(L);
}

it fails to link because there is no tag functions anymore, is there
an easy way to make this work? So that on my script I can easily set
and get these global variables both on Lua and C?

Thanks,
Paulo