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

A bit more modularization

Apr 7, 2014 at 8:11 PM
Hello ClearScript,

is there any option for a bit more modularization? As far as I can see, Compile, Evaluate and Execute are additionaly so that with each call the JavaScript Code is a bit bigger. What I want is a bit more separation for pieces of code, like
var compiledscript = engine.Compile(modulescript);
engine.Execute(compiledscript, " some java script code");
The second parameter relates to the compiled script and is only valid for that.

I guess I may use another instance of the script engine to achive this, but it might be to expensive.

Best regards,

P.S. What does ExecuteCommand other then Execute or Evaluate? Is the return value the only difference?
Apr 7, 2014 at 10:38 PM
Hi Torsten,

You appear to be concerned about unrelated scripts leaving functions and other objects around, eventually leading to a cluttered and unpredictable execution environment for subsequent scripts. Is that correct?

If so, then you should definitely use separate script engine instances. Creating a new script engine instance is the only way to guarantee a pristine execution environment. If concurrency is not a requirement, you can reduce costs by reusing the V8 runtime, which would allow you to reuse previously compiled scripts as well. Have a look at the V8Runtime class in the library reference.

Oh, and yes, for JavaScript, ExecuteCommand() is just like Evaluate() except for its treatment of the execution result.

Good luck!
Apr 18, 2014 at 3:34 PM
Hello ClearScript,

what I initially wanted was something like require in node.js:
var app = require('modulename');


externals.use('modulename'); // loads the script modulename and put it by _Evaluate_ to the running instance of the script engine

... usage...
I hoped to get a little more separation what it is in the app variable. Instead of require I had a package manager, who compiled the script that modulename refers. But I didn't find any way to use it (i.e. call a method explicit within the compiled script). I may add the compiled script to any script engine instance of the same runtime, but I cannot see any other usage for this scenario.

Meanwhile I think this approach is problematically. Because the compile method compiles only the script that's in the parameter and it will not been executed, something like a composition of scripts will fail when it refer to other objects and/or scripts and will bring to unexpected results because its never apparent what code is currently within the engine execution.

So it seems like I have to put down my idea and use it as it looks like: compile once and use it in many instances of the script engine.

Kindly regards,
Apr 22, 2014 at 2:23 AM
Hi Torsten,

If using a JavaScript package manager such as RequireJS is practical in your scenario, then we highly recommend it. These frameworks are specifically designed to manage dependencies, eliminate namespace pollution, and prevent the kind of unpredictable binding that you're concerned about. Have a look here for an example of how RequireJS can be used with ClearScript.

Good luck!