StrictModeScope.cs 1.1 KB

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