lua script error checking

后端 未结 3 1987
南笙
南笙 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:44

    (This was originally meant as a reply to the first comment to Krtek's question, but I ran out of space there and to be honest it works as an answer just fine.)

    Functions are essentially values, and thus a named function is actually a variable of that name. Variables, by their very definition, can change as a script is executed. Hell, someone might accidentally redefine one of those functions. Is that bad? To sum my thoughts up: depending on the script, parameters passed and/or actual implementations of those pre-defined functions you speak of (one might unset itself or others, for example), it is not possible to guarantee things work unless you are willing to narrow down some of your demands. Lua is too dynamic for what you are looking for. :)

    If you want a flawless test: create a dummy environment with all bells and whistles in place, and see if it crashes anywhere along the way (loading, executing, etc). This is basically a sort of unit test, and as such would be pretty heavy.

    If you want a basic check to see if a script has a valid syntax: Krtek gave an answer for that already. I am quite sure (but not 100%) that the lua equivalent is to loadfile or loadstring, and the respective C equivalent is to try and lua_load() the code, each of which convert readable script to bytecode which you would already need to do before you could actually execute the code in your normal all-is-well usecase. (And if that contained function definitions, those would need to be executed later on for the code inside those to execute.)

    However, these are the extent of your options with regards to pre-empting errors before they actually happen. Lua is a very dynamic language, and what is a great strength also makes for a weakness when you want to prove correctness. There are simply too many variables involved for a perfect solution.

    0 讨论(0)
  • 2020-12-28 22:46

    You can use the LUA Compiler. It will only compile your file to bytecode without executing it.

    Your program will also have the advantage the run faster if it is compiled.

    You can even use the -p option to only perform a syntax checking, according to the linked man page :

    -p load files but do not generate any output file. Used mainly for syntax checking or testing precompiled chunks: corrupted files will probably generate errors when loaded. For a thourough integrity test, use -t.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题