allow to bind functions with reference args in global lua namespace

This commit is contained in:
Robin Gareus 2016-03-18 19:59:37 +01:00
parent ee2d88a5cc
commit 2c71196a6c
2 changed files with 53 additions and 0 deletions

View File

@ -246,6 +246,48 @@ struct CFunc
}
};
//----------------------------------------------------------------------------
/**
lua_CFunction to call a function with references as arguments.
*/
template <class FnPtr,
class ReturnType = typename FuncTraits <FnPtr>::ReturnType>
struct CallRef
{
typedef typename FuncTraits <FnPtr>::Params Params;
static int f (lua_State* L)
{
assert (isfulluserdata (L, lua_upvalueindex (1)));
FnPtr const& fnptr = *static_cast <FnPtr const*> (lua_touserdata (L, lua_upvalueindex (1)));
assert (fnptr != 0);
ArgList <Params, 1> args (L);
Stack <typename FuncTraits <FnPtr>::ReturnType>::push (L, FuncTraits <FnPtr>::call (fnptr, args));
LuaRef v (newTable (L));
FuncArgs <Params, 0>::refs (v, args);
v.push(L);
return 2;
}
};
template <class FnPtr>
struct CallRef <FnPtr, void>
{
typedef typename FuncTraits <FnPtr>::Params Params;
static int f (lua_State* L)
{
assert (isfulluserdata (L, lua_upvalueindex (1)));
FnPtr const& fnptr = *static_cast <FnPtr const*> (lua_touserdata (L, lua_upvalueindex (1)));
assert (fnptr != 0);
ArgList <Params, 1> args (L);
FuncTraits <FnPtr>::call (fnptr, args);
LuaRef v (newTable (L));
FuncArgs <Params, 0>::refs (v, args);
v.push(L);
return 1;
}
};
//----------------------------------------------------------------------------
/**
lua_CFunction to call a class member function with a return value.

View File

@ -1382,6 +1382,17 @@ public:
return *this;
}
template <class FP>
Namespace& addRefFunction (char const* name, FP const fp)
{
assert (lua_istable (L, -1));
new (lua_newuserdata (L, sizeof (fp))) FP (fp);
lua_pushcclosure (L, &CFunc::CallRef <FP>::f, 1);
rawsetfield (L, -2, name);
return *this;
}
//----------------------------------------------------------------------------
/**