Search code examples
lualua-5.2

Read data before executing lua file


I want to read a table inside a Lua file before executing it. Is there a way to do this with loadfile. It returns only a function and I can't seem to be able to read what is inside (what is declared but not executed).

The other option I tried is to check if the environment changed, but yet again I couldn't read inside the function returned by loadfile().

Is there a way to do this without opening the file as text and searching the table?

Here is an example of the table I try to retrieve:

--file to be loaded  
local library = require("library")  --random requires...  
table = { author = "myself", dependencies = "library > 1.0"} --table to get before execution

Solution

  • What you want is not possible. There are no declarations in Lua, only executable statements. You need to execute a script to see what it does.

    However, you could read the file as text and try to extract the info you need using pattern matching. This won't be foolproof but it'll probably work in most cases if the files are written in the same way.