Check for validity of lua #61

Closed
opened 2019-11-06 16:34:53 +00:00 by orange451 · 3 comments
orange451 commented 2019-11-06 16:34:53 +00:00 (Migrated from github.com)

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

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
Enyby commented 2019-11-06 22:20:21 +00:00 (Migrated from github.com)

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.

function my_func() end
my_funk()

And you can make a mistake in the syntax so that you get a valid lua file, but not the one you wanted.

function a(v) print('a', v) end
function b(v) print('b', v) end
local my_print = print
(a or b)(5)
my_print('OK')
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. ``` function my_func() end my_funk() ``` And you can make a mistake in the syntax so that you get a valid lua file, but not the one you wanted. ``` function a(v) print('a', v) end function b(v) print('b', v) end local my_print = print (a or b)(5) my_print('OK') ```
orange451 commented 2019-11-07 14:55:47 +00:00 (Migrated from github.com)

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?

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?
Enyby commented 2019-11-08 04:05:21 +00:00 (Migrated from github.com)

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:

do
	local function my_func() end
end
my_func()
function my_func() end
_G['my_func'] = nil
my_func()
function my_func() end
my_func = nil
my_func()
function A()
	function my_func() end
end
function B()
	my_func = nil
end
A()
my_func()
B()
my_func()
if something then
	function my_func() end
end
my_func()
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: ``` do local function my_func() end end my_func() ``` ``` function my_func() end _G['my_func'] = nil my_func() ``` ``` function my_func() end my_func = nil my_func() ``` ``` function A() function my_func() end end function B() my_func = nil end A() my_func() B() my_func() ``` ``` if something then function my_func() end end my_func() ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: open-autonomous-connection/luaj#61