Skip to content

Commit 113b1f2

Browse files
committed
Add basic support for Jint in preparation for basic Mono support. Jint does not support JSX Transformation.
1 parent 7fe48f8 commit 113b1f2

8 files changed

+127
-6
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2014, 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+
using System.Text;
13+
14+
namespace React.Exceptions
15+
{
16+
/// <summary>
17+
/// Thrown when the JavaScript engine does not support JSX transformation
18+
/// </summary>
19+
[Serializable]
20+
public class JsxUnsupportedEngineException : ReactException
21+
{
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="JsxUnsupportedEngineException"/> class.
24+
/// </summary>
25+
public JsxUnsupportedEngineException() : base(GetMessage()) { }
26+
27+
/// <summary>
28+
/// Used by deserialization
29+
/// </summary>
30+
protected JsxUnsupportedEngineException(SerializationInfo info, StreamingContext context)
31+
: base(info, context) { }
32+
33+
/// <summary>
34+
/// Gets a message that describes the current exception.
35+
/// </summary>
36+
private static string GetMessage()
37+
{
38+
return
39+
"The current JavaScript engine does not support compilation of JSX files. If " +
40+
"you are on Windows, try upgrading your version of Internet Explorer to 9 or " +
41+
"above to use the updated engine. \n\nJSX Transformation is currently " +
42+
"unsupported on Mono. If you are using Mono, it is suggested to precompile all " +
43+
"JSX files on Windows before deployment. Refer to the ReactJS.NET documentation " +
44+
"for more details.";
45+
}
46+
}
47+
}

src/React/IReactEnvironment.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ namespace React
1515
/// </summary>
1616
public interface IReactEnvironment
1717
{
18+
/// <summary>
19+
/// Determines if this JavaScript engine supports the JSX transformer.
20+
/// </summary>
21+
bool EngineSupportsJsxTransformer { get; }
22+
1823
/// <summary>
1924
/// Executes the provided JavaScript code.
2025
/// </summary>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2014, 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 JavaScriptEngineSwitcher.Core;
11+
using JavaScriptEngineSwitcher.Jint;
12+
13+
namespace React
14+
{
15+
/// <summary>
16+
/// Extension methods for <see cref="IJsEngine" />.
17+
/// </summary>
18+
public static class JavaScriptEngineExtensions
19+
{
20+
/// <summary>
21+
/// Determines if this JavaScript engine supports the JSX transformer.
22+
/// </summary>
23+
/// <param name="jsEngine">JavaScript engine</param>
24+
/// <returns><c>true</c> if JSXTransformer is supported</returns>
25+
public static bool SupportsJsxTransformer(this IJsEngine jsEngine)
26+
{
27+
// Jint overflows the stack if you attempt to run the JSX Transformer :(
28+
return !(jsEngine is JintJsEngine);
29+
}
30+
}
31+
}

src/React/JavaScriptEngineFactory.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics;
55
using System.Threading;
66
using JavaScriptEngineSwitcher.Core;
7+
using JavaScriptEngineSwitcher.Jint;
78
using JavaScriptEngineSwitcher.Msie;
89
using JavaScriptEngineSwitcher.Msie.Configuration;
910
using React.Exceptions;
@@ -77,16 +78,16 @@ private static Func<IJsEngine> GetFactory()
7778
{
7879
var availableEngineFactories = new List<Func<IJsEngine>>
7980
{
80-
// TODO JS RT
81-
() => new MsieJsEngine(new MsieConfiguration { EngineMode = JsEngineMode.ChakraActiveScript })
82-
// TODO: Add Jint
83-
// TODO: Add V8
81+
() => new MsieJsEngine(new MsieConfiguration { EngineMode = JsEngineMode.ChakraActiveScript }),
82+
() => new MsieJsEngine(new MsieConfiguration { EngineMode = JsEngineMode.Classic }),
83+
() => new JintJsEngine()
8484
};
8585
foreach (var engineFactory in availableEngineFactories)
8686
{
87-
var engine = engineFactory();
87+
IJsEngine engine = null;
8888
try
8989
{
90+
engine = engineFactory();
9091
// Perform a sanity test to ensure this engine is usable
9192
if (engine.Evaluate<int>("1 + 1") == 2)
9293
{

src/React/JsxTransformer.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public string TransformJsx(string input)
8686
return input;
8787
}
8888

89+
EnsureJsxTransformerSupported();
8990
try
9091
{
9192
var encodedInput = JsonConvert.SerializeObject(input);
@@ -100,5 +101,17 @@ public string TransformJsx(string input)
100101
throw new JsxException(ex.Message, ex);
101102
}
102103
}
104+
105+
/// <summary>
106+
/// Ensures that the current JavaScript engine supports JSXTransformer. Throws an exception
107+
/// if it doesn't.
108+
/// </summary>
109+
private void EnsureJsxTransformerSupported()
110+
{
111+
if (!_environment.EngineSupportsJsxTransformer)
112+
{
113+
throw new JsxUnsupportedEngineException();
114+
}
115+
}
103116
}
104117
}

src/React/React.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,18 @@
5050
<SpecificVersion>False</SpecificVersion>
5151
<HintPath>..\packages\JavaScriptEngineSwitcher.Core.1.1.3\lib\net40\JavaScriptEngineSwitcher.Core.dll</HintPath>
5252
</Reference>
53+
<Reference Include="JavaScriptEngineSwitcher.Jint, Version=1.1.5.0, Culture=neutral, PublicKeyToken=c608b2a8cc9e4472, processorArchitecture=MSIL">
54+
<SpecificVersion>False</SpecificVersion>
55+
<HintPath>..\packages\JavaScriptEngineSwitcher.Jint.1.1.5\lib\net40\JavaScriptEngineSwitcher.Jint.dll</HintPath>
56+
</Reference>
5357
<Reference Include="JavaScriptEngineSwitcher.Msie, Version=1.1.4.0, Culture=neutral, PublicKeyToken=c608b2a8cc9e4472, processorArchitecture=MSIL">
5458
<SpecificVersion>False</SpecificVersion>
5559
<HintPath>..\packages\JavaScriptEngineSwitcher.Msie.1.1.4\lib\net40\JavaScriptEngineSwitcher.Msie.dll</HintPath>
5660
</Reference>
61+
<Reference Include="Jint, Version=2.1.0.0, Culture=neutral, PublicKeyToken=2e92ba9c8d81157f, processorArchitecture=MSIL">
62+
<SpecificVersion>False</SpecificVersion>
63+
<HintPath>..\packages\JavaScriptEngineSwitcher.Jint.1.1.5\lib\net40\Jint.dll</HintPath>
64+
</Reference>
5765
<Reference Include="MsieJavaScriptEngine, Version=1.4.2.0, Culture=neutral, PublicKeyToken=a3a2846a37ac0d3e, processorArchitecture=MSIL">
5866
<SpecificVersion>False</SpecificVersion>
5967
<HintPath>..\packages\MsieJavaScriptEngine.1.4.2\lib\net40\MsieJavaScriptEngine.dll</HintPath>
@@ -75,6 +83,7 @@
7583
<Link>Properties\SharedAssemblyVersionInfo.cs</Link>
7684
</Compile>
7785
<Compile Include="AssemblyRegistration.cs" />
86+
<Compile Include="Exceptions\JsxUnsupportedEngineException.cs" />
7887
<Compile Include="Exceptions\ReactConfigurationException.cs" />
7988
<Compile Include="Exceptions\ReactException.cs" />
8089
<Compile Include="Exceptions\ReactInvalidComponentException.cs" />
@@ -89,6 +98,7 @@
8998
<Compile Include="IReactEnvironment.cs" />
9099
<Compile Include="IReactSiteConfiguration.cs" />
91100
<Compile Include="Exceptions\JsxException.cs" />
101+
<Compile Include="JavaScriptEngineExtensions.cs" />
92102
<Compile Include="JavaScriptEngineFactory.cs" />
93103
<Compile Include="JsxTransformer.cs" />
94104
<Compile Include="NullCache.cs" />

src/React/ReactEnvironment.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ public IJsxTransformer JsxTransformer
108108
get { return _jsxTransformer.Value; }
109109
}
110110

111+
/// <summary>
112+
/// Determines if this JavaScript engine supports the JSX transformer.
113+
/// </summary>
114+
public bool EngineSupportsJsxTransformer
115+
{
116+
get { return Engine.SupportsJsxTransformer(); }
117+
}
118+
111119
/// <summary>
112120
/// Loads standard React and JSXTransformer scripts into the engine.
113121
/// </summary>
@@ -116,8 +124,13 @@ private void InitialiseEngine(IJsEngine engine)
116124
var thisAssembly = GetType().Assembly;
117125
engine.ExecuteResource("React.Resources.shims.js", thisAssembly);
118126
engine.ExecuteResource("React.Resources.react-with-addons.js", thisAssembly);
119-
engine.ExecuteResource("React.Resources.JSXTransformer.js", thisAssembly);
120127
engine.Execute("var React = global.React");
128+
129+
// Only load JSX Transformer if engine supports it
130+
if (engine.SupportsJsxTransformer())
131+
{
132+
engine.ExecuteResource("React.Resources.JSXTransformer.js", thisAssembly);
133+
}
121134
}
122135

123136
/// <summary>

src/React/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="JavaScriptEngineSwitcher.Core" version="1.1.3" targetFramework="net40" />
4+
<package id="JavaScriptEngineSwitcher.Jint" version="1.1.5" targetFramework="net40" />
45
<package id="JavaScriptEngineSwitcher.Msie" version="1.1.4" targetFramework="net40" />
56
<package id="MsieJavaScriptEngine" version="1.4.2" targetFramework="net40" />
67
<package id="Newtonsoft.Json" version="5.0.4" targetFramework="net40" />

0 commit comments

Comments
 (0)