5
5
using System . Reflection ;
6
6
using System . Runtime . InteropServices ;
7
7
using System . Runtime . InteropServices . Expando ;
8
+ using System . Text ;
8
9
9
10
using MsieJavaScriptEngine . ActiveScript . Debugging ;
10
11
using MsieJavaScriptEngine . Constants ;
@@ -81,6 +82,11 @@ internal abstract partial class ActiveScriptJsEngineBase : InnerJsEngineBase
81
82
/// </summary>
82
83
private uint _nextSourceContext = 1 ;
83
84
85
+ /// <summary>
86
+ /// Lowest supported version of Internet Explorer
87
+ /// </summary>
88
+ private readonly string _lowerIeVersion ;
89
+
84
90
/// <summary>
85
91
/// Prefix of error category name
86
92
/// </summary>
@@ -104,39 +110,62 @@ protected ActiveScriptJsEngineBase(JsEngineSettings settings, string clsid,
104
110
ScriptLanguageVersion languageVersion , string lowerIeVersion , string errorCategoryNamePrefix )
105
111
: base ( settings )
106
112
{
113
+ _lowerIeVersion = lowerIeVersion ;
107
114
_errorCategoryNamePrefix = errorCategoryNamePrefix ;
108
115
109
- _dispatcher . Invoke ( ( ) =>
116
+ try
110
117
{
111
- try
118
+ _dispatcher . Invoke ( ( ) =>
112
119
{
113
120
_activeScriptWrapper = CreateActiveScriptWrapper ( clsid , languageVersion ) ;
114
- }
115
- catch ( COMException e )
116
- {
117
- if ( e . ErrorCode == ComErrorCode . E_CLASS_NOT_REGISTERED )
121
+
122
+ if ( _settings . EnableDebugging )
118
123
{
119
- throw new JsEngineLoadException (
120
- string . Format ( CommonStrings . Engine_IeJsEngineNotLoaded ,
121
- _engineModeName , lowerIeVersion , e . Message ) ,
122
- _engineModeName
123
- ) ;
124
+ StartDebugging ( ) ;
124
125
}
125
126
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 ;
128
142
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 ) )
130
150
{
131
- StartDebugging ( ) ;
151
+ throw new JsEngineException ( e . Message , _engineModeName , e ) ;
132
152
}
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
+ }
140
169
}
141
170
142
171
/// <summary>
@@ -212,13 +241,11 @@ private IActiveScriptWrapper CreateActiveScriptWrapper(string clsid, ScriptLangu
212
241
213
242
if ( Utils . Is64BitProcess ( ) )
214
243
{
215
- activeScriptWrapper = new ActiveScriptWrapper64 ( _engineModeName , clsid , languageVersion ,
216
- _settings . EnableDebugging ) ;
244
+ activeScriptWrapper = new ActiveScriptWrapper64 ( clsid , languageVersion , _settings . EnableDebugging ) ;
217
245
}
218
246
else
219
247
{
220
- activeScriptWrapper = new ActiveScriptWrapper32 ( _engineModeName , clsid , languageVersion ,
221
- _settings . EnableDebugging ) ;
248
+ activeScriptWrapper = new ActiveScriptWrapper32 ( clsid , languageVersion , _settings . EnableDebugging ) ;
222
249
}
223
250
224
251
return activeScriptWrapper ;
@@ -253,32 +280,6 @@ private void StartDebugging()
253
280
/// <returns>Instance of the Active Script site</returns>
254
281
protected abstract ScriptSiteBase CreateScriptSite ( ) ;
255
282
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
-
282
283
/// <summary>
283
284
/// Initializes a script context
284
285
/// </summary>
@@ -543,8 +544,24 @@ private object[] MapToHostType(object[] args)
543
544
return TypeMappingHelpers . MapToHostType ( args ) ;
544
545
}
545
546
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 )
548
565
{
549
566
JsException wrapperException ;
550
567
string message = originalException . Message ;
@@ -567,10 +584,6 @@ private JsException ConvertOriginalExceptionToWrapperException(
567
584
wrapperException = new JsInterruptedException ( message , _engineModeName , originalException ) ;
568
585
break ;
569
586
570
- case JsErrorCategory . Engine :
571
- wrapperException = new JsEngineException ( message , _engineModeName , originalException ) ;
572
- break ;
573
-
574
587
default :
575
588
wrapperException = new JsException ( message , _engineModeName , originalException ) ;
576
589
break ;
@@ -591,6 +604,28 @@ private JsException ConvertOriginalExceptionToWrapperException(
591
604
return wrapperException ;
592
605
}
593
606
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
+
594
629
/// <summary>
595
630
/// Shortens a name of error category
596
631
/// </summary>
@@ -625,7 +660,7 @@ public override object Evaluate(string expression, string documentName)
625
660
}
626
661
catch ( ActiveScriptException e )
627
662
{
628
- throw ConvertOriginalExceptionToWrapperException ( e ) ;
663
+ throw WrapActiveScriptException ( e ) ;
629
664
}
630
665
} ) ;
631
666
@@ -646,7 +681,7 @@ public override void Execute(string code, string documentName)
646
681
}
647
682
catch ( ActiveScriptException e )
648
683
{
649
- throw ConvertOriginalExceptionToWrapperException ( e ) ;
684
+ throw WrapActiveScriptException ( e ) ;
650
685
}
651
686
} ) ;
652
687
}
@@ -665,7 +700,7 @@ public override object CallFunction(string functionName, params object[] args)
665
700
}
666
701
catch ( ActiveScriptException e )
667
702
{
668
- throw ConvertOriginalExceptionToWrapperException ( e ) ;
703
+ throw WrapActiveScriptException ( e ) ;
669
704
}
670
705
catch ( MissingMemberException )
671
706
{
@@ -696,7 +731,7 @@ public override bool HasVariable(string variableName)
696
731
}
697
732
catch ( ActiveScriptException e )
698
733
{
699
- throw ConvertOriginalExceptionToWrapperException ( e ) ;
734
+ throw WrapActiveScriptException ( e ) ;
700
735
}
701
736
catch ( MissingMemberException )
702
737
{
@@ -721,7 +756,7 @@ public override object GetVariableValue(string variableName)
721
756
}
722
757
catch ( ActiveScriptException e )
723
758
{
724
- throw ConvertOriginalExceptionToWrapperException ( e ) ;
759
+ throw WrapActiveScriptException ( e ) ;
725
760
}
726
761
catch ( MissingMemberException )
727
762
{
@@ -751,7 +786,7 @@ public override void SetVariableValue(string variableName, object value)
751
786
}
752
787
catch ( ActiveScriptException e )
753
788
{
754
- throw ConvertOriginalExceptionToWrapperException ( e ) ;
789
+ throw WrapActiveScriptException ( e ) ;
755
790
}
756
791
} ) ;
757
792
}
@@ -768,7 +803,7 @@ public override void RemoveVariable(string variableName)
768
803
}
769
804
catch ( ActiveScriptException e )
770
805
{
771
- throw ConvertOriginalExceptionToWrapperException ( e ) ;
806
+ throw WrapActiveScriptException ( e ) ;
772
807
}
773
808
} ) ;
774
809
}
@@ -787,7 +822,7 @@ public override void EmbedHostObject(string itemName, object value)
787
822
}
788
823
catch ( ActiveScriptException e )
789
824
{
790
- throw ConvertOriginalExceptionToWrapperException ( e ) ;
825
+ throw WrapActiveScriptException ( e ) ;
791
826
}
792
827
} ) ;
793
828
}
@@ -806,7 +841,7 @@ public override void EmbedHostType(string itemName, Type type)
806
841
}
807
842
catch ( ActiveScriptException e )
808
843
{
809
- throw ConvertOriginalExceptionToWrapperException ( e ) ;
844
+ throw WrapActiveScriptException ( e ) ;
810
845
}
811
846
} ) ;
812
847
}
@@ -866,21 +901,14 @@ private void Dispose(bool disposing)
866
901
_debugDocuments . Clear ( ) ;
867
902
}
868
903
869
- if ( _documentNames != null )
870
- {
871
- _documentNames . Clear ( ) ;
872
- }
873
-
874
904
if ( _processDebugManagerWrapper != null )
875
905
{
876
906
_processDebugManagerWrapper . RemoveApplication ( _debugApplicationCookie ) ;
877
907
_debugApplicationWrapper . Close ( ) ;
878
908
}
879
909
880
- if ( _hostItems != null )
881
- {
882
- _hostItems . Clear ( ) ;
883
- }
910
+ _documentNames ? . Clear ( ) ;
911
+ _hostItems ? . Clear ( ) ;
884
912
885
913
_lastException = null ;
886
914
}
0 commit comments