@@ -330,56 +330,6 @@ private void ThrowError()
330330 }
331331 }
332332
333- private void InvokeScript ( Action action )
334- {
335- _dispatcher . Invoke ( ( ) =>
336- {
337- try
338- {
339- action ( ) ;
340- }
341- catch ( ActiveScriptException e )
342- {
343- throw ConvertActiveScriptExceptionToJsRuntimeException ( e ) ;
344- }
345- catch ( TargetInvocationException e )
346- {
347- var activeScriptException = e . InnerException as ActiveScriptException ;
348- if ( activeScriptException != null )
349- {
350- throw ConvertActiveScriptExceptionToJsRuntimeException ( activeScriptException ) ;
351- }
352-
353- throw ;
354- }
355- } ) ;
356- }
357-
358- private T InvokeScript < T > ( Func < T > func )
359- {
360- return _dispatcher . Invoke ( ( ) =>
361- {
362- try
363- {
364- return func ( ) ;
365- }
366- catch ( ActiveScriptException e )
367- {
368- throw ConvertActiveScriptExceptionToJsRuntimeException ( e ) ;
369- }
370- catch ( TargetInvocationException e )
371- {
372- var activeScriptException = e . InnerException as ActiveScriptException ;
373- if ( activeScriptException != null )
374- {
375- throw ConvertActiveScriptExceptionToJsRuntimeException ( activeScriptException ) ;
376- }
377-
378- throw ;
379- }
380- } ) ;
381- }
382-
383333 /// <summary>
384334 /// Executes a script text
385335 /// </summary>
@@ -516,35 +466,33 @@ private void InnerSetVariableValue(string variableName, object value)
516466 }
517467 }
518468
519- private void EmbedHostItem ( string itemName , object value )
469+ private void InnerEmbedHostItem ( string itemName , object value )
520470 {
521- InvokeScript ( ( ) =>
471+ object oldValue = null ;
472+ if ( _hostItems . ContainsKey ( itemName ) )
522473 {
523- object oldValue = null ;
524- if ( _hostItems . ContainsKey ( itemName ) )
525- {
526- oldValue = _hostItems [ itemName ] ;
527- }
528- _hostItems [ itemName ] = value ;
474+ oldValue = _hostItems [ itemName ] ;
475+ }
476+ _hostItems [ itemName ] = value ;
529477
530- try
478+ try
479+ {
480+ _activeScriptWrapper . AddNamedItem ( itemName , ScriptItemFlags . IsVisible | ScriptItemFlags . GlobalMembers ) ;
481+ }
482+ catch
483+ {
484+ if ( oldValue != null )
531485 {
532- _activeScriptWrapper . AddNamedItem ( itemName , ScriptItemFlags . IsVisible | ScriptItemFlags . GlobalMembers ) ;
486+ _hostItems [ itemName ] = oldValue ;
533487 }
534- catch ( Exception )
488+ else
535489 {
536- if ( oldValue != null )
537- {
538- _hostItems [ itemName ] = oldValue ;
539- }
540- else
541- {
542- _hostItems . Remove ( itemName ) ;
543- }
544-
545- throw ;
490+ _hostItems . Remove ( itemName ) ;
546491 }
547- } ) ;
492+
493+ ThrowError ( ) ;
494+ throw ;
495+ }
548496 }
549497
550498 /// <summary>
@@ -614,30 +562,52 @@ public override string Mode
614562
615563 public override object Evaluate ( string expression , string documentName )
616564 {
617- object result = InvokeScript ( ( ) => InnerExecute ( expression , documentName , true ) ) ;
565+ object result = _dispatcher . Invoke ( ( ) =>
566+ {
567+ try
568+ {
569+ return InnerExecute ( expression , documentName , true ) ;
570+ }
571+ catch ( ActiveScriptException e )
572+ {
573+ throw ConvertActiveScriptExceptionToJsRuntimeException ( e ) ;
574+ }
575+ } ) ;
576+
618577 result = MapToHostType ( result ) ;
619578
620579 return result ;
621580 }
622581
623582 public override void Execute ( string code , string documentName )
624583 {
625- InvokeScript ( ( ) =>
584+ _dispatcher . Invoke ( ( ) =>
626585 {
627- InnerExecute ( code , documentName , false ) ;
586+ try
587+ {
588+ InnerExecute ( code , documentName , false ) ;
589+ }
590+ catch ( ActiveScriptException e )
591+ {
592+ throw ConvertActiveScriptExceptionToJsRuntimeException ( e ) ;
593+ }
628594 } ) ;
629595 }
630596
631597 public override object CallFunction ( string functionName , params object [ ] args )
632598 {
633599 object [ ] processedArgs = MapToScriptType ( args ) ;
634600
635- object result = InvokeScript ( ( ) =>
601+ object result = _dispatcher . Invoke ( ( ) =>
636602 {
637603 try
638604 {
639605 return InnerCallFunction ( functionName , processedArgs ) ;
640606 }
607+ catch ( ActiveScriptException e )
608+ {
609+ throw ConvertActiveScriptExceptionToJsRuntimeException ( e ) ;
610+ }
641611 catch ( MissingMemberException )
642612 {
643613 throw new JsRuntimeException (
@@ -652,7 +622,7 @@ public override object CallFunction(string functionName, params object[] args)
652622
653623 public override bool HasVariable ( string variableName )
654624 {
655- bool result = InvokeScript ( ( ) =>
625+ bool result = _dispatcher . Invoke ( ( ) =>
656626 {
657627 bool variableExist ;
658628
@@ -661,6 +631,10 @@ public override bool HasVariable(string variableName)
661631 object variableValue = InnerGetVariableValue ( variableName ) ;
662632 variableExist = variableValue != null ;
663633 }
634+ catch ( ActiveScriptException e )
635+ {
636+ throw ConvertActiveScriptExceptionToJsRuntimeException ( e ) ;
637+ }
664638 catch ( MissingMemberException )
665639 {
666640 variableExist = false ;
@@ -674,12 +648,16 @@ public override bool HasVariable(string variableName)
674648
675649 public override object GetVariableValue ( string variableName )
676650 {
677- object result = InvokeScript ( ( ) =>
651+ object result = _dispatcher . Invoke ( ( ) =>
678652 {
679653 try
680654 {
681655 return InnerGetVariableValue ( variableName ) ;
682656 }
657+ catch ( ActiveScriptException e )
658+ {
659+ throw ConvertActiveScriptExceptionToJsRuntimeException ( e ) ;
660+ }
683661 catch ( MissingMemberException )
684662 {
685663 throw new JsRuntimeException (
@@ -695,32 +673,72 @@ public override object GetVariableValue(string variableName)
695673 public override void SetVariableValue ( string variableName , object value )
696674 {
697675 object processedValue = MapToScriptType ( value ) ;
698- InvokeScript ( ( ) => InnerSetVariableValue ( variableName , processedValue ) ) ;
676+
677+ _dispatcher . Invoke ( ( ) =>
678+ {
679+ try
680+ {
681+ InnerSetVariableValue ( variableName , processedValue ) ;
682+ }
683+ catch ( ActiveScriptException e )
684+ {
685+ throw ConvertActiveScriptExceptionToJsRuntimeException ( e ) ;
686+ }
687+ } ) ;
699688 }
700689
701690 public override void RemoveVariable ( string variableName )
702691 {
703- InvokeScript ( ( ) =>
692+ _dispatcher . Invoke ( ( ) =>
704693 {
705- InnerSetVariableValue ( variableName , null ) ;
694+ try
695+ {
696+ InnerSetVariableValue ( variableName , null ) ;
706697
707- if ( _hostItems . ContainsKey ( variableName ) )
698+ if ( _hostItems . ContainsKey ( variableName ) )
699+ {
700+ _hostItems . Remove ( variableName ) ;
701+ }
702+ }
703+ catch ( ActiveScriptException e )
708704 {
709- _hostItems . Remove ( variableName ) ;
705+ throw ConvertActiveScriptExceptionToJsRuntimeException ( e ) ;
710706 }
711707 } ) ;
712708 }
713709
714710 public override void EmbedHostObject ( string itemName , object value )
715711 {
716712 object processedValue = MapToScriptType ( value ) ;
717- EmbedHostItem ( itemName , processedValue ) ;
713+
714+ _dispatcher . Invoke ( ( ) =>
715+ {
716+ try
717+ {
718+ InnerEmbedHostItem ( itemName , processedValue ) ;
719+ }
720+ catch ( ActiveScriptException e )
721+ {
722+ throw ConvertActiveScriptExceptionToJsRuntimeException ( e ) ;
723+ }
724+ } ) ;
718725 }
719726
720727 public override void EmbedHostType ( string itemName , Type type )
721728 {
722729 var typeValue = new HostType ( type , _engineMode ) ;
723- EmbedHostItem ( itemName , typeValue ) ;
730+
731+ _dispatcher . Invoke ( ( ) =>
732+ {
733+ try
734+ {
735+ InnerEmbedHostItem ( itemName , typeValue ) ;
736+ }
737+ catch ( ActiveScriptException e )
738+ {
739+ throw ConvertActiveScriptExceptionToJsRuntimeException ( e ) ;
740+ }
741+ } ) ;
724742 }
725743
726744 public override void CollectGarbage ( )
0 commit comments