Skip to content

Commit

Permalink
Add support for checking constraints from engine
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Feb 18, 2023
1 parent cf3714b commit 355c329
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Jint.Tests.PublicInterface/ConstraintUsageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ namespace Jint.Tests.PublicInterface;
[Collection("ConstraintUsageTests")]
public class ConstraintUsageTests
{
// this test case is problematic due to nature of cancellation token source in old framework
// in NET 6 it's better designed and signals more reliably
#if NET6_0_OR_GREATER
[Fact]
public void CanFindAndResetCancellationConstraint()
{
Expand Down Expand Up @@ -45,4 +48,23 @@ string WaitAndCompute()
return result.AsString();
}
}
#endif

[Fact]
public void CanObserveConstraintsFromCustomCode()
{
var engine = new Engine(o => o.TimeoutInterval(TimeSpan.FromMilliseconds(100)));
engine.SetValue("slowFunction", new Func<string>(() =>
{
for (var i = 0; i < 100; ++i)
{
Thread.Sleep(TimeSpan.FromMilliseconds(200));
engine.CheckConstraints();
}
return "didn't throw!";
}));

Assert.Throws<TimeoutException>(() => engine.Execute("slowFunction()"));
}
}
9 changes: 9 additions & 0 deletions Jint/Constraints/ConstraintsOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public static Options MaxStatements(this Options options, int maxStatements = 0)
return options;
}

/// <summary>
/// Sets constraint based on memory usage in bytes.
/// </summary>
public static Options LimitMemory(this Options options, long memoryLimit)
{
options.WithoutConstraint(x => x is MemoryLimitConstraint);
Expand All @@ -31,6 +34,9 @@ public static Options LimitMemory(this Options options, long memoryLimit)
return options;
}

/// <summary>
/// Sets constraint based on fixed time interval.
/// </summary>
public static Options TimeoutInterval(this Options options, TimeSpan timeoutInterval)
{
if (timeoutInterval > TimeSpan.Zero && timeoutInterval < TimeSpan.MaxValue)
Expand All @@ -40,6 +46,9 @@ public static Options TimeoutInterval(this Options options, TimeSpan timeoutInte
return options;
}

/// <summary>
/// Sets cancellation token to be observed. NOTE that this can be unreliable/imprecise on full framework due to timer logic.
/// </summary>
public static Options CancellationToken(this Options options, CancellationToken cancellationToken)
{
options.WithoutConstraint(x => x is CancellationConstraint);
Expand Down
11 changes: 11 additions & 0 deletions Jint/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ internal void LeaveExecutionContext()
_executionContexts.Pop();
}

/// <summary>
/// Checks engine's active constraints. Propagates exceptions from constraints.
/// </summary>
public void CheckConstraints()
{
foreach (var constraint in _constraints)
{
constraint.Check();
}
}

/// <summary>
/// Initializes the statements count
/// </summary>
Expand Down

0 comments on commit 355c329

Please sign in to comment.