55using System . Reflection ;
66using System . Runtime . InteropServices ;
77using System . Runtime . InteropServices . Expando ;
8- using System . Windows . Threading ;
98
109using EXCEPINFO = System . Runtime . InteropServices . ComTypes . EXCEPINFO ;
1110
@@ -87,9 +86,9 @@ internal abstract class ActiveScriptJsEngineBase : IInnerJsEngine, IActiveScript
8786 private readonly string _engineModeName ;
8887
8988 /// <summary>
90- /// <see cref="System.Windows.Threading.Dispatcher"/> for the thread currently executing
89+ /// Script dispatcher
9190 /// </summary>
92- private readonly Dispatcher _dispatcher = Dispatcher . CurrentDispatcher ;
91+ private static readonly ScriptDispatcher _dispatcher = new ScriptDispatcher ( ) ;
9392
9493 /// <summary>
9594 /// Flag that object is destroyed
@@ -111,53 +110,57 @@ protected ActiveScriptJsEngineBase(string clsid, JsEngineMode engineMode, string
111110 {
112111 _engineMode = engineMode ;
113112 _engineModeName = JsEngineModeHelpers . GetModeName ( engineMode ) ;
114- _pActiveScript = IntPtr . Zero ;
113+ _documentVersion = DateTime . UtcNow . ToString ( "o" ) ;
115114
116- try
117- {
118- _pActiveScript = ComHelpers . CreateInstanceByClsid < IActiveScript > ( clsid ) ;
119- _activeScript = ( IActiveScript ) Marshal . GetObjectForIUnknown ( _pActiveScript ) ;
120- }
121- catch ( Exception e )
115+ _dispatcher . Invoke ( ( ) =>
122116 {
123- throw new JsEngineLoadException (
124- string . Format ( CommonStrings . Runtime_IeJsEngineNotLoaded ,
125- _engineModeName , lowerIeVersion , e . Message ) , _engineModeName ) ;
126- }
117+ _pActiveScript = IntPtr . Zero ;
127118
128- if ( languageVersion != ScriptLanguageVersion . None )
129- {
130- var activeScriptProperty = _activeScript as IActiveScriptProperty ;
131- if ( activeScriptProperty != null )
119+ try
120+ {
121+ _pActiveScript = ComHelpers . CreateInstanceByClsid < IActiveScript > ( clsid ) ;
122+ _activeScript = ( IActiveScript ) Marshal . GetObjectForIUnknown ( _pActiveScript ) ;
123+ }
124+ catch ( Exception e )
125+ {
126+ throw new JsEngineLoadException (
127+ string . Format ( CommonStrings . Runtime_IeJsEngineNotLoaded ,
128+ _engineModeName , lowerIeVersion , e . Message ) , _engineModeName ) ;
129+ }
130+
131+ if ( languageVersion != ScriptLanguageVersion . None )
132132 {
133- object scriptLanguageVersion = ( int ) languageVersion ;
134- uint result = activeScriptProperty . SetProperty ( ( uint ) ScriptProperty . InvokeVersioning ,
135- IntPtr . Zero , ref scriptLanguageVersion ) ;
136- if ( result != ( uint ) ScriptHResult . Ok )
133+ var activeScriptProperty = _activeScript as IActiveScriptProperty ;
134+ if ( activeScriptProperty != null )
137135 {
138- throw new JsEngineLoadException (
139- string . Format ( NetFrameworkStrings . Runtime_ActiveScriptLanguageVersionSelectionFailed , languageVersion ) ) ;
136+ object scriptLanguageVersion = ( int ) languageVersion ;
137+ uint result = activeScriptProperty . SetProperty ( ( uint ) ScriptProperty . InvokeVersioning ,
138+ IntPtr . Zero , ref scriptLanguageVersion ) ;
139+ if ( result != ( uint ) ScriptHResult . Ok )
140+ {
141+ throw new JsEngineLoadException (
142+ string . Format ( NetFrameworkStrings . Runtime_ActiveScriptLanguageVersionSelectionFailed , languageVersion ) ) ;
143+ }
140144 }
141145 }
142- }
143146
144- _activeScriptParse = new ActiveScriptParseWrapper ( _pActiveScript , _activeScript ) ;
145- _activeScriptParse . InitNew ( ) ;
147+ _activeScriptParse = new ActiveScriptParseWrapper ( _pActiveScript , _activeScript ) ;
148+ _activeScriptParse . InitNew ( ) ;
146149
147- _pActiveScriptGarbageCollector = ComHelpers . QueryInterfaceNoThrow < IActiveScriptGarbageCollector > ( _pActiveScript ) ;
148- _activeScriptGarbageCollector = _activeScript as IActiveScriptGarbageCollector ;
150+ _pActiveScriptGarbageCollector = ComHelpers . QueryInterfaceNoThrow < IActiveScriptGarbageCollector > ( _pActiveScript ) ;
151+ _activeScriptGarbageCollector = _activeScript as IActiveScriptGarbageCollector ;
149152
150- _activeScript . SetScriptSite ( this ) ;
151- _activeScript . SetScriptState ( ScriptState . Started ) ;
153+ _activeScript . SetScriptSite ( this ) ;
154+ _activeScript . SetScriptState ( ScriptState . Started ) ;
152155
153- InitScriptDispatch ( ) ;
154- _documentVersion = DateTime . UtcNow . ToString ( "o" ) ;
156+ InitScriptDispatch ( ) ;
157+ } ) ;
155158
156159 LoadResources ( useEcmaScript5Polyfill , useJson2Library ) ;
157160 }
158161
159162 /// <summary>
160- /// Destructs instance of ActiveScript JavaScript engine
163+ /// Destructs an instance of ActiveScript JavaScript engine
161164 /// </summary>
162165 ~ ActiveScriptJsEngineBase ( )
163166 {
@@ -318,46 +321,52 @@ private void ThrowError()
318321
319322 private void InvokeScript ( Action action )
320323 {
321- try
324+ _dispatcher . Invoke ( ( ) =>
322325 {
323- _dispatcher . Invoke ( DispatcherPriority . Input , action ) ;
324- }
325- catch ( ActiveScriptException e )
326- {
327- throw ConvertActiveScriptExceptionToJsRuntimeException ( e ) ;
328- }
329- catch ( TargetInvocationException e )
330- {
331- var activeScriptException = e . InnerException as ActiveScriptException ;
332- if ( activeScriptException != null )
326+ try
327+ {
328+ action ( ) ;
329+ }
330+ catch ( ActiveScriptException e )
333331 {
334- throw ConvertActiveScriptExceptionToJsRuntimeException ( activeScriptException ) ;
332+ throw ConvertActiveScriptExceptionToJsRuntimeException ( e ) ;
335333 }
334+ catch ( TargetInvocationException e )
335+ {
336+ var activeScriptException = e . InnerException as ActiveScriptException ;
337+ if ( activeScriptException != null )
338+ {
339+ throw ConvertActiveScriptExceptionToJsRuntimeException ( activeScriptException ) ;
340+ }
336341
337- throw ;
338- }
342+ throw ;
343+ }
344+ } ) ;
339345 }
340346
341347 private T InvokeScript < T > ( Func < T > func )
342348 {
343- try
349+ return _dispatcher . Invoke ( ( ) =>
344350 {
345- return ( T ) _dispatcher . Invoke ( DispatcherPriority . Input , func ) ;
346- }
347- catch ( ActiveScriptException e )
348- {
349- throw ConvertActiveScriptExceptionToJsRuntimeException ( e ) ;
350- }
351- catch ( TargetInvocationException e )
352- {
353- var activeScriptException = e . InnerException as ActiveScriptException ;
354- if ( activeScriptException != null )
351+ try
352+ {
353+ return func ( ) ;
354+ }
355+ catch ( ActiveScriptException e )
355356 {
356- throw ConvertActiveScriptExceptionToJsRuntimeException ( activeScriptException ) ;
357+ throw ConvertActiveScriptExceptionToJsRuntimeException ( e ) ;
357358 }
359+ catch ( TargetInvocationException e )
360+ {
361+ var activeScriptException = e . InnerException as ActiveScriptException ;
362+ if ( activeScriptException != null )
363+ {
364+ throw ConvertActiveScriptExceptionToJsRuntimeException ( activeScriptException ) ;
365+ }
358366
359- throw ;
360- }
367+ throw ;
368+ }
369+ } ) ;
361370 }
362371
363372 /// <summary>
@@ -554,51 +563,6 @@ private void ExecuteResource(string resourceName, Assembly assembly)
554563 Execute ( code ) ;
555564 }
556565
557- /// <summary>
558- /// Destroys object
559- /// </summary>
560- /// <param name="disposing">Flag, allowing destruction of
561- /// managed objects contained in fields of class</param>
562- private void Dispose ( bool disposing )
563- {
564- _dispatcher . Invoke ( DispatcherPriority . Input , ( Action ) ( ( ) =>
565- {
566- if ( _disposedFlag . Set ( ) )
567- {
568- if ( _dispatch != null )
569- {
570- ComHelpers . ReleaseComObject ( ref _dispatch , ! disposing ) ;
571- _dispatch = null ;
572- }
573-
574- _activeScriptGarbageCollector = null ;
575- ComHelpers . ReleaseAndEmpty ( ref _pActiveScriptGarbageCollector ) ;
576-
577- if ( _activeScriptParse != null )
578- {
579- _activeScriptParse . Dispose ( ) ;
580- _activeScriptParse = null ;
581- }
582-
583- if ( _activeScript != null )
584- {
585- _activeScript . Close ( ) ;
586- Marshal . FinalReleaseComObject ( _activeScript ) ;
587- _activeScript = null ;
588- }
589-
590- ComHelpers . ReleaseAndEmpty ( ref _pActiveScript ) ;
591-
592- if ( _hostItems != null )
593- {
594- _hostItems . Clear ( ) ;
595- }
596-
597- _lastException = null ;
598- }
599- } ) ) ;
600- }
601-
602566 #region IActiveScriptSite implementation
603567
604568 /// <summary>
@@ -832,7 +796,7 @@ public void EmbedHostType(string itemName, Type type)
832796
833797 public void CollectGarbage ( )
834798 {
835- InvokeScript ( ( ) => InnerCollectGarbage ( ScriptGCType . Exhaustive ) ) ;
799+ _dispatcher . Invoke ( ( ) => InnerCollectGarbage ( ScriptGCType . Exhaustive ) ) ;
836800 }
837801
838802 #endregion
@@ -848,6 +812,54 @@ public void Dispose()
848812 GC . SuppressFinalize ( this ) ;
849813 }
850814
815+ /// <summary>
816+ /// Destroys object
817+ /// </summary>
818+ /// <param name="disposing">Flag, allowing destruction of
819+ /// managed objects contained in fields of class</param>
820+ private void Dispose ( bool disposing )
821+ {
822+ if ( _disposedFlag . Set ( ) )
823+ {
824+ _dispatcher . Invoke ( ( ) =>
825+ {
826+ if ( _dispatch != null )
827+ {
828+ ComHelpers . ReleaseComObject ( ref _dispatch , ! disposing ) ;
829+ _dispatch = null ;
830+ }
831+
832+ _activeScriptGarbageCollector = null ;
833+ ComHelpers . ReleaseAndEmpty ( ref _pActiveScriptGarbageCollector ) ;
834+
835+ if ( _activeScriptParse != null )
836+ {
837+ _activeScriptParse . Dispose ( ) ;
838+ _activeScriptParse = null ;
839+ }
840+
841+ if ( _activeScript != null )
842+ {
843+ _activeScript . Close ( ) ;
844+ Marshal . FinalReleaseComObject ( _activeScript ) ;
845+ _activeScript = null ;
846+ }
847+
848+ ComHelpers . ReleaseAndEmpty ( ref _pActiveScript ) ;
849+
850+ if ( disposing )
851+ {
852+ if ( _hostItems != null )
853+ {
854+ _hostItems . Clear ( ) ;
855+ }
856+
857+ _lastException = null ;
858+ }
859+ } ) ;
860+ }
861+ }
862+
851863 #endregion
852864 }
853865}
0 commit comments