Browse Source

Fixing global regex literals cache

Fixes #364
Sebastien Ros 8 years ago
parent
commit
c3284a58bf
2 changed files with 25 additions and 1 deletions
  1. 16 0
      Jint.Tests/Runtime/EngineTests.cs
  2. 9 1
      Jint/Runtime/ExpressionIntepreter.cs

+ 16 - 0
Jint.Tests/Runtime/EngineTests.cs

@@ -1847,5 +1847,21 @@ namespace Jint.Tests.Runtime
                 Assert.Equal(3, ex.LineNumber);
             }
         }
+
+		[Fact]
+		public void GlobalRegexLiteralShouldNotKeepState()
+		{
+			RunTest(@"
+				var url = 'https://www.example.com';
+
+				assert(isAbsolutePath(url));
+				assert(isAbsolutePath(url));
+				assert(isAbsolutePath(url));
+
+				function isAbsolutePath(path) {
+					return /\.+/g.test(path);
+				}
+            ");
+		}
     }
 }

+ 9 - 1
Jint/Runtime/ExpressionIntepreter.cs

@@ -632,7 +632,15 @@ namespace Jint.Runtime
 
             if (literal.Type == SyntaxNodes.RegularExpressionLiteral)
             {
-                literal.CachedValue = _engine.RegExp.Construct(literal.Raw);
+				var regexp = _engine.RegExp.Construct(literal.Raw);
+
+				if (regexp.Global)
+				{
+					// A Global regexp literal can't be cached or its state would evolve
+					return regexp;
+				}
+
+				literal.CachedValue = regexp;
             }
             else
             {