Skip to content

Commit

Permalink
Do not throw error immediately when receiving null Regex from Esprima (
Browse files Browse the repository at this point in the history
…#931)

* enabled working regex test
* added parsing tests for [^] and [] regex patterns
* don't fail when handling null regex from esprima
  • Loading branch information
KurtGokhan committed Jul 18, 2021
1 parent e0589fe commit e752354
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
6 changes: 0 additions & 6 deletions Jint.Tests.Test262/Test262Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,6 @@ public static IEnumerable<object[]> SourceFiles(string pathPrefix, bool skipped)
reason = "Unicode support and its special cases need more work";
}

if (name.StartsWith("built-ins/RegExp/CharacterClassEscapes/"))
{
skip = true;
reason = "for-of not implemented";
}

// Promises
if (name.StartsWith("built-ins/Promise/allSettled") ||
name.StartsWith("built-ins/Promise/any"))
Expand Down
12 changes: 12 additions & 0 deletions Jint.Tests/Runtime/RegExpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,17 @@ public void ToStringWithRealRegExpInstance()

Assert.Equal("/test/g", result);
}

[Fact]
public void ShouldNotThrowErrorOnIncompatibleRegex()
{
var engine = new Engine();
Assert.NotNull(engine.Evaluate(@"/[^]*?(:[rp][el]a[\w-]+)[^]*/"));
Assert.NotNull(engine.Evaluate("/[^]a/"));
Assert.NotNull(engine.Evaluate("new RegExp('[^]a')"));

Assert.NotNull(engine.Evaluate("/[]/"));
Assert.NotNull(engine.Evaluate("new RegExp('[]')"));
}
}
}
6 changes: 3 additions & 3 deletions Jint/Native/RegExp/RegExpConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private ObjectInstance RegExpInitialize(RegExpInstance r, JsValue pattern, JsVal
var timeout = _engine.Options._RegexTimeoutInterval;
if (timeout.Ticks > 0)
{
r.Value = new Regex(r.Value.ToString(), r.Value.Options, timeout);
r.Value = r.Value != null ? new Regex(r.Value.ToString(), r.Value.Options, timeout) : null;
}
}
catch (Exception ex)
Expand Down Expand Up @@ -142,12 +142,12 @@ public RegExpInstance Construct(Regex regExp, string flags)
r._prototype = PrototypeObject;

r.Flags = flags;
r.Source = regExp.ToString();
r.Source = regExp?.ToString();

var timeout = _engine.Options._RegexTimeoutInterval;
if (timeout.Ticks > 0)
{
r.Value = new Regex(regExp.ToString(), regExp.Options, timeout);
r.Value = regExp != null ? new Regex(regExp.ToString(), regExp.Options, timeout) : null;
}
else
{
Expand Down

0 comments on commit e752354

Please sign in to comment.