Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: User Edit of Script without reloading entirely

0
0
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!

Viewing all articles
Browse latest Browse all 2297