lua - 如何获取消息、lua_pcall 调用者和错误处理程序
问题描述
我叫 lua_pcall 函数是一个 c++ calss 对象,而错误处理程序是 Lua_CFunction,所以没有类信息,如何从错误处理程序中获取一些信息?
// The error handler
static int traceback( lua_State *L)
{
auto msg = CLua::GetCallStack(L);
cout << msg << endl;
return 0;
}
// the class function
bool CLua::RunFunction(const string & strFunctionName , ...)
{
// push error handler
lua_pushcfunction(m_pScriptContext,traceback);
auto nErr = lua_gettop(m_pScriptContext);
PushFunction(strFunctionName);
...
// push params.
...
auto res = lua_pcall(m_pScriptContext, 4, LUA_MULTRET, nErr);
lua_remove( m_pScriptContext, nErr );
if( res != LUA_OK )
{
**// In here I want get msg from the error handler.**
return false;
}
}
当错误发生时,lua 会调用错误处理函数“traceback”。然后,这个函数会做一些事情,然后返回。而在 lua_pcall 函数调用者“RunFunction”中,只有一个错误代码,如何获取回溯函数信息?
这个类会有很多对象,所以不能使用全局变量来传递它。还有其他方法吗?
解决方案
推荐阅读
- regex - Perl:显示正则表达式,不匹配
- python - 如何使用克莱默规则(Python)找到变量的值?
- c - 为什么 strlen() 有效而我的方法无效?
- angular - Angular 7 ExpressionChangedAfterItHasBeenCheckedError 与 NgsScrollReveal
- javascript - 如何将表格数据放入jQuery中的数组中?
- batch-file - 如何修复批处理文件中的 goto 问题
- django - 如何从 Django 模型中 ImageField 的图像目录中选择一个随机图像?
- android - 仅在从 android studio 的 Datepickerdialog 中选择之后,如何才能显示选定的日期和名称?
- c# - 模型中的虚拟字段在保存时出错
- pdf - 通过使用 Ghostscript 向 PDF 添加边距来缩放页面中的图像