89 static void GetNames(std::vector<std::string> &names,
const std::string &prefix =
"",
bool methodsOnly =
false);
95 static void *
TestUserdata(lua_State *l,
int index,
const char *name);
98 static void *
CheckUserdata(lua_State *l,
int index,
const char *name);
106 template <
typename Rt,
typename... Args>
110 template <
typename Rt,
typename... Args>
111 typename std::enable_if<!std::is_same<Rt, void>::value,
int>::type
static free_fn_wrapper_(lua_State *L)
113 if (
size_t(lua_gettop(L)) <
sizeof...(Args))
114 return luaL_error(L,
"Invalid number of arguments for function %s (have %d, need %lu)",
115 lua_tostring(L, lua_upvalueindex(1)), lua_gettop(L),
sizeof...(Args));
125 template <
typename Rt,
typename... Args>
126 typename std::enable_if<std::is_same<Rt, void>::value,
int>::type
static free_fn_wrapper_(lua_State *L)
128 if (
size_t(lua_gettop(L)) <
sizeof...(Args))
129 return luaL_error(L,
"Invalid number of arguments for function %s (have %d, need %lu)",
130 lua_tostring(L, lua_upvalueindex(1)), lua_gettop(L),
sizeof...(Args));
143 template <
typename T,
typename Dt>
146 template <
typename T,
typename Dt>
149 T *ptr = LuaPull<T *>(L, 1);
150 const char *name = lua_tostring(L, lua_upvalueindex(1));
153 return luaL_error(L,
"Null or invalid userdata accessed for property %s", name);
155 if (lua_gettop(L) > 2)
156 return luaL_error(L,
"Invalid number of arguments for property getter/setter %s", name);
158 auto &t = PullPointerToMember<member_pointer<T, Dt>>(L, lua_upvalueindex(2));
159 if (lua_gettop(L) == 1) {
160 LuaPush<Dt>(L, ptr->*(t));
163 (ptr->*(t)) = LuaPull<Dt>(L, 2);
172 template <
typename T>
175 template <
typename T>
178 T *ptr = LuaPull<T *>(L, 1);
179 const char *name = lua_tostring(L, lua_upvalueindex(1));
180 luaL_checktype(L, lua_upvalueindex(2), LUA_TLIGHTUSERDATA);
183 return luaL_error(L,
"Invalid userdata accessed for function %s", name);
185 auto fn = PullFreeFunction<member_cfunction<T>>(L, lua_upvalueindex(2));
190 template <
typename T>
193 T *ptr = LuaPull<T *>(L, 1);
194 const char *name = lua_tostring(L, lua_upvalueindex(1));
195 auto getter = PullFreeFunction<member_cfunction<T>>(L, lua_upvalueindex(2));
198 return luaL_error(L,
"Null or invalid userdata accessed for property %s", name);
200 if (lua_gettop(L) > 2)
201 return luaL_error(L,
"Invalid number of arguments for property getter/setter %s", name);
203 if (lua_gettop(L) > 1) {
204 auto setter = PullFreeFunction<member_cfunction<T>>(L, lua_upvalueindex(3));
205 if (setter !=
nullptr)
206 return setter(L, ptr);
208 return luaL_error(L,
"Attempt to call undefined setter for property %s", name);
211 return getter(L, ptr);
218 template <
typename T,
typename Rt,
typename... Args>
221 template <
typename T,
typename Rt,
typename... Args>
225 template <
typename T,
typename Rt,
typename... Args>
226 typename std::enable_if<!std::is_same<Rt, void>::value,
int>::type
static member_fn_wrapper_(lua_State *L)
228 T *ptr = LuaPull<T *>(L, 1);
229 const char *name = lua_tostring(L, lua_upvalueindex(1));
232 return luaL_error(L,
"Invalid userdata accessed for function %s", name);
234 if (
size_t(lua_gettop(L) - 1) <
sizeof...(Args))
235 return luaL_error(L,
"Invalid number of arguments for function %s", name);
245 template <
typename T,
typename Rt,
typename... Args>
246 typename std::enable_if<std::is_same<Rt, void>::value,
int>::type
static member_fn_wrapper_(lua_State *L)
248 T *ptr = LuaPull<T *>(L, 1);
249 const char *name = lua_tostring(L, lua_upvalueindex(1));
252 return luaL_error(L,
"Invalid userdata accessed for function %s", name);
254 if (
size_t(lua_gettop(L) - 1) <
sizeof...(Args))
255 return luaL_error(L,
"Invalid number of arguments for function %s", name);
264 template <
typename T,
typename Dt,
typename Dt2>
267 T *ptr = LuaPull<T *>(L, 1);
268 const char *name = lua_tostring(L, lua_upvalueindex(1));
269 auto &getter = PullPointerToMember<member_function<T, Dt>>(L, lua_upvalueindex(2));
272 return luaL_error(L,
"Null or invalid userdata accessed for property %s", name);
274 if (lua_gettop(L) > 2)
275 return luaL_error(L,
"Invalid number of arguments for property getter/setter %s", name);
277 if (lua_gettop(L) > 1) {
278 auto &setter = PullPointerToMember<member_function<T, void, Dt2>>(L, lua_upvalueindex(3));
279 if (setter !=
nullptr) {
283 return luaL_error(L,
"Attempt to call undefined setter for property %s", name);
286 LuaPush<Dt>(L, value);
295 template <
typename MemT>
298 *
reinterpret_cast<MemT *
>(lua_newuserdata(L,
sizeof(MemT))) = obj;
301 template <
typename MemT>
304 return *
reinterpret_cast<MemT *
>(lua_touserdata(L, idx));
307 template <
typename T>
310 static_assert(
sizeof(T) ==
sizeof(
void *),
"Free functions cannot be 'fat' lambdas!");
311 lua_pushlightuserdata(L,
reinterpret_cast<void *
>(obj));
314 template <
typename T>
317 return reinterpret_cast<T
>(lua_touserdata(L, index));
323 luaL_getsubtable(L, index,
"attrs");
329 luaL_getsubtable(L, index,
"methods");
364 m_protected = enabled;
372 lua_pushcfunction(
m_lua, getter);
376 lua_setfield(
m_lua, -2, name);
385 lua_pushcfunction(
m_lua, func);
389 lua_setfield(
m_lua, -2, name);
394 template <
typename Rt,
typename... Args>
401 lua_pushcclosure(
m_lua, &free_fn_wrapper_<Rt, Args...>, 2);
405 lua_setfield(
m_lua, -2, name);
412 lua_pushcfunction(
m_lua, func);
421 template <
typename Rt,
typename... Args>
426 lua_pushcclosure(
m_lua, &free_fn_wrapper_<Rt, Args...>, 2);
438 lua_getmetatable(
m_lua, -1);
440 lua_pushcfunction(
m_lua, func);
444 lua_setfield(
m_lua, -2,
"__call");
454 lua_pushcfunction(
m_lua, func);
458 lua_setfield(
m_lua, -2,
"New");
464 bool m_protected =
false;
495 template <
typename Dt>
498 lua_State *L =
m_lua;
501 lua_pushstring(L, (
m_typeName +
"." + name).c_str());
503 lua_pushcclosure(L, &member_wrapper_<T, Dt>, 2);
507 lua_setfield(L, -2, name);
517 lua_State *L =
m_lua;
520 lua_pushstring(L, (
m_typeName +
"." + name).c_str());
523 lua_pushcclosure(L, &getter_member_cfn_wrapper_<T>, 3);
527 lua_setfield(L, -2, name);
535 template <
typename Dt,
typename Dt2 = Dt>
543 template <
typename Dt,
typename Dt2 = Dt>
546 lua_State *L =
m_lua;
549 lua_pushstring(L, (
m_typeName +
"." + name).c_str());
552 lua_pushcclosure(L, &getter_member_fn_wrapper_<T, Dt, Dt2>, 3);
556 lua_setfield(L, -2, name);
564 template <
typename Rt,
typename... Args>
576 template <
typename Rt,
typename... Args>
579 lua_State *L =
m_lua;
582 lua_pushstring(L, (
m_typeName +
"." + name).c_str());
584 lua_pushcclosure(L, &member_fn_wrapper_<T, Rt, Args...>, 2);
588 lua_setfield(L, -2, name);
600 lua_State *L =
m_lua;
603 lua_pushstring(L, (
m_typeName +
"." + name).c_str());
605 lua_pushcclosure(L, &member_cfn_wrapper_<T>, 2);
609 lua_setfield(L, -2, name);
621 lua_pushcfunction(
m_lua, func);
625 lua_setfield(
m_lua, -2, name);
632 template <
typename Rt,
typename... Args>
644 template <
typename Rt,
typename... Args>
647 lua_State *L =
m_lua;
649 lua_pushstring(L, (
m_typeName +
"." + name).c_str());
651 lua_pushcclosure(L, &member_fn_wrapper_<T, Rt, Args...>, 2);
655 lua_setfield(L,
m_index, name);
666 lua_State *L =
m_lua;
668 lua_pushstring(L, (
m_typeName +
"." + name).c_str());
670 lua_pushcclosure(L, &member_cfn_wrapper_<T>, 2);
674 lua_setfield(L,
m_index, name);
682 lua_pushcfunction(
m_lua, func);
693 lua_State *L =
m_lua;
696 for (
const luaL_Reg *func = functions; func->name; ++func) {
697 lua_pushcfunction(L, func->func);
701 lua_setfield(L, -2, func->name);
710 lua_getmetatable(
m_lua, -1);
712 lua_pushcfunction(
m_lua, func);
716 lua_setfield(
m_lua, -2,
"__call");
726 lua_pushcfunction(
m_lua, func);
730 lua_setfield(
m_lua, -2,
"New");
736 bool m_protected =
false;
Ret pi_lua_multiple_call(lua_State *l, int index, Ret(*fn)())
Definition LuaCall.h:19
int secure_trampoline(lua_State *l)
Definition Sandbox.cpp:441