在“运行时错误”之后,lua+luabind栈顶无错误信息。

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

handlers={}

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

这使我收到了一个 luabind 的 "runtime error" 异常。我到处搜索,根据互联网(和 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

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

// 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 函数的注册(update 函数正在运行。我知道这一点,因为我已经取消了注释,并在之前打印了该调用!),但没有更好的错误信息,我不能确定!哈哈

我已解决错误(如果有帮助的话)。我定义 move_object 函数是这样的:

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

但显然,您不能将静态成员函数定义为 luabind 的常规函数。我将其更改为(在 C++ 中使 move_object 成为常规全局函数):

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

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

点赞
用户908326
用户908326

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

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

这是我使用的错误处理程序(其中"print"是一些自定义函数,可以记录一个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;
}
2013-01-05 22:14:53