Currently, I'm trying to figure out whether something like this is a safe thing to do:
My example above runs and works as expected, but it might be undefined behaviour, therefore I'm asking here.
In my real world application I'm currently creating new engine instances each time a recursion is hit, but this seems to take a lot of overhead, so I'm trying to improve performance (see the following example).
To sum up, I want to know whether this is supported:
internal static class Program
{
private static void Main()
{
using (var engine = new JScriptEngine())
{
engine.AddHostObject(@"host", new HostFunctions(engine));
Console.WriteLine(engine.Evaluate(@"host.RunIt()"));
}
}
}
public sealed class HostFunctions
{
private readonly ScriptEngine _engine;
public HostFunctions(ScriptEngine engine)
{
_engine = engine;
}
public object RunIt()
{
return _engine.Evaluate(@"1+2");
}
}
I.e. I'm executing a script and within the script, there is another call to the same scripting engine instance.My example above runs and works as expected, but it might be undefined behaviour, therefore I'm asking here.
In my real world application I'm currently creating new engine instances each time a recursion is hit, but this seems to take a lot of overhead, so I'm trying to improve performance (see the following example).
internal static class Program
{
private static void Main()
{
using (var engine = new JScriptEngine())
{
engine.AddHostObject(@"host", new HostFunctions());
Console.WriteLine(engine.Evaluate(@"host.RunIt()"));
}
}
}
public sealed class HostFunctions
{
public object RunIt()
{
using (var engine = new JScriptEngine())
{
return engine.Evaluate(@"1+2");
}
}
}
I did some poor-man's benchmarking with the StopWatch
.NET class and about 50,000 loops and it seems that the version with only one ScriptEngine
instance is roughly about 30% faster than the one with multiple nested ScriptEngine
instances.To sum up, I want to know whether this is supported:
- Call Execute or Evaluate of one script engine instance recursively/nested
- Do this on one thread only (so no multi-threading)