StrictModeScope.cs 935 B

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