Skip to content

Commit

Permalink
Support luau debugname
Browse files Browse the repository at this point in the history
  • Loading branch information
kunitoki committed Feb 25, 2024
1 parent eb3a9c3 commit af273a8
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 144 deletions.
74 changes: 37 additions & 37 deletions Source/LuaBridge/detail/CFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -1154,91 +1154,91 @@ inline int try_overload_functions(lua_State* L)
//=================================================================================================

// Lua CFunction
inline void push_function(lua_State* L, lua_CFunction fp)
inline void push_function(lua_State* L, lua_CFunction fp, const char* debugname)
{
#if LUABRIDGE_SAFE_LUA_C_EXCEPTION_HANDLING && LUABRIDGE_HAS_EXCEPTIONS
lua_pushcfunction_x(L, fp);
lua_pushcclosure_x(L, invoke_safe_cfunction, 1);
#else
lua_pushcfunction_x(L, fp);
lua_pushcfunction_x(L, fp, debugname);
lua_pushcclosure_x(L, invoke_safe_cfunction, debugname, 1);
#else
lua_pushcfunction_x(L, fp, debugname);
#endif
}

// Generic function pointer
template <class ReturnType, class... Params>
inline void push_function(lua_State* L, ReturnType (*fp)(Params...))
inline void push_function(lua_State* L, ReturnType (*fp)(Params...), const char* debugname)
{
using FnType = decltype(fp);

lua_pushlightuserdata(L, reinterpret_cast<void*>(fp));
lua_pushcclosure_x(L, &invoke_proxy_function<FnType>, 1);
lua_pushcclosure_x(L, &invoke_proxy_function<FnType>, debugname, 1);
}

template <class ReturnType, class... Params>
inline void push_function(lua_State* L, ReturnType (*fp)(Params...) noexcept)
inline void push_function(lua_State* L, ReturnType (*fp)(Params...) noexcept, const char* debugname)
{
using FnType = decltype(fp);

lua_pushlightuserdata(L, reinterpret_cast<void*>(fp));
lua_pushcclosure_x(L, &invoke_proxy_function<FnType>, 1);
lua_pushcclosure_x(L, &invoke_proxy_function<FnType>, debugname, 1);
}

// Callable object (lambdas)
template <class F, class = std::enable_if<is_callable_v<F> && !std::is_pointer_v<F> && !std::is_member_function_pointer_v<F>>>
inline void push_function(lua_State* L, F&& f)
inline void push_function(lua_State* L, F&& f, const char* debugname)
{
lua_newuserdata_aligned<F>(L, std::forward<F>(f));
lua_pushcclosure_x(L, &invoke_proxy_functor<F>, 1);
lua_pushcclosure_x(L, &invoke_proxy_functor<F>, debugname, 1);
}

//=================================================================================================
// Lua CFunction
template <class T>
void push_member_function(lua_State* L, lua_CFunction fp)
void push_member_function(lua_State* L, lua_CFunction fp, const char* debugname)
{
#if LUABRIDGE_SAFE_LUA_C_EXCEPTION_HANDLING && LUABRIDGE_HAS_EXCEPTIONS
lua_pushcfunction_x(L, fp);
lua_pushcclosure_x(L, invoke_safe_cfunction, 1);
lua_pushcfunction_x(L, fp, debugname);
lua_pushcclosure_x(L, invoke_safe_cfunction, debugname, 1);
#else
lua_pushcfunction_x(L, fp);
lua_pushcfunction_x(L, fp, debugname);
#endif
}

// Generic function pointer
template <class T, class ReturnType, class... Params>
void push_member_function(lua_State* L, ReturnType (*fp)(T*, Params...))
void push_member_function(lua_State* L, ReturnType (*fp)(T*, Params...), const char* debugname)
{
using FnType = decltype(fp);

lua_pushlightuserdata(L, reinterpret_cast<void*>(fp));
lua_pushcclosure_x(L, &invoke_proxy_function<FnType>, 1);
lua_pushcclosure_x(L, &invoke_proxy_function<FnType>, debugname, 1);
}

template <class T, class ReturnType, class... Params>
void push_member_function(lua_State* L, ReturnType (*fp)(T*, Params...) noexcept)
void push_member_function(lua_State* L, ReturnType (*fp)(T*, Params...) noexcept, const char* debugname)
{
using FnType = decltype(fp);

lua_pushlightuserdata(L, reinterpret_cast<void*>(fp));
lua_pushcclosure_x(L, &invoke_proxy_function<FnType>, 1);
lua_pushcclosure_x(L, &invoke_proxy_function<FnType>, debugname, 1);
}

template <class T, class ReturnType, class... Params>
void push_member_function(lua_State* L, ReturnType (*fp)(const T*, Params...))
void push_member_function(lua_State* L, ReturnType (*fp)(const T*, Params...), const char* debugname)
{
using FnType = decltype(fp);

lua_pushlightuserdata(L, reinterpret_cast<void*>(fp));
lua_pushcclosure_x(L, &invoke_proxy_function<FnType>, 1);
lua_pushcclosure_x(L, &invoke_proxy_function<FnType>, debugname, 1);
}

template <class T, class ReturnType, class... Params>
void push_member_function(lua_State* L, ReturnType (*fp)(const T*, Params...) noexcept)
void push_member_function(lua_State* L, ReturnType (*fp)(const T*, Params...) noexcept, const char* debugname)
{
using FnType = decltype(fp);

lua_pushlightuserdata(L, reinterpret_cast<void*>(fp));
lua_pushcclosure_x(L, &invoke_proxy_function<FnType>, 1);
lua_pushcclosure_x(L, &invoke_proxy_function<FnType>, debugname, 1);
}

// Callable object (lambdas)
Expand All @@ -1247,82 +1247,82 @@ template <class T, class F, class = std::enable_if<
std::is_object_v<F> &&
!std::is_pointer_v<F> &&
!std::is_member_function_pointer_v<F>>>
void push_member_function(lua_State* L, F&& f)
void push_member_function(lua_State* L, F&& f, const char* debugname)
{
static_assert(std::is_same_v<T, remove_cvref_t<std::remove_pointer_t<function_argument_or_void_t<0, F>>>>);

lua_newuserdata_aligned<F>(L, std::forward<F>(f));
lua_pushcclosure_x(L, &invoke_proxy_functor<F>, 1);
lua_pushcclosure_x(L, &invoke_proxy_functor<F>, debugname, 1);
}

// Non const member function pointer
template <class T, class U, class ReturnType, class... Params>
void push_member_function(lua_State* L, ReturnType (U::*mfp)(Params...))
void push_member_function(lua_State* L, ReturnType (U::*mfp)(Params...), const char* debugname)
{
static_assert(std::is_same_v<T, U> || std::is_base_of_v<U, T>);

using F = decltype(mfp);

new (lua_newuserdata_x<F>(L, sizeof(F))) F(mfp);
lua_pushcclosure_x(L, &invoke_member_function<F, T>, 1);
lua_pushcclosure_x(L, &invoke_member_function<F, T>, debugname, 1);
}

template <class T, class U, class ReturnType, class... Params>
void push_member_function(lua_State* L, ReturnType (U::*mfp)(Params...) noexcept)
void push_member_function(lua_State* L, ReturnType (U::*mfp)(Params...) noexcept, const char* debugname)
{
static_assert(std::is_same_v<T, U> || std::is_base_of_v<U, T>);

using F = decltype(mfp);

new (lua_newuserdata_x<F>(L, sizeof(F))) F(mfp);
lua_pushcclosure_x(L, &invoke_member_function<F, T>, 1);
lua_pushcclosure_x(L, &invoke_member_function<F, T>, debugname, 1);
}

// Const member function pointer
template <class T, class U, class ReturnType, class... Params>
void push_member_function(lua_State* L, ReturnType (U::*mfp)(Params...) const)
void push_member_function(lua_State* L, ReturnType (U::*mfp)(Params...) const, const char* debugname)
{
static_assert(std::is_same_v<T, U> || std::is_base_of_v<U, T>);

using F = decltype(mfp);

new (lua_newuserdata_x<F>(L, sizeof(F))) F(mfp);
lua_pushcclosure_x(L, &detail::invoke_const_member_function<F, T>, 1);
lua_pushcclosure_x(L, &detail::invoke_const_member_function<F, T>, debugname, 1);
}

template <class T, class U, class ReturnType, class... Params>
void push_member_function(lua_State* L, ReturnType (U::*mfp)(Params...) const noexcept)
void push_member_function(lua_State* L, ReturnType (U::*mfp)(Params...) const noexcept, const char* debugname)
{
static_assert(std::is_same_v<T, U> || std::is_base_of_v<U, T>);

using F = decltype(mfp);

new (lua_newuserdata_x<F>(L, sizeof(F))) F(mfp);
lua_pushcclosure_x(L, &detail::invoke_const_member_function<F, T>, 1);
lua_pushcclosure_x(L, &detail::invoke_const_member_function<F, T>, debugname, 1);
}

// Non const member Lua CFunction pointer
template <class T, class U = T>
void push_member_function(lua_State* L, int (U::*mfp)(lua_State*))
void push_member_function(lua_State* L, int (U::*mfp)(lua_State*), const char* debugname)
{
static_assert(std::is_same_v<T, U> || std::is_base_of_v<U, T>);

using F = decltype(mfp);

new (lua_newuserdata_x<F>(L, sizeof(F))) F(mfp);
lua_pushcclosure_x(L, &invoke_member_cfunction<T>, 1);
lua_pushcclosure_x(L, &invoke_member_cfunction<T>, debugname, 1);
}

// Const member Lua CFunction pointer
template <class T, class U = T>
void push_member_function(lua_State* L, int (U::*mfp)(lua_State*) const)
void push_member_function(lua_State* L, int (U::*mfp)(lua_State*) const, const char* debugname)
{
static_assert(std::is_same_v<T, U> || std::is_base_of_v<U, T>);

using F = decltype(mfp);

new (lua_newuserdata_x<F>(L, sizeof(F))) F(mfp);
lua_pushcclosure_x(L, &invoke_const_member_cfunction<T>, 1);
lua_pushcclosure_x(L, &invoke_const_member_cfunction<T>, debugname, 1);
}

//=================================================================================================
Expand Down
16 changes: 10 additions & 6 deletions Source/LuaBridge/detail/LuaHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ inline void* lua_newuserdata_x(lua_State* L, size_t sz)
});
}

inline void lua_pushcfunction_x(lua_State *L, lua_CFunction fn)
inline void lua_pushcfunction_x(lua_State *L, lua_CFunction fn, const char* debugname)
{
lua_pushcfunction(L, fn, "");
lua_pushcfunction(L, fn, debugname);
}

inline void lua_pushcclosure_x(lua_State* L, lua_CFunction fn, int n)
inline void lua_pushcclosure_x(lua_State* L, lua_CFunction fn, const char* debugname, int n)
{
lua_pushcclosure(L, fn, "", n);
lua_pushcclosure(L, fn, debugname, n);
}

inline int lua_error_x(lua_State* L)
Expand Down Expand Up @@ -91,13 +91,17 @@ inline void* lua_newuserdata_x(lua_State* L, size_t sz)
return lua_newuserdata(L, sz);
}

inline void lua_pushcfunction_x(lua_State *L, lua_CFunction fn)
inline void lua_pushcfunction_x(lua_State *L, lua_CFunction fn, const char* debugname = "")
{
unused(debugname);

lua_pushcfunction(L, fn);
}

inline void lua_pushcclosure_x(lua_State* L, lua_CFunction fn, int n)
inline void lua_pushcclosure_x(lua_State* L, lua_CFunction fn, const char* debugname, int n)
{
unused(debugname);

lua_pushcclosure(L, fn, n);
}

Expand Down
2 changes: 1 addition & 1 deletion Source/LuaBridge/detail/LuaRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ class LuaRef : public LuaRefBase<LuaRef, LuaRef>
return { L };
#endif

detail::push_function(L, std::forward<F>(func));
detail::push_function(L, std::forward<F>(func), "");
return LuaRef(L, FromStack());
}

Expand Down
Loading

0 comments on commit af273a8

Please sign in to comment.