public static void Configure(JsEngineSwitcher engineSwitcher) { engineSwitcher.EngineFactories .AddChakraCore() .AddJint() .AddJurassic() .AddMsie(new MsieSettings { EngineMode = JsEngineMode.ChakraIeJsRt, UseEcmaScript5Polyfill = true, UseJson2Library = true }) .AddV8() .AddVroom() ; engineSwitcher.DefaultEngineName = ChakraCoreJsEngine.EngineName; }
/// <summary> /// Initializes a new instance of the <see cref="JavaScriptEngineFactory"/> class. /// </summary> public JavaScriptEngineFactory( JsEngineSwitcher jsEngineSwitcher, IReactSiteConfiguration config, IFileSystem fileSystem ) { _jsEngineSwitcher = jsEngineSwitcher; _config = config; _fileSystem = fileSystem; #pragma warning disable 618 _factory = GetFactory(_jsEngineSwitcher, config.AllowMsieEngine); #pragma warning restore 618 if (_config.ReuseJavaScriptEngines) { _pool = CreatePool(); } }
public JsEvaluationService(JsEngineSwitcher engineSwitcher) { _engineSwitcher = engineSwitcher; }
/// <summary> /// Ensures that some engines have been registered with JavaScriptEngineSwitcher. IF not, /// registers some default engines. /// </summary> /// <param name="jsEngineSwitcher">JavaScript Engine Switcher instance</param> /// <param name="allowMsie">Whether to allow the MSIE JS engine</param> private static void EnsureJsEnginesRegistered(JsEngineSwitcher jsEngineSwitcher, bool allowMsie) { if (jsEngineSwitcher.EngineFactories.Any()) { // Engines have been registered, nothing to do here! return; } Trace.WriteLine( "No JavaScript engines were registered, falling back to a default config! It is " + "recommended that you configure JavaScriptEngineSwitcher in your app. See " + "https://github.com/Taritsyn/JavaScriptEngineSwitcher/wiki/Registration-of-JS-engines " + "for more information." ); #if NET40 jsEngineSwitcher.EngineFactories.AddV8(); #endif jsEngineSwitcher.EngineFactories.Add(new VroomJsEngine.Factory()); if (allowMsie) { jsEngineSwitcher.EngineFactories.AddMsie(); } #if !NET40 jsEngineSwitcher.EngineFactories.AddChakraCore(); #endif }
/// <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(); }