-
-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: lph <philipp.luethi@sva-ag.ch> Co-authored-by: Marko Lahma <marko.lahma@gmail.com> Co-authored-by: Sébastien Ros <sebastienros@gmail.com>
- Loading branch information
1 parent
9d84326
commit 0c223f4
Showing
22 changed files
with
1,860 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.IO; | ||
using Jint.Native; | ||
using Jint.Native.Object; | ||
using Jint.Runtime; | ||
using Jint.Runtime.Interop; | ||
|
||
namespace Jint.Tests.Test262.Language | ||
{ | ||
// Hacky way to get objects from assert.js and sta.js into the module context | ||
internal sealed class ModuleTestHost : Host | ||
{ | ||
private readonly static Dictionary<string, JsValue> _staticValues = new(); | ||
|
||
static ModuleTestHost() | ||
{ | ||
var assemblyPath = new Uri(typeof(ModuleTestHost).GetTypeInfo().Assembly.Location).LocalPath; | ||
var assemblyDirectory = new FileInfo(assemblyPath).Directory; | ||
|
||
var basePath = assemblyDirectory.Parent.Parent.Parent.FullName; | ||
|
||
var engine = new Engine(); | ||
var assertSource = File.ReadAllText(Path.Combine(basePath, "harness", "assert.js")); | ||
var staSource = File.ReadAllText(Path.Combine(basePath, "harness", "sta.js")); | ||
|
||
engine.Execute(assertSource); | ||
engine.Execute(staSource); | ||
|
||
_staticValues["assert"] = engine.GetValue("assert"); | ||
_staticValues["Test262Error"] = engine.GetValue("Test262Error"); | ||
_staticValues["$ERROR"] = engine.GetValue("$ERROR"); | ||
_staticValues["$DONOTEVALUATE"] = engine.GetValue("$DONOTEVALUATE"); | ||
|
||
_staticValues["print"] = new ClrFunctionInstance(engine, "print", (thisObj, args) => TypeConverter.ToString(args.At(0))); | ||
} | ||
|
||
protected override ObjectInstance CreateGlobalObject(Realm realm) | ||
{ | ||
var globalObj = base.CreateGlobalObject(realm); | ||
|
||
foreach (var key in _staticValues.Keys) | ||
{ | ||
globalObj.FastAddProperty(key, _staticValues[key], true, true, true); | ||
} | ||
|
||
return globalObj; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,72 @@ | ||
using Jint.Runtime; | ||
using Jint.Runtime.Modules; | ||
using System; | ||
using Xunit; | ||
using Xunit.Sdk; | ||
|
||
namespace Jint.Tests.Test262.Language | ||
namespace Jint.Tests.Test262.Language; | ||
|
||
public class ModuleTests : Test262Test | ||
{ | ||
public class ModuleTests : Test262Test | ||
[Theory(DisplayName = "language\\module-code")] | ||
[MemberData(nameof(SourceFiles), "language\\module-code", false)] | ||
[MemberData(nameof(SourceFiles), "language\\module-code", true, Skip = "Skipped")] | ||
protected void ModuleCode(SourceFile sourceFile) | ||
{ | ||
RunModuleTest(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); | ||
} | ||
|
||
[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) | ||
{ | ||
[Theory(DisplayName = "language\\module-code", Skip = "TODO")] | ||
[MemberData(nameof(SourceFiles), "language\\module-code", false)] | ||
[MemberData(nameof(SourceFiles), "language\\module-code", true, Skip = "Skipped")] | ||
protected void ModuleCode(SourceFile sourceFile) | ||
if (sourceFile.Skip) | ||
{ | ||
RunTestInternal(sourceFile); | ||
return; | ||
} | ||
|
||
[Theory(DisplayName = "language\\export")] | ||
[MemberData(nameof(SourceFiles), "language\\export", false)] | ||
[MemberData(nameof(SourceFiles), "language\\export", true, Skip = "Skipped")] | ||
protected void Export(SourceFile sourceFile) | ||
var code = sourceFile.Code; | ||
|
||
var options = new Options(); | ||
options.Host.Factory = _ => new ModuleTestHost(); | ||
options.Modules.Enabled = true; | ||
options.WithModuleLoader(new DefaultModuleLoader(null)); | ||
|
||
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) | ||
{ | ||
RunTestInternal(sourceFile); | ||
lastError = ex.ToString(); | ||
} | ||
|
||
[Theory(DisplayName = "language\\import", Skip = "TODO")] | ||
[MemberData(nameof(SourceFiles), "language\\import", false)] | ||
[MemberData(nameof(SourceFiles), "language\\import", true, Skip = "Skipped")] | ||
protected void Import(SourceFile sourceFile) | ||
if (!negative && !string.IsNullOrWhiteSpace(lastError)) | ||
{ | ||
RunTestInternal(sourceFile); | ||
throw new XunitException(lastError); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Collections.Generic; | ||
using Jint.Runtime.Modules; | ||
|
||
namespace Jint | ||
{ | ||
public partial class Engine | ||
{ | ||
internal IModuleLoader ModuleLoader { get; set; } | ||
|
||
private readonly Dictionary<ModuleCacheKey, JsModule> _modules = new(); | ||
|
||
public JsModule LoadModule(string specifier) => LoadModule(null, specifier); | ||
|
||
internal JsModule LoadModule(string referencingModuleLocation, string specifier) | ||
{ | ||
var key = new ModuleCacheKey(referencingModuleLocation ?? string.Empty, specifier); | ||
|
||
if (_modules.TryGetValue(key, out var module)) | ||
{ | ||
return module; | ||
} | ||
|
||
var (loadedModule, location) = ModuleLoader.LoadModule(this, specifier, referencingModuleLocation); | ||
module = new JsModule(this, _host.CreateRealm(), loadedModule, location.AbsoluteUri, false); | ||
|
||
_modules[key] = module; | ||
|
||
return module; | ||
} | ||
|
||
internal readonly record struct ModuleCacheKey(string ReferencingModuleLocation, string Specifier); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.