Skip to content

Commit

Permalink
Module support fixes and improvements (#1102)
Browse files Browse the repository at this point in the history
Co-authored-by: Marko Lahma <marko.lahma@gmail.com>
  • Loading branch information
christianrondeau and lahma committed Mar 9, 2022
1 parent 5dc5033 commit 6098a14
Show file tree
Hide file tree
Showing 34 changed files with 1,638 additions and 1,260 deletions.
51 changes: 0 additions & 51 deletions Jint.Tests.Test262/Language/ModuleTestHost.cs

This file was deleted.

49 changes: 3 additions & 46 deletions Jint.Tests.Test262/Language/ModuleTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using Jint.Runtime;
using Jint.Runtime.Modules;
using System;
using System.IO;
using System.Reflection;
using Xunit;
using Xunit.Sdk;

namespace Jint.Tests.Test262.Language;

Expand All @@ -15,59 +9,22 @@ public class ModuleTests : Test262Test
[MemberData(nameof(SourceFiles), "language\\module-code", true, Skip = "Skipped")]
protected void ModuleCode(SourceFile sourceFile)
{
RunModuleTest(sourceFile);
RunTestInternal(sourceFile);
}

[Theory(DisplayName = "language\\export")]
[MemberData(nameof(SourceFiles), "language\\export", false)]
[MemberData(nameof(SourceFiles), "language\\export", true, Skip = "Skipped")]
protected void Export(SourceFile sourceFile)
{
RunModuleTest(sourceFile);
RunTestInternal(sourceFile);
}

[Theory(DisplayName = "language\\import")]
[MemberData(nameof(SourceFiles), "language\\import", false)]
[MemberData(nameof(SourceFiles), "language\\import", true, Skip = "Skipped")]
protected void Import(SourceFile sourceFile)
{
RunModuleTest(sourceFile);
}

private static void RunModuleTest(SourceFile sourceFile)
{
if (sourceFile.Skip)
{
return;
}

var code = sourceFile.Code;

var options = new Options();
options.Host.Factory = _ => new ModuleTestHost();
options.EnableModules(Path.Combine(BasePath, "test"));

var engine = new Engine(options);

var negative = code.IndexOf("negative:", StringComparison.OrdinalIgnoreCase) != -1;
string lastError = null;

try
{
engine.LoadModule(sourceFile.FullPath);
}
catch (JavaScriptException ex)
{
lastError = ex.ToString();
}
catch (Exception ex)
{
lastError = ex.ToString();
}

if (!negative && !string.IsNullOrWhiteSpace(lastError))
{
throw new XunitException(lastError);
}
RunTestInternal(sourceFile);
}
}
4 changes: 2 additions & 2 deletions Jint.Tests.Test262/SingleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public void TestSingle()

if (code.IndexOf("onlyStrict", StringComparison.Ordinal) < 0)
{
RunTestCode(sourceFile.Source, code, strict: false);
RunTestCode(sourceFile.Source, code, strict: false, null);
}

if (code.IndexOf("noStrict", StringComparison.Ordinal) < 0)
{
RunTestCode(sourceFile.Source, code, strict: true);
RunTestCode(sourceFile.Source, code, strict: true, null);
}
}
}
Expand Down
35 changes: 26 additions & 9 deletions Jint.Tests.Test262/Test262Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public abstract class Test262Test

private static readonly HashSet<string> _strictSkips = new(StringComparer.OrdinalIgnoreCase);

private static readonly Regex _moduleFlagRegex = new Regex(@"flags:\s*?\[.*?module.*?]", RegexOptions.Compiled);

static Test262Test()
{
//NOTE: The Date tests in test262 assume the local timezone is Pacific Standard Time
Expand Down Expand Up @@ -96,12 +98,19 @@ static Test262Test()
}
}

protected void RunTestCode(string fileName, string code, bool strict)
protected void RunTestCode(string fileName, string code, bool strict, string fullPath)
{
var engine = new Engine(cfg => cfg
.LocalTimeZone(_pacificTimeZone)
.Strict(strict)
);
var module = _moduleFlagRegex.IsMatch(code);

var engine = new Engine(cfg =>
{
cfg.LocalTimeZone(_pacificTimeZone);
cfg.Strict(strict);
if (module)
{
cfg.EnableModules(Path.Combine(BasePath, "test", Path.GetDirectoryName(fullPath)!));
}
});

engine.Execute(Sources["sta.js"]);
engine.Execute(Sources["assert.js"]);
Expand Down Expand Up @@ -162,7 +171,15 @@ protected void RunTestCode(string fileName, string code, bool strict)
bool negative = code.IndexOf("negative:", StringComparison.Ordinal) > -1;
try
{
engine.Execute(new JavaScriptParser(code, new ParserOptions(fileName)).ParseScript());
if (module)
{
engine.AddModule(fullPath, builder => builder.AddSource(code));
engine.ImportModule(fullPath);
}
else
{
engine.Execute(new JavaScriptParser(code, new ParserOptions(fileName)).ParseScript());
}
}
catch (JavaScriptException j)
{
Expand All @@ -186,15 +203,15 @@ protected void RunTestInternal(SourceFile sourceFile)
return;
}

if (sourceFile.Code.IndexOf("onlyStrict", StringComparison.Ordinal) < 0)
if (sourceFile.Code.IndexOf("onlyStrict", StringComparison.Ordinal) < 0 && !_moduleFlagRegex.IsMatch(sourceFile.Code))
{
RunTestCode(sourceFile.Source, sourceFile.Code, strict: false);
RunTestCode(sourceFile.Source, sourceFile.Code, strict: false, fullPath: sourceFile.FullPath);
}

if (!_strictSkips.Contains(sourceFile.Source)
&& sourceFile.Code.IndexOf("noStrict", StringComparison.Ordinal) < 0)
{
RunTestCode(sourceFile.Source, sourceFile.Code, strict: true);
RunTestCode(sourceFile.Source, sourceFile.Code, strict: true, fullPath: sourceFile.FullPath);
}
}

Expand Down
8 changes: 8 additions & 0 deletions Jint.Tests.Test262/test/skipped.json
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,14 @@
"source": "language/statements/class/subclass/builtin-objects/GeneratorFunction/regular-subclassing.js",
"reason": "generators not implemented"
},
{
"source": "language/module-code/instn-local-bndng-export-gen.js",
"reason": "generators not implemented"
},
{
"source": "language/module-code/instn-local-bndng-gen.js",
"reason": "generators not implemented"
},

// Eval problems

Expand Down

0 comments on commit 6098a14

Please sign in to comment.