Browse Source

Parse scripts only once while testing (#910)

Marko Lahma 4 years ago
parent
commit
3b33cb1135
3 changed files with 20 additions and 15 deletions
  1. 5 2
      Jint.Tests.Ecma/EcmaTest.cs
  2. 2 2
      Jint.Tests.Test262/SingleTest.cs
  3. 13 11
      Jint.Tests.Test262/Test262Test.cs

+ 5 - 2
Jint.Tests.Ecma/EcmaTest.cs

@@ -2,6 +2,8 @@
 using System.Collections.Generic;
 using System.IO;
 using System.Reflection;
+using Esprima;
+using Esprima.Ast;
 using Jint.Runtime;
 using Newtonsoft.Json.Linq;
 using Xunit;
@@ -153,7 +155,7 @@ namespace Jint.Tests.Ecma
     public abstract class EcmaTest
     {
         private static string _lastError;
-        private static string staSource;
+        private static Script staSource;
         private static readonly string BasePath;
         private static readonly List<SourceFile> _sourceFiles = new List<SourceFile>(10_000);
         private static readonly TimeZoneInfo _pacificTimeZone;
@@ -216,7 +218,8 @@ namespace Jint.Tests.Ecma
             if (staSource == null)
             {
                 var driverFilename = Path.Combine(BasePath, "TestCases/sta.js");
-                staSource = File.ReadAllText(driverFilename);
+                var source = File.ReadAllText(driverFilename);
+                staSource = new JavaScriptParser(source, new ParserOptions("sta.js")).ParseScript();
             }
 
             engine.Execute(staSource);

+ 2 - 2
Jint.Tests.Test262/SingleTest.cs

@@ -34,12 +34,12 @@ namespace Jint.Tests.Test262
 
             if (code.IndexOf("onlyStrict", StringComparison.Ordinal) < 0)
             {
-                RunTestCode(code, strict: false);
+                RunTestCode(sourceFile.Source, code, strict: false);
             }
 
             if (code.IndexOf("noStrict", StringComparison.Ordinal) < 0)
             {
-                RunTestCode(code, strict: true);
+                RunTestCode(sourceFile.Source, code, strict: true);
             }
         }
     }

+ 13 - 11
Jint.Tests.Test262/Test262Test.cs

@@ -6,6 +6,7 @@ using System.Linq;
 using System.Reflection;
 using System.Text.RegularExpressions;
 using Esprima;
+using Esprima.Ast;
 using Jint.Runtime;
 using Jint.Runtime.Descriptors;
 using Jint.Runtime.Interop;
@@ -17,7 +18,7 @@ namespace Jint.Tests.Test262
 {
     public abstract class Test262Test
     {
-        private static readonly Dictionary<string, string> Sources;
+        private static readonly Dictionary<string, Script> Sources;
 
         private static readonly string BasePath;
 
@@ -69,10 +70,11 @@ namespace Jint.Tests.Test262
                 "fnGlobalObject.js"
             };
 
-            Sources = new Dictionary<string, string>(files.Length);
+            Sources = new Dictionary<string, Script>(files.Length);
             for (var i = 0; i < files.Length; i++)
             {
-                Sources[files[i]] = File.ReadAllText(Path.Combine(BasePath, "harness", files[i]));
+                var source = File.ReadAllText(Path.Combine(BasePath, "harness", files[i]));
+                Sources[files[i]] = new JavaScriptParser(source, new ParserOptions(files[i])).ParseScript();
             }
 
             var content = File.ReadAllText(Path.Combine(BasePath, "test/skipped.json"));
@@ -88,15 +90,15 @@ namespace Jint.Tests.Test262
             }
         }
 
-        protected void RunTestCode(string code, bool strict)
+        protected void RunTestCode(string fileName, string code, bool strict)
         {
             var engine = new Engine(cfg => cfg
                 .LocalTimeZone(_pacificTimeZone)
                 .Strict(strict)
             );
 
-            engine.Execute(Sources["sta.js"], CreateParserOptions("sta.js"));
-            engine.Execute(Sources["assert.js"], CreateParserOptions("assert.js"));
+            engine.Execute(Sources["sta.js"]);
+            engine.Execute(Sources["assert.js"]);
             engine.SetValue("print",
                 new ClrFunctionInstance(engine, "print", (thisObj, args) => TypeConverter.ToString(args.At(0))));
 
@@ -123,13 +125,13 @@ namespace Jint.Tests.Test262
                 var files = includes.Groups[1].Captures[0].Value.Split(',');
                 foreach (var file in files)
                 {
-                    engine.Execute(Sources[file.Trim()], CreateParserOptions(file.Trim()));
+                    engine.Execute(Sources[file.Trim()]);
                 }
             }
 
             if (code.IndexOf("propertyHelper.js", StringComparison.OrdinalIgnoreCase) != -1)
             {
-                engine.Execute(Sources["propertyHelper.js"], CreateParserOptions("propertyHelper.js"));
+                engine.Execute(Sources["propertyHelper.js"]);
             }
 
             string lastError = null;
@@ -137,7 +139,7 @@ namespace Jint.Tests.Test262
             bool negative = code.IndexOf("negative:", StringComparison.Ordinal) > -1;
             try
             {
-                engine.Execute(code);
+                engine.Execute(new JavaScriptParser(code, new ParserOptions(fileName)).ParseScript());
             }
             catch (JavaScriptException j)
             {
@@ -163,13 +165,13 @@ namespace Jint.Tests.Test262
 
             if (sourceFile.Code.IndexOf("onlyStrict", StringComparison.Ordinal) < 0)
             {
-                RunTestCode(sourceFile.Code, strict: false);
+                RunTestCode(sourceFile.Source, sourceFile.Code, strict: false);
             }
 
             if (!_strictSkips.Contains(sourceFile.Source)
                 && sourceFile.Code.IndexOf("noStrict", StringComparison.Ordinal) < 0)
             {
-                RunTestCode(sourceFile.Code, strict: true);
+                RunTestCode(sourceFile.Source, sourceFile.Code, strict: true);
             }
         }