Skip to content

Commit

Permalink
Fix TimeoutInterval crashing ImportModule (#1165)
Browse files Browse the repository at this point in the history
  • Loading branch information
christianrondeau committed May 11, 2022
1 parent b07562e commit 8c89089
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Jint.Tests/Runtime/ModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Reflection;
#endif
using System;
using System.Collections.Generic;
using System.Linq;
using Jint.Native;
Expand Down Expand Up @@ -302,6 +303,15 @@ public void ShouldAllowCyclicImport()
Assert.Equal("a", nsA.Get("a").AsString());
Assert.Equal("b", nsB.Get("b").AsString());
}

[Fact]
public void ShouldSupportConstraints()
{
var engine = new Engine(opts => opts.TimeoutInterval(TimeSpan.FromTicks(1)));

engine.AddModule("my-module", @"for(var i = 0; i < 100000; i++) { } export const result = 'ok';");
Assert.Throws<TimeoutException>(() => engine.ImportModule("my-module"));
}

#if(NET6_0_OR_GREATER)

Expand Down
6 changes: 4 additions & 2 deletions Jint/Engine.Modules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ internal ObjectInstance ImportModule(string specifier, string? referencingModule

if (cyclicModule.Status == ModuleStatus.Linked)
{
EvaluateModule(specifier, cyclicModule);
ExecuteWithConstraints(true, () => EvaluateModule(specifier, cyclicModule));
}

if (cyclicModule.Status != ModuleStatus.Evaluated)
Expand All @@ -133,7 +133,7 @@ internal ObjectInstance ImportModule(string specifier, string? referencingModule
return ModuleRecord.GetModuleNamespace(module);
}

private void EvaluateModule(string specifier, ModuleRecord cyclicModule)
private JsValue EvaluateModule(string specifier, ModuleRecord cyclicModule)
{
var ownsContext = _activeEvaluationContext is null;
_activeEvaluationContext ??= new EvaluationContext(this);
Expand Down Expand Up @@ -163,6 +163,8 @@ private void EvaluateModule(string specifier, ModuleRecord cyclicModule)
{
ExceptionHelper.ThrowInvalidOperationException($"Error while evaluating module: Module evaluation did not return a fulfilled promise: {promise.State}");
}

return evaluationResult;
}
}
}

0 comments on commit 8c89089

Please sign in to comment.