Lua - restart script state without reprocessing it

I have an application that runs Lua scripts. Each Lua script will probably run several times. Some scripts may run every time you press a key.

I would like these scripts to be "reset" between each run. I. If the user sets the variable Foo, then Foo should not exist in the script the next time it runs, until the user defines it again.

The problem is that if I want to have this behavior, I need to create a new lua_State every time, and then open the libraries in it each time, and then analyze the script file every time, which seems very unoptimized.

Downloading libraries can be quite an easy operation (I guess), but parsing scripts is probably not.

Is there a way to reset the state of a Lua script (i.e. clear user-defined variables) without creating a new lua_State and renaming the entire Lua script file? I just want the script files to be parsed once at application startup, since they are not modified at runtime.

Thanks.:)

EDIT : I found this topic, but it is not said in detail: http://lua-users.org/lists/lua-l/2006-01/msg00493.html p>

EDIT : lua_setfenv seems to be related to this. I will dig a little more.

EDIT . It seems that with LUA 5.2 there is no longer lua_setfenv. Since I'm using 5.3, I would need to set the environement (i.e. the nammed _ENV hidden table where the variables are stored) to do this, and thus reload everything I don't want to do ...

+5
source share
2 answers

The last time I looked at this answer, unfortunately, there was no answer.

You also need to remember that Lua can call libraries that can open files, malloc() , etc., and that any โ€œresetโ€ must deal with closing these files, freeing this memory, etc.

As an alternative to โ€œresettingโ€ the Lua state, you can simply arrange your code so that it does not need to reset; this explicitly requires that Lua code be written in a certain way. One way to do this is to insist that your Lua code is completely (or almost completely) contained within a function (s) and calls one or more functions for each action. Out-of-function code can (for example) return a Lua table consisting of links to invoke specific entry points; it could be called only once. Function (s), when called, will be cleared after itself, including clearing any hosted element libraries, open files, etc. Global variables should be avoided (if not constant). We successfully use this approach to ensure that Lua is analyzed only once, entry points are determined once, but relative small functions could be called very quickly with little overhead.

In the comments you suggested, you can lexically wrap Lua code in a function block. I think this is less flexible than the above approach, and has the following disadvantages:

  • You lose the ability to do "once init" (for example, for example, reading a fixed constant from disk)

  • You risk unpredictable things if the user inserts (for example) an inappropriate end ... function B() into his code

  • You limit yourself to one entry point per Lua state.

This means that the Lua code must be written differently (in fact, the Lua encoder submits the code in the required form). One possible way to do this is to use a fixed framework for this and require in code that will be called as libraries. I have not tried this approach.

+1
source

You can not clear lua_State? Remove all threads and manually set global variables. You may need to isolate the user environment from the global environment.

0
source

Source: https://habr.com/ru/post/1243141/


All Articles