55using System . Reflection ;
66using System . Runtime . InteropServices ;
77using System . Runtime . InteropServices . Expando ;
8+ using System . Text ;
89
910using MsieJavaScriptEngine . ActiveScript . Debugging ;
1011using MsieJavaScriptEngine . Constants ;
@@ -81,6 +82,11 @@ internal abstract partial class ActiveScriptJsEngineBase : InnerJsEngineBase
8182 /// </summary>
8283 private uint _nextSourceContext = 1 ;
8384
85+ /// <summary>
86+ /// Lowest supported version of Internet Explorer
87+ /// </summary>
88+ private readonly string _lowerIeVersion ;
89+
8490 /// <summary>
8591 /// Prefix of error category name
8692 /// </summary>
@@ -104,39 +110,62 @@ protected ActiveScriptJsEngineBase(JsEngineSettings settings, string clsid,
104110 ScriptLanguageVersion languageVersion , string lowerIeVersion , string errorCategoryNamePrefix )
105111 : base ( settings )
106112 {
113+ _lowerIeVersion = lowerIeVersion ;
107114 _errorCategoryNamePrefix = errorCategoryNamePrefix ;
108115
109- _dispatcher . Invoke ( ( ) =>
116+ try
110117 {
111- try
118+ _dispatcher . Invoke ( ( ) =>
112119 {
113120 _activeScriptWrapper = CreateActiveScriptWrapper ( clsid , languageVersion ) ;
114- }
115- catch ( COMException e )
116- {
117- if ( e . ErrorCode == ComErrorCode . E_CLASS_NOT_REGISTERED )
121+
122+ if ( _settings . EnableDebugging )
118123 {
119- throw new JsEngineLoadException (
120- string . Format ( CommonStrings . Engine_IeJsEngineNotLoaded ,
121- _engineModeName , lowerIeVersion , e . Message ) ,
122- _engineModeName
123- ) ;
124+ StartDebugging ( ) ;
124125 }
125126
126- throw ;
127- }
127+ _activeScriptWrapper . SetScriptSite ( CreateScriptSite ( ) ) ;
128+ _activeScriptWrapper . InitNew ( ) ;
129+ _activeScriptWrapper . SetScriptState ( ScriptState . Started ) ;
130+
131+ _dispatch = WrapScriptDispatch ( _activeScriptWrapper . GetScriptDispatch ( ) ) ;
132+ } ) ;
133+ }
134+ catch ( COMException e )
135+ {
136+ throw WrapCOMException ( e ) ;
137+ }
138+ catch ( InvalidOperationException e )
139+ {
140+ string message = string . Format ( CommonStrings . Engine_JsEngineNotLoaded , _engineModeName ) + " " +
141+ e . Message ;
128142
129- if ( _settings . EnableDebugging )
143+ throw new JsEngineLoadException ( message , _engineModeName , e ) ;
144+ }
145+ catch ( ActiveScriptException e )
146+ {
147+ int errorNumber = ComHelpers . HResult . GetFacility ( e . ErrorCode ) == ComErrorCode . FACILITY_CONTROL ?
148+ ComHelpers . HResult . GetCode ( e . ErrorCode ) : 0 ;
149+ if ( ActiveScriptJsErrorHelpers . IsEngineError ( errorNumber ) )
130150 {
131- StartDebugging ( ) ;
151+ throw new JsEngineException ( e . Message , _engineModeName , e ) ;
132152 }
133-
134- _activeScriptWrapper . SetScriptSite ( CreateScriptSite ( ) ) ;
135- _activeScriptWrapper . InitNew ( ) ;
136- _activeScriptWrapper . SetScriptState ( ScriptState . Started ) ;
137-
138- InitScriptDispatch ( ) ;
139- } ) ;
153+ else
154+ {
155+ throw WrapActiveScriptException ( e ) ;
156+ }
157+ }
158+ catch ( Exception e )
159+ {
160+ throw JsErrorHelpers . WrapUnknownEngineLoadException ( e , _engineModeName ) ;
161+ }
162+ finally
163+ {
164+ if ( _dispatch == null )
165+ {
166+ Dispose ( ) ;
167+ }
168+ }
140169 }
141170
142171 /// <summary>
@@ -212,13 +241,11 @@ private IActiveScriptWrapper CreateActiveScriptWrapper(string clsid, ScriptLangu
212241
213242 if ( Utils . Is64BitProcess ( ) )
214243 {
215- activeScriptWrapper = new ActiveScriptWrapper64 ( _engineModeName , clsid , languageVersion ,
216- _settings . EnableDebugging ) ;
244+ activeScriptWrapper = new ActiveScriptWrapper64 ( clsid , languageVersion , _settings . EnableDebugging ) ;
217245 }
218246 else
219247 {
220- activeScriptWrapper = new ActiveScriptWrapper32 ( _engineModeName , clsid , languageVersion ,
221- _settings . EnableDebugging ) ;
248+ activeScriptWrapper = new ActiveScriptWrapper32 ( clsid , languageVersion , _settings . EnableDebugging ) ;
222249 }
223250
224251 return activeScriptWrapper ;
@@ -253,32 +280,6 @@ private void StartDebugging()
253280 /// <returns>Instance of the Active Script site</returns>
254281 protected abstract ScriptSiteBase CreateScriptSite ( ) ;
255282
256- /// <summary>
257- /// Initializes a script dispatch
258- /// </summary>
259- private void InitScriptDispatch ( )
260- {
261- IExpando dispatch = null ;
262- object obj ;
263-
264- _activeScriptWrapper . GetScriptDispatch ( null , out obj ) ;
265-
266- if ( obj != null && obj . GetType ( ) . IsCOMObject )
267- {
268- dispatch = obj as IExpando ;
269- }
270-
271- if ( dispatch == null )
272- {
273- throw new JsEngineLoadException (
274- NetFrameworkStrings . Engine_ActiveScriptDispatcherNotInitialized ,
275- _engineModeName
276- ) ;
277- }
278-
279- _dispatch = dispatch ;
280- }
281-
282283 /// <summary>
283284 /// Initializes a script context
284285 /// </summary>
@@ -543,8 +544,24 @@ private object[] MapToHostType(object[] args)
543544 return TypeMappingHelpers . MapToHostType ( args ) ;
544545 }
545546
546- private JsException ConvertOriginalExceptionToWrapperException (
547- ActiveScriptException originalException )
547+ private static IExpando WrapScriptDispatch ( object dispatch )
548+ {
549+ IExpando wrappedDispatch = null ;
550+ if ( dispatch != null && dispatch . GetType ( ) . IsCOMObject )
551+ {
552+ wrappedDispatch = dispatch as IExpando ;
553+ }
554+
555+ if ( wrappedDispatch == null )
556+ {
557+ throw new InvalidOperationException (
558+ NetFrameworkStrings . Engine_ActiveScriptDispatcherNotInitialized ) ;
559+ }
560+
561+ return wrappedDispatch ;
562+ }
563+
564+ private JsException WrapActiveScriptException ( ActiveScriptException originalException )
548565 {
549566 JsException wrapperException ;
550567 string message = originalException . Message ;
@@ -567,10 +584,6 @@ private JsException ConvertOriginalExceptionToWrapperException(
567584 wrapperException = new JsInterruptedException ( message , _engineModeName , originalException ) ;
568585 break ;
569586
570- case JsErrorCategory . Engine :
571- wrapperException = new JsEngineException ( message , _engineModeName , originalException ) ;
572- break ;
573-
574587 default :
575588 wrapperException = new JsException ( message , _engineModeName , originalException ) ;
576589 break ;
@@ -591,6 +604,28 @@ private JsException ConvertOriginalExceptionToWrapperException(
591604 return wrapperException ;
592605 }
593606
607+ private JsEngineLoadException WrapCOMException ( COMException originalComException )
608+ {
609+ string jsEngineNotLoadedPart = string . Format ( CommonStrings . Engine_JsEngineNotLoaded , _engineModeName ) ;
610+ string message ;
611+
612+ if ( originalComException . ErrorCode == ComErrorCode . E_CLASS_NOT_REGISTERED )
613+ {
614+ message = jsEngineNotLoadedPart + " " +
615+ string . Format ( CommonStrings . Engine_AssemblyNotRegistered ,
616+ _settings . EngineMode == JsEngineMode . Classic ? DllName . JScript : DllName . JScript9 ) + " " +
617+ string . Format ( CommonStrings . Engine_IeInstallationRequired , _lowerIeVersion )
618+ ;
619+ }
620+ else
621+ {
622+ message = jsEngineNotLoadedPart + " " +
623+ string . Format ( CommonStrings . Common_SeeOriginalErrorMessage , originalComException . Message ) ;
624+ }
625+
626+ return new JsEngineLoadException ( message , _engineModeName , originalComException ) ;
627+ }
628+
594629 /// <summary>
595630 /// Shortens a name of error category
596631 /// </summary>
@@ -625,7 +660,7 @@ public override object Evaluate(string expression, string documentName)
625660 }
626661 catch ( ActiveScriptException e )
627662 {
628- throw ConvertOriginalExceptionToWrapperException ( e ) ;
663+ throw WrapActiveScriptException ( e ) ;
629664 }
630665 } ) ;
631666
@@ -646,7 +681,7 @@ public override void Execute(string code, string documentName)
646681 }
647682 catch ( ActiveScriptException e )
648683 {
649- throw ConvertOriginalExceptionToWrapperException ( e ) ;
684+ throw WrapActiveScriptException ( e ) ;
650685 }
651686 } ) ;
652687 }
@@ -665,7 +700,7 @@ public override object CallFunction(string functionName, params object[] args)
665700 }
666701 catch ( ActiveScriptException e )
667702 {
668- throw ConvertOriginalExceptionToWrapperException ( e ) ;
703+ throw WrapActiveScriptException ( e ) ;
669704 }
670705 catch ( MissingMemberException )
671706 {
@@ -696,7 +731,7 @@ public override bool HasVariable(string variableName)
696731 }
697732 catch ( ActiveScriptException e )
698733 {
699- throw ConvertOriginalExceptionToWrapperException ( e ) ;
734+ throw WrapActiveScriptException ( e ) ;
700735 }
701736 catch ( MissingMemberException )
702737 {
@@ -721,7 +756,7 @@ public override object GetVariableValue(string variableName)
721756 }
722757 catch ( ActiveScriptException e )
723758 {
724- throw ConvertOriginalExceptionToWrapperException ( e ) ;
759+ throw WrapActiveScriptException ( e ) ;
725760 }
726761 catch ( MissingMemberException )
727762 {
@@ -751,7 +786,7 @@ public override void SetVariableValue(string variableName, object value)
751786 }
752787 catch ( ActiveScriptException e )
753788 {
754- throw ConvertOriginalExceptionToWrapperException ( e ) ;
789+ throw WrapActiveScriptException ( e ) ;
755790 }
756791 } ) ;
757792 }
@@ -768,7 +803,7 @@ public override void RemoveVariable(string variableName)
768803 }
769804 catch ( ActiveScriptException e )
770805 {
771- throw ConvertOriginalExceptionToWrapperException ( e ) ;
806+ throw WrapActiveScriptException ( e ) ;
772807 }
773808 } ) ;
774809 }
@@ -787,7 +822,7 @@ public override void EmbedHostObject(string itemName, object value)
787822 }
788823 catch ( ActiveScriptException e )
789824 {
790- throw ConvertOriginalExceptionToWrapperException ( e ) ;
825+ throw WrapActiveScriptException ( e ) ;
791826 }
792827 } ) ;
793828 }
@@ -806,7 +841,7 @@ public override void EmbedHostType(string itemName, Type type)
806841 }
807842 catch ( ActiveScriptException e )
808843 {
809- throw ConvertOriginalExceptionToWrapperException ( e ) ;
844+ throw WrapActiveScriptException ( e ) ;
810845 }
811846 } ) ;
812847 }
@@ -866,21 +901,14 @@ private void Dispose(bool disposing)
866901 _debugDocuments . Clear ( ) ;
867902 }
868903
869- if ( _documentNames != null )
870- {
871- _documentNames . Clear ( ) ;
872- }
873-
874904 if ( _processDebugManagerWrapper != null )
875905 {
876906 _processDebugManagerWrapper . RemoveApplication ( _debugApplicationCookie ) ;
877907 _debugApplicationWrapper . Close ( ) ;
878908 }
879909
880- if ( _hostItems != null )
881- {
882- _hostItems . Clear ( ) ;
883- }
910+ _documentNames ? . Clear ( ) ;
911+ _hostItems ? . Clear ( ) ;
884912
885913 _lastException = null ;
886914 }
0 commit comments