From e75235445c6b4951ecdc7141cb238d81e4560907 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6khan=20Kurt?= Date: Sun, 18 Jul 2021 15:48:44 +0300 Subject: [PATCH] Do not throw error immediately when receiving null Regex from Esprima (#931) * enabled working regex test * added parsing tests for [^] and [] regex patterns * don't fail when handling null regex from esprima --- Jint.Tests.Test262/Test262Test.cs | 6 ------ Jint.Tests/Runtime/RegExpTests.cs | 12 ++++++++++++ Jint/Native/RegExp/RegExpConstructor.cs | 6 +++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Jint.Tests.Test262/Test262Test.cs b/Jint.Tests.Test262/Test262Test.cs index 32c4c42249..a830f09157 100644 --- a/Jint.Tests.Test262/Test262Test.cs +++ b/Jint.Tests.Test262/Test262Test.cs @@ -321,12 +321,6 @@ public static IEnumerable 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")) diff --git a/Jint.Tests/Runtime/RegExpTests.cs b/Jint.Tests/Runtime/RegExpTests.cs index c149233c18..a098f43f23 100644 --- a/Jint.Tests/Runtime/RegExpTests.cs +++ b/Jint.Tests/Runtime/RegExpTests.cs @@ -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('[]')")); + } } } diff --git a/Jint/Native/RegExp/RegExpConstructor.cs b/Jint/Native/RegExp/RegExpConstructor.cs index f5f9019ba2..c8b75336cd 100644 --- a/Jint/Native/RegExp/RegExpConstructor.cs +++ b/Jint/Native/RegExp/RegExpConstructor.cs @@ -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) @@ -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 {