StrictModeScope.cs 1019 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. namespace Jint
  2. {
  3. public readonly struct StrictModeScope : IDisposable
  4. {
  5. private readonly bool _strict;
  6. private readonly bool _force;
  7. private readonly ushort _forcedRefCount;
  8. [ThreadStatic]
  9. private static ushort _refCount;
  10. public StrictModeScope(bool strict = true, bool force = false)
  11. {
  12. _strict = strict;
  13. _force = force;
  14. if (_force)
  15. {
  16. _forcedRefCount = _refCount;
  17. _refCount = 0;
  18. }
  19. else
  20. {
  21. _forcedRefCount = 0;
  22. }
  23. if (_strict)
  24. {
  25. _refCount++;
  26. }
  27. }
  28. public void Dispose()
  29. {
  30. if (_strict)
  31. {
  32. _refCount--;
  33. }
  34. if (_force)
  35. {
  36. _refCount = _forcedRefCount;
  37. }
  38. }
  39. public static bool IsStrictModeCode => _refCount > 0;
  40. }
  41. }