Skip to content

Commit ed35b17

Browse files
committed
In configuration settings of the Jint JS engine was added one new property - AllowReflection (default false)
1 parent 2ce7391 commit ed35b17

File tree

10 files changed

+209
-149
lines changed

10 files changed

+209
-149
lines changed

src/JavaScriptEngineSwitcher.Jint/JavaScriptEngineSwitcher.Jint.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<Description>JavaScriptEngineSwitcher.Jint contains adapter `JintJsEngine` (wrapper for the Jint JavaScript Engine (http://github.com/sebastienros/jint) version 3.0.0 Beta 2046).</Description>
2222
<PackageTags>$(PackageCommonTags);Jint</PackageTags>
2323
<PackageIconFullPath>../../Icons/JavaScriptEngineSwitcher_Jint_Logo128x128.png</PackageIconFullPath>
24-
<PackageReleaseNotes>Jint was updated to version 3.0.0 Beta 2046.</PackageReleaseNotes>
24+
<PackageReleaseNotes>In configuration settings of the Jint JS engine was added one new property - `AllowReflection` (default `false`).</PackageReleaseNotes>
2525
</PropertyGroup>
2626

2727
<ItemGroup>

src/JavaScriptEngineSwitcher.Jint/JintJsEngine.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using OriginalRuntimeException = Jint.Runtime.JintException;
1919
using OriginalStatementsCountOverflowException = Jint.Runtime.StatementsCountOverflowException;
2020
using OriginalTypeReference = Jint.Runtime.Interop.TypeReference;
21+
using OriginalTypeResolver = Jint.Runtime.Interop.TypeResolver;
2122
using OriginalTypes = Jint.Runtime.Types;
2223
using OriginalValue = Jint.Native.JsValue;
2324

@@ -118,6 +119,12 @@ public JintJsEngine(JintSettings settings)
118119
try
119120
{
120121
_jsEngine = new OriginalEngine(options => {
122+
if (jintSettings.AllowReflection)
123+
{
124+
options.SetTypeResolver(new OriginalTypeResolver());
125+
options.Interop.AllowGetType = true;
126+
}
127+
121128
options
122129
.CancellationToken(_cancellationTokenSource.Token)
123130
.DebuggerStatementHandling(debuggerStatementHandlingMode)

src/JavaScriptEngineSwitcher.Jint/JintSettings.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ namespace JavaScriptEngineSwitcher.Jint
99
/// </summary>
1010
public sealed class JintSettings
1111
{
12+
/// <summary>
13+
/// Gets or sets a flag for whether to allow the usage of reflection API in the script code
14+
/// </summary>
15+
/// <remarks>
16+
/// This affects <see cref="Object.GetType"/> and <see cref="Exception.GetType"/>.
17+
/// </remarks>
18+
public bool AllowReflection
19+
{
20+
get;
21+
set;
22+
}
23+
1224
/// <summary>
1325
/// Gets or sets a flag for whether to allow the <c>debugger</c> statement
1426
/// to be called in a script
@@ -150,6 +162,7 @@ public TimeSpan TimeoutInterval
150162
/// </summary>
151163
public JintSettings()
152164
{
165+
AllowReflection = false;
153166
DebuggerBreakCallback = null;
154167
DebuggerStatementHandlingMode = JsDebuggerStatementHandlingMode.Ignore;
155168
DebuggerStepCallback = null;

src/JavaScriptEngineSwitcher.Jint/readme.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
=============
1919
RELEASE NOTES
2020
=============
21-
Jint was updated to version 3.0.0 Beta 2046.
21+
In configuration settings of the Jint JS engine was added one new property -
22+
`AllowReflection` (default `false`).
2223

2324
=============
2425
DOCUMENTATION

test/JavaScriptEngineSwitcher.Tests/InteropTestsBase.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,28 @@ public virtual void EmbeddingOfInstancesOfCustomReferenceTypesAndCallingOfMethod
452452
Assert.Equal(targetOutput2, output2);
453453
}
454454

455+
[Fact]
456+
public virtual void EmbeddingOfInstanceOfCustomValueTypeAndCallingOfItsGetTypeMethod()
457+
{
458+
// Arrange
459+
var date = new Date();
460+
461+
const string input = "date.GetType();";
462+
string targetOutput = typeof(Date).FullName;
463+
464+
// Act
465+
string output;
466+
467+
using (var jsEngine = CreateJsEngine())
468+
{
469+
jsEngine.EmbedHostObject("date", date);
470+
output = jsEngine.Evaluate<string>(input);
471+
}
472+
473+
// Assert
474+
Assert.Equal(targetOutput, output);
475+
}
476+
455477
[Fact]
456478
public virtual void EmbeddingOfInstanceOfCustomReferenceTypeAndCallingOfItsGetTypeMethod()
457479
{

test/JavaScriptEngineSwitcher.Tests/JavaScriptEngineSwitcher.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<VersionPrefix>3.20.10</VersionPrefix>
66
<TargetFrameworks>net452;net471;netcoreapp2.1;netcoreapp3.1;net5.0;net6.0;net7.0</TargetFrameworks>
77
<OutputType>Library</OutputType>
8+
<LangVersion>latest</LangVersion>
89
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
910
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
1011
<IsPackable>false</IsPackable>

test/JavaScriptEngineSwitcher.Tests/Jint/InteropTests.cs

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Xunit;
66

77
using JavaScriptEngineSwitcher.Core;
8+
using JavaScriptEngineSwitcher.Jint;
89

910
using JavaScriptEngineSwitcher.Tests.Interop;
1011
using JavaScriptEngineSwitcher.Tests.Interop.Animals;
@@ -24,31 +25,51 @@ protected override string EngineName
2425
#region Objects with methods
2526

2627
[Fact]
27-
public override void EmbeddingOfInstanceOfCustomReferenceTypeAndCallingOfItsGetTypeMethod()
28+
public override void EmbeddingOfInstanceOfCustomValueTypeAndCallingOfItsGetTypeMethod()
2829
{
2930
// Arrange
30-
var cat = new Cat();
31+
static string TestAllowReflectionSetting(bool allowReflection)
32+
{
33+
var date = new Date();
3134

32-
const string input = "cat.GetType();";
35+
const string input = "date.GetType();";
3336

34-
// Act
35-
JsRuntimeException exception = null;
37+
using (var jsEngine = new JintJsEngine(new JintSettings { AllowReflection = allowReflection }))
38+
{
39+
jsEngine.EmbedHostObject("date", date);
40+
return jsEngine.Evaluate<string>(input);
41+
}
42+
}
3643

37-
using (var jsEngine = CreateJsEngine())
44+
// Act and Assert
45+
Assert.Equal(typeof(Date).FullName, TestAllowReflectionSetting(true));
46+
47+
var exception = Assert.Throws<JsRuntimeException>(() => TestAllowReflectionSetting(false));
48+
Assert.Equal("Runtime error", exception.Category);
49+
Assert.Equal("Property 'GetType' of object is not a function", exception.Description);
50+
}
51+
52+
[Fact]
53+
public override void EmbeddingOfInstanceOfCustomReferenceTypeAndCallingOfItsGetTypeMethod()
54+
{
55+
// Arrange
56+
static string TestAllowReflectionSetting(bool allowReflection)
3857
{
39-
try
58+
var cat = new Cat();
59+
60+
const string input = "cat.GetType();";
61+
62+
using (var jsEngine = new JintJsEngine(new JintSettings { AllowReflection = allowReflection }))
4063
{
4164
jsEngine.EmbedHostObject("cat", cat);
42-
jsEngine.Evaluate<string>(input);
43-
}
44-
catch (JsRuntimeException e)
45-
{
46-
exception = e;
65+
return jsEngine.Evaluate<string>(input);
4766
}
4867
}
4968

50-
// Assert
51-
Assert.NotNull(exception);
69+
// Act and Assert
70+
Assert.Equal(typeof(Cat).FullName, TestAllowReflectionSetting(true));
71+
72+
var exception = Assert.Throws<JsRuntimeException>(() => TestAllowReflectionSetting(false));
5273
Assert.Equal("Runtime error", exception.Category);
5374
Assert.Equal("Property 'GetType' of object is not a function", exception.Description);
5475
}
@@ -343,28 +364,23 @@ public void MappingHostErrorDuringRecursiveExecutionOfFiles()
343364
public override void CreatingAnInstanceOfEmbeddedCustomExceptionAndCallingOfItsGetTypeMethod()
344365
{
345366
// Arrange
346-
Type loginFailedExceptionType = typeof(LoginFailedException);
347-
348-
const string input = "new LoginFailedError(\"Wrong password entered!\").GetType();";
367+
static string TestAllowReflectionSetting(bool allowReflection)
368+
{
369+
Type loginFailedExceptionType = typeof(LoginFailedException);
349370

350-
// Act
351-
JsRuntimeException exception = null;
371+
const string input = "new LoginFailedError(\"Wrong password entered!\").GetType();";
352372

353-
using (var jsEngine = CreateJsEngine())
354-
{
355-
try
373+
using (var jsEngine = new JintJsEngine(new JintSettings { AllowReflection = allowReflection }))
356374
{
357375
jsEngine.EmbedHostType("LoginFailedError", loginFailedExceptionType);
358-
jsEngine.Evaluate<string>(input);
359-
}
360-
catch (JsRuntimeException e)
361-
{
362-
exception = e;
376+
return jsEngine.Evaluate<string>(input);
363377
}
364378
}
365379

366-
// Assert
367-
Assert.NotNull(exception);
380+
// Act and Assert
381+
Assert.Equal(typeof(LoginFailedException).FullName, TestAllowReflectionSetting(true));
382+
383+
var exception = Assert.Throws<JsRuntimeException>(() => TestAllowReflectionSetting(false));
368384
Assert.Equal("Runtime error", exception.Category);
369385
Assert.Equal("Property 'GetType' of object is not a function", exception.Description);
370386
}

test/JavaScriptEngineSwitcher.Tests/Jurassic/InteropTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@ public override void EmbeddingOfInstanceOfAnonymousTypeWithProperties()
2525

2626
#region Objects with methods
2727

28+
public override void EmbeddingOfInstanceOfCustomValueTypeAndCallingOfItsGetTypeMethod()
29+
{
30+
// Arrange
31+
var date = new Date();
32+
33+
const string input = "date.GetType();";
34+
string targetOutput = typeof(Date).FullName;
35+
36+
// Act
37+
string output;
38+
39+
using (var jsEngine = CreateJsEngine())
40+
{
41+
jsEngine.EmbedHostObject("date", date);
42+
output = jsEngine.Evaluate(input).ToString();
43+
}
44+
45+
// Assert
46+
Assert.Equal(targetOutput, output);
47+
}
48+
2849
public override void EmbeddingOfInstanceOfCustomReferenceTypeAndCallingOfItsGetTypeMethod()
2950
{
3051
// Arrange

test/JavaScriptEngineSwitcher.Tests/NiL/InteropTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using JavaScriptEngineSwitcher.Core;
77

8+
using JavaScriptEngineSwitcher.Tests.Interop;
89
using JavaScriptEngineSwitcher.Tests.Interop.Animals;
910

1011
namespace JavaScriptEngineSwitcher.Tests.NiL
@@ -21,6 +22,36 @@ protected override string EngineName
2122

2223
#region Objects with methods
2324

25+
[Fact]
26+
public override void EmbeddingOfInstanceOfCustomValueTypeAndCallingOfItsGetTypeMethod()
27+
{
28+
// Arrange
29+
var date = new Date();
30+
31+
const string input = "date.GetType();";
32+
33+
// Act
34+
JsRuntimeException exception = null;
35+
36+
using (var jsEngine = CreateJsEngine())
37+
{
38+
try
39+
{
40+
jsEngine.EmbedHostObject("date", date);
41+
jsEngine.Evaluate<string>(input);
42+
}
43+
catch (JsRuntimeException e)
44+
{
45+
exception = e;
46+
}
47+
}
48+
49+
// Assert
50+
Assert.NotNull(exception);
51+
Assert.Equal("Runtime error", exception.Category);
52+
Assert.Equal("date.GetType is not a function", exception.Description);
53+
}
54+
2455
[Fact]
2556
public override void EmbeddingOfInstanceOfCustomReferenceTypeAndCallingOfItsGetTypeMethod()
2657
{

0 commit comments

Comments
 (0)