简体   繁体   English

发生错误后,如何确保(luabind)lua状态良好?

[英]How do I make sure that the (luabind) lua state is good after an error has occurred?

When lua code causes an exception, luabind will leave an error message on the stack for me to collect. 当lua代码导致异常时,luabind将在堆栈上留下一条错误消息供我收集。 I am wondering how to guarantee that the lua stack will be in a sensible state after I have handled the exception: 我想知道如何在处理异常后确保lua堆栈处于明智状态:

  1. The examples I've found tells me to use luabind::from_stack(e.state(), -1) . 我发现的示例告诉我使用luabind::from_stack(e.state(), -1) Won't this leave the error message still on the stack? 这不会将错误消息保留在堆栈中吗? Shouldn't I pop it? 我不应该弹出它吗?

  2. Is it sufficient to pop the error message from the stack? 从堆栈弹出错误消息是否足够? Does the error cause other garbage to be left on the stack? 该错误是否会导致其他垃圾遗留在堆栈上?

How do I make sure that the lua state is good after an error has occurred? 发生错误后,如何确保lua状态良好?

This is what I've got: 这就是我得到的:

try  {
    // Do lua-stuff here that causes an exception from lua
}
catch (const luabind::error& e) {
    luabind::object error_msg(luabind::from_stack(e.state(), -1));

    std::stringstream ss;
    ss << error_msg;

    throw my_own_exception_class(ss.str());
}
  1. It will leave the error message on the stack. 它将错误消息留在堆栈上。 Whether you should pop it depends entirely on what you are doing with the stack next. 是否弹出它完全取决于接下来要处理的堆栈。 If you don't need the message anymore (since you saved it somewhere else) then pop it. 如果您不再需要该消息(因为您已将其保存在其他位置),则将其弹出。 If you are going to use it further down the catch chain, then leave it. 如果要在锁扣链的更下方使用它,则将其留下。 The point is, you use the Lua stack to communicate with the Lua API, what you have on it depends entirely on what you want to tell the API. 关键是,您使用Lua堆栈与Lua API进行通信,它所具有的功能完全取决于您要告诉API的内容。

  2. There are two ways to interpret "garbage" here: 这里有两种解释“垃圾”的方法:

    • As in "the Lua stack has compromised internal structure and any call to lua_XXX functions will SEGFAULT/crash/etc". 如“ Lua堆栈已破坏内部结构,并且对lua_XXX函数的任何调用都将发生SEGFAULT / crash / etc”一样。 This should never happen no matter what C++ exceptions you throw around, it's Luabind responsibility to guard against that. 无论您抛出什么C ++异常,这都永远不会发生,这是Luabind的责任,谨防这种情况。 Any C++ exceptions are thrown and handled by Luabind as Lua itself is written in C and in its world there are no such things as exceptions. Luabind会抛出和处理任何C ++异常,因为Lua本身是用C编写的,并且在世界上没有异常之类的东西。

    • As in "there are some values on the stack I no longer need". 就像“堆栈中不再需要某些值”中一样。 There shouldn't be garbage left on the stack. 堆栈上不应留有垃圾。 If you feel paranoid, feel free to clear the stack with lua_settop(0) before any chain of Lua API calls 如果您感到偏执,请在调用任何Lua API链之前,先使用lua_settop(0)清除堆栈。

Simplest way is probably this 最简单的方法可能是这样

 int luaErr = luaL_dofile(luaState, "main.lua"); // or other lua stuff
 if (luaErr != 0)
      std::cout << "Lua error: " << lua_tostring(luaState, -1) << std::end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM