lua-users home
lua-l archive

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


>I have wrapped lua_State into a C++ class such that one can say
>cLuaState ls;
>However, I had to add a field to lua_State in order to be able to get
to my
>cLuaState from registered application functions written in C++.
Needless to

I recommend this (from my LuaState C++ code):

class cLuaState
{
	// This is the FIRST and ONLY member.  NO virtual functions.
	lua_State* m_state;
	// Accessors...
};

typedef int (*cLuaStateCFunction)(cLuaState state);

void cLuaState::Register(const char* funcName, lua_CFunction function)
{
	lua_pushstring(m_state, funcName);
	lua_pushcclosure(m_state, (lua_CFunction)function, 0);
	lua_settable(m_state, m_stackIndex);
}

void cLuaState::Register(const char* funcName, cLuaStateCFunction
function)
{
	lua_pushstring(m_state, funcName);
	lua_pushcclosure(m_state, (lua_CFunction)function, 0);
	lua_settable(m_state, m_stackIndex);
}

Works like a charm:

static int MyCallback(cLuaState state)
{
	state.whatever();
	return 0;
}


state.Register("MyCallback", MyCallback);

-Josh