Scripting language with change and continue or hot swap support? (Maybe in Lua?)
I am making my existing .Net Application Scriptable for non-programming users. I added lua, it works like a charm. Then I added a debug function (pause / continue / step) via debug.sethook. It works just like a charm.
Now I understand that my application needs to be edited and continued by a function like Visual Studio. You pause execution, you can edit the code, and then resume from the current state with your changes. This feature is very important to me. I thought this would be easy for scripting languages.
I read everywhere that scripting languages can do this. But even after a lot of searching, I haven't found a Lua implementation yet. It doesn't have to be Lua, but the hot swap code in Lua will be my first choice.
How can you offer the user the ability to pause and edit the script and continue execution with the changes made?
NOTE: It doesn't have to be Lua, every scripting language will be fine
@Schollii Here's an example:
function doOnX() if getValue() == "200" then value = getCalculation() doSomething() -- many function calls, each can take about 2s doSomething() doSomething() print(value) doX(value) end end doOnX()
Thanks for your suggestions. This is how it might work:
- I will use https://github.com/frabert/NetLua Its a very cool, well written 100% C # Lua Interpreter. It first generates the AST tree and then directly executes it.
- The parser needs to be changed. In Parser.cs, parseTree is
public Ast.Block ParseString(string Chunk)
generated first. parseTree.tokens [i]. locations contain the exact position of each token. Then
it is parsed again and converted to
, but there is no location information. I will need to change this so that later I will find out which expression the string is in.
- Now every statement from the AST is executed directly through the EvalBlock. A debug function should be added (for example, in my C Binding lua Interpreter DynamicLua via debug.setHook). This can be done in the LuaInterpreter.cs internal static LuaArguments EvalBlock (`. Pause / continue / step functions should not be a problem. I can also add the currently selected line because each statement contains information about the position lines.
- When execution is suspended and code is being edited, the current LuaContxct is saved. It contains all the variables. The last expression with the last line of execution is also saved.
- Now the String code is processed again by the new AST tree. It is being fulfilled. But all statements are skipped until a stored statement with a string statement is reached. The saved one is
restored and execution can continue with any changes made.
New variables can be added after the last executed line, because a new statement
can simply add a new variable to the current LuaContext and everything should work fine.
Will this work?
source to share
I think it is quite difficult and difficult to do it right.
Probably the only way to do this is to completely recompile a piece of code. In a function, this will mean the entire function regardless of where the edit is in the function. Then call the function again. Obviously, the function must be rewired, otherwise its side effects (such as increasing the global or high value) must be reversed, which is not possible. If it's not reentrant it will still work, it just won't give the expected results (for example, if a function increments the global variable by 1 by calling it again, it will cause the global variable to be incremented by 2 as soon as the function finally returns ).
But finding lines in the script where the nodes and ends would be tricky, if indeed a general solution. For a specific solution, you will need to post the specific example scripts you want to run and the example lines you want to change. If the entire user script is recompiled and rerun, this is not a problem, but side effects are still a problem, examples may help there too.
source to share