ExceptionCheck.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. #ifdef EXCEPTION_CHECK
  7. enum ExceptionType
  8. {
  9. ExceptionType_None = 0x00000000,
  10. ExceptionType_OutOfMemory = 0x00000001,
  11. ExceptionType_StackOverflow = 0x00000002,
  12. ExceptionType_JavascriptException = 0x00000004,
  13. ExceptionType_HasStackProbe = 0x00000008,
  14. ExceptionType_DisableCheck = 0x80000000,
  15. ExceptionType_All = 0x0FFFFFFF,
  16. };
  17. class ExceptionCheck
  18. {
  19. public:
  20. static void SetHandledExceptionType(ExceptionType e);
  21. static ExceptionType ClearHandledExceptionType();
  22. static BOOL CanHandleOutOfMemory();
  23. static BOOL CanHandleStackOverflow(bool isExternal);
  24. static BOOL HasStackProbe();
  25. struct Data
  26. {
  27. ExceptionType handledExceptionType;
  28. };
  29. static ExceptionCheck::Data Save();
  30. static void Restore(ExceptionCheck::Data& savedData);
  31. static ExceptionCheck::Data GetData();
  32. private:
  33. static BOOL IsEmpty();
  34. THREAD_LOCAL static Data data;
  35. };
  36. class AutoHandledExceptionType
  37. {
  38. public:
  39. AutoHandledExceptionType(ExceptionType e);
  40. ~AutoHandledExceptionType();
  41. };
  42. class AutoNestedHandledExceptionType
  43. {
  44. public:
  45. AutoNestedHandledExceptionType(ExceptionType e);
  46. ~AutoNestedHandledExceptionType();
  47. private:
  48. ExceptionCheck::Data savedData;
  49. };
  50. class AutoFilterExceptionRegion
  51. {
  52. public:
  53. AutoFilterExceptionRegion(ExceptionType e);
  54. ~AutoFilterExceptionRegion();
  55. private:
  56. ExceptionCheck::Data savedData;
  57. };
  58. #define AssertCanHandleOutOfMemory() Assert(ExceptionCheck::CanHandleOutOfMemory())
  59. #define AssertCanHandleStackOverflow() Assert(ExceptionCheck::CanHandleStackOverflow(false))
  60. #define AssertCanHandleStackOverflowCall(isExternal) Assert(ExceptionCheck::CanHandleStackOverflow(isExternal))
  61. #define DECLARE_EXCEPTION_CHECK_DATA \
  62. ExceptionCheck::Data __exceptionCheck;
  63. #define SAVE_EXCEPTION_CHECK \
  64. __exceptionCheck = ExceptionCheck::Save();
  65. #define RESTORE_EXCEPTION_CHECK \
  66. ExceptionCheck::Restore(__exceptionCheck);
  67. #define AUTO_HANDLED_EXCEPTION_TYPE(type) AutoHandledExceptionType __autoHandledExceptionType(type)
  68. #define AUTO_NESTED_HANDLED_EXCEPTION_TYPE(type) AutoNestedHandledExceptionType __autoNestedHandledExceptionType(type)
  69. #define AUTO_FILTER_EXCEPTION_REGION(type) AutoFilterExceptionRegion __autoFilterExceptionRegion(type)
  70. #define AUTO_NO_EXCEPTION_REGION AUTO_FILTER_EXCEPTION_REGION(ExceptionType_All)
  71. #else
  72. #define AssertCanHandleOutOfMemory()
  73. #define AssertCanHandleStackOverflow()
  74. #define AssertCanHandleStackOverflowCall(isExternal)
  75. #define DECLARE_EXCEPTION_CHECK_DATA
  76. #define SAVE_EXCEPTION_CHECK
  77. #define RESTORE_EXCEPTION_CHECK
  78. #define AUTO_HANDLED_EXCEPTION_TYPE(type)
  79. #define AUTO_NESTED_HANDLED_EXCEPTION_TYPE(type)
  80. #define AUTO_FILTER_EXCEPTION_REGION(type)
  81. #define AUTO_NO_EXCEPTION_REGION
  82. #endif
  83. #if DBG
  84. class DebugCheckNoException
  85. {
  86. public:
  87. DebugCheckNoException() : hasException(true) { SAVE_EXCEPTION_CHECK;}
  88. ~DebugCheckNoException() { Assert(!hasException); RESTORE_EXCEPTION_CHECK; }
  89. DECLARE_EXCEPTION_CHECK_DATA;
  90. bool hasException;
  91. };
  92. #define BEGIN_NO_EXCEPTION { DebugCheckNoException __debugCheckNoException;
  93. #define END_NO_EXCEPTION __debugCheckNoException.hasException = false; }
  94. #else
  95. #define BEGIN_NO_EXCEPTION
  96. #define END_NO_EXCEPTION
  97. #endif