Skip to content

Commit 6f34d26

Browse files
committed
In configuration settings of the Jint JS engine was added new property - RegexTimeoutInterval (default null)
1 parent 6aed82f commit 6f34d26

File tree

5 files changed

+74
-13
lines changed

5 files changed

+74
-13
lines changed

src/JavaScriptEngineSwitcher.Jint/JavaScriptEngineSwitcher.Jint.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<PackageTags>JavaScriptEngineSwitcher;JavaScript;ECMAScript;Jint</PackageTags>
1515
<PackageReleaseNotes>1. Jint was updated to version 3.0.0 Beta 1598;
1616
2. No longer supports a .NET Framework 4.0 Client and .NET Standard 1.3;
17-
3. In configuration settings of the Jint JS engine was added one new property - `MemoryLimit` (default `0`).</PackageReleaseNotes>
17+
3. In configuration settings of the Jint JS engine was added two new properties: `MemoryLimit` (default `0`) and `RegexTimeoutInterval` (default `null`).</PackageReleaseNotes>
1818
</PropertyGroup>
1919

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

src/JavaScriptEngineSwitcher.Jint/JintJsEngine.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,22 @@ public JintJsEngine(JintSettings settings)
9090

9191
try
9292
{
93-
_jsEngine = new OriginalEngine(c => c
94-
.AllowDebuggerStatement(jintSettings.AllowDebuggerStatement)
95-
.DebugMode(jintSettings.EnableDebugging)
96-
.LimitMemory(jintSettings.MemoryLimit)
97-
.LimitRecursion(jintSettings.MaxRecursionDepth)
98-
.LocalTimeZone(jintSettings.LocalTimeZone ?? TimeZoneInfo.Local)
99-
.MaxStatements(jintSettings.MaxStatements)
100-
.Strict(jintSettings.StrictMode)
101-
.TimeoutInterval(jintSettings.TimeoutInterval)
102-
);
93+
_jsEngine = new OriginalEngine(options => {
94+
options
95+
.AllowDebuggerStatement(jintSettings.AllowDebuggerStatement)
96+
.DebugMode(jintSettings.EnableDebugging)
97+
.LimitMemory(jintSettings.MemoryLimit)
98+
.LimitRecursion(jintSettings.MaxRecursionDepth)
99+
.LocalTimeZone(jintSettings.LocalTimeZone ?? TimeZoneInfo.Local)
100+
.MaxStatements(jintSettings.MaxStatements)
101+
.Strict(jintSettings.StrictMode)
102+
.TimeoutInterval(jintSettings.TimeoutInterval)
103+
;
104+
if (jintSettings.RegexTimeoutInterval.HasValue)
105+
{
106+
options.RegexTimeoutInterval(jintSettings.RegexTimeoutInterval.Value);
107+
}
108+
});
103109
}
104110
catch (Exception e)
105111
{

src/JavaScriptEngineSwitcher.Jint/JintSettings.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ public long MemoryLimit
6464
set;
6565
}
6666

67+
/// <summary>
68+
/// Gets or sets a timeout interval for regular expressions.
69+
/// If the value of this property is null, then the value of regular expression
70+
/// timeout interval are taken from the <see cref="TimeoutInterval"/> property.
71+
/// </summary>
72+
public TimeSpan? RegexTimeoutInterval
73+
{
74+
get;
75+
set;
76+
}
77+
6778
/// <summary>
6879
/// Gets or sets a flag for whether to allow run the script in strict mode
6980
/// </summary>
@@ -104,6 +115,7 @@ public JintSettings()
104115
MaxRecursionDepth = -1;
105116
MaxStatements = 0;
106117
MemoryLimit = 0;
118+
RegexTimeoutInterval = null;
107119
StrictMode = false;
108120
TimeoutInterval = TimeSpan.Zero;
109121
}

src/JavaScriptEngineSwitcher.Jint/readme.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
=============
2020
1. Jint was updated to version 3.0.0 Beta 1598;
2121
2. No longer supports a .NET Framework 4.0 Client and .NET Standard 1.3;
22-
3. In configuration settings of the Jint JS engine was added one new property -
23-
`MemoryLimit` (default `0`).
22+
3. In configuration settings of the Jint JS engine was added two new properties:
23+
`MemoryLimit` (default `0`) and `RegexTimeoutInterval` (default `null`).
2424

2525
=============
2626
DOCUMENTATION

test/JavaScriptEngineSwitcher.Tests/Jint/CommonTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,49 @@ public void MappingTimeoutErrorDuringExecutionOfCodeIsCorrect()
352352
Assert.Empty(exception.CallStack);
353353
}
354354

355+
[Fact]
356+
public void MappingTimeoutErrorDuringRegexHangingIsCorrect()
357+
{
358+
// Arrange
359+
const string input = @"var regexp = /^(\w+\s?)*$/,
360+
str = 'An input string that takes a long time or even makes this regular expression to hang!'
361+
;
362+
363+
// Will take a very long time
364+
regexp.test(str);";
365+
366+
JsTimeoutException exception = null;
367+
368+
// Act
369+
using (var jsEngine = new JintJsEngine(
370+
new JintSettings
371+
{
372+
RegexTimeoutInterval = TimeSpan.FromMilliseconds(25)
373+
}
374+
))
375+
{
376+
try
377+
{
378+
jsEngine.Execute(input, "regexp-hanging.js");
379+
}
380+
catch (JsTimeoutException e)
381+
{
382+
exception = e;
383+
}
384+
}
385+
386+
// Assert
387+
Assert.NotNull(exception);
388+
Assert.Equal("Timeout error", exception.Category);
389+
Assert.Equal("Script execution exceeded timeout.", exception.Description);
390+
Assert.Empty(exception.Type);
391+
Assert.Empty(exception.DocumentName);
392+
Assert.Equal(0, exception.LineNumber);
393+
Assert.Equal(0, exception.ColumnNumber);
394+
Assert.Empty(exception.SourceFragment);
395+
Assert.Empty(exception.CallStack);
396+
}
397+
355398
#endregion
356399

357400
#region Generation of error messages

0 commit comments

Comments
 (0)