Skip to content

Commit 2ce7391

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

File tree

13 files changed

+603
-16
lines changed

13 files changed

+603
-16
lines changed

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"sdk": {
3-
"version": "7.0.102"
3+
"version": "7.0.103"
44
}
55
}

samples/JavaScriptEngineSwitcher.Sample.Logic/JavaScriptEngineSwitcher.Sample.Logic.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
</ItemGroup>
4545

4646
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
47-
<FrameworkReference Include="Microsoft.AspNetCore.App" Version="6.0.13" />
47+
<FrameworkReference Include="Microsoft.AspNetCore.App" Version="6.0.14" />
4848
</ItemGroup>
4949

5050
<ItemGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
51-
<FrameworkReference Include="Microsoft.AspNetCore.App" Version="7.0.2" />
51+
<FrameworkReference Include="Microsoft.AspNetCore.App" Version="7.0.3" />
5252
</ItemGroup>
5353

5454
</Project>

src/JavaScriptEngineSwitcher.V8/JavaScriptEngineSwitcher.V8.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ This package does not contain the native ClearScript.V8 assemblies. Therefore, y
3131
* Microsoft.ClearScript.V8.Native.osx-arm64</Description>
3232
<PackageTags>$(PackageCommonTags);V8;ClearScript</PackageTags>
3333
<PackageIconFullPath>../../Icons/JavaScriptEngineSwitcher_V8_Logo128x128.png</PackageIconFullPath>
34-
<PackageReleaseNotes>Microsoft ClearScript.V8 was updated to version 7.3.7.</PackageReleaseNotes>
34+
<PackageReleaseNotes>In configuration settings of the V8 JS engine was added one new property - `AllowReflection` (default `false`).</PackageReleaseNotes>
3535
</PropertyGroup>
3636

3737
<ItemGroup>

src/JavaScriptEngineSwitcher.V8/V8JsEngine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public V8JsEngine(V8Settings settings)
130130
{
131131
_jsEngine = new OriginalEngine(constraints, flags, debugPort)
132132
{
133+
AllowReflection = v8Settings.AllowReflection,
133134
DisableDynamicBinding = v8Settings.DisableDynamicBinding,
134135
MaxRuntimeHeapSize = v8Settings.MaxHeapSize,
135136
RuntimeHeapSizeSampleInterval = v8Settings.HeapSizeSampleInterval,

src/JavaScriptEngineSwitcher.V8/V8Settings.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ namespace JavaScriptEngineSwitcher.V8
77
/// </summary>
88
public sealed class V8Settings
99
{
10+
/// <summary>
11+
/// Gets or sets a flag for whether to allow the usage of reflection API in the script code
12+
/// </summary>
13+
/// <remarks>
14+
/// This affects <see cref="Object.GetType"/>, <see cref="Exception.GetType"/>,
15+
/// <see cref="Exception.TargetSite"/> and <see cref="Delegate.Method"/>.
16+
/// By default, any attempt to access these members from the script code will throw an exception.
17+
/// </remarks>
18+
public bool AllowReflection
19+
{
20+
get;
21+
set;
22+
}
23+
1024
/// <summary>
1125
/// Gets or sets a flag for whether to the script engine is to wait for a debugger connection
1226
/// and schedule a pause before executing the first line of application script code
@@ -209,6 +223,7 @@ public UIntPtr MaxStackUsage
209223
/// </summary>
210224
public V8Settings()
211225
{
226+
AllowReflection = false;
212227
AwaitDebuggerAndPauseOnStart = false;
213228
DebugPort = 9222;
214229
DisableDynamicBinding = false;

src/JavaScriptEngineSwitcher.V8/readme.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
=============
3232
RELEASE NOTES
3333
=============
34-
Microsoft ClearScript.V8 was updated to version 7.3.7.
34+
In configuration settings of the V8 JS engine was added one new property -
35+
`AllowReflection` (default `false`).
3536

3637
=============
3738
DOCUMENTATION
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
4+
namespace JavaScriptEngineSwitcher.Tests.Interop
5+
{
6+
[Serializable]
7+
public class LoginFailedException : Exception
8+
{
9+
private string _userName;
10+
11+
public string UserName
12+
{
13+
get { return _userName; }
14+
set { _userName = value; }
15+
}
16+
17+
18+
public LoginFailedException()
19+
{ }
20+
21+
public LoginFailedException(string message)
22+
: base(message)
23+
{ }
24+
25+
public LoginFailedException(string message, Exception innerException)
26+
: base(message, innerException)
27+
{ }
28+
29+
protected LoginFailedException(SerializationInfo info, StreamingContext context)
30+
: base(info, context)
31+
{
32+
if (info != null)
33+
{
34+
_userName = info.GetString("UserName");
35+
}
36+
}
37+
38+
39+
public override void GetObjectData(SerializationInfo info, StreamingContext context)
40+
{
41+
if (info == null)
42+
{
43+
throw new ArgumentNullException(nameof(info));
44+
}
45+
46+
base.GetObjectData(info, context);
47+
info.AddValue("UserName", this._userName);
48+
}
49+
}
50+
}

test/JavaScriptEngineSwitcher.Tests/InteropTestsBase.cs

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ public virtual void EmbeddingOfInstanceOfCustomReferenceTypeWithMethod()
421421
}
422422

423423
[Fact]
424-
public virtual void CallingOfMethodOfCustomReferenceTypeWithInterfaceParameter()
424+
public virtual void EmbeddingOfInstancesOfCustomReferenceTypesAndCallingOfMethodOfWithInterfaceParameter()
425425
{
426426
// Arrange
427427
var animalTrainer = new AnimalTrainer();
@@ -452,6 +452,28 @@ public virtual void CallingOfMethodOfCustomReferenceTypeWithInterfaceParameter()
452452
Assert.Equal(targetOutput2, output2);
453453
}
454454

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

457479
#region Delegates
@@ -586,7 +608,7 @@ public virtual void EmbeddingOfInstanceOfDelegateWithoutResult()
586608
}
587609

588610
[Fact]
589-
public virtual void EmbeddedInstanceOfDelegateHasFunctionPrototype()
611+
public virtual void EmbeddingOfInstanceOfDelegateAndCheckingItsPrototype()
590612
{
591613
// Arrange
592614
var someFunc = new Func<int>(() => 42);
@@ -607,7 +629,7 @@ public virtual void EmbeddedInstanceOfDelegateHasFunctionPrototype()
607629
}
608630

609631
[Fact]
610-
public virtual void CallingOfEmbeddedDelegateWithMissingParameter()
632+
public virtual void EmbeddingOfInstanceOfDelegateAndCallingItWithMissingParameter()
611633
{
612634
// Arrange
613635
var sumFunc = new Func<int, int, int>((a, b) => a + b);
@@ -635,7 +657,7 @@ public virtual void CallingOfEmbeddedDelegateWithMissingParameter()
635657
}
636658

637659
[Fact]
638-
public virtual void CallingOfEmbeddedDelegateWithExtraParameter()
660+
public virtual void EmbeddingOfInstanceOfDelegateAndCallingItWithExtraParameter()
639661
{
640662
// Arrange
641663
var sumFunc = new Func<int, int, int>((a, b) => a + b);
@@ -656,6 +678,29 @@ public virtual void CallingOfEmbeddedDelegateWithExtraParameter()
656678
Assert.Equal(targetOutput, output);
657679
}
658680

681+
[Fact]
682+
public virtual void EmbeddingOfInstanceOfDelegateAndGettingItsMethodProperty()
683+
{
684+
// Arrange
685+
var cat = new Cat();
686+
var cryFunc = new Func<string>(cat.Cry);
687+
688+
const string input = "cry.Method;";
689+
string targetOutput = "undefined";
690+
691+
// Act
692+
string output;
693+
694+
using (var jsEngine = CreateJsEngine())
695+
{
696+
jsEngine.EmbedHostObject("cry", cryFunc);
697+
output = jsEngine.Evaluate<string>(input);
698+
}
699+
700+
// Assert
701+
Assert.Equal(targetOutput, output);
702+
}
703+
659704
#endregion
660705

661706
#region Recursive calls
@@ -847,6 +892,49 @@ public virtual void CreatingAnInstanceOfEmbeddedCustomReferenceType()
847892
Assert.Equal(targetOutput, output);
848893
}
849894

895+
[Fact]
896+
public virtual void CreatingAnInstanceOfEmbeddedBuiltinExceptionAndGettingItsTargetSiteProperty()
897+
{
898+
// Arrange
899+
Type invalidOperationExceptionType = typeof(InvalidOperationException);
900+
901+
const string input = "new InvalidOperationError(\"A terrible thing happened!\").TargetSite;";
902+
903+
// Act
904+
string output;
905+
906+
using (var jsEngine = CreateJsEngine())
907+
{
908+
jsEngine.EmbedHostType("InvalidOperationError", invalidOperationExceptionType);
909+
output = jsEngine.Evaluate<string>(input);
910+
}
911+
912+
// Assert
913+
Assert.Null(output);
914+
}
915+
916+
[Fact]
917+
public virtual void CreatingAnInstanceOfEmbeddedCustomExceptionAndCallingOfItsGetTypeMethod()
918+
{
919+
// Arrange
920+
Type loginFailedExceptionType = typeof(LoginFailedException);
921+
922+
const string input = "new LoginFailedError(\"Wrong password entered!\").GetType();";
923+
string targetOutput = loginFailedExceptionType.FullName;
924+
925+
// Act
926+
string output;
927+
928+
using (var jsEngine = CreateJsEngine())
929+
{
930+
jsEngine.EmbedHostType("LoginFailedError", loginFailedExceptionType);
931+
output = jsEngine.Evaluate<string>(input);
932+
}
933+
934+
// Assert
935+
Assert.Equal(targetOutput, output);
936+
}
937+
850938
#endregion
851939

852940
#region Types with constants

test/JavaScriptEngineSwitcher.Tests/Jint/InteropTests.cs

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using JavaScriptEngineSwitcher.Core;
88

99
using JavaScriptEngineSwitcher.Tests.Interop;
10+
using JavaScriptEngineSwitcher.Tests.Interop.Animals;
1011

1112
namespace JavaScriptEngineSwitcher.Tests.Jint
1213
{
@@ -20,10 +21,44 @@ protected override string EngineName
2021

2122
#region Embedding of objects
2223

24+
#region Objects with methods
25+
26+
[Fact]
27+
public override void EmbeddingOfInstanceOfCustomReferenceTypeAndCallingOfItsGetTypeMethod()
28+
{
29+
// Arrange
30+
var cat = new Cat();
31+
32+
const string input = "cat.GetType();";
33+
34+
// Act
35+
JsRuntimeException exception = null;
36+
37+
using (var jsEngine = CreateJsEngine())
38+
{
39+
try
40+
{
41+
jsEngine.EmbedHostObject("cat", cat);
42+
jsEngine.Evaluate<string>(input);
43+
}
44+
catch (JsRuntimeException e)
45+
{
46+
exception = e;
47+
}
48+
}
49+
50+
// Assert
51+
Assert.NotNull(exception);
52+
Assert.Equal("Runtime error", exception.Category);
53+
Assert.Equal("Property 'GetType' of object is not a function", exception.Description);
54+
}
55+
56+
#endregion
57+
2358
#region Delegates
2459

2560
[Fact]
26-
public override void CallingOfEmbeddedDelegateWithMissingParameter()
61+
public override void EmbeddingOfInstanceOfDelegateAndCallingItWithMissingParameter()
2762
{
2863
// Arrange
2964
var sumFunc = new Func<int, int, int>((a, b) => a + b);
@@ -298,6 +333,45 @@ public void MappingHostErrorDuringRecursiveExecutionOfFiles()
298333
#endregion
299334

300335
#endregion
336+
337+
338+
#region Embedding of types
339+
340+
#region Creating of instances
341+
342+
[Fact]
343+
public override void CreatingAnInstanceOfEmbeddedCustomExceptionAndCallingOfItsGetTypeMethod()
344+
{
345+
// Arrange
346+
Type loginFailedExceptionType = typeof(LoginFailedException);
347+
348+
const string input = "new LoginFailedError(\"Wrong password entered!\").GetType();";
349+
350+
// Act
351+
JsRuntimeException exception = null;
352+
353+
using (var jsEngine = CreateJsEngine())
354+
{
355+
try
356+
{
357+
jsEngine.EmbedHostType("LoginFailedError", loginFailedExceptionType);
358+
jsEngine.Evaluate<string>(input);
359+
}
360+
catch (JsRuntimeException e)
361+
{
362+
exception = e;
363+
}
364+
}
365+
366+
// Assert
367+
Assert.NotNull(exception);
368+
Assert.Equal("Runtime error", exception.Category);
369+
Assert.Equal("Property 'GetType' of object is not a function", exception.Description);
370+
}
371+
372+
#endregion
373+
374+
#endregion
301375
}
302376
}
303377
#endif

0 commit comments

Comments
 (0)