Skip to content

Commit b80daf8

Browse files
committed
In JavaScriptEngineSwitcher.Msie:
1. MSIE JavaScript Engine was updated to version 3.2.1; 2. In configuration settings of the MSIE JS engine was added one new property - `AllowReflection` (default `false`).
1 parent 193644d commit b80daf8

File tree

8 files changed

+251
-43
lines changed

8 files changed

+251
-43
lines changed

src/JavaScriptEngineSwitcher.Msie/JavaScriptEngineSwitcher.Msie.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
<Description>JavaScriptEngineSwitcher.Msie contains adapter `MsieJsEngine` (wrapper for the MSIE JavaScript Engine for .NET (http://github.com/Taritsyn/MsieJavaScriptEngine)). For correct working of the MSIE JavaScript Engine it is recommended to install Internet Explorer 9+ or Microsoft Edge Legacy on the machine.</Description>
2222
<PackageTags>$(PackageCommonTags);MSIE;IE;Chakra</PackageTags>
2323
<PackageIconFullPath>../../Icons/JavaScriptEngineSwitcher_Msie_Logo128x128.png</PackageIconFullPath>
24-
<PackageReleaseNotes>MSIE JavaScript Engine was updated to version 3.1.0.</PackageReleaseNotes>
24+
<PackageReleaseNotes>1. MSIE JavaScript Engine was updated to version 3.2.1;
25+
2. In configuration settings of the MSIE JS engine was added one new property - `AllowReflection` (default `false`).</PackageReleaseNotes>
2526
</PropertyGroup>
2627

2728
<ItemGroup>
28-
<PackageReference Include="MsieJavaScriptEngine" Version="3.1.0" />
29+
<PackageReference Include="MsieJavaScriptEngine" Version="3.2.1" />
2930
<PackageReference Include="ResxToCs.MSBuild" Version="1.0.0-alpha6" PrivateAssets="All" />
3031

3132
<ProjectReference Include="../JavaScriptEngineSwitcher.Core/JavaScriptEngineSwitcher.Core.csproj" />

src/JavaScriptEngineSwitcher.Msie/MsieJsEngine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public MsieJsEngine(MsieSettings settings)
7676
{
7777
_jsEngine = new OriginalEngine(new OriginalEngineSettings
7878
{
79+
AllowReflection = msieSettings.AllowReflection,
7980
EnableDebugging = msieSettings.EnableDebugging,
8081
EngineMode = Utils.GetEnumFromOtherEnum<JsEngineMode, OriginalEngineMode>(
8182
msieSettings.EngineMode),

src/JavaScriptEngineSwitcher.Msie/MsieSettings.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ public sealed class MsieSettings
2828
private int _maxStackSize;
2929

3030
#endif
31+
/// <summary>
32+
/// Gets or sets a flag for whether to allow the usage of reflection API in the script code
33+
/// </summary>
34+
/// <remarks>
35+
/// This affects <see cref="Object.GetType"/>, <c>Exception.GetType</c>,
36+
/// <c>Exception.TargetSite</c> and <c>Delegate.Method</c>.
37+
/// </remarks>
38+
public bool AllowReflection
39+
{
40+
get;
41+
set;
42+
}
43+
3144
/// <summary>
3245
/// Gets or sets a flag for whether to enable script debugging features
3346
/// </summary>
@@ -96,6 +109,7 @@ public bool UseJson2Library
96109
/// </summary>
97110
public MsieSettings()
98111
{
112+
AllowReflection = false;
99113
EnableDebugging = false;
100114
EngineMode = JsEngineMode.Auto;
101115
#if !NETSTANDARD1_3

src/JavaScriptEngineSwitcher.Msie/readme.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
=============
2020
RELEASE NOTES
2121
=============
22-
MSIE JavaScript Engine was updated to version 3.1.0.
22+
1. MSIE JavaScript Engine was updated to version 3.2.1;
23+
2. In configuration settings of the MSIE JS engine was added one new property -
24+
`AllowReflection` (default `false`).
2325

2426
=============
2527
DOCUMENTATION

test/JavaScriptEngineSwitcher.Tests/JavaScriptEngineSwitcher.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
<Import Project="../../build/common.props" />
1616

1717
<ItemGroup>
18-
<PackageReference Include="Microsoft.NET.Test.Sdk" VersionOverride="17.3.2" />
1918
<PackageReference Include="xunit" VersionOverride="2.4.2" />
2019
<PackageReference Include="JavaScriptEngineSwitcher.ChakraCore.Native.win-arm" />
2120
<PackageReference Include="JavaScriptEngineSwitcher.ChakraCore.Native.win-arm64" />
@@ -54,10 +53,12 @@
5453
</ItemGroup>
5554

5655
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' Or '$(TargetFramework)' == 'netcoreapp2.1' ">
56+
<PackageReference Include="Microsoft.NET.Test.Sdk" VersionOverride="17.3.2" />
5757
<PackageReference Include="xunit.runner.visualstudio" VersionOverride="2.4.3" />
5858
</ItemGroup>
5959

6060
<ItemGroup Condition=" '$(TargetFramework)' != 'net452' And '$(TargetFramework)' != 'netcoreapp2.1' ">
61+
<PackageReference Include="Microsoft.NET.Test.Sdk" VersionOverride="17.5.0" />
6162
<PackageReference Include="xunit.runner.visualstudio" VersionOverride="2.4.5" />
6263
</ItemGroup>
6364

test/JavaScriptEngineSwitcher.Tests/Jint/InteropTests.cs

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ protected override string EngineName
2020
}
2121

2222

23+
private IJsEngine CreateJsEngine(bool allowReflection)
24+
{
25+
var jsEngine = new JintJsEngine(new JintSettings
26+
{
27+
AllowReflection = allowReflection
28+
});
29+
30+
return jsEngine;
31+
}
32+
2333
#region Embedding of objects
2434

2535
#region Objects with methods
@@ -28,16 +38,14 @@ protected override string EngineName
2838
public override void EmbeddingOfInstanceOfCustomValueTypeAndCallingOfItsGetTypeMethod()
2939
{
3040
// Arrange
31-
static string TestAllowReflectionSetting(bool allowReflection)
41+
string TestAllowReflectionSetting(bool allowReflection)
3242
{
3343
var date = new Date();
3444

35-
const string input = "date.GetType();";
36-
37-
using (var jsEngine = new JintJsEngine(new JintSettings { AllowReflection = allowReflection }))
45+
using (var jsEngine = CreateJsEngine(allowReflection: allowReflection))
3846
{
3947
jsEngine.EmbedHostObject("date", date);
40-
return jsEngine.Evaluate<string>(input);
48+
return jsEngine.Evaluate<string>("date.GetType();");
4149
}
4250
}
4351

@@ -53,16 +61,14 @@ static string TestAllowReflectionSetting(bool allowReflection)
5361
public override void EmbeddingOfInstanceOfCustomReferenceTypeAndCallingOfItsGetTypeMethod()
5462
{
5563
// Arrange
56-
static string TestAllowReflectionSetting(bool allowReflection)
64+
string TestAllowReflectionSetting(bool allowReflection)
5765
{
5866
var cat = new Cat();
5967

60-
const string input = "cat.GetType();";
61-
62-
using (var jsEngine = new JintJsEngine(new JintSettings { AllowReflection = allowReflection }))
68+
using (var jsEngine = CreateJsEngine(allowReflection: allowReflection))
6369
{
6470
jsEngine.EmbedHostObject("cat", cat);
65-
return jsEngine.Evaluate<string>(input);
71+
return jsEngine.Evaluate<string>("cat.GetType();");
6672
}
6773
}
6874

@@ -100,6 +106,27 @@ public override void EmbeddingOfInstanceOfDelegateAndCallingItWithMissingParamet
100106
Assert.Equal(targetOutput, output);
101107
}
102108

109+
[Fact]
110+
public override void EmbeddingOfInstanceOfDelegateAndGettingItsMethodProperty()
111+
{
112+
// Arrange
113+
string TestAllowReflectionSetting(bool allowReflection)
114+
{
115+
var cat = new Cat();
116+
var cryFunc = new Func<string>(cat.Cry);
117+
118+
using (var jsEngine = CreateJsEngine(allowReflection: allowReflection))
119+
{
120+
jsEngine.EmbedHostObject("cry", cryFunc);
121+
return jsEngine.Evaluate<string>("cry.Method;");
122+
}
123+
}
124+
125+
// Act and Assert
126+
Assert.Equal("undefined", TestAllowReflectionSetting(true));
127+
Assert.Equal("undefined", TestAllowReflectionSetting(false));
128+
}
129+
103130
#endregion
104131

105132
#region Recursive calls
@@ -360,20 +387,38 @@ public void MappingHostErrorDuringRecursiveExecutionOfFiles()
360387

361388
#region Creating of instances
362389

390+
[Fact]
391+
public override void CreatingAnInstanceOfEmbeddedBuiltinExceptionAndGettingItsTargetSiteProperty()
392+
{
393+
// Arrange
394+
string TestAllowReflectionSetting(bool allowReflection)
395+
{
396+
Type invalidOperationExceptionType = typeof(InvalidOperationException);
397+
398+
using (var jsEngine = CreateJsEngine(allowReflection: allowReflection))
399+
{
400+
jsEngine.EmbedHostType("InvalidOperationError", invalidOperationExceptionType);
401+
return jsEngine.Evaluate<string>("new InvalidOperationError(\"A terrible thing happened!\").TargetSite;");
402+
}
403+
}
404+
405+
// Act and Assert
406+
Assert.Null(TestAllowReflectionSetting(true));
407+
Assert.Null(TestAllowReflectionSetting(false));
408+
}
409+
363410
[Fact]
364411
public override void CreatingAnInstanceOfEmbeddedCustomExceptionAndCallingOfItsGetTypeMethod()
365412
{
366413
// Arrange
367-
static string TestAllowReflectionSetting(bool allowReflection)
414+
string TestAllowReflectionSetting(bool allowReflection)
368415
{
369416
Type loginFailedExceptionType = typeof(LoginFailedException);
370417

371-
const string input = "new LoginFailedError(\"Wrong password entered!\").GetType();";
372-
373-
using (var jsEngine = new JintJsEngine(new JintSettings { AllowReflection = allowReflection }))
418+
using (var jsEngine = CreateJsEngine(allowReflection: allowReflection))
374419
{
375420
jsEngine.EmbedHostType("LoginFailedError", loginFailedExceptionType);
376-
return jsEngine.Evaluate<string>(input);
421+
return jsEngine.Evaluate<string>("new LoginFailedError(\"Wrong password entered!\").GetType();");
377422
}
378423
}
379424

test/JavaScriptEngineSwitcher.Tests/Msie/InteropTests.cs

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
using Xunit;
55

66
using JavaScriptEngineSwitcher.Core;
7+
using JavaScriptEngineSwitcher.Msie;
8+
9+
using JavaScriptEngineSwitcher.Tests.Interop;
10+
using JavaScriptEngineSwitcher.Tests.Interop.Animals;
711

812
namespace JavaScriptEngineSwitcher.Tests.Msie
913
{
@@ -15,8 +19,94 @@ protected override string EngineName
1519
}
1620

1721

22+
private IJsEngine CreateJsEngine(bool allowReflection)
23+
{
24+
var jsEngine = new MsieJsEngine(new MsieSettings
25+
{
26+
AllowReflection = allowReflection,
27+
EngineMode = JsEngineMode.ChakraIeJsRt
28+
});
29+
30+
return jsEngine;
31+
}
32+
1833
#region Embedding of objects
1934

35+
#region Objects with methods
36+
37+
[Fact]
38+
public override void EmbeddingOfInstanceOfCustomValueTypeAndCallingOfItsGetTypeMethod()
39+
{
40+
// Arrange
41+
string TestAllowReflectionSetting(bool allowReflection)
42+
{
43+
var date = new Date();
44+
45+
using (var jsEngine = CreateJsEngine(allowReflection: allowReflection))
46+
{
47+
jsEngine.EmbedHostObject("date", date);
48+
return jsEngine.Evaluate<string>("date.GetType();");
49+
}
50+
}
51+
52+
// Act and Assert
53+
Assert.Equal(typeof(Date).FullName, TestAllowReflectionSetting(true));
54+
55+
var exception = Assert.Throws<JsRuntimeException>(() => TestAllowReflectionSetting(false));
56+
Assert.Equal("Runtime error", exception.Category);
57+
Assert.Equal("Object doesn't support property or method 'GetType'", exception.Description);
58+
}
59+
60+
[Fact]
61+
public override void EmbeddingOfInstanceOfCustomReferenceTypeAndCallingOfItsGetTypeMethod()
62+
{
63+
// Arrange
64+
string TestAllowReflectionSetting(bool allowReflection)
65+
{
66+
var cat = new Cat();
67+
68+
using (var jsEngine = CreateJsEngine(allowReflection: allowReflection))
69+
{
70+
jsEngine.EmbedHostObject("cat", cat);
71+
return jsEngine.Evaluate<string>("cat.GetType();");
72+
}
73+
}
74+
75+
// Act and Assert
76+
Assert.Equal(typeof(Cat).FullName, TestAllowReflectionSetting(true));
77+
78+
JsRuntimeException exception = Assert.Throws<JsRuntimeException>(() => TestAllowReflectionSetting(false));
79+
Assert.Equal("Runtime error", exception.Category);
80+
Assert.Equal("Object doesn't support property or method 'GetType'", exception.Description);
81+
}
82+
83+
#endregion
84+
85+
#region Delegates
86+
87+
[Fact]
88+
public override void EmbeddingOfInstanceOfDelegateAndGettingItsMethodProperty()
89+
{
90+
// Arrange
91+
string TestAllowReflectionSetting(bool allowReflection)
92+
{
93+
var cat = new Cat();
94+
var cryFunc = new Func<string>(cat.Cry);
95+
96+
using (var jsEngine = CreateJsEngine(allowReflection: allowReflection))
97+
{
98+
jsEngine.EmbedHostObject("cry", cryFunc);
99+
return jsEngine.Evaluate<string>("cry.Method;");
100+
}
101+
}
102+
103+
// Act and Assert
104+
Assert.Equal("undefined", TestAllowReflectionSetting(true));
105+
Assert.Equal("undefined", TestAllowReflectionSetting(false));
106+
}
107+
108+
#endregion
109+
20110
#region Recursive calls
21111

22112
#region Mapping of errors
@@ -74,5 +164,57 @@ public void MappingRuntimeErrorDuringRecursiveEvaluationOfFiles()
74164
#endregion
75165

76166
#endregion
167+
168+
169+
#region Embedding of types
170+
171+
#region Creating of instances
172+
173+
[Fact]
174+
public override void CreatingAnInstanceOfEmbeddedBuiltinExceptionAndGettingItsTargetSiteProperty()
175+
{
176+
// Arrange
177+
string TestAllowReflectionSetting(bool allowReflection)
178+
{
179+
Type invalidOperationExceptionType = typeof(InvalidOperationException);
180+
181+
using (var jsEngine = CreateJsEngine(allowReflection: allowReflection))
182+
{
183+
jsEngine.EmbedHostType("InvalidOperationError", invalidOperationExceptionType);
184+
return jsEngine.Evaluate<string>("new InvalidOperationError(\"A terrible thing happened!\").TargetSite;");
185+
}
186+
}
187+
188+
// Act and Assert
189+
Assert.Null(TestAllowReflectionSetting(true));
190+
Assert.Equal("undefined", TestAllowReflectionSetting(false));
191+
}
192+
193+
[Fact]
194+
public override void CreatingAnInstanceOfEmbeddedCustomExceptionAndCallingOfItsGetTypeMethod()
195+
{
196+
// Arrange
197+
string TestAllowReflectionSetting(bool allowReflection)
198+
{
199+
Type loginFailedExceptionType = typeof(LoginFailedException);
200+
201+
using (var jsEngine = CreateJsEngine(allowReflection: allowReflection))
202+
{
203+
jsEngine.EmbedHostType("LoginFailedError", loginFailedExceptionType);
204+
return jsEngine.Evaluate<string>("new LoginFailedError(\"Wrong password entered!\").GetType();");
205+
}
206+
}
207+
208+
// Act and Assert
209+
Assert.Equal(typeof(LoginFailedException).FullName, TestAllowReflectionSetting(true));
210+
211+
var exception = Assert.Throws<JsRuntimeException>(() => TestAllowReflectionSetting(false));
212+
Assert.Equal("Runtime error", exception.Category);
213+
Assert.Equal("Object doesn't support property or method 'GetType'", exception.Description);
214+
}
215+
216+
#endregion
217+
218+
#endregion
77219
}
78220
}

0 commit comments

Comments
 (0)