2

I'm trying to embed Lua in my C program to extend the program's debugability. Lua tutorials describe how to use Lua as a config file. In the config file, Lua variables, or even functions can be defined, so a C program can load the file and use the Lua functions to extend some of its ability.

However, if the config file is modified after it's loaded, the new content won't take effect. My question is how to support hot reload without restarting my C program?

Edit: A more basic question: since the Lua functions/variables has been loaded, can they be overwritten? for example, the C program has been calling function f1(x) return x*x end in a loop, then the function is changed to be function f1(x) return x*x*x end in the config file. If we can hot reload it in some way, will the C program use the new definition?

Wei Huang
  • 590
  • 1
  • 5
  • 18
  • 2
    You need an event loop that listens for the file being changed on disk. On Linux you can use [`inotify`](http://man7.org/linux/man-pages/man7/inotify.7.html). – Henri Menke Jun 26 '19 at 08:22
  • 1
    Please specify your operating system. – Dirk Horsten Jun 26 '19 at 10:36
  • @DirkHorsten It's Linux. Will different operating systems have different solutions? – Wei Huang Jun 26 '19 at 11:06
  • 1
    Just execute the *.lua again in same environment, it will overwite. Config is usually something that persists in memory. When changes are made or application exits it is written to disk. If you want to reload when a file is modified, do you want a lua solution or a C solution? For lua, lfs will work. I suggest luamarshal to serialize a table and write it to disk for persistent storage which works for functions and userdata. Alternatively you can change a print table function to write to disk: https://stackoverflow.com/a/42062321/5113346 (ie. return { ... }) – Alundaio Jun 27 '19 at 04:24
  • @Alundaio Can your first sentence "Just execute the *.lua again in same environment, it will overwite." answer the basic question I raised above? The new function definition takes effect as soon as the *.lua is executed again? – Wei Huang Jul 02 '19 at 02:33

0 Answers0