Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support luau debugname #164

Merged
merged 4 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 38 additions & 38 deletions Source/LuaBridge/detail/CFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ int invoke_const_member_function(lua_State* L)
template <class T>
int invoke_member_cfunction(lua_State* L)
{
using F = int (T::*)(lua_State * L);
using F = int (T::*)(lua_State* L);

LUABRIDGE_ASSERT(isfulluserdata(L, lua_upvalueindex(1)));

Expand Down 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
18 changes: 11 additions & 7 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 Expand Up @@ -501,7 +505,7 @@ void* lua_newuserdata_aligned(lua_State* L, Args&&... args)
void* pointer = lua_newuserdata_x<T>(L, maximum_space_needed_to_align<T>());

lua_newtable(L);
lua_pushcfunction_x(L, &lua_deleteuserdata_aligned<T>);
lua_pushcfunction_x(L, &lua_deleteuserdata_aligned<T>, "");
rawsetfield(L, -2, "__gc");
lua_setmetatable(L, -2);
#endif
Expand Down
6 changes: 4 additions & 2 deletions Source/LuaBridge/detail/LuaRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -999,20 +999,22 @@ class LuaRef : public LuaRefBase<LuaRef, LuaRef>
* @brief Create a new function on the top of a Lua stack and return a reference to it.
*
* @param L A Lua state.
* @param func The c++ function to map to lua.
* @param debugname Optional debug name (used only by Luau).
*
* @returns A reference to the newly created function.
*
* @see luabridge::newFunction()
*/
template <class F>
static LuaRef newFunction(lua_State* L, F&& func)
static LuaRef newFunction(lua_State* L, F&& func, const char* debugname = "")
{
#if LUABRIDGE_SAFE_STACK_CHECKS
if (! lua_checkstack(L, 1))
return { L };
#endif

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

Expand Down
Loading
Loading