@@ -19,8 +19,8 @@ public class JavaScriptEngine : IScriptEngine
19
19
{
20
20
#region Fields
21
21
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 ;
24
24
25
25
#endregion
26
26
@@ -60,20 +60,13 @@ public String Type
60
60
#region Methods
61
61
62
62
/// <summary>
63
- /// Gets the associated Jint engine, if any .
63
+ /// Gets the associated Jint engine or creates it .
64
64
/// </summary>
65
65
/// <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 )
68
68
{
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 ;
77
70
}
78
71
79
72
/// <summary>
@@ -84,20 +77,29 @@ public Engine GetJint(IDocument document)
84
77
/// <param name="cancel">The cancellation token to transport.</param>
85
78
public async Task EvaluateScriptAsync ( IResponse response , ScriptOptions options , CancellationToken cancel )
86
79
{
87
- var instance = default ( EngineInstance ) ;
88
- var objectContext = options . Document . DefaultView ;
89
-
90
80
using ( var reader = new StreamReader ( response . Content , options . Encoding ?? Encoding . UTF8 , true ) )
91
81
{
92
82
var content = await reader . ReadToEndAsync ( ) . ConfigureAwait ( false ) ;
83
+ GetOrCreateInstance ( options . Document ) . RunScript ( content ) ;
84
+ }
85
+ }
93
86
94
- if ( ! _contexts . TryGetValue ( objectContext , out instance ) )
95
- {
96
- _contexts . Add ( objectContext , instance = new EngineInstance ( objectContext , _external ) ) ;
97
- }
87
+ #endregion
88
+
89
+ #region Helpers
98
90
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 ) ;
100
100
}
101
+
102
+ return instance ;
101
103
}
102
104
103
105
#endregion
0 commit comments