Skip to content

Commit 24d5510

Browse files
committed
In JavaScriptEngineSwitcher.V8 in configuration settings of the V8 JS engine was added 3 new properties: HeapSizeSampleInterval (default TimeSpan.Zero), MaxHeapSize (default UIntPtr.Zero) and MaxStackUsage (default UIntPtr.Zero)
1 parent 1018120 commit 24d5510

File tree

4 files changed

+83
-6
lines changed

4 files changed

+83
-6
lines changed

NuGet/JavaScriptEngineSwitcher.V8/JavaScriptEngineSwitcher.V8.nuspec

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ This package does not contain the native ClearScript and V8 assemblies. Therefor
1717
* JavaScriptEngineSwitcher.V8.Native.win-x86
1818
* JavaScriptEngineSwitcher.V8.Native.win-x64</description>
1919
<summary>JavaScriptEngineSwitcher.V8 contains adapter `V8JsEngine` (wrapper for the Microsoft ClearScript.V8 version of May 13, 2017).</summary>
20-
<releaseNotes>Improved implementation of the `CallFunction` method.</releaseNotes>
20+
<releaseNotes>1. Improved implementation of the `CallFunction` method;
21+
2. Removed unnecessary locks from the `V8JsEngine` class;
22+
3. In configuration settings of the V8 JS engine was added 3 new properties: `HeapSizeSampleInterval` (default `TimeSpan.Zero`), `MaxHeapSize` (default `UIntPtr.Zero`) and `MaxStackUsage` (default `UIntPtr.Zero`).</releaseNotes>
2123
<copyright>Copyright (c) 2013-2018 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
2224
<language>en-US</language>
2325
<tags>JavaScriptEngineSwitcher JavaScript ECMAScript V8 ClearScript</tags>

NuGet/JavaScriptEngineSwitcher.V8/readme.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525
=============
2626
RELEASE NOTES
2727
=============
28-
Improved implementation of the `CallFunction` method.
28+
1. Improved implementation of the `CallFunction` method;
29+
2. Removed unnecessary locks from the `V8JsEngine` class;
30+
3. In configuration settings of the V8 JS engine was added 3 new properties:
31+
`HeapSizeSampleInterval` (default `TimeSpan.Zero`), `MaxHeapSize` (default
32+
`UIntPtr.Zero`) and `MaxStackUsage` (default `UIntPtr.Zero`).
2933

3034
=============
3135
DOCUMENTATION

src/JavaScriptEngineSwitcher.V8/V8JsEngine.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ public V8JsEngine(V8Settings settings)
117117
try
118118
{
119119
_jsEngine = new V8ScriptEngine(constraints, flags, debugPort);
120+
_jsEngine.MaxRuntimeHeapSize = v8Settings.MaxHeapSize;
121+
_jsEngine.RuntimeHeapSizeSampleInterval = v8Settings.HeapSizeSampleInterval;
122+
_jsEngine.MaxRuntimeStackUsage = v8Settings.MaxStackUsage;
120123
}
121124
catch (Exception e)
122125
{

src/JavaScriptEngineSwitcher.V8/V8Settings.cs

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace JavaScriptEngineSwitcher.V8
1+
using System;
2+
3+
namespace JavaScriptEngineSwitcher.V8
24
{
35
/// <summary>
46
/// Settings of the V8 JS engine
@@ -33,6 +35,57 @@ public bool DisableGlobalMembers
3335
set;
3436
}
3537

38+
/// <summary>
39+
/// Gets or sets a minimum time interval between consecutive heap size samples
40+
/// </summary>
41+
/// <remarks>
42+
/// <para>
43+
/// This property is effective only when heap size monitoring is enabled (see
44+
/// <see cref="MaxHeapSize"/> property)
45+
/// </para>
46+
/// </remarks>
47+
public TimeSpan HeapSizeSampleInterval
48+
{
49+
get;
50+
set;
51+
}
52+
53+
/// <summary>
54+
/// Gets or sets a maximum size of the executable code heap in mebibytes
55+
/// </summary>
56+
public int MaxExecutableSize
57+
{
58+
get;
59+
set;
60+
}
61+
62+
/// <summary>
63+
/// Gets or sets a soft limit for the size of the V8 runtime's heap in bytes
64+
/// </summary>
65+
/// <remarks>
66+
/// <para>
67+
/// When it is set to the default value, heap size monitoring is disabled, and
68+
/// scripts with memory leaks or excessive memory usage can cause unrecoverable
69+
/// errors and process termination.
70+
/// </para>
71+
/// <para>
72+
/// A V8 runtime unconditionally terminates the process when it exceeds its resource
73+
/// constraints. This property enables external heap size monitoring that can prevent
74+
/// termination in some scenarios. To be effective, it should be set to a value that
75+
/// is significantly lower than <see cref="MaxOldSpaceSize"/> property. Note that
76+
/// enabling heap size monitoring results in slower script execution.
77+
/// </para>
78+
/// <para>
79+
/// Exceeding this limit causes the V8 runtime to interrupt script execution and throw
80+
/// an exception.
81+
/// </para>
82+
/// </remarks>
83+
public UIntPtr MaxHeapSize
84+
{
85+
get;
86+
set;
87+
}
88+
3689
/// <summary>
3790
/// Gets or sets a maximum size of the new object heap in mebibytes
3891
/// </summary>
@@ -52,9 +105,21 @@ public int MaxOldSpaceSize
52105
}
53106

54107
/// <summary>
55-
/// Gets or sets a maximum size of the executable code heap in mebibytes
108+
/// Gets or sets a maximum amount by which the V8 runtime is permitted to grow
109+
/// the stack during script execution in bytes
56110
/// </summary>
57-
public int MaxExecutableSize
111+
/// <remarks>
112+
/// <para>
113+
/// When it is set to the default value, no stack usage limit is enforced, and
114+
/// scripts with unchecked recursion or other excessive stack usage can cause
115+
/// unrecoverable errors and process termination.
116+
/// </para>
117+
/// <para>
118+
/// Note that the V8 runtime does not monitor stack usage while a host call is in progress.
119+
/// Monitoring is resumed when control returns to the runtime.
120+
/// </para>
121+
/// </remarks>
122+
public UIntPtr MaxStackUsage
58123
{
59124
get;
60125
set;
@@ -69,9 +134,12 @@ public V8Settings()
69134
EnableDebugging = false;
70135
DebugPort = 9222;
71136
DisableGlobalMembers = false;
137+
HeapSizeSampleInterval = TimeSpan.Zero;
138+
MaxExecutableSize = 0;
139+
MaxHeapSize = UIntPtr.Zero;
72140
MaxNewSpaceSize = 0;
73141
MaxOldSpaceSize = 0;
74-
MaxExecutableSize = 0;
142+
MaxStackUsage = UIntPtr.Zero;
75143
}
76144
}
77145
}

0 commit comments

Comments
 (0)