File tree Expand file tree Collapse file tree 5 files changed +74
-13
lines changed
src/JavaScriptEngineSwitcher.Jint
test/JavaScriptEngineSwitcher.Tests/Jint Expand file tree Collapse file tree 5 files changed +74
-13
lines changed Original file line number Diff line number Diff line change 14
14
<PackageTags >JavaScriptEngineSwitcher;JavaScript;ECMAScript;Jint</PackageTags >
15
15
<PackageReleaseNotes >1. Jint was updated to version 3.0.0 Beta 1598;
16
16
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 >
18
18
</PropertyGroup >
19
19
20
20
<Import Project =" ../../build/common.props" />
Original file line number Diff line number Diff line change @@ -90,16 +90,22 @@ public JintJsEngine(JintSettings settings)
90
90
91
91
try
92
92
{
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
+ } ) ;
103
109
}
104
110
catch ( Exception e )
105
111
{
Original file line number Diff line number Diff line change @@ -64,6 +64,17 @@ public long MemoryLimit
64
64
set ;
65
65
}
66
66
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
+
67
78
/// <summary>
68
79
/// Gets or sets a flag for whether to allow run the script in strict mode
69
80
/// </summary>
@@ -104,6 +115,7 @@ public JintSettings()
104
115
MaxRecursionDepth = - 1 ;
105
116
MaxStatements = 0 ;
106
117
MemoryLimit = 0 ;
118
+ RegexTimeoutInterval = null ;
107
119
StrictMode = false ;
108
120
TimeoutInterval = TimeSpan . Zero ;
109
121
}
Original file line number Diff line number Diff line change 19
19
=============
20
20
1. Jint was updated to version 3.0.0 Beta 1598;
21
21
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`) .
24
24
25
25
=============
26
26
DOCUMENTATION
Original file line number Diff line number Diff line change @@ -352,6 +352,49 @@ public void MappingTimeoutErrorDuringExecutionOfCodeIsCorrect()
352
352
Assert . Empty ( exception . CallStack ) ;
353
353
}
354
354
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
+
355
398
#endregion
356
399
357
400
#region Generation of error messages
You can’t perform that action at this time.
0 commit comments