Skip to content
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

Fix TimeoutInterval crashing ImportModule #1165

Merged
merged 1 commit into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
}
}