-
-
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
+1,860
−38
Merged
Changes from 6 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
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,141 @@ | ||
| using Esprima; | ||
| using Esprima.Ast; | ||
| using System.Collections.Generic; | ||
| using Jint.Native; | ||
| using Jint.Native.Promise; | ||
| using Jint.Runtime; | ||
| using Jint.Runtime.Descriptors; | ||
| using Jint.Runtime.Interop; | ||
| using Jint.Runtime.Modules; | ||
|
|
||
| namespace Jint | ||
| { | ||
| public partial class Engine | ||
| { | ||
| public IModuleLoader ModuleLoader { get; internal set; } | ||
|
|
||
| private readonly Dictionary<ModuleCacheKey, JsModule> _modules = new Dictionary<ModuleCacheKey, JsModule>(); | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
| if(!ModuleLoader.TryLoadModule(specifier, referencingModuleLocation, out var moduleSourceCode, out var moduleLocation)) | ||
| { | ||
| ExceptionHelper.ThrowSyntaxError(Realm, "Error while loading module: module with specifier '" + specifier + "' could not be located"); | ||
| } | ||
|
|
||
| Module moduleSource; | ||
| try | ||
| { | ||
| var parserOptions = new ParserOptions(moduleLocation) | ||
| { | ||
| AdaptRegexp = true, | ||
| Tolerant = true | ||
| }; | ||
|
|
||
| moduleSource = new JavaScriptParser(moduleSourceCode, parserOptions).ParseModule(); | ||
| } | ||
| catch (ParserException ex) | ||
| { | ||
| ExceptionHelper.ThrowSyntaxError(Realm, "Error while loading module: error in module '" + specifier + "': " + ex.Error?.ToString() ?? ex.Message); | ||
| moduleSource = null; | ||
| } | ||
|
|
||
| module = new JsModule(this, _host.CreateRealm(), moduleSource, moduleLocation, false); | ||
|
|
||
| _modules[key] = module; | ||
|
|
||
| return module; | ||
|
|
||
| } | ||
|
|
||
| //https://tc39.es/ecma262/#sec-hostresolveimportedmodule | ||
| internal JsModule ResolveImportedModule(JsModule referencingModule, string specifier) | ||
| { | ||
| return LoadModule(referencingModule._location, specifier); | ||
| } | ||
|
|
||
| //https://tc39.es/ecma262/#sec-hostimportmoduledynamically | ||
| internal void ImportModuleDynamically(JsModule referencingModule, string specifier, PromiseCapability promiseCapability) | ||
| { | ||
|
|
||
| var promise = RegisterPromise(); | ||
|
|
||
| try | ||
| { | ||
| LoadModule(referencingModule._location, specifier); | ||
| promise.Resolve(JsValue.Undefined); | ||
|
|
||
| } | ||
| catch (JavaScriptException ex) | ||
| { | ||
| promise.Reject(ex.Error); | ||
| } | ||
|
|
||
| FinishDynamicImport(referencingModule, specifier, promiseCapability, (PromiseInstance)promise.Promise); | ||
| } | ||
|
|
||
| //https://tc39.es/ecma262/#sec-finishdynamicimport | ||
| internal void FinishDynamicImport(JsModule referencingModule, string specifier, PromiseCapability promiseCapability, PromiseInstance innerPromise) | ||
| { | ||
| var onFulfilled = new ClrFunctionInstance(this, "", (thisObj, args) => | ||
| { | ||
| var moduleRecord = ResolveImportedModule(referencingModule, specifier); | ||
| try | ||
| { | ||
| var ns = JsModule.GetModuleNamespace(moduleRecord); | ||
| promiseCapability.Resolve.Call(ns); | ||
| } | ||
| catch (JavaScriptException ex) | ||
| { | ||
| promiseCapability.Reject.Call(ex.Error); | ||
| } | ||
| return JsValue.Undefined; | ||
| }, 0, PropertyFlag.Configurable); | ||
|
|
||
| var onRejected = new ClrFunctionInstance(this, "", (thisObj, args) => | ||
| { | ||
| var error = args.At(0); | ||
| promiseCapability.Reject.Call(error); | ||
| return JsValue.Undefined; | ||
| }, 0, PropertyFlag.Configurable); | ||
|
|
||
| PromiseOperations.PerformPromiseThen(this, innerPromise, onFulfilled, onRejected, null); | ||
| } | ||
|
|
||
| internal readonly struct ModuleCacheKey | ||
lahma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| internal readonly Key ReferencingModuleLocation; | ||
lahma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| internal readonly Key Specifier; | ||
| private readonly int _hashCode; | ||
|
|
||
| internal ModuleCacheKey(string referencingModuleLocation, string specifier) | ||
| { | ||
| ReferencingModuleLocation = referencingModuleLocation; | ||
lahma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Specifier = specifier; | ||
| unchecked | ||
| { | ||
| _hashCode = 31 * ReferencingModuleLocation.HashCode + Specifier.HashCode; | ||
| } | ||
| } | ||
|
|
||
| public override bool Equals(object obj) | ||
| { | ||
| return (obj is ModuleCacheKey other) && (ReferencingModuleLocation.Equals(other.ReferencingModuleLocation) && Specifier.Equals(other.Specifier)); | ||
| } | ||
|
|
||
| public override int GetHashCode() | ||
| { | ||
| return _hashCode; | ||
lahma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| using Jint.Runtime.Environments; | ||
| using Jint.Runtime.Interpreter; | ||
| using Jint.Runtime.Interpreter.Expressions; | ||
| using Jint.Runtime.Modules; | ||
|
|
||
| namespace Jint | ||
| { | ||
|
|
@@ -256,6 +257,117 @@ internal static Record DefineMethod(this ClassProperty m, ObjectInstance obj, Ob | |
| return new Record(property, closure); | ||
| } | ||
|
|
||
| internal static void GetImportEntries(this ImportDeclaration import, List<ImportEntry> importEntries) | ||
| { | ||
| var source = import.Source.StringValue; | ||
| var specifiers = import.Specifiers; | ||
|
|
||
| foreach (var specifier in specifiers) | ||
| { | ||
| switch (specifier) | ||
| { | ||
| case ImportNamespaceSpecifier namespaceSpecifier: | ||
| importEntries.Add(new ImportEntry(source, "*", namespaceSpecifier.Local.Name)); | ||
| break; | ||
| case ImportSpecifier importSpecifier: | ||
| importEntries.Add(new ImportEntry(source, importSpecifier.Imported.Name, importSpecifier.Local.Name)); | ||
| break; | ||
| case ImportDefaultSpecifier defaultSpecifier: | ||
| importEntries.Add(new ImportEntry(source, "default", defaultSpecifier.Local.Name)); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal static void GetExportEntries(this ExportDeclaration export, List<ExportEntry> exportEntries) | ||
| { | ||
| switch (export) | ||
| { | ||
| case ExportDefaultDeclaration defaultDeclaration: | ||
| GetExportEntries(true, defaultDeclaration.Declaration, exportEntries); | ||
| break; | ||
| case ExportAllDeclaration allDeclaration: | ||
| //Note: there is a pending PR for Esprima to support exporting an imported modules content as a namespace i.e. 'export * as ns from "mod"' | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still? |
||
| exportEntries.Add(new(null, allDeclaration.Source.StringValue, "*", null)); | ||
| break; | ||
| case ExportNamedDeclaration namedDeclaration: | ||
| var specifiers = namedDeclaration.Specifiers; | ||
| if (specifiers.Count == 0) | ||
| { | ||
| GetExportEntries(false, namedDeclaration.Declaration!, exportEntries, namedDeclaration.Source?.StringValue); | ||
| } | ||
| else | ||
| { | ||
| foreach(var specifier in specifiers) | ||
| { | ||
| exportEntries.Add(new(specifier.Local.Name, namedDeclaration.Source?.StringValue, specifier.Exported.Name, null)); | ||
| } | ||
| } | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| private static void GetExportEntries(bool defaultExport, StatementListItem declaration, List<ExportEntry> exportEntries, string? moduleRequest = null) | ||
| { | ||
| var names = GetExportNames(declaration); | ||
|
|
||
| if(names.Count == 0) | ||
| { | ||
| if (!defaultExport) | ||
| { | ||
| ExceptionHelper.ThrowTypeErrorNoEngine("export declaration requires an identifier"); | ||
| } | ||
|
|
||
| exportEntries.Add(new("default", null, null, "*default*")); | ||
| } | ||
| else | ||
| { | ||
| for(var i = 0; i < names.Count; i++) | ||
| { | ||
| var name = names[i]; | ||
| var exportName = defaultExport ? "default" : name; | ||
| exportEntries.Add(new(exportName, moduleRequest, null, name)); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private static List<string> GetExportNames(StatementListItem declaration) | ||
| { | ||
| var result = new List<string>(); | ||
|
|
||
| switch (declaration) | ||
| { | ||
| case FunctionDeclaration functionDeclaration: | ||
| var funcName = functionDeclaration.Id?.Name; | ||
| if(funcName is not null) | ||
| { | ||
| result.Add(funcName); | ||
| } | ||
| break; | ||
| case ClassDeclaration classDeclaration: | ||
| var className = classDeclaration.Id?.Name; | ||
| if(className is not null) | ||
| { | ||
| result.Add(className); | ||
| } | ||
| break; | ||
| case VariableDeclaration variableDeclaration: | ||
| var declarators = variableDeclaration.Declarations; | ||
| foreach(var declarator in declarators) | ||
| { | ||
| var varName = declarator.Id.As<Identifier>()?.Name; | ||
| if(varName is not null) | ||
| { | ||
| result.Add(varName); | ||
| } | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| internal readonly struct Record | ||
| { | ||
| public Record(JsValue key, ScriptFunctionInstance closure) | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.