What happens if I call Engine.Execute(ScriptText) a second time on the same engine with a different string in console.log()? Is there a duplicate foo() function in my script?
No. The first invocation creates a global property
whose value is a function. The second simply assigns a new value to
. The original function is eventually cleaned up by the garbage collector.
If on the other hand the second invocation changed the function name, then yes, you'd end up with two functions.
How can I let the user edit a script that has been executed without reloading the entire script and host objects?
You can re-execute scripts with or without modifications as many times as you'd like, but you need to understand the potential consequences.
The question is, what capabilities do the user scripts in your application require? Do they need to be able to install global functions and overwrite global data directly (as opposed to via some API that you provide for them)? Your description makes it sound
as if they do, and if so, then there's really no way to restrict them.
If not, then there are things you can do. For example, when the Run button is clicked, instead of executing the user script directly, you could wrap it within an anonymous function that internally enables
, thus preventing any damage to the global environment.