gpt4 book ai didi

c++ - 为什么 std::uncaught_exception 在 LuaJIT 抛出时返回 false?

转载 作者:搜寻专家 更新时间:2023-10-31 02:15:26 24 4
gpt4 key购买 nike

LuaJIT 手册 states :

Lua errors can be caught on the C++ side with catch(...). The corresponding Lua error message can be retrieved from the Lua stack.

这按预期工作 - 除了 std::uncaught_exception() 在这种情况下不返回 true。

以下是说明该问题的最小示例。 checker 的析构函数应该在堆栈展开期间执行,因此其中的 std::uncaught_exception() 应该返回 true,但它没有。

这怎么可能?是我误解了这个过程还是 LuaJIT 以粗略的方式执行异常引发?

struct checker {
~checker() {
if(std::uncaught_exception()) {
// condition should evaluate true, but doesn't
}
}
}

auto l = luaL_newstate();
try {
{
checker c;
luaL_checknumber(l, -1); // this line causes LuaJIT to raise an error
}
}
catch(...) {
// this will be executed, as intended
auto err = lua_tostring(state, -1); // read the LuaJIT error, works too
// ...
}

最佳答案

原始 Lua(不是 LuaJIT)使用不是 C++ 异常的 C 风格异常(长跳转)。grep LUAI_THROW 的 Lua 来源并注意 C/C++ 异常处理之间的区别。

/*
@@ LUAI_THROW/LUAI_TRY define how Lua does exception handling.
** CHANGE them if you prefer to use longjmp/setjmp even with C++
** or if want/don't to use _longjmp/_setjmp instead of regular
** longjmp/setjmp. By default, Lua handles errors with exceptions when
** compiling as C++ code, with _longjmp/_setjmp when asked to use them,
** and with longjmp/setjmp otherwise.
*/
#if defined(__cplusplus)
/* C++ exceptions */
#define LUAI_THROW(L,c) throw(c)
#define LUAI_TRY(L,c,a) try { a } catch(...) \
{ if ((c)->status == 0) (c)->status = -1; }
#define luai_jmpbuf int /* dummy variable */

#elif defined(LUA_USE_ULONGJMP)
/* in Unix, try _longjmp/_setjmp (more efficient) */
#define LUAI_THROW(L,c) _longjmp((c)->b, 1)
#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a }
#define luai_jmpbuf jmp_buf

#else
/* default handling with long jumps */
#define LUAI_THROW(L,c) longjmp((c)->b, 1)
#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a }
#define luai_jmpbuf jmp_buf

#endif

我不确定是否可行,但您可以尝试使用 C++ 编译 Lua,这将允许您捕获 Lua 异常。注意,编译为 C 代码的原始 Lua 不支持 C++ 栈帧展开!

至于 LuaJIT,它看起来像是自己实现了帧展开,有关详细信息,请参阅 lj_err.c。这就是为什么在此过程中可能不会设置某些 CRT 变量的原因。

/*
** LuaJIT can either use internal or external frame unwinding:
**
** - Internal frame unwinding (INT) is free-standing and doesn't require
** any OS or library support.
**
** - External frame unwinding (EXT) uses the system-provided unwind handler.
**
** Pros and Cons:
**
** - EXT requires unwind tables for *all* functions on the C stack between
** the pcall/catch and the error/throw. This is the default on x64,
** but needs to be manually enabled on x86/PPC for non-C++ code.
**
** - INT is faster when actually throwing errors (but this happens rarely).
** Setting up error handlers is zero-cost in any case.
**
** - EXT provides full interoperability with C++ exceptions. You can throw
** Lua errors or C++ exceptions through a mix of Lua frames and C++ frames.
** C++ destructors are called as needed. C++ exceptions caught by pcall
** are converted to the string "C++ exception". Lua errors can be caught
** with catch (...) in C++.
**
** - INT has only limited support for automatically catching C++ exceptions
** on POSIX systems using DWARF2 stack unwinding. Other systems may use
** the wrapper function feature. Lua errors thrown through C++ frames
** cannot be caught by C++ code and C++ destructors are not run.
**
** EXT is the default on x64 systems, INT is the default on all other systems.
**
** EXT can be manually enabled on POSIX systems using GCC and DWARF2 stack
** unwinding with -DLUAJIT_UNWIND_EXTERNAL. *All* C code must be compiled
** with -funwind-tables (or -fexceptions). This includes LuaJIT itself (set
** TARGET_CFLAGS), all of your C/Lua binding code, all loadable C modules
** and all C libraries that have callbacks which may be used to call back
** into Lua. C++ code must *not* be compiled with -fno-exceptions.
**
** EXT cannot be enabled on WIN32 since system exceptions use code-driven SEH.
** EXT is mandatory on WIN64 since the calling convention has an abundance
** of callee-saved registers (rbx, rbp, rsi, rdi, r12-r15, xmm6-xmm15).
** EXT is mandatory on POSIX/x64 since the interpreter doesn't save r12/r13.
*/

附言我想您已经了解安全 Lua 类型检查 (lua_is*) 和安全函数,例如 lua_pcall

关于c++ - 为什么 std::uncaught_exception 在 LuaJIT 抛出时返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38493718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com