11using System ;
22using System . Collections . Concurrent ;
3- using System . Collections . Generic ;
43using System . Diagnostics ;
54using System . Linq ;
65using System . Threading ;
@@ -28,7 +27,11 @@ public class JavaScriptEngineFactory : IDisposable, IJavaScriptEngineFactory
2827 /// <summary>
2928 /// Function used to create new JavaScript engine instances.
3029 /// </summary>
31- protected readonly Func < IJsEngine > _factory ;
30+ protected readonly Func < IJsEngine > _factory ;
31+ /// <summary>
32+ /// The JavaScript Engine Switcher instance used by ReactJS.NET
33+ /// </summary>
34+ protected readonly JsEngineSwitcher _jsEngineSwitcher ;
3235 /// <summary>
3336 /// Contains all current JavaScript engine instances. One per thread, keyed on thread ID.
3437 /// </summary>
@@ -51,14 +54,17 @@ protected readonly ConcurrentDictionary<int, IJsEngine> _engines
5154 /// Initializes a new instance of the <see cref="JavaScriptEngineFactory"/> class.
5255 /// </summary>
5356 public JavaScriptEngineFactory (
54- IEnumerable < Registration > availableFactories ,
57+ JsEngineSwitcher jsEngineSwitcher ,
5558 IReactSiteConfiguration config ,
5659 IFileSystem fileSystem
5760 )
5861 {
62+ _jsEngineSwitcher = jsEngineSwitcher ;
5963 _config = config ;
6064 _fileSystem = fileSystem ;
61- _factory = GetFactory ( availableFactories , config . AllowMsieEngine ) ;
65+ #pragma warning disable 618
66+ _factory = GetFactory ( _jsEngineSwitcher , config . AllowMsieEngine ) ;
67+ #pragma warning restore 618
6268 if ( _config . ReuseJavaScriptEngines )
6369 {
6470 _pool = CreatePool ( ) ;
@@ -229,21 +235,19 @@ public virtual void ReturnEngineToPool(IJsEngine engine)
229235 /// The first functioning JavaScript engine with the lowest priority will be used.
230236 /// </summary>
231237 /// <returns>Function to create JavaScript engine</returns>
232- private static Func < IJsEngine > GetFactory ( IEnumerable < Registration > availableFactories , bool allowMsie )
238+ private static Func < IJsEngine > GetFactory ( JsEngineSwitcher jsEngineSwitcher , bool allowMsie )
233239 {
234- var availableEngineFactories = availableFactories
235- . OrderBy ( x => x . Priority )
236- . Select ( x => x . Factory ) ;
237- foreach ( var engineFactory in availableEngineFactories )
240+ EnsureJsEnginesRegistered ( jsEngineSwitcher , allowMsie ) ;
241+ foreach ( var engineFactory in jsEngineSwitcher . EngineFactories )
238242 {
239243 IJsEngine engine = null ;
240244 try
241245 {
242- engine = engineFactory ( ) ;
246+ engine = engineFactory . CreateEngine ( ) ;
243247 if ( EngineIsUsable ( engine , allowMsie ) )
244248 {
245249 // Success! Use this one.
246- return engineFactory ;
250+ return engineFactory . CreateEngine ;
247251 }
248252 }
249253 catch ( Exception ex )
@@ -329,20 +333,35 @@ public void EnsureValidState()
329333 }
330334
331335 /// <summary>
332- /// Represents a factory for a supported JavaScript engine.
336+ /// Ensures that some engines have been registered with JavaScriptEngineSwitcher. IF not,
337+ /// registers some default engines.
333338 /// </summary>
334- public class Registration
339+ /// <param name="jsEngineSwitcher">JavaScript Engine Switcher instance</param>
340+ /// <param name="allowMsie">Whether to allow the MSIE JS engine</param>
341+ private static void EnsureJsEnginesRegistered ( JsEngineSwitcher jsEngineSwitcher , bool allowMsie )
335342 {
336- /// <summary>
337- /// Gets or sets the factory for this JavaScript engine
338- /// </summary>
339- public Func < IJsEngine > Factory { get ; set ; }
343+ if ( jsEngineSwitcher . EngineFactories . Any ( ) )
344+ {
345+ // Engines have been registered, nothing to do here!
346+ return ;
347+ }
348+
349+ Trace . WriteLine (
350+ "No JavaScript engines were registered, falling back to a default config! It is " +
351+ "recommended that you configure JavaScriptEngineSwitcher in your app. See " +
352+ "https://github.com/Taritsyn/JavaScriptEngineSwitcher/wiki/Registration-of-JS-engines " +
353+ "for more information."
354+ ) ;
340355
341- /// <summary>
342- /// Gets or sets the priority for this JavaScript engine. Engines with lower priority
343- /// are preferred.
344- /// </summary>
345- public int Priority { get ; set ; }
356+ jsEngineSwitcher . EngineFactories . AddV8 ( ) ;
357+ if ( allowMsie )
358+ {
359+ jsEngineSwitcher . EngineFactories . AddMsie ( ) ;
360+ }
361+ if ( JavaScriptEngineUtils . EnvironmentSupportsVroomJs ( ) )
362+ {
363+ jsEngineSwitcher . EngineFactories . Add ( new VroomJsEngine . Factory ( ) ) ;
364+ }
346365 }
347366 }
348367}
0 commit comments