TimeConstraint.cs 736 B

12345678910111213141516171819202122232425262728293031
  1. using Jint.Runtime;
  2. using System;
  3. namespace Jint.Constraints
  4. {
  5. internal sealed class TimeConstraint : IConstraint
  6. {
  7. private readonly long _maxTicks;
  8. private long _timeoutTicks;
  9. public TimeConstraint(TimeSpan timeout)
  10. {
  11. _maxTicks = timeout.Ticks;
  12. }
  13. public void Check()
  14. {
  15. if (_timeoutTicks > 0 && _timeoutTicks < DateTime.UtcNow.Ticks)
  16. {
  17. ExceptionHelper.ThrowTimeoutException();
  18. }
  19. }
  20. public void Reset()
  21. {
  22. var timeoutIntervalTicks = _maxTicks;
  23. _timeoutTicks = timeoutIntervalTicks > 0 ? DateTime.UtcNow.Ticks + timeoutIntervalTicks : 0;
  24. }
  25. }
  26. }