This project has moved and is read-only. For the latest updates, please go here.

User Edit of Script without reloading entirely

Jan 26, 2015 at 11:27 PM
If I have a JavaScript function like the one below, I understand that I must call Engine.Execute(ScriptText) from the Host to both load and execute the code.
function foo() {
  console.log("some message");
}  
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?

The reason I am asking is that my app will include a bunch of host object loaded into an engine then one (or more) user written scripts. All are loaded into the same engine using Execute. The user then develops their script and presses a Run button to load and execute their piece of the puzzle. After executing, the user may want to edit the script and update the engine with the changes but without reloading the entire engine (don't want to reload all the host objects and other support scripts etc.). The executed (executing) script is very stateful and updates itself and its variables through callbacks and interfacing with other dynamic systems so I want to preserve state as much as possible.

How can I let the user edit a script that has been executed without reloading the entire script and host objects?
Coordinator
Jan 27, 2015 at 3:41 PM
Hello!

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 foo whose value is a function. The second simply assigns a new value to foo. 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.

Scripts can easily change or delete objects set up by the host or by other scripts. They can mess up the environment by replacing or deleting JavaScript built-ins and host objects (depending on how the host objects were set up).

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 strict mode, thus preventing any damage to the global environment.

Good luck!
Jan 27, 2015 at 4:39 PM
That is good to know.

The nature of the application is such that they are not likely to mess up the global environment but if they do, we can tell them not to do that. The app won't have wide distribution - it will only be used people who work with me and a few select customers.

I like the idea of wrapping in an anonymous function with strict mode but I'll need to see how that impacts the rest of the application and it's interaction with the Host. In general, I think if I just document the behaviour you described that should be sufficient. The user will have the option to reload a clean environment or keep editing the existing environment. I'll give them something like a Reload button that will recreate a new clean script engine and reload everthing including their latest user script. They can choose to Run their dirty script or Reload a clean script.

Thanks for your answer.