@@ -68,7 +68,12 @@ public class ReactEnvironment : IReactEnvironment, IDisposable
6868 /// <summary>
6969 /// Version number of ReactJS.NET
7070 /// </summary>
71- protected readonly Lazy < string > _version = new Lazy < string > ( GetVersion ) ;
71+ protected readonly Lazy < string > _version = new Lazy < string > ( GetVersion ) ;
72+ /// <summary>
73+ /// Contains an engine acquired from a pool of engines. Only used if
74+ /// <see cref="IReactSiteConfiguration.ReuseJavaScriptEngines"/> is enabled.
75+ /// </summary>
76+ protected readonly Lazy < IJsEngine > _engineFromPool ;
7277
7378 /// <summary>
7479 /// Number of components instantiated in this environment
@@ -103,17 +108,19 @@ IFileCacheHash fileCacheHash
103108 _jsxTransformer = new Lazy < IJsxTransformer > ( ( ) =>
104109 new JsxTransformer ( this , _cache , _fileSystem , _fileCacheHash , _config )
105110 ) ;
111+ _engineFromPool = new Lazy < IJsEngine > ( ( ) => _engineFactory . GetEngine ( ) ) ;
106112 }
107113
108114 /// <summary>
109- /// Gets the JavaScript engine for the current thread. If an engine has not yet been
110- /// created, create it and execute the startup scripts.
115+ /// Gets the JavaScript engine to use for this environment.
111116 /// </summary>
112117 protected virtual IJsEngine Engine
113118 {
114119 get
115120 {
116- return _engineFactory . GetEngineForCurrentThread ( InitialiseEngine ) ;
121+ return _config . ReuseJavaScriptEngines
122+ ? _engineFromPool . Value
123+ : _engineFactory . GetEngineForCurrentThread ( ) ;
117124 }
118125 }
119126
@@ -149,23 +156,6 @@ public virtual string Version
149156 get { return _version . Value ; }
150157 }
151158
152- /// <summary>
153- /// Loads standard React and JSXTransformer scripts into the engine.
154- /// </summary>
155- protected virtual void InitialiseEngine ( IJsEngine engine )
156- {
157- var thisAssembly = typeof ( ReactEnvironment ) . Assembly ;
158- engine . ExecuteResource ( "React.Resources.shims.js" , thisAssembly ) ;
159- engine . ExecuteResource ( "React.Resources.react-with-addons.js" , thisAssembly ) ;
160- engine . Execute ( "var React = global.React" ) ;
161-
162- // Only load JSX Transformer if engine supports it
163- if ( engine . SupportsJsxTransformer ( ) )
164- {
165- engine . ExecuteResource ( "React.Resources.JSXTransformer.js" , thisAssembly ) ;
166- }
167- }
168-
169159 /// <summary>
170160 /// Ensures any user-provided scripts have been loaded
171161 /// </summary>
@@ -362,6 +352,13 @@ public string TransformJsx(string input)
362352 /// <returns>Result returned from JavaScript code</returns>
363353 public virtual T ExecuteWithLargerStackIfRequired < T > ( string function , params object [ ] args )
364354 {
355+ // This hack is not required when pooling JavaScript engines, since pooled MSIE
356+ // engines already execute on their own thread with a larger stack.
357+ if ( _config . ReuseJavaScriptEngines )
358+ {
359+ return Execute < T > ( function , args ) ;
360+ }
361+
365362 try
366363 {
367364 return Execute < T > ( function , args ) ;
@@ -375,10 +372,11 @@ public virtual T ExecuteWithLargerStackIfRequired<T>(string function, params obj
375372 Exception innerEx = null ;
376373 var thread = new Thread ( ( ) =>
377374 {
375+ // New engine will be created here (as this is a new thread)
376+ var engine = _engineFactory . GetEngineForCurrentThread ( ) ;
378377 try
379378 {
380- // New engine will be created here (as this is a new thread)
381- result = Execute < T > ( function , args ) ;
379+ result = engine . CallFunction < T > ( function , args ) ;
382380 }
383381 catch ( Exception threadEx )
384382 {
@@ -421,6 +419,10 @@ private static string GetVersion()
421419 public virtual void Dispose ( )
422420 {
423421 _engineFactory . DisposeEngineForCurrentThread ( ) ;
422+ if ( _engineFromPool . IsValueCreated )
423+ {
424+ _engineFactory . ReturnEngineToPool ( _engineFromPool . Value ) ;
425+ }
424426 }
425427
426428 /// <summary>
0 commit comments