Skip to content

Commit

Permalink
dynamic import
Browse files Browse the repository at this point in the history
  • Loading branch information
christianrondeau committed Mar 9, 2022
1 parent 5323e1a commit 20ef459
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Jint.Tests/Runtime/ModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void ShouldImportAll()
Assert.Equal("exported value", ns.Get("exported").AsString());
}

[Fact(Skip = "Import promise is not resolved")]
[Fact]
public void ShouldImportDynamically()
{
var received = false;
Expand Down
8 changes: 7 additions & 1 deletion Jint/Engine.Modules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ public void AddModule(string specifier, ModuleBuilder moduleBuilder)

public ObjectInstance ImportModule(string specifier)
{
var moduleResolution = ModuleLoader.Resolve(referencingModuleLocation: null, specifier);
return ImportModule(specifier, null);
}

internal ObjectInstance ImportModule(string specifier, string? referencingModuleLocation)
{
var moduleResolution = ModuleLoader.Resolve(referencingModuleLocation, specifier);

if (!_modules.TryGetValue(moduleResolution.Key, out var module))
{
Expand Down Expand Up @@ -145,6 +150,7 @@ private void EvaluateModule(string specifier, ModuleRecord cyclicModule)
}
}

// This should instead be returned and resolved in ImportModule(specifier) only so Host.ImportModuleDynamically can use this promise
if (evaluationResult is not PromiseInstance promise)
{
ExceptionHelper.ThrowInvalidOperationException($"Error while evaluating module: Module evaluation did not return a promise: {evaluationResult.Type}");
Expand Down
3 changes: 2 additions & 1 deletion Jint/Runtime/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ internal virtual void ImportModuleDynamically(IScriptOrModule? referencingModule

try
{
Engine.LoadModule(referencingModule?.Location, specifier);
// This should instead return the PromiseInstance returned by ModuleRecord.Evaluate (currently done in Engine.EvaluateModule), but until we have await this will do.
Engine.ImportModule(specifier, referencingModule?.Location);
promise.Resolve(JsValue.Undefined);
}
catch (JavaScriptException ex)
Expand Down

0 comments on commit 20ef459

Please sign in to comment.