Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lua script error checking

Tags:

lua

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))
    {
        fprintf (stderr, "%s", lua_tostring (L, -1));
        lua_pop (L, 1);
    }

    if(lua_pcall(L, 0, 0, 0))
    {
        fprintf (stderr, "%s", lua_tostring (L, -1));
        lua_pop (L, 1);
    }

But if the script contains errors it passes first if and it is executed. I want to know if it contains errors when I load it, not when I execute it. Is this possible?

like image 987
Mircea Ispas Avatar asked Mar 09 '11 17:03

Mircea Ispas


People also ask

How do I check Lua errors?

From Blizzard Client, open World of Warcraft. Navigate to the Game Menu. Tap on the Help menu, which is in the left panel. Now, in the right panel find Display LUA Errors.

What does Pcall do in Lua?

The pcall function calls its first argument in protected mode, so that it catches any errors while the function is running. If there are no errors, pcall returns true, plus any values returned by the call. Otherwise, it returns false, plus the error message.

What is a Lua programming error?

A Lua error is caused when the code that is being ran is improper. There are many reasons for why a Lua error might occur, but understanding what a Lua error is and how to read it is an important skill that any developer needs to have.


1 Answers

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.

like image 157
krtek Avatar answered Oct 16 '22 07:10

krtek