首页 > 解决方案 > 如何获取消息、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”中,只有一个错误代码,如何获取回溯函数信息?

这个类会有很多对象,所以不能使用全局变量来传递它。还有其他方法吗?

标签: lua

解决方案


推荐阅读