【问题标题】:lua+luabind, no error information at top of stack, after "runtime error"lua+luabind,栈顶没有错误信息,在“运行时错误”之后
【发布时间】:2012-06-01 20:58:39
【问题描述】:

我正在尝试将 lua 嵌入到我的游戏引擎中。直到现在一切都很好。我开始注册与游戏引擎交互的函数(例如 move_object(id, x, y)),并且注册工作正常。接下来,我尝试这样使用它:

handlers={}

handlers["update"] = function(objId)
--  print(objId)
    move_object(objId, 0, 0)
end

这给了我一个来自 luabind 的“运行时错误”异常。我到处搜索,根据互联网(和 luabind 源),错误消息应该仍然在 lua 堆栈的顶部。我尝试捕获 luabind::error,并像这样打印堆栈的顶部:

std::cout << "error: lua: " << lua_tostring(exc.state(), -1) << std::endl;

像这样:

std::cout << "error: lua: " << luabind::object(luabind::from_stack(exc.state(), -1)) << std::endl

并且都打印这个:

error: lua: update

我假设这只是我从处理程序表中调用更新函数时推送的最后一个值。这个值应该已经被覆盖,在遇到错误时,使用详细的错误字符串,对吗?根据class error定义处的luabind源文件“error.hpp”:

// this exception usually means that the lua function you called
// from C++ failed with an error code. You will have to
// read the error code from the top of the lua stack
// the reason why this exception class doesn't contain
// the message itself is that std::string's copy constructor
// may throw, if the copy constructor of an exception that is
// being thrown throws another exception, terminate will be called
// and the entire application is killed.

我相信我的 lua 代码的问题实际上在于我的 move_object 函数的注册(更新函数运行。我知道这一点,因为我之前取消了对打印调用的注释),但如果没有更好的错误信息,我无法确定!哈哈

如果有任何帮助,我已修复错误。我是这样定义 move_object 函数的:

luabind::module(m_L)[
    luabind::def("move_object", &ScriptEntityManager::move_object)
];

但显然,您不能使用 luabind 将静态成员函数定义为常规函数。我把它改成了这个(使move_object成为C++中的常规全局函数):

luabind::module(m_L)[
    luabind::def("move_object", move_object)
];

我不知道这是否有助于解决原始问题,但我认为更多信息不会造成伤害! :)

【问题讨论】:

    标签: c++ lua runtime-error luabind


    【解决方案1】:

    在捕获 luabind 异常时,我也会得到错误消息的错误值。我通过设置 luabind (set_pcall_callback) 使用的错误处理程序解决了这个问题,以便它打印错误(和调用堆栈),这发生在抛出异常之前。这样对我来说效果很好。

    但是如果有人真的知道为什么在捕获异常时堆栈顶部不包含错误消息,我也很感兴趣:-)

    这是我使用的错误处理程序,以防它可以帮助某人(其中“打印”是一些可以记录 std::string 的自定义函数):

    int luabindErrorHandler( lua_State* L )
    {
        // log the error message
        luabind::object msg( luabind::from_stack( L, -1 ) );
        std::ostringstream str;
        str << "lua> run-time error: " << msg;
        print( str.str() );
    
        // log the callstack
        std::string traceback = luabind::call_function<std::string>( luabind::globals(L)["debug"]["traceback"] );
        traceback = std::string( "lua> " ) + traceback;
        print( traceback.c_str() );
    
        // return unmodified error object
        return 1;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-19
      • 2013-07-25
      • 1970-01-01
      • 2014-07-16
      • 2017-10-30
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 2018-01-12
      相关资源
      最近更新 更多