It looks like calling a JavaScript function from c# with an implicit this object will be executed twice if an exception ist thrown during execution.
It is possible to work around this issue via explicit this.
Consider the following test code:
```
var engine = new V8ScriptEngine( "Test", V8ScriptEngineFlags.DisableGlobalMembers );
dynamic funcObj = engine.Evaluate( "({ func: function(counter) { counter.val = counter.val + 1; throw new Error('Counter was ' + counter.val); } })" );
dynamic counter1 = engine.Evaluate( "({val: 0})" );
dynamic counter2 = engine.Evaluate( "({val: 0})" );
try
{
funcObj.func( counter1 );
}
catch( Exception ex )
{
System.Console.WriteLine( "Counter 1: " + counter1.val );
// Counter 1: 2
}
try
{
var func = funcObj.func;
func.call( funcObj, counter2 );
}
catch( Exception ex )
{
System.Console.WriteLine( "Counter 2: " + counter2.val );
// Counter 2: 1
}
```
Am I doing something wrong here?
Comments: Hi ZoneRunner, You aren't doing anything wrong. This issue is the result of an unexpected interaction between ClearScript and the C# dynamic runtime. Specifically, the C# syntax `funcObj.func(counter1)` can have two meanings: "invoke the `func` method" or "retrieve the `func` property and invoke the result (e.g., if it's a delegate)". So the runtime tries it both ways, oblivious to the fact that in JavaScript they amount to the same thing. Thanks for reporting this issue!
It is possible to work around this issue via explicit this.
Consider the following test code:
```
var engine = new V8ScriptEngine( "Test", V8ScriptEngineFlags.DisableGlobalMembers );
dynamic funcObj = engine.Evaluate( "({ func: function(counter) { counter.val = counter.val + 1; throw new Error('Counter was ' + counter.val); } })" );
dynamic counter1 = engine.Evaluate( "({val: 0})" );
dynamic counter2 = engine.Evaluate( "({val: 0})" );
try
{
funcObj.func( counter1 );
}
catch( Exception ex )
{
System.Console.WriteLine( "Counter 1: " + counter1.val );
// Counter 1: 2
}
try
{
var func = funcObj.func;
func.call( funcObj, counter2 );
}
catch( Exception ex )
{
System.Console.WriteLine( "Counter 2: " + counter2.val );
// Counter 2: 1
}
```
Am I doing something wrong here?
Comments: Hi ZoneRunner, You aren't doing anything wrong. This issue is the result of an unexpected interaction between ClearScript and the C# dynamic runtime. Specifically, the C# syntax `funcObj.func(counter1)` can have two meanings: "invoke the `func` method" or "retrieve the `func` property and invoke the result (e.g., if it's a delegate)". So the runtime tries it both ways, oblivious to the fact that in JavaScript they amount to the same thing. Thanks for reporting this issue!