7

C/C++ から lua スタック内のすべてのエラーを取得することは可能ですか? これが私が試したものです

c++

int main()
{
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);

    if (luaL_loadfile(L, "LuaBridgeScript.lua"))
    {
        throw std::runtime_error("Unable to find lua file");
    }

    int error = lua_pcall(L, 0, 0, 0);
    while (error && lua_gettop(L))
    {
        std::cout << "stack = " << lua_gettop(L) << "\n";
        std::cout << "error = " << error << "\n";
        std::cout << "message = " << lua_tostring(L, -1) << "\n";
        lua_pop(L, 1);
        error = lua_pcall(L, 0, 0, 0);
    }
}

ルア:

printMessage("hi")
printMessage2("hi2")

出力:

stack = 1
error = 2
message = LuaBridgeScript.lua:1: attempt to call global 'printMessage' <a nil value>

スタックサイズが0または負の場合でもループを試みましたが、スタックが負になる可能性があり、数回試行するとプログラムがクラッシュする方法がわかりません。

4

2 に答える 2

5

To wrap up my comments into an answer:

According to Lua docs on lua_pcall, pcall returns on either success (end of chunk) or first error thrown. So in the latter case, it will push only one message to the stack. It will never continue execution after the first error.

What you're trying to achieve is checking for possible errors in the file. In statically typed languages like C, every variable has to be defined at compile time, so the compiler can spot instances of calling non existing function.

Lua, however, is a dynamically typed language, in which variables have no types but rather are placeholders for values (which do have types). This means, that Lua cannot tell in advance whether printMessage is a function, a string, a value or if it doesn't exist (nil). It is only at run time when the variable is about to be called that Lua checks its type.

It is therefore not possible to achieve what you want that way. Running the code beyond the first unhandled error is pointless as well, as the error may render assumptions in subsequent fragments invalid (for instance about global variables that the non existing function should have set) - it would be a mess.

As to syntax errors, these are generally caught when compiling the source file, that is while loading. However, Lua parser stops at first encountered syntax error. This is due to the fact that many times syntax errors in one place invalidate everything that follows it. As Etan pointed out in his comment, many parsers report subsequent errors that disappear or change once you've fixed errors that precede them. It is also true for even heavy weight parsers like those in MSVS.

于 2014-08-13T17:47:25.927 に答える