Skip to content

Commit

Permalink
Update amalgamation file
Browse files Browse the repository at this point in the history
  • Loading branch information
kunitoki authored and actions-user committed Oct 22, 2023
1 parent 3bd338b commit 87f36c9
Showing 1 changed file with 155 additions and 15 deletions.
170 changes: 155 additions & 15 deletions Distribution/LuaBridge/LuaBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
#define LUABRIDGE_SAFE_STACK_CHECKS 1
#endif

#if !defined(LUABRIDGE_SAFE_LUA_C_EXCEPTION_HANDLING)
#define LUABRIDGE_SAFE_LUA_C_EXCEPTION_HANDLING 0
#endif

#if !defined(LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE)
#if LUABRIDGE_HAS_EXCEPTIONS
#define LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE 1
Expand Down Expand Up @@ -164,6 +168,11 @@ inline int lua_error_x(lua_State* L)
return 0;
}

inline int lua_getstack_x(lua_State* L, int level, lua_Debug* ar)
{
return lua_getinfo(L, level, "nlS", ar);
}

inline int lua_getstack_info_x(lua_State* L, int level, const char* what, lua_Debug* ar)
{
return lua_getinfo(L, level, what, ar);
Expand Down Expand Up @@ -194,6 +203,11 @@ inline int lua_error_x(lua_State* L)
return lua_error(L);
}

inline int lua_getstack_x(lua_State* L, int level, lua_Debug* ar)
{
return lua_getstack(L, level, ar);
}

inline int lua_getstack_info_x(lua_State* L, int level, const char* what, lua_Debug* ar)
{
lua_getstack(L, level, ar);
Expand Down Expand Up @@ -6694,7 +6708,21 @@ int invoke_member_cfunction(lua_State* L)
const F& func = *static_cast<const F*>(lua_touserdata(L, lua_upvalueindex(1)));
LUABRIDGE_ASSERT(func != nullptr);

return (t->*func)(L);
#if LUABRIDGE_HAS_EXCEPTIONS
try
{
#endif
return (t->*func)(L);

#if LUABRIDGE_HAS_EXCEPTIONS
}
catch (const std::exception& e)
{
raise_lua_error(L, "%s", e.what());
}

return 0;
#endif
}

template <class T>
Expand All @@ -6709,7 +6737,21 @@ int invoke_const_member_cfunction(lua_State* L)
const F& func = *static_cast<const F*>(lua_touserdata(L, lua_upvalueindex(1)));
LUABRIDGE_ASSERT(func != nullptr);

return (t->*func)(L);
#if LUABRIDGE_HAS_EXCEPTIONS
try
{
#endif
return (t->*func)(L);

#if LUABRIDGE_HAS_EXCEPTIONS
}
catch (const std::exception& e)
{
raise_lua_error(L, "%s", e.what());
}

return 0;
#endif
}

template <class F>
Expand Down Expand Up @@ -6737,6 +6779,26 @@ int invoke_proxy_functor(lua_State* L)
return function<typename FnTraits::result_type, typename FnTraits::argument_types, 1>::call(L, func);
}

#if LUABRIDGE_SAFE_LUA_C_EXCEPTION_HANDLING && LUABRIDGE_HAS_EXCEPTIONS
inline int invoke_safe_cfunction(lua_State* L)
{
LUABRIDGE_ASSERT(lua_iscfunction(L, lua_upvalueindex(1)));

auto func = lua_tocfunction(L, lua_upvalueindex(1));

try
{
return func(L);
}
catch (const std::exception& e)
{
raise_lua_error(L, "%s", e.what());
}

return 0;
}
#endif

template <class F>
int invoke_proxy_constructor(lua_State* L)
{
Expand Down Expand Up @@ -6829,7 +6891,12 @@ inline int try_overload_functions(lua_State* L)

inline void push_function(lua_State* L, lua_CFunction fp)
{
#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);
#endif
}

template <class ReturnType, class... Params>
Expand Down Expand Up @@ -6860,7 +6927,12 @@ inline void push_function(lua_State* L, F&& f)
template <class T>
void push_member_function(lua_State* L, lua_CFunction fp)
{
#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);
#endif
}

template <class T, class ReturnType, class... Params>
Expand Down Expand Up @@ -6981,14 +7053,14 @@ void push_member_function(lua_State* L, int (U::*mfp)(lua_State*) const)
template <class T, class Args>
struct constructor
{
static T* call(const Args& args)
static T* construct(const Args& args)
{
auto alloc = [](auto&&... args) { return new T(std::forward<decltype(args)>(args)...); };

return std::apply(alloc, args);
}

static T* call(void* ptr, const Args& args)
static T* construct(void* ptr, const Args& args)
{
auto alloc = [ptr](auto&&... args) { return new (ptr) T(std::forward<decltype(args)>(args)...); };

Expand Down Expand Up @@ -7037,7 +7109,21 @@ int constructor_container_proxy(lua_State* L)
{
using T = typename ContainerTraits<C>::Type;

T* object = constructor<T, Args>::call(detail::make_arguments_list<Args, 2>(L));
T* object = nullptr;

#if LUABRIDGE_HAS_EXCEPTIONS
try
{
#endif
object = constructor<T, Args>::construct(detail::make_arguments_list<Args, 2>(L));

#if LUABRIDGE_HAS_EXCEPTIONS
}
catch (const std::exception& e)
{
raise_lua_error(L, "%s", e.what());
}
#endif

auto result = UserdataSharedHelper<C, false>::push(L, object);
if (! result)
Expand All @@ -7056,8 +7142,21 @@ int constructor_placement_proxy(lua_State* L)
if (! value)
raise_lua_error(L, "%s", ec.message().c_str());

constructor<T, Args>::call(value->getObject(), std::move(args));
#if LUABRIDGE_HAS_EXCEPTIONS
try
{
#endif

constructor<T, Args>::construct(value->getObject(), std::move(args));

#if LUABRIDGE_HAS_EXCEPTIONS
}
catch (const std::exception& e)
{
raise_lua_error(L, "%s", e.what());
}
#endif

value->commit();

return 1;
Expand All @@ -7083,12 +7182,25 @@ struct constructor_forwarder
if (! value)
raise_lua_error(L, "%s", ec.message().c_str());

T* obj = placement_constructor<T>::construct(
value->getObject(), m_func, std::move(args));
T* object = nullptr;

#if LUABRIDGE_HAS_EXCEPTIONS
try
{
#endif
object = placement_constructor<T>::construct(value->getObject(), m_func, std::move(args));

#if LUABRIDGE_HAS_EXCEPTIONS
}
catch (const std::exception& e)
{
raise_lua_error(L, "%s", e.what());
}
#endif

value->commit();

return obj;
return object;
}

private:
Expand All @@ -7109,14 +7221,28 @@ struct factory_forwarder
using FnTraits = function_traits<Alloc>;
using FnArgs = typename FnTraits::argument_types;

T* obj = external_constructor<T>::construct(m_alloc, make_arguments_list<FnArgs, 0>(L));
T* object = nullptr;

#if LUABRIDGE_HAS_EXCEPTIONS
try
{
#endif
object = external_constructor<T>::construct(m_alloc, make_arguments_list<FnArgs, 0>(L));

#if LUABRIDGE_HAS_EXCEPTIONS
}
catch (const std::exception& e)
{
raise_lua_error(L, "%s", e.what());
}
#endif

std::error_code ec;
auto* value = UserdataValueExternal<T>::place(L, obj, m_dealloc, ec);
auto* value = UserdataValueExternal<T>::place(L, object, m_dealloc, ec);
if (! value)
raise_lua_error(L, "%s", ec.message().c_str());

return obj;
return object;
}

private:
Expand All @@ -7137,13 +7263,27 @@ struct container_forwarder
using FnTraits = function_traits<F>;
using FnArgs = typename FnTraits::argument_types;

auto obj = container_constructor<C>::construct(m_func, make_arguments_list<FnArgs, 2>(L));
C object;

#if LUABRIDGE_HAS_EXCEPTIONS
try
{
#endif
object = container_constructor<C>::construct(m_func, make_arguments_list<FnArgs, 2>(L));

#if LUABRIDGE_HAS_EXCEPTIONS
}
catch (const std::exception& e)
{
raise_lua_error(L, "%s", e.what());
}
#endif

auto result = UserdataSharedHelper<C, false>::push(L, obj);
auto result = UserdataSharedHelper<C, false>::push(L, object);
if (! result)
raise_lua_error(L, "%s", result.message().c_str());

return obj;
return object;
}

private:
Expand Down

0 comments on commit 87f36c9

Please sign in to comment.