Skip to content

Commit 2021527

Browse files
committed
Use ReactNET_transform wrapper function for JSX transformation.
1 parent 3085c5a commit 2021527

File tree

7 files changed

+34
-26
lines changed

7 files changed

+34
-26
lines changed

build.proj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ of patent rights can be found in the PATENTS file in the same directory.
1111
<PropertyGroup>
1212
<Major>0</Major>
1313
<Minor>2</Minor>
14-
<Build>0</Build>
14+
<Build>1</Build>
1515
<Revision>0</Revision>
1616
<DevBuild>true</DevBuild>
1717
<DevNuGetServer>http://reactjs.net/packages/</DevNuGetServer>

src/React.Tests/Core/JsxTransformerTests.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,17 @@ public void ShouldTransformJsxIfAnnotationPresent()
6060
_jsxTransformer.TransformJsx(input);
6161

6262
_environment.Verify(x => x.ExecuteWithLargerStackIfRequired<string>(
63-
@"global.JSXTransformer.transform(""/** @jsx React.DOM */ <div>Hello World</div>"").code"
63+
"ReactNET_transform",
64+
"/** @jsx React.DOM */ <div>Hello World</div>"
6465
));
6566
}
6667

6768
[Test]
6869
public void ShouldWrapExceptionsInJsxExeption()
6970
{
7071
_environment.Setup(x => x.ExecuteWithLargerStackIfRequired<string>(
71-
@"global.JSXTransformer.transform(""/** @jsx React.DOM */ <div>Hello World</div>"").code"
72+
"ReactNET_transform",
73+
"/** @jsx React.DOM */ <div>Hello World</div>"
7274
)).Throws(new Exception("Something broke..."));
7375

7476
const string input = "/** @jsx React.DOM */ <div>Hello World</div>";
@@ -124,7 +126,8 @@ public void ShouldTransformJsxIfFileCacheHashInvalid()
124126

125127
_jsxTransformer.TransformJsxFile("foo.jsx");
126128
_environment.Verify(x => x.ExecuteWithLargerStackIfRequired<string>(
127-
@"global.JSXTransformer.transform(""/** @jsx React.DOM */ <div>Hello World</div>"").code"
129+
"ReactNET_transform",
130+
"/** @jsx React.DOM */ <div>Hello World</div>"
128131
));
129132
}
130133

@@ -137,7 +140,8 @@ public void ShouldTransformJsxIfNoCache()
137140

138141
_jsxTransformer.TransformJsxFile("foo.jsx");
139142
_environment.Verify(x => x.ExecuteWithLargerStackIfRequired<string>(
140-
@"global.JSXTransformer.transform(""/** @jsx React.DOM */ <div>Hello World</div>"").code"
143+
"ReactNET_transform",
144+
"/** @jsx React.DOM */ <div>Hello World</div>"
141145
));
142146
}
143147

@@ -146,7 +150,8 @@ public void ShouldSaveTransformationResult()
146150
{
147151
_fileSystem.Setup(x => x.ReadAsString("foo.jsx")).Returns("/** @jsx React.DOM */ <div>Hello World</div>");
148152
_environment.Setup(x => x.ExecuteWithLargerStackIfRequired<string>(
149-
@"global.JSXTransformer.transform(""/** @jsx React.DOM */ <div>Hello World</div>"").code"
153+
"ReactNET_transform",
154+
"/** @jsx React.DOM */ <div>Hello World</div>"
150155
)).Returns("React.DOM.div('Hello World')");
151156

152157
string result = null;

src/React.Tests/Core/ReactEnvironmentTest.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public void ExecuteWithLargerStackIfRequiredWithNoNewThread()
2323
var mocks = new Mocks();
2424
var environment = mocks.CreateReactEnvironment();
2525

26-
environment.ExecuteWithLargerStackIfRequired<int>("1+1");
27-
mocks.Engine.Verify(x => x.Evaluate<int>("1+1"));
26+
environment.ExecuteWithLargerStackIfRequired<int>("foo");
27+
mocks.Engine.Verify(x => x.CallFunction<int>("foo"), Times.Exactly(1));
2828
}
2929

3030
[Test]
@@ -34,11 +34,11 @@ public void ExecuteWithLargerStackIfRequiredWithNewThread()
3434
var environment = mocks.CreateReactEnvironment();
3535
// Fail the first time Evaluate is called, succeed the second
3636
// http://stackoverflow.com/a/7045636
37-
mocks.Engine.Setup(x => x.Evaluate<int>("1+1"))
38-
.Callback(() => mocks.Engine.Setup(x => x.Evaluate<int>("1+1")))
37+
mocks.Engine.Setup(x => x.CallFunction<int>("foo"))
38+
.Callback(() => mocks.Engine.Setup(x => x.CallFunction<int>("foo")))
3939
.Throws(new Exception("Out of stack space"));
4040

41-
environment.ExecuteWithLargerStackIfRequired<int>("1+1");
41+
environment.ExecuteWithLargerStackIfRequired<int>("foo");
4242
mocks.EngineFactory.Verify(
4343
x => x.GetEngineForCurrentThread(It.IsAny<Action<IJsEngine>>()),
4444
Times.Exactly(2),
@@ -57,12 +57,12 @@ public void ExecuteWithLargerStackIfRequiredShouldBubbleExceptions()
5757
var mocks = new Mocks();
5858
var environment = mocks.CreateReactEnvironment();
5959
// Always fail
60-
mocks.Engine.Setup(x => x.Evaluate<int>("1+1"))
60+
mocks.Engine.Setup(x => x.CallFunction<int>("foobar"))
6161
.Throws(new Exception("Something bad happened :("));
6262

6363
Assert.Throws<Exception>(() =>
6464
{
65-
environment.ExecuteWithLargerStackIfRequired<int>("1+1");
65+
environment.ExecuteWithLargerStackIfRequired<int>("foobar");
6666
});
6767
}
6868

src/React/IReactEnvironment.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ public interface IReactEnvironment
4949
/// than what ASP.NET allows by default (256 KB).
5050
/// </summary>
5151
/// <typeparam name="T">Type to return from JavaScript call</typeparam>
52-
/// <param name="code">JavaScript code to execute</param>
52+
/// <param name="function">JavaScript function to execute</param>
53+
/// <param name="args">Arguments to pass to function</param>
5354
/// <returns>Result returned from JavaScript code</returns>
54-
T ExecuteWithLargerStackIfRequired<T>(string code);
55+
T ExecuteWithLargerStackIfRequired<T>(string function, params object[] args);
5556

5657
/// <summary>
5758
/// Determines if the specified variable exists in the JavaScript engine

src/React/JsxTransformer.cs

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

1010
using System;
1111
using System.IO;
12-
using Newtonsoft.Json;
1312
using React.Exceptions;
1413

1514
namespace React
@@ -130,11 +129,10 @@ public string TransformJsx(string input)
130129
EnsureJsxTransformerSupported();
131130
try
132131
{
133-
var encodedInput = JsonConvert.SerializeObject(input);
134-
var output = _environment.ExecuteWithLargerStackIfRequired<string>(string.Format(
135-
"global.JSXTransformer.transform({0}, {{ harmony: true }}).code",
136-
encodedInput
137-
));
132+
var output = _environment.ExecuteWithLargerStackIfRequired<string>(
133+
"ReactNET_transform",
134+
input
135+
);
138136
return output;
139137
}
140138
catch (Exception ex)

src/React/ReactEnvironment.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using System.Collections.Generic;
1212
using System.Diagnostics;
1313
using System.Reflection;
14-
using System.Runtime.Remoting.Messaging;
1514
using System.Text;
1615
using System.Threading;
1716
using JavaScriptEngineSwitcher.Core;
@@ -272,13 +271,14 @@ public string TransformJsx(string input)
272271
/// than what ASP.NET allows by default (256 KB).
273272
/// </summary>
274273
/// <typeparam name="T">Type to return from JavaScript call</typeparam>
275-
/// <param name="code">JavaScript code to execute</param>
274+
/// <param name="function">JavaScript function to execute</param>
275+
/// <param name="args">Arguments to pass to function</param>
276276
/// <returns>Result returned from JavaScript code</returns>
277-
public T ExecuteWithLargerStackIfRequired<T>(string code)
277+
public T ExecuteWithLargerStackIfRequired<T>(string function, params object[] args)
278278
{
279279
try
280280
{
281-
return Engine.Evaluate<T>(code);
281+
return Engine.CallFunction<T>(function, args);
282282
}
283283
catch (Exception)
284284
{
@@ -292,7 +292,7 @@ public T ExecuteWithLargerStackIfRequired<T>(string code)
292292
try
293293
{
294294
// New engine will be created here (as this is a new thread)
295-
result = Engine.Evaluate<T>(code);
295+
result = Engine.CallFunction<T>(function, args);
296296
}
297297
catch (Exception threadEx)
298298
{

src/React/Resources/shims.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ var console = console || {
1717

1818
if (!Object.freeze) {
1919
Object.freeze = function() { };
20+
}
21+
22+
function ReactNET_transform(input) {
23+
return global.JSXTransformer.transform(input, { harmony: true }).code;
2024
}

0 commit comments

Comments
 (0)