Lua+Luabind,"runtime error"后堆栈顶部没有错误信息
lua+luabind, no error information at top of stack, after "runtime error"
我正在尝试将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
我假设这只是我从处理程序表中调用更新函数时推送的最后一个值。在遇到错误时,此值应该被详细的错误字符串覆盖,对吗?根据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函数的注册(更新函数运行。我知道这一点,因为我之前已经取消了该打印调用的注释),但是如果没有更好的错误信息,我无法确定!哈哈
我修复了错误,如果它有任何帮助。我像这样定义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)
];
我不知道这是否有助于解决原始问题,但我认为更多信息不会有什么坏处! :)
在捕获 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;
}
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 我的代码中有错误,未声明的标识符
- C++ LeetCode #377 的 DP 解决方案中,此代码是否有错误?
- 试图建立银行管理系统,但有错误
- 我试图创建临时对象的方式有错误吗
- 我的随机生成器是否不工作,或者我决定人/骨架是否击中对手的方式是否有错误
- 我在OpenCV中有错误的"approxPolyDP(ROI_Vertices, ROI_Poly, 1.0, true)"
- 我在这个 C++ 代码上有错误吗?
- 是视觉C++优化器错误还是我的代码中有错误?
- 为什么一个简单的C++程序会有错误的结果?是 #define 引起的吗?
- 对此有错误
- C :数据文件有错误:预期的无限制ID
- 一个带有参数的程序,为什么此程序有错误的答案
- 仅使用boost.datetime在Windows上有错误
- 使用向量循环时,是否有错误
- Microsoft Visual Studio发现我所有的CUDA设备端函数都有错误
- 继承默认构造函数在 gcc 中失败并在 clang 中工作,哪个有错误?
- 为什么有错误未定义_getws_s
- 在快速排序程序中有错误
- C - 检查CIN是否有错误