Skip to content

Commit 171d56b

Browse files
committed
In JavaScriptEngineSwitcher.ChakraCore in configuration settings of the ChakraCore JS engine was added one new property - MaxStackSize
1 parent 4a6a6c6 commit 171d56b

File tree

8 files changed

+75
-33
lines changed

8 files changed

+75
-33
lines changed

src/JavaScriptEngineSwitcher.ChakraCore/ChakraCoreJsEngine.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public sealed class ChakraCoreJsEngine : JsEngineBase
9393
/// <summary>
9494
/// Script dispatcher
9595
/// </summary>
96-
private readonly ScriptDispatcher _dispatcher = new ScriptDispatcher();
96+
private readonly ScriptDispatcher _dispatcher;
9797

9898
/// <summary>
9999
/// Unique document name manager
@@ -155,6 +155,7 @@ public ChakraCoreJsEngine(ChakraCoreSettings settings)
155155
attributes |= JsRuntimeAttributes.EnableExperimentalFeatures;
156156
}
157157

158+
_dispatcher = new ScriptDispatcher(chakraCoreSettings.MaxStackSize);
158159
_externalObjectFinalizeCallback = ExternalObjectFinalizeCallback;
159160
_promiseContinuationCallback = PromiseContinuationCallback;
160161

src/JavaScriptEngineSwitcher.ChakraCore/ChakraCoreSettings.cs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,30 @@
22

33
using JavaScriptEngineSwitcher.Core.Utilities;
44

5+
using JavaScriptEngineSwitcher.ChakraCore.Resources;
6+
57
namespace JavaScriptEngineSwitcher.ChakraCore
68
{
79
/// <summary>
810
/// Settings of the ChakraCore JS engine
911
/// </summary>
1012
public sealed class ChakraCoreSettings
1113
{
14+
/// <summary>
15+
/// The stack size is sufficient to run the code of modern JavaScript libraries in 32-bit process
16+
/// </summary>
17+
const int STACK_SIZE_32 = 492 * 1024; // like 32-bit Node.js
18+
19+
/// <summary>
20+
/// The stack size is sufficient to run the code of modern JavaScript libraries in 64-bit process
21+
/// </summary>
22+
const int STACK_SIZE_64 = 984 * 1024; // like 64-bit Node.js
23+
24+
/// <summary>
25+
/// The maximum stack size in bytes
26+
/// </summary>
27+
private int _maxStackSize;
28+
1229
/// <summary>
1330
/// Gets or sets a flag for whether to disable any background work (such as garbage collection)
1431
/// on background threads
@@ -55,6 +72,31 @@ public bool EnableExperimentalFeatures
5572
set;
5673
}
5774

75+
/// <summary>
76+
/// Gets or sets a maximum stack size in bytes
77+
/// </summary>
78+
/// <remarks>
79+
/// <para>Set a <code>0</code> to use the default maximum stack size specified in the header
80+
/// for the executable.
81+
/// </para>
82+
/// </remarks>
83+
public int MaxStackSize
84+
{
85+
get { return _maxStackSize; }
86+
set
87+
{
88+
if (value < 0)
89+
{
90+
throw new ArgumentOutOfRangeException(
91+
nameof(value),
92+
Strings.Engine_MaxStackSizeMustBeNonNegative
93+
);
94+
}
95+
96+
_maxStackSize = value;
97+
}
98+
}
99+
58100
/// <summary>
59101
/// Gets or sets a current memory limit for a runtime in bytes
60102
/// </summary>
@@ -70,13 +112,15 @@ public UIntPtr MemoryLimit
70112
/// </summary>
71113
public ChakraCoreSettings()
72114
{
115+
bool is64BitProcess = Utils.Is64BitProcess();
116+
73117
DisableBackgroundWork = false;
74118
DisableEval = false;
75119
DisableFatalOnOOM = false;
76120
DisableNativeCodeGeneration = false;
77121
EnableExperimentalFeatures = false;
78-
MemoryLimit = Utils.Is64BitProcess() ?
79-
new UIntPtr(ulong.MaxValue) : new UIntPtr(uint.MaxValue);
122+
MaxStackSize = is64BitProcess ? STACK_SIZE_64 : STACK_SIZE_32;
123+
MemoryLimit = is64BitProcess ? new UIntPtr(ulong.MaxValue) : new UIntPtr(uint.MaxValue);
80124
}
81125
}
82126
}

src/JavaScriptEngineSwitcher.ChakraCore/JavaScriptEngineSwitcher.ChakraCore.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ This package does not contain the native implementations of ChakraCore. Therefor
2222
* JavaScriptEngineSwitcher.ChakraCore.Native.osx-x64</Description>
2323
<PackageIconUrl>https://raw.githubusercontent.com/Taritsyn/JavaScriptEngineSwitcher/master/Icons/JavaScriptEngineSwitcher_ChakraCore_Logo128x128.png</PackageIconUrl>
2424
<PackageTags>JavaScriptEngineSwitcher;JavaScript;ECMAScript;ChakraCore</PackageTags>
25-
<PackageReleaseNotes>1. ChakraCore was updated to version 1.7.5;
26-
2. In configuration settings of the ChakraCore JS engine was added two new properties - `MemoryLimit` and `DisableFatalOnOOM` (default `false`);
27-
3. Now during calling of the `CollectGarbage` method is no longer performed blocking.</PackageReleaseNotes>
25+
<PackageReleaseNotes>In configuration settings of the ChakraCore JS engine was added one new property - `MaxStackSize` (default `492` or `984` KB).</PackageReleaseNotes>
2826
</PropertyGroup>
2927

3028
<Import Project="../../build/common.props" />

src/JavaScriptEngineSwitcher.ChakraCore/Resources/Strings.Designer.cs

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/JavaScriptEngineSwitcher.ChakraCore/Resources/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,7 @@
183183
<data name="Engine_VcRedist2015InstallationRequired" xml:space="preserve">
184184
<value>In addition, you still need to install the Microsoft Visual C++ 2015 Redistributable (https://www.microsoft.com/en-us/download/details.aspx?id=53840).</value>
185185
</data>
186+
<data name="Engine_MaxStackSizeMustBeNonNegative" xml:space="preserve">
187+
<value>Maximum stack size must be non-negative.</value>
188+
</data>
186189
</root>

src/JavaScriptEngineSwitcher.ChakraCore/Resources/Strings.ru-ru.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,7 @@
183183
<data name="Engine_VcRedist2015InstallationRequired" xml:space="preserve">
184184
<value>Кроме того, еще требуется установка распространяемого компонента Microsoft Visual C++ 2015 (https://www.microsoft.com/ru-ru/download/details.aspx?id=53840).</value>
185185
</data>
186+
<data name="Engine_MaxStackSizeMustBeNonNegative" xml:space="preserve">
187+
<value>Максимальный размер стека должен быть неотрицательным!</value>
188+
</data>
186189
</root>

src/JavaScriptEngineSwitcher.ChakraCore/ScriptDispatcher.cs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,12 @@
1414
namespace JavaScriptEngineSwitcher.ChakraCore
1515
{
1616
/// <summary>
17-
/// Provides services for managing the queue of script tasks on the thread with increased stack size
17+
/// Provides services for managing the queue of script tasks on the thread with modified stack size
1818
/// </summary>
1919
internal sealed class ScriptDispatcher : IDisposable
2020
{
21-
#if !NETSTANDARD1_3
2221
/// <summary>
23-
/// The stack size is sufficient to run the code of modern JavaScript libraries in 32-bit process
24-
/// </summary>
25-
const int STACK_SIZE_32 = 492 * 1024; // like 32-bit Node.js
26-
27-
/// <summary>
28-
/// The stack size is sufficient to run the code of modern JavaScript libraries in 64-bit process
29-
/// </summary>
30-
const int STACK_SIZE_64 = 984 * 1024; // like 64-bit Node.js
31-
32-
#endif
33-
/// <summary>
34-
/// The thread with increased stack size
22+
/// The thread with modified stack size
3523
/// </summary>
3624
private Thread _thread;
3725

@@ -59,14 +47,14 @@ internal sealed class ScriptDispatcher : IDisposable
5947
/// <summary>
6048
/// Constructs an instance of script dispatcher
6149
/// </summary>
62-
public ScriptDispatcher()
50+
/// <param name="maxStackSize">The maximum stack size, in bytes, to be used by the thread,
51+
/// or 0 to use the default maximum stack size specified in the header for the executable.</param>
52+
public ScriptDispatcher(int maxStackSize)
6353
{
6454
#if NETSTANDARD1_3
6555
_thread = new Thread(StartThread)
6656
#else
67-
int sufficientStackSize = Utils.Is64BitProcess() ? STACK_SIZE_64 : STACK_SIZE_32;
68-
69-
_thread = new Thread(StartThread, sufficientStackSize)
57+
_thread = new Thread(StartThread, maxStackSize)
7058
#endif
7159
{
7260
IsBackground = true
@@ -85,7 +73,7 @@ private void VerifyNotDisposed()
8573
}
8674

8775
/// <summary>
88-
/// Starts a thread with increased stack size.
76+
/// Starts a thread with modified stack size.
8977
/// Loops forever, processing script tasks from the queue.
9078
/// </summary>
9179
private void StartThread()
@@ -141,7 +129,7 @@ private void EnqueueTask(ScriptTask task)
141129
}
142130

143131
/// <summary>
144-
/// Runs a specified delegate on the thread with increased stack size,
132+
/// Runs a specified delegate on the thread with modified stack size,
145133
/// and returns its result as an <see cref="System.Object"/>.
146134
/// Blocks until the invocation of delegate is completed.
147135
/// </summary>
@@ -176,7 +164,7 @@ private object InnnerInvoke(Func<object> del)
176164
}
177165

178166
/// <summary>
179-
/// Runs a specified delegate on the thread with increased stack size,
167+
/// Runs a specified delegate on the thread with modified stack size,
180168
/// and returns its result as an <typeparamref name="T" />.
181169
/// Blocks until the invocation of delegate is completed.
182170
/// </summary>
@@ -197,7 +185,7 @@ public T Invoke<T>(Func<T> func)
197185
}
198186

199187
/// <summary>
200-
/// Runs a specified delegate on the thread with increased stack size.
188+
/// Runs a specified delegate on the thread with modified stack size.
201189
/// Blocks until the invocation of delegate is completed.
202190
/// </summary>
203191
/// <param name="action">Delegate to invocation</param>

src/JavaScriptEngineSwitcher.ChakraCore/readme.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@
3030
=============
3131
RELEASE NOTES
3232
=============
33-
1. ChakraCore was updated to version 1.7.5;
34-
2. In configuration settings of the ChakraCore JS engine was added two new
35-
properties - `MemoryLimit` and `DisableFatalOnOOM` (default `false`);
36-
3. Now during calling of the `CollectGarbage` method is no longer performed
37-
blocking.
33+
In configuration settings of the ChakraCore JS engine was added one new
34+
property - `MaxStackSize` (default `492` or `984` KB).
3835

3936
=============
4037
DOCUMENTATION

0 commit comments

Comments
 (0)