Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 0 additions & 51 deletions src/React.Core/Exceptions/ClearScriptV8InitialisationException.cs

This file was deleted.

51 changes: 0 additions & 51 deletions src/React.Core/Exceptions/VroomJsInitialisationException.cs

This file was deleted.

14 changes: 6 additions & 8 deletions src/React.Core/IReactSiteConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,15 @@ public interface IReactSiteConfiguration
/// </summary>
IReactSiteConfiguration SetMaxUsagesPerEngine(int? maxUsagesPerEngine);

/// <summary>
/// Gets or sets whether the MSIE engine should be used if V8 is unavailable.
///<summary>
/// Gets or sets the name of the default JavaScript engine.
/// </summary>
[Obsolete("This should be managed in the JavaScriptEngineSwitcher configuration instead")]
bool AllowMsieEngine { get; set; }
string DefaultEngineName { get; set; }
/// <summary>
/// Sets whether the MSIE engine should be used if V8 is unavailable.
/// Sets the name of the default JavaScript engine.
/// </summary>
/// <returns></returns>
[Obsolete("This should be managed in the JavaScriptEngineSwitcher configuration instead")]
IReactSiteConfiguration SetAllowMsieEngine(bool allowMsieEngine);
/// <param name="defaultEngineName">Name of a default JavaScript engine.</param>
IReactSiteConfiguration SetDefaultEngineName(string defaultEngineName);

/// <summary>
/// Gets or sets whether the built-in version of React is loaded. If <c>false</c>, you must
Expand Down
78 changes: 22 additions & 56 deletions src/React.Core/JavaScriptEngineFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
using System.Reflection;
using System.Threading;
using JavaScriptEngineSwitcher.Core;
using JavaScriptEngineSwitcher.Msie;
#if NET40
using JavaScriptEngineSwitcher.V8;
#else
using JavaScriptEngineSwitcher.ChakraCore;
#endif
using JSPool;
using React.Exceptions;

Expand Down Expand Up @@ -68,7 +62,7 @@ IFileSystem fileSystem
_config = config;
_fileSystem = fileSystem;
#pragma warning disable 618
_factory = GetFactory(_jsEngineSwitcher, config.AllowMsieEngine);
_factory = GetFactory(_jsEngineSwitcher);
#pragma warning restore 618
if (_config.ReuseJavaScriptEngines)
{
Expand Down Expand Up @@ -237,11 +231,12 @@ public virtual PooledJsEngine GetEngine()
/// 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)
private Func<IJsEngine> GetFactory(JsEngineSwitcher jsEngineSwitcher)
{
EnsureJsEnginesRegistered(jsEngineSwitcher, allowMsie);
EnsureJsEnginesRegistered(jsEngineSwitcher);

string defaultEngineName = jsEngineSwitcher.DefaultEngineName;
// First try the config's default
string defaultEngineName = _config.DefaultEngineName;
if (!string.IsNullOrWhiteSpace(defaultEngineName))
{
var engineFactory = jsEngineSwitcher.EngineFactories.Get(defaultEngineName);
Expand All @@ -252,18 +247,19 @@ private static Func<IJsEngine> GetFactory(JsEngineSwitcher jsEngineSwitcher, boo
else
{
throw new ReactEngineNotFoundException(
"Could not find a factory that creates an instance of the JavaScript " +
"Could not find a factory that creates an instance of the default JavaScript " +
"engine with name `" + defaultEngineName + "`.");
}
}

// Since our config doesn't care, just get the first working factory
foreach (var engineFactory in jsEngineSwitcher.EngineFactories)
{
IJsEngine engine = null;
try
{
engine = engineFactory.CreateEngine();
if (EngineIsUsable(engine, allowMsie))
if (EngineIsUsable(engine))
{
// Success! Use this one.
return engineFactory.CreateEngine;
Expand All @@ -284,38 +280,18 @@ private static Func<IJsEngine> GetFactory(JsEngineSwitcher jsEngineSwitcher, boo
}

// 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>
/// Performs a sanity check to ensure the specified engine type is usable.
/// </summary>
/// <param name="engine">Engine to test</param>
/// <param name="allowMsie">Whether the MSIE engine can be used</param>
/// <returns></returns>
private static bool EngineIsUsable(IJsEngine engine, bool allowMsie)
/// <returns>Whether the engine is usable.</returns>
private static bool EngineIsUsable(IJsEngine engine)
{
// Perform a sanity test to ensure this engine is usable
var isUsable = engine.Evaluate<int>("1 + 1") == 2;
var isMsie = engine is MsieJsEngine;
return isUsable && (!isMsie || allowMsie);
return engine.Evaluate<int>("1 + 1") == 2;
}

/// <summary>
Expand Down Expand Up @@ -360,33 +336,23 @@ public void EnsureValidState()
/// 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)
private static void EnsureJsEnginesRegistered(JsEngineSwitcher jsEngineSwitcher)
{
if (jsEngineSwitcher.EngineFactories.Any())
if (!string.IsNullOrWhiteSpace(jsEngineSwitcher.DefaultEngineName))
{
// Engines have been registered, nothing to do here!
return;
jsEngineSwitcher.CreateDefaultEngine();
}

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)
if (!jsEngineSwitcher.EngineFactories.Any())
{
jsEngineSwitcher.EngineFactories.AddMsie();
throw new ReactEngineNotFoundException(
"No JavaScript engines were registered! " +
"Set the configuration's PreferredEngineName to choose a default. " +
"Alternately, configure JavaScriptEngineSwitcher in your app. See " +
"https://github.com/Taritsyn/JavaScriptEngineSwitcher/wiki/Registration-of-JS-engines " +
"for more detailed information."
);
}
#if !NET40
jsEngineSwitcher.EngineFactories.AddChakraCore();
#endif
}
}
}
26 changes: 18 additions & 8 deletions src/React.Core/ReactSiteConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JavaScriptEngineSwitcher.Core;
using Newtonsoft.Json;
using React.Exceptions;

Expand Down Expand Up @@ -37,7 +38,6 @@ public ReactSiteConfiguration()
{
BabelConfig = new BabelConfig();
ReuseJavaScriptEngines = true;
AllowMsieEngine = true;
LoadBabel = true;
LoadReact = true;
JsonSerializerSettings = new JsonSerializerSettings
Expand Down Expand Up @@ -215,18 +215,28 @@ public IReactSiteConfiguration SetMaxUsagesPerEngine(int? maxUsagesPerEngine)
return this;
}

/// <summary>
/// Gets or sets whether the MSIE engine should be used if V8 is unavailable.
///<summary>
/// Gets or sets the name of the default JsEngineSwitcher engine.
/// </summary>
public bool AllowMsieEngine { get; set; }
public string DefaultEngineName
{
get
{
return JsEngineSwitcher.Instance.DefaultEngineName;
}
set
{
JsEngineSwitcher.Instance.DefaultEngineName = value;
}
}

/// <summary>
/// Sets whether the MSIE engine should be used if V8 is unavailable.
/// Sets the name of the default JsEngineSwitcher engine.
/// </summary>
/// <returns></returns>
public IReactSiteConfiguration SetAllowMsieEngine(bool allowMsieEngine)
/// <param name="defaultEngineName">Name of a default JsEngineSwitcher engine.</param>
public IReactSiteConfiguration SetDefaultEngineName(string defaultEngineName)
{
AllowMsieEngine = allowMsieEngine;
DefaultEngineName = defaultEngineName;
return this;
}

Expand Down
Loading