@@ -19,16 +19,6 @@ namespace MsieJavaScriptEngine.ActiveScript
1919 /// </summary>
2020 internal abstract partial class ActiveScriptJsEngineBase : InnerJsEngineBase
2121 {
22- /// <summary>
23- /// Name of resource, which contains a ECMAScript 5 Polyfill
24- /// </summary>
25- private const string ES5_POLYFILL_RESOURCE_NAME = "MsieJavaScriptEngine.Resources.ES5.min.js" ;
26-
27- /// <summary>
28- /// Name of resource, which contains a JSON2 library
29- /// </summary>
30- private const string JSON2_LIBRARY_RESOURCE_NAME = "MsieJavaScriptEngine.Resources.json2.min.js" ;
31-
3222 /// <summary>
3323 /// Instance of Active Script wrapper
3424 /// </summary>
@@ -96,10 +86,7 @@ internal abstract partial class ActiveScriptJsEngineBase : InnerJsEngineBase
9686 /// </summary>
9787 /// <param name="engineMode">JS engine mode</param>
9888 /// <param name="enableDebugging">Flag for whether to enable script debugging features</param>
99- /// <param name="useEcmaScript5Polyfill">Flag for whether to use the ECMAScript 5 Polyfill</param>
100- /// <param name="useJson2Library">Flag for whether to use the JSON2 library</param>
101- protected ActiveScriptJsEngineBase ( JsEngineMode engineMode , bool enableDebugging ,
102- bool useEcmaScript5Polyfill , bool useJson2Library )
89+ protected ActiveScriptJsEngineBase ( JsEngineMode engineMode , bool enableDebugging )
10390 : base ( engineMode )
10491 {
10592 string lowerIeVersion ;
@@ -119,36 +106,41 @@ protected ActiveScriptJsEngineBase(JsEngineMode engineMode, bool enableDebugging
119106 throw new NotSupportedException ( ) ;
120107 }
121108
122- _dispatcher . Invoke ( ( ) =>
109+ try
123110 {
124- try
111+ _dispatcher . Invoke ( ( ) =>
125112 {
126113 _activeScriptWrapper = Utils . Is64BitProcess ( ) ?
127114 ( IActiveScriptWrapper ) new ActiveScriptWrapper64 ( engineMode , enableDebugging )
128115 :
129116 new ActiveScriptWrapper32 ( engineMode , enableDebugging )
130117 ;
131- }
132- catch ( Exception e )
133- {
134- throw new JsEngineLoadException (
135- string . Format ( CommonStrings . Runtime_IeJsEngineNotLoaded ,
136- _engineModeName , lowerIeVersion , e . Message ) , _engineModeName ) ;
137- }
138-
139- if ( enableDebugging )
140- {
141- StartDebugging ( ) ;
142- }
143118
144- _activeScriptWrapper . SetScriptSite ( new ScriptSite ( this ) ) ;
145- _activeScriptWrapper . InitNew ( ) ;
146- _activeScriptWrapper . SetScriptState ( ScriptState . Started ) ;
119+ if ( enableDebugging )
120+ {
121+ StartDebugging ( ) ;
122+ }
147123
148- InitScriptDispatch ( ) ;
149- } ) ;
124+ _activeScriptWrapper . SetScriptSite ( new ScriptSite ( this ) ) ;
125+ _activeScriptWrapper . InitNew ( ) ;
126+ _activeScriptWrapper . SetScriptState ( ScriptState . Started ) ;
150127
151- LoadResources ( useEcmaScript5Polyfill , useJson2Library ) ;
128+ _dispatch = WrapScriptDispatch ( _activeScriptWrapper . GetScriptDispatch ( ) ) ;
129+ } ) ;
130+ }
131+ catch ( Exception e )
132+ {
133+ throw new JsEngineLoadException (
134+ string . Format ( CommonStrings . Runtime_IeJsEngineNotLoaded ,
135+ _engineModeName , lowerIeVersion , e . Message ) , _engineModeName ) ;
136+ }
137+ finally
138+ {
139+ if ( _dispatch == null )
140+ {
141+ Dispose ( ) ;
142+ }
143+ }
152144 }
153145
154146 /// <summary>
@@ -276,24 +268,7 @@ private JsRuntimeException ConvertActiveScriptExceptionToJsRuntimeException(
276268 /// <returns>Short name of error category</returns>
277269 private string ShortenErrorCategoryName ( string categoryName )
278270 {
279- if ( categoryName == null )
280- {
281- throw new ArgumentNullException ( "categoryName" ) ;
282- }
283-
284- string shortCategoryName = categoryName ;
285- if ( categoryName . StartsWith ( _errorCategoryNamePrefix , StringComparison . Ordinal ) )
286- {
287- shortCategoryName = categoryName . Substring ( _errorCategoryNamePrefix . Length ) ;
288- if ( shortCategoryName . Length > 0 )
289- {
290- char [ ] chars = shortCategoryName . ToCharArray ( ) ;
291- chars [ 0 ] = char . ToUpperInvariant ( chars [ 0 ] ) ;
292- shortCategoryName = new string ( chars ) ;
293- }
294- }
295-
296- return shortCategoryName ;
271+ return ActiveScriptJsErrorHelpers . ShortenErrorItemName ( categoryName , _errorCategoryNamePrefix ) ;
297272 }
298273
299274 /// <summary>
@@ -319,27 +294,21 @@ private void StartDebugging()
319294 }
320295 }
321296
322- /// <summary>
323- /// Initializes a script dispatch
324- /// </summary>
325- private void InitScriptDispatch ( )
297+ private static IExpando WrapScriptDispatch ( object dispatch )
326298 {
327- IExpando dispatch = null ;
328- object obj ;
329-
330- _activeScriptWrapper . GetScriptDispatch ( null , out obj ) ;
331-
332- if ( obj != null && obj . GetType ( ) . IsCOMObject )
299+ IExpando wrappedDispatch = null ;
300+ if ( dispatch != null && dispatch . GetType ( ) . IsCOMObject )
333301 {
334- dispatch = obj as IExpando ;
302+ wrappedDispatch = dispatch as IExpando ;
335303 }
336304
337- if ( dispatch == null )
305+ if ( wrappedDispatch == null )
338306 {
339- throw new InvalidOperationException ( NetFrameworkStrings . Runtime_ActiveScriptDispatcherNotInitialized ) ;
307+ throw new InvalidOperationException (
308+ NetFrameworkStrings . Runtime_ActiveScriptDispatcherNotInitialized ) ;
340309 }
341310
342- _dispatch = dispatch ;
311+ return wrappedDispatch ;
343312 }
344313
345314 /// <summary>
@@ -536,55 +505,6 @@ private void InnerCollectGarbage(ScriptGCType type)
536505 _activeScriptWrapper . CollectGarbage ( type ) ;
537506 }
538507
539- /// <summary>
540- /// Loads a resources
541- /// </summary>
542- /// <param name="useEcmaScript5Polyfill">Flag for whether to use the ECMAScript 5 Polyfill</param>
543- /// <param name="useJson2Library">Flag for whether to use the JSON2 library</param>
544- private void LoadResources ( bool useEcmaScript5Polyfill , bool useJson2Library )
545- {
546- Assembly assembly = GetType ( ) . GetTypeInfo ( ) . Assembly ;
547-
548- if ( useEcmaScript5Polyfill )
549- {
550- ExecuteResource ( ES5_POLYFILL_RESOURCE_NAME , assembly ) ;
551- }
552-
553- if ( useJson2Library )
554- {
555- ExecuteResource ( JSON2_LIBRARY_RESOURCE_NAME , assembly ) ;
556- }
557- }
558-
559- /// <summary>
560- /// Executes a code from embedded JS-resource
561- /// </summary>
562- /// <param name="resourceName">The case-sensitive resource name</param>
563- /// <param name="assembly">The assembly, which contains the embedded resource</param>
564- private void ExecuteResource ( string resourceName , Assembly assembly )
565- {
566- if ( resourceName == null )
567- {
568- throw new ArgumentNullException (
569- "resourceName" , string . Format ( CommonStrings . Common_ArgumentIsNull , "resourceName" ) ) ;
570- }
571-
572- if ( assembly == null )
573- {
574- throw new ArgumentNullException (
575- "assembly" , string . Format ( CommonStrings . Common_ArgumentIsNull , "assembly" ) ) ;
576- }
577-
578- if ( string . IsNullOrWhiteSpace ( resourceName ) )
579- {
580- throw new ArgumentException (
581- string . Format ( CommonStrings . Common_ArgumentIsEmpty , "resourceName" ) , "resourceName" ) ;
582- }
583-
584- string code = Utils . GetResourceAsString ( resourceName , assembly ) ;
585- Execute ( code , resourceName ) ;
586- }
587-
588508 #region IInnerJsEngine implementation
589509
590510 public override string Mode
0 commit comments