Check for validity of lua #61
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
I'm wondering if there's a way to check if a specific lua script is valid before executing it. I'm writing an application where users can write their own lua code, and I would like to check if the code should be able to be executed
You can try loading the file. If there are no errors, then the syntax is respected.
But this will only say that the syntax is respected. Most often, errors are not in the syntax, but in the logic of the code.
And you can make a mistake in the syntax so that you get a valid lua file, but not the one you wanted.
Well the plan I had, was to try to check if the lua is valid while they're still typing it. Similar to how an IDE works, where if it detects a simple error it will highlight the code where it happened.
Like in your example, my_funk() isn't a function name in the scope of that lua program, so it should know that it doesn't exist. I know this functionality already exists, I just need to know where in luaJ this would be accessible?
As I said, all that you can, at every opportunity (for example, completing a line input), try to load the contents as chunk and handle the exception if it does not load. But it gives almost nothing.
Lua does not provide input validation.
You can write such validators yourself.
Here some examples which you can not validate: