Skip to content

Commit fe63904

Browse files
committed
Include V8 JavaScript engine (ClearScript/VroomJs) as standard in React.Core
1 parent 238c716 commit fe63904

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+263
-727
lines changed

build.proj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ of patent rights can be found in the PATENTS file in the same directory.
3131
<PackageAssemblies Include="System.Web.Optimization.React" />
3232
<PackageAssemblies Include="Cassette.React" />
3333
<PackageAssemblies Include="React.MSBuild" />
34-
<PackageAssemblies Include="React.JavaScriptEngine.VroomJs" />
35-
<PackageAssemblies Include="React.JavaScriptEngine.ClearScriptV8" />
3634
<PackageAssembliesAspNet5 Include="React.AspNet" />
3735
<AspNet5ProjectJson Include="src/**/project.json" />
3836
</ItemGroup>

src/React.AspNet/React.AspNet.kproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
<PropertyGroup>
1818
<SchemaVersion>2.0</SchemaVersion>
1919
</PropertyGroup>
20+
<ItemGroup>
21+
<ProjectReference Include="..\React.Core\React.Core.csproj" />
22+
</ItemGroup>
2023
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
2124
<ProjectExtensions>
2225
<VisualStudio>

src/React.Core/AssemblyRegistration.cs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

10-
using JavaScriptEngineSwitcher.Jint;
1110
using JavaScriptEngineSwitcher.Msie;
1211
using JavaScriptEngineSwitcher.Msie.Configuration;
12+
using JavaScriptEngineSwitcher.V8;
1313
using React.TinyIoC;
1414

1515
namespace React
@@ -40,23 +40,47 @@ public void Register(TinyIoCContainer container)
4040
container.Register<IJavaScriptEngineFactory, JavaScriptEngineFactory>().AsSingleton();
4141

4242
container.Register<IReactEnvironment, ReactEnvironment>().AsPerRequestSingleton();
43+
RegisterSupportedEngines(container);
44+
}
45+
46+
/// <summary>
47+
/// Registers JavaScript engines that may be able to run in the current environment
48+
/// </summary>
49+
/// <param name="container"></param>
50+
private void RegisterSupportedEngines(TinyIoCContainer container)
51+
{
52+
if (JavaScriptEngineUtils.EnvironmentSupportsClearScript())
53+
{
54+
container.Register(new JavaScriptEngineFactory.Registration
55+
{
56+
Factory = () => new V8JsEngine(),
57+
Priority = 10
58+
}, "ClearScriptV8");
59+
}
60+
if (JavaScriptEngineUtils.EnvironmentSupportsVroomJs())
61+
{
62+
container.Register(new JavaScriptEngineFactory.Registration
63+
{
64+
Factory = () => new VroomJsEngine(),
65+
Priority = 10
66+
}, "VroomJs");
67+
}
4368

44-
// JavaScript engines
4569
container.Register(new JavaScriptEngineFactory.Registration
4670
{
47-
Factory = () => new MsieJsEngine(new MsieConfiguration { EngineMode = JsEngineMode.ChakraActiveScript }),
71+
Factory = () => new MsieJsEngine(new MsieConfiguration { EngineMode = JsEngineMode.ChakraJsRt }),
4872
Priority = 20
49-
}, "MsieChakra");
73+
}, "MsieChakraRT");
5074
container.Register(new JavaScriptEngineFactory.Registration
5175
{
52-
Factory = () => new MsieJsEngine(new MsieConfiguration { EngineMode = JsEngineMode.Classic }),
76+
Factory = () => new MsieJsEngine(new MsieConfiguration { EngineMode = JsEngineMode.ChakraActiveScript }),
5377
Priority = 30
54-
}, "MsieClassic");
78+
}, "MsieChakraActiveScript");
5579
container.Register(new JavaScriptEngineFactory.Registration
5680
{
57-
Factory = () => new JintJsEngine(),
58-
Priority = 100
59-
}, "Jint");
81+
Factory = () => new MsieJsEngine(new MsieConfiguration { EngineMode = JsEngineMode.Classic }),
82+
Priority = 40
83+
}, "MsieClassic");
6084
}
6185
}
6286
}

src/React.JavaScriptEngine.ClearScriptV8/ClearScriptV8InitialisationException.cs renamed to src/React.Core/Exceptions/ClearScriptV8InitialisationException.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99

1010
using System;
1111
using System.Runtime.Serialization;
12-
using React.Exceptions;
1312

14-
namespace React.JavaScriptEngine.ClearScriptV8
13+
namespace React.Exceptions
1514
{
1615
/// <summary>
17-
/// Thrown when the JavaScript engine does not support JSX transformation
16+
/// Thrown when the ClearScript V8 JavaScript engine fails to initialise
1817
/// </summary>
1918
[Serializable]
2019
public class ClearScriptV8InitialisationException : ReactException
@@ -38,8 +37,10 @@ private static string GetMessage(string innerMessage)
3837
{
3938
return
4039
"Failed to initialise ClearScript V8. This is most likely caused by the native libraries " +
41-
"(ClearScriptV8-64.dll and v8-x64.dll) missing from your app's Bin directory. Please " +
42-
"ensure your app is referencing the JavaScriptEngineSwitcher.V8 NuGet package.\n\n" +
40+
"(ClearScriptV8-64.dll and v8-x64.dll) missing from your app's Bin directory, or the " +
41+
"Visual C++ runtime not being installed. Please ensure your app is referencing the " +
42+
"JavaScriptEngineSwitcher.V8 NuGet package, and refer to the ReactJS.NET site for more" +
43+
"debugging tips.\n\n" +
4344
"More details: " + innerMessage;
4445
}
4546
}

src/React.Core/Exceptions/JsxUnsupportedEngineException.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
using System;
11+
using System.Runtime.Serialization;
12+
13+
namespace React.Exceptions
14+
{
15+
/// <summary>
16+
/// Thrown when no valid JavaScript engine is found.
17+
/// </summary>
18+
[Serializable]
19+
public class ReactEngineNotFoundException : ReactException
20+
{
21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="ReactEngineNotFoundException"/> class.
23+
/// </summary>
24+
public ReactEngineNotFoundException() : base(GetMessage()) { }
25+
26+
/// <summary>
27+
/// Used by deserialization
28+
/// </summary>
29+
protected ReactEngineNotFoundException(SerializationInfo info, StreamingContext context)
30+
: base(info, context) { }
31+
32+
/// <summary>
33+
/// Gets a message that describes the current exception.
34+
/// </summary>
35+
private static string GetMessage()
36+
{
37+
return
38+
"No usable JavaScript engine was found. Please install a JavaScript engine such " +
39+
"as React.JavaScriptEngine.ClearScriptV8 (on Windows) or " +
40+
"React.JavaScriptEngine.VroomJs (on Linux and Mac OS X). Refer to the ReactJS.NET " +
41+
"documentation for more details.";
42+
}
43+
}
44+
}

src/React.JavaScriptEngine.VroomJs/VroomJsInitialisationException.cs renamed to src/React.Core/Exceptions/VroomJsInitialisationException.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99

1010
using System;
1111
using System.Runtime.Serialization;
12-
using React.Exceptions;
1312

14-
namespace React.JavaScriptEngine.VroomJs
13+
namespace React.Exceptions
1514
{
1615
/// <summary>
17-
/// Thrown when the JavaScript engine does not support JSX transformation
16+
/// Thrown when the VroomJs V8 JavaScript engine fails to initialise.
1817
/// </summary>
1918
[Serializable]
2019
public class VroomJsInitialisationException : ReactException
@@ -40,7 +39,8 @@ private static string GetMessage(string innerMessage)
4039
"Failed to initialise VroomJs. This is most likely caused by the native library " +
4140
"(libVroomJsNative.so) being out of date or your system lacking a compatible version of " +
4241
"V8. Please run Mono with the `MONO_LOG_LEVEL=debug` environment variable for " +
43-
"more debugging information. \n\n " +
42+
"more debugging information, and refer to the ReactJS.NET Mono documentation at " +
43+
"http://reactjs.net/guides/mono.html for further debugging hints.\n\n " +
4444
"More details: " + innerMessage;
4545
}
4646
}

src/React.Core/JavaScriptEngineExtensions.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/React.Core/JavaScriptEngineFactory.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using System.Threading;
88
using JavaScriptEngineSwitcher.Core;
9+
using JavaScriptEngineSwitcher.V8;
910
using JSPool;
1011
using React.Exceptions;
1112

@@ -133,12 +134,7 @@ protected virtual void InitialiseEngine(IJsEngine engine)
133134
engine.ExecuteResource("React.Resources.shims.js", thisAssembly);
134135
engine.ExecuteResource("React.Resources.react-with-addons.js", thisAssembly);
135136
engine.Execute("var React = global.React");
136-
137-
// Only load JSX Transformer if engine supports it
138-
if (engine.SupportsJsxTransformer())
139-
{
140-
engine.ExecuteResource("React.Resources.JSXTransformer.js", thisAssembly);
141-
}
137+
engine.ExecuteResource("React.Resources.JSXTransformer.js", thisAssembly);
142138
}
143139

144140
/// <summary>
@@ -234,7 +230,20 @@ private static Func<IJsEngine> GetFactory(IEnumerable<Registration> availableFac
234230
}
235231

236232
// Epic fail, none of the engines worked. Nothing we can do now.
237-
throw new ReactException("No usable JavaScript engine found :(");
233+
// Throw an error relevant to the engine they should be able to use.
234+
if (JavaScriptEngineUtils.EnvironmentSupportsClearScript())
235+
{
236+
JavaScriptEngineUtils.EnsureEngineFunctional<V8JsEngine, ClearScriptV8InitialisationException>(
237+
ex => new ClearScriptV8InitialisationException(ex.Message)
238+
);
239+
}
240+
else if (JavaScriptEngineUtils.EnvironmentSupportsVroomJs())
241+
{
242+
JavaScriptEngineUtils.EnsureEngineFunctional<VroomJsEngine, VroomJsInitialisationException>(
243+
ex => new VroomJsInitialisationException(ex.Message)
244+
);
245+
}
246+
throw new ReactEngineNotFoundException();
238247
}
239248

240249
/// <summary>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
using System;
11+
using JavaScriptEngineSwitcher.Core;
12+
using React.Exceptions;
13+
14+
namespace React
15+
{
16+
/// <summary>
17+
/// Various helper methods for the JavaScript engine environment.
18+
/// </summary>
19+
public static class JavaScriptEngineUtils
20+
{
21+
/// <summary>
22+
/// Determines if the current environment supports the VroomJs engine.
23+
/// </summary>
24+
/// <returns><c>true</c> if VroomJs is supported</returns>
25+
public static bool EnvironmentSupportsVroomJs()
26+
{
27+
return Environment.OSVersion.Platform == PlatformID.Unix;
28+
}
29+
30+
/// <summary>
31+
/// Determines if the current environment supports the ClearScript V8 engine
32+
/// </summary>
33+
/// <returns><c>true</c> if ClearScript is supported</returns>
34+
public static bool EnvironmentSupportsClearScript()
35+
{
36+
return Environment.OSVersion.Platform == PlatformID.Win32NT;
37+
}
38+
39+
/// <summary>
40+
/// Attempts to use the specified engine and throws an exception if it doesn't work.
41+
/// </summary>
42+
public static void EnsureEngineFunctional<TEngine, TException>(
43+
Func<Exception, TException> exceptionFactory
44+
)
45+
where TEngine : IJsEngine, new()
46+
where TException : Exception
47+
{
48+
int result;
49+
try
50+
{
51+
using (var engine = new TEngine())
52+
{
53+
result = engine.Evaluate<int>("1 + 1");
54+
}
55+
}
56+
catch (Exception ex)
57+
{
58+
throw exceptionFactory(ex);
59+
}
60+
61+
if (result != 2)
62+
{
63+
throw new ReactException("Mathematics is broken. 1 + 1 = " + result);
64+
}
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)