Skip to content

Commit 6f6eb9c

Browse files
committed
In configuration settings of the Jint JS engine was added one new property - CompileRegex (default false)
1 parent 28f0501 commit 6f6eb9c

4 files changed

Lines changed: 39 additions & 4 deletions

File tree

src/JavaScriptEngineSwitcher.Jint/JavaScriptEngineSwitcher.Jint.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
<PackageIconFullPath>../../Icons/JavaScriptEngineSwitcher_Jint_Logo128x128.png</PackageIconFullPath>
2323
<Description>JavaScriptEngineSwitcher.Jint contains a `JintJsEngine` adapter (wrapper for the Jint).</Description>
2424
<PackageTags>$(PackageCommonTags);Jint</PackageTags>
25-
<PackageReleaseNotes>Jint was updated to version 4.8.0.</PackageReleaseNotes>
25+
<PackageReleaseNotes>1. Jint was updated to version 4.8.0;
26+
2. In configuration settings of the Jint JS engine was added one new property - `CompileRegex` (default `false`).</PackageReleaseNotes>
2627
</PropertyGroup>
2728

2829
<ItemGroup>

src/JavaScriptEngineSwitcher.Jint/JintJsEngine.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
using OriginalMemoryLimitExceededException = Jint.Runtime.MemoryLimitExceededException;
2020
using OriginalObjectInstance = Jint.Native.Object.ObjectInstance;
2121
using OriginalParsedScript = Jint.Prepared<Acornima.Ast.Script>;
22+
using OriginalParsingOptions = Jint.ScriptParsingOptions;
23+
using OriginalPreparationOptions = Jint.ScriptPreparationOptions;
2224
using OriginalRecursionDepthOverflowException = Jint.Runtime.RecursionDepthOverflowException;
2325
using OriginalScriptPreparationException = Jint.ScriptPreparationException;
2426
using OriginalStatementsCountOverflowException = Jint.Runtime.StatementsCountOverflowException;
@@ -63,6 +65,11 @@ public sealed class JintJsEngine : JsEngineBase
6365
/// </summary>
6466
private OriginalEngine _jsEngine;
6567

68+
/// <summary>
69+
/// Script preparation options
70+
/// </summary>
71+
private OriginalPreparationOptions _preparationOptions;
72+
6673
/// <summary>
6774
/// Token source for canceling of script execution
6875
/// </summary>
@@ -86,7 +93,7 @@ public sealed class JintJsEngine : JsEngineBase
8693
/// <summary>
8794
/// Flag for whether to allow run the script in strict mode
8895
/// </summary>
89-
private bool _strictMode;
96+
private readonly bool _strictMode;
9097

9198
/// <summary>
9299
/// Synchronizer of script execution
@@ -151,6 +158,14 @@ public JintJsEngine(JintSettings settings)
151158
options.SetTypeResolver(jintSettings.AllowReflection ?
152159
CustomTypeResolvers.AllowingReflection : CustomTypeResolvers.DisallowingReflection);
153160
});
161+
_preparationOptions = new OriginalPreparationOptions
162+
{
163+
ParsingOptions = new OriginalParsingOptions
164+
{
165+
CompileRegex = settings.CompileRegex,
166+
RegexTimeout = settings.RegexTimeoutInterval
167+
}
168+
};
154169
_cancellationConstraint = _jsEngine.Constraints.Find<OriginalCancellationConstraint>();
155170
if (_debuggerBreakCallback is not null)
156171
{
@@ -378,7 +393,8 @@ protected override IPrecompiledScript InnerPrecompile(string code, string docume
378393

379394
try
380395
{
381-
parsedScript = OriginalEngine.PrepareScript(code, uniqueDocumentName, _strictMode);
396+
parsedScript = OriginalEngine.PrepareScript(code, uniqueDocumentName, _strictMode,
397+
_preparationOptions);
382398
}
383399
catch (OriginalException e)
384400
{
@@ -710,6 +726,7 @@ public override void Dispose()
710726
_debuggerStepCallback = null;
711727
_debuggerBreakCallback = null;
712728
_cancellationConstraint = null;
729+
_preparationOptions = null;
713730

714731
if (_cancellationTokenSource is not null)
715732
{

src/JavaScriptEngineSwitcher.Jint/JintSettings.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Text.RegularExpressions;
23

34
using OriginalDebuggerEventHandler = Jint.Runtime.Debugger.DebugHandler.DebugEventHandler;
45

@@ -33,6 +34,19 @@ public bool AllowDebuggerStatement
3334
set { DebuggerStatementHandlingMode = value ? JsDebuggerStatementHandlingMode.Clr : JsDebuggerStatementHandlingMode.Ignore; }
3435
}
3536

37+
/// <summary>
38+
/// Gets or sets a value that determines whether to create compiled instances of regular
39+
/// expressions during script pre-compilation:
40+
/// <c>true</c> - regular expression are compiled by using the <see cref="RegexOptions.Compiled"/> option;
41+
/// <c>false</c> - interpreted .NET regular expression instances are created;
42+
/// <c>null</c> - inherits from the base options of original JS engine.
43+
/// </summary>
44+
public bool? CompileRegex
45+
{
46+
get;
47+
set;
48+
}
49+
3650
/// <summary>
3751
/// Gets or sets a debugger break callback
3852
/// </summary>
@@ -183,6 +197,7 @@ public TimeSpan TimeoutInterval
183197
public JintSettings()
184198
{
185199
AllowReflection = false;
200+
CompileRegex = false;
186201
DebuggerBreakCallback = null;
187202
DebuggerStatementHandlingMode = JsDebuggerStatementHandlingMode.Ignore;
188203
DebuggerStepCallback = null;

src/JavaScriptEngineSwitcher.Jint/readme.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
=============
1818
RELEASE NOTES
1919
=============
20-
Jint was updated to version 4.8.0.
20+
1. Jint was updated to version 4.8.0;
21+
2. In configuration settings of the Jint JS engine was added one new property -
22+
`CompileRegex` (default `false`).
2123

2224
=============
2325
DOCUMENTATION

0 commit comments

Comments
 (0)