lua script error checking

后端 未结 3 1997
南笙
南笙 2020-12-28 22:21

Is it possible to check if a lua script contains errors without executing it? I have fallowing code:


    if(luaL_loadbuffer(L, data, size, name))
    {
             


        
3条回答
  •  礼貌的吻别
    2020-12-28 22:55

    In general it is not possible, as Lua is a dynamic language, and most of errors happen in runtime.

    If you want to check for syntax errors, use luac -p option. I use it as a part of my pre-commit hook, for example.

    Other common errors are triggering by misusing the global variables. You may analyze output of luac -l to catch these cases. See here: http://lua-users.org/wiki/DetectingUndefinedVariables.

    If you want something more advanced, there are several more-or-less functional static analysis tools for Lua code. Start with LuaInspect.

    In any case, you are advised to write unit tests instead of just relying on static code checks. Less pain, more gain.

提交回复
热议问题