@@ -19,8 +19,8 @@ public class JavaScriptEngine : IScriptEngine
1919 {
2020 #region Fields
2121
22- readonly ConditionalWeakTable < IWindow , EngineInstance > _contexts ;
23- readonly Dictionary < String , Object > _external ;
22+ private readonly ConditionalWeakTable < IWindow , EngineInstance > _contexts ;
23+ private readonly Dictionary < String , Object > _external ;
2424
2525 #endregion
2626
@@ -60,20 +60,13 @@ public String Type
6060 #region Methods
6161
6262 /// <summary>
63- /// Gets the associated Jint engine, if any .
63+ /// Gets the associated Jint engine or creates it .
6464 /// </summary>
6565 /// <param name="document">The current document.</param>
66- /// <returns>The engine object, if any .</returns>
67- public Engine GetJint ( IDocument document )
66+ /// <returns>The engine object.</returns>
67+ public Engine GetOrCreateJint ( IDocument document )
6868 {
69- var instance = default ( EngineInstance ) ;
70-
71- if ( _contexts . TryGetValue ( document . DefaultView , out instance ) )
72- {
73- return instance . Jint ;
74- }
75-
76- return null ;
69+ return GetOrCreateInstance ( document ) . Jint ;
7770 }
7871
7972 /// <summary>
@@ -84,20 +77,29 @@ public Engine GetJint(IDocument document)
8477 /// <param name="cancel">The cancellation token to transport.</param>
8578 public async Task EvaluateScriptAsync ( IResponse response , ScriptOptions options , CancellationToken cancel )
8679 {
87- var instance = default ( EngineInstance ) ;
88- var objectContext = options . Document . DefaultView ;
89-
9080 using ( var reader = new StreamReader ( response . Content , options . Encoding ?? Encoding . UTF8 , true ) )
9181 {
9282 var content = await reader . ReadToEndAsync ( ) . ConfigureAwait ( false ) ;
83+ GetOrCreateInstance ( options . Document ) . RunScript ( content ) ;
84+ }
85+ }
9386
94- if ( ! _contexts . TryGetValue ( objectContext , out instance ) )
95- {
96- _contexts . Add ( objectContext , instance = new EngineInstance ( objectContext , _external ) ) ;
97- }
87+ #endregion
88+
89+ #region Helpers
9890
99- instance . RunScript ( content ) ;
91+ private EngineInstance GetOrCreateInstance ( IDocument document )
92+ {
93+ var instance = default ( EngineInstance ) ;
94+ var objectContext = document . DefaultView ;
95+
96+ if ( ! _contexts . TryGetValue ( objectContext , out instance ) )
97+ {
98+ instance = new EngineInstance ( objectContext , _external ) ;
99+ _contexts . Add ( objectContext , instance ) ;
100100 }
101+
102+ return instance ;
101103 }
102104
103105 #endregion
0 commit comments