lua-users home
lua-l archive

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

Leo Razoumov wrote:
On Fri, 23 Jan 2009, Ignacio Burgueño wrote:

What is the purpose of

typedef int (T::*mfp)(lua_State* L);

Type mpf refers to a C++ member function of class T and cannot be made into Lua C-closure because it implicitly passes this-pointer as an extra argument.

Indeed, its not made into a closure. It is used instead by this function:
static int thunk_methods(lua_State* L) {
		// stack has userdata, followed by method args
		T* obj = Base::check(L, 1);  // get 'self', or if you prefer, 'this'
//lua_remove(L, 1); // remove self so member function args start at index 1
		// get member function from upvalue
RegType* l = static_cast<RegType*>(lua_touserdata(L, lua_upvalueindex(1)));
		return (obj->*(l->mfunc))(L);  // call member function

l->mfunc is of type mfp, and the function thunk_methods is the c closure indeed.