-
-
Notifications
You must be signed in to change notification settings - Fork 586
Module loading support #990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f437162
Initial commit
9b24b29
Merge branch 'sebastienros:main' into module-loading
pluethi1 fe8e090
Added module namespace handling
pluethi1 523ae62
Resolve merge conflicts
pluethi1 3a4afc7
Merge branch 'sebastienros-main' into module-loading
pluethi1 dfa11c8
Corrections from merging
pluethi1 fb27663
skip json-modules and top-level-await tests, enable module tests
lahma 546462e
skip import-assertions testing
lahma 5ef2334
Refactoring/Tests
pluethi1 b90ea26
resolve merge conflicts
pluethi1 7151f25
Merge branch 'main' into module-loading
lahma 64234f0
Merge branch 'main' into module-loading
lahma d2da380
tweak code to work with latest esprima
lahma 368e0b5
small cleanup and documentation
lahma 30d735f
Update ModuleTestHost.cs
sebastienros ad51147
Update Jint/Native/Promise/PromiseOperations.cs
lahma 62351ad
simplify design by removing IModuleSource for now, make require regis…
lahma 3685589
Check base path
sebastienros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,70 @@ | ||
| using Jint.Runtime; | ||
| 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; | ||
|
|
||
| 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still?