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

Edited Issue: Repeated method call [105]

$
0
0
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?

Viewing all articles
Browse latest Browse all 2297

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>