Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Jint.Tests/Runtime/RegExpTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using System.Text.RegularExpressions;
using Acornima;
using Acornima.Ast;
using Jint.Native;
using Jint.Native.Function;
using Jint.Native.Object;
using Jint.Runtime.Descriptors;

namespace Jint.Tests.Runtime;

Expand Down Expand Up @@ -139,4 +144,50 @@ public void Issue506()
var result = engine.Evaluate("/[^]?(:[rp][el]a[\\w-]+)[^]/.test(':reagent-')").AsBoolean();
Assert.True(result);
}

[Fact]
public void RegExpLiteralInGetterShouldWork()
{
// Reproduces issue where using RegExp literal notation in getters parsed via Parser().ParseExpression()
// resulted in NullReferenceException due to uncompiled regex
var engine = new Engine();
var obj = new JsObject(engine);

// This should not throw NullReferenceException
var getterFunction = (IFunction)new Parser().ParseExpression(
"function() { return 'test'.match(/[a-z]/)?.[0]; }"
);
var getter = new ScriptFunction(engine, getterFunction, true);

obj.FastSetProperty("name", new GetSetPropertyDescriptor(getter, null, true, false));
engine.SetValue("customObj", obj);

engine.Execute(string.Empty);
var result = engine.GetValue("customObj").AsObject();
var name = result.Get("name").ToString();

Assert.Equal("t", name);
}

[Fact]
public void RegExpLiteralInGetterWorksWithConstructorToo()
{
// Ensure the alternative RegExp constructor approach continues to work
var engine = new Engine();
var obj = new JsObject(engine);

var getterFunction = (IFunction)new Parser().ParseExpression(
"function() { return 'test'.match(new RegExp('[a-z]'))?.[0]; }"
);
var getter = new ScriptFunction(engine, getterFunction, true);

obj.FastSetProperty("name", new GetSetPropertyDescriptor(getter, null, true, false));
engine.SetValue("customObj", obj);

engine.Execute(string.Empty);
var result = engine.GetValue("customObj").AsObject();
var name = result.Get("name").ToString();

Assert.Equal("t", name);
}
}
23 changes: 22 additions & 1 deletion Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,28 @@
var regExpParseResult = regExpLiteral.ParseResult;
if (regExpParseResult.Success)
{
var regex = regExpLiteral.UserData as Regex ?? regExpParseResult.Regex!;
var regex = regExpLiteral.UserData as Regex ?? regExpParseResult.Regex;
if (regex is null)
{
// Regex was not compiled during parsing, compile it now
var parserOptions = context.Engine.GetActiveParserOptions();
var compiledResult = Tokenizer.AdaptRegExp(
regExpLiteral.RegExp.Pattern,

Check failure on line 77 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux - ARM

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 77 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux - ARM

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 77 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux - ARM

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 77 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 77 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 77 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 77 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / macos

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 77 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / macos

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 77 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / macos

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
regExpLiteral.RegExp.Flags,

Check failure on line 78 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux - ARM

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 78 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux - ARM

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 78 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux - ARM

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 78 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 78 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 78 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 78 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / macos

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 78 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / macos

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 78 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / macos

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
compiled: false,

Check failure on line 79 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux - ARM

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 79 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux - ARM

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 79 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 79 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 79 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / macos

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 79 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / macos

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
parserOptions.RegexTimeout,
ecmaVersion: parserOptions.EcmaVersion,
experimentalESFeatures: parserOptions.ExperimentalESFeatures);

if (!compiledResult.Success)
{
Throw.SyntaxError(context.Engine.Realm, $"Unsupported regular expression. {compiledResult.ConversionError!.Description}");
}

regex = compiledResult.Regex!;
regExpParseResult = compiledResult;
}

Check failure on line 92 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux - ARM

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 92 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux - ARM

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 92 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 92 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / linux

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 92 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / macos

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 92 in Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs

View workflow job for this annotation

GitHub Actions / macos

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
return context.Engine.Realm.Intrinsics.RegExp.Construct(regex, regExpLiteral.RegExp.Pattern, regExpLiteral.RegExp.Flags, regExpParseResult);
}

Expand Down
Loading