/// <summary> /// Gets a factory for the most appropriate JavaScript engine for the current environment. /// The first functioning JavaScript engine with the lowest priority will be used. /// </summary> /// <returns>Function to create JavaScript engine</returns> private static Func <IJsEngine> GetFactory(JsEngineSwitcher jsEngineSwitcher, bool allowMsie) { EnsureJsEnginesRegistered(jsEngineSwitcher, allowMsie); foreach (var engineFactory in jsEngineSwitcher.EngineFactories) { IJsEngine engine = null; try { engine = engineFactory.CreateEngine(); if (EngineIsUsable(engine, allowMsie)) { // Success! Use this one. return(engineFactory.CreateEngine); } } catch (Exception ex) { // This engine threw an exception, try the next one Trace.WriteLine(string.Format("Error initialising {0}: {1}", engineFactory, ex)); } finally { if (engine != null) { engine.Dispose(); } } } // Epic fail, none of the engines worked. Nothing we can do now. // Throw an error relevant to the engine they should be able to use. #if NET40 if (JavaScriptEngineUtils.EnvironmentSupportsClearScript()) { JavaScriptEngineUtils.EnsureEngineFunctional <V8JsEngine, ClearScriptV8InitialisationException>( ex => new ClearScriptV8InitialisationException(ex) ); } #endif #if NET40 || NETSTANDARD1_6 if (JavaScriptEngineUtils.EnvironmentSupportsVroomJs()) { JavaScriptEngineUtils.EnsureEngineFunctional <VroomJsEngine, VroomJsInitialisationException>( ex => new VroomJsInitialisationException(ex.Message) ); } #endif throw new ReactEngineNotFoundException(); }
/// <summary> /// Gets a factory for the most appropriate JavaScript engine for the current environment. /// The first functioning JavaScript engine with the lowest priority will be used. /// </summary> /// <returns>Function to create JavaScript engine</returns> private static Func <IJsEngine> GetFactory(IEnumerable <Registration> availableFactories) { var availableEngineFactories = availableFactories .OrderBy(x => x.Priority) .Select(x => x.Factory); foreach (var engineFactory in availableEngineFactories) { IJsEngine engine = null; try { engine = engineFactory(); // Perform a sanity test to ensure this engine is usable if (engine.Evaluate <int>("1 + 1") == 2) { // Success! Use this one. return(engineFactory); } } catch (Exception ex) { // This engine threw an exception, try the next one Trace.WriteLine(string.Format("Error initialising {0}: {1}", engineFactory, ex)); } finally { if (engine != null) { engine.Dispose(); } } } // Epic fail, none of the engines worked. Nothing we can do now. // Throw an error relevant to the engine they should be able to use. if (JavaScriptEngineUtils.EnvironmentSupportsClearScript()) { JavaScriptEngineUtils.EnsureEngineFunctional <V8JsEngine, ClearScriptV8InitialisationException>( ex => new ClearScriptV8InitialisationException(ex.Message) ); } else if (JavaScriptEngineUtils.EnvironmentSupportsVroomJs()) { JavaScriptEngineUtils.EnsureEngineFunctional <VroomJsEngine, VroomJsInitialisationException>( ex => new VroomJsInitialisationException(ex.Message) ); } throw new ReactEngineNotFoundException(); }
/// <summary> /// Registers JavaScript engines that may be able to run in the current environment /// </summary> /// <param name="container"></param> private void RegisterSupportedEngines(TinyIoCContainer container) { if (JavaScriptEngineUtils.EnvironmentSupportsClearScript()) { container.Register(new JavaScriptEngineFactory.Registration { Factory = () => new V8JsEngine(), Priority = 10 }, "ClearScriptV8"); } if (JavaScriptEngineUtils.EnvironmentSupportsVroomJs()) { container.Register(new JavaScriptEngineFactory.Registration { Factory = () => new VroomJsEngine(), Priority = 10 }, "VroomJs"); } container.Register(new JavaScriptEngineFactory.Registration { Factory = () => new MsieJsEngine(new MsieConfiguration { EngineMode = JsEngineMode.ChakraEdgeJsRt }), Priority = 20 }, "MsieChakraEdgeRT"); container.Register(new JavaScriptEngineFactory.Registration { Factory = () => new MsieJsEngine(new MsieConfiguration { EngineMode = JsEngineMode.ChakraIeJsRt }), Priority = 30 }, "MsieChakraIeRT"); container.Register(new JavaScriptEngineFactory.Registration { Factory = () => new MsieJsEngine(new MsieConfiguration { EngineMode = JsEngineMode.ChakraActiveScript }), Priority = 40 }, "MsieChakraActiveScript"); container.Register(new JavaScriptEngineFactory.Registration { Factory = () => new MsieJsEngine(new MsieConfiguration { EngineMode = JsEngineMode.Classic }), Priority = 50 }, "MsieClassic"); }
/// <summary> /// Gets a factory for the most appropriate JavaScript engine for the current environment. /// The first functioning JavaScript engine with the lowest priority will be used. /// </summary> /// <returns>Function to create JavaScript engine</returns> private static Func <IJsEngine> GetFactory(IJsEngineSwitcher jsEngineSwitcher, bool allowMsie) { EnsureJsEnginesRegistered(jsEngineSwitcher, allowMsie); string defaultEngineName = jsEngineSwitcher.DefaultEngineName; if (!string.IsNullOrWhiteSpace(defaultEngineName)) { var engineFactory = jsEngineSwitcher.EngineFactories.Get(defaultEngineName); if (engineFactory != null) { return(engineFactory.CreateEngine); } else { throw new ReactEngineNotFoundException( "Could not find a factory that creates an instance of the JavaScript " + "engine with name `" + defaultEngineName + "`."); } } foreach (var engineFactory in jsEngineSwitcher.EngineFactories) { IJsEngine engine = null; try { engine = engineFactory.CreateEngine(); if (EngineIsUsable(engine, allowMsie)) { // Success! Use this one. return(engineFactory.CreateEngine); } } catch (Exception ex) { // This engine threw an exception, try the next one Trace.WriteLine(string.Format("Error initialising {0}: {1}", engineFactory, ex)); } finally { if (engine != null) { engine.Dispose(); } } } // Epic fail, none of the engines worked. Nothing we can do now. // Throw an error relevant to the engine they should be able to use. #if NET45 if (JavaScriptEngineUtils.EnvironmentSupportsClearScript()) { JavaScriptEngineUtils.EnsureEngineFunctional <V8JsEngine, ClearScriptV8InitialisationException>( ex => new ClearScriptV8InitialisationException(ex) ); } #endif if (JavaScriptEngineUtils.EnvironmentSupportsVroomJs()) { JavaScriptEngineUtils.EnsureEngineFunctional <VroomJsEngine, VroomJsInitialisationException>( ex => new VroomJsInitialisationException(ex.Message) ); } throw new ReactEngineNotFoundException(); }