StrictModeScope.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. namespace Jint
  3. {
  4. public class StrictModeScope : IDisposable
  5. {
  6. private readonly bool _strict;
  7. private readonly bool _force;
  8. private readonly int _forcedRefCount;
  9. [ThreadStatic]
  10. private static int _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. if (_strict)
  21. {
  22. _refCount++;
  23. }
  24. }
  25. public void Dispose()
  26. {
  27. if (_strict)
  28. {
  29. _refCount--;
  30. }
  31. if (_force)
  32. {
  33. _refCount = _forcedRefCount;
  34. }
  35. }
  36. public static bool IsStrictModeCode
  37. {
  38. get { return _refCount > 0; }
  39. }
  40. public static int RefCount
  41. {
  42. get
  43. {
  44. return _refCount;
  45. }
  46. set
  47. {
  48. _refCount = value;
  49. }
  50. }
  51. }
  52. }