JavascriptException.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. namespace Js {
  7. class JavascriptExceptionObject;
  8. //
  9. // JavascriptException wraps a runtime JavascriptExceptionObject. To ensure
  10. // a thrown JavascriptExceptionObject is kept alive before being caught by
  11. // a handler, we store the JavascriptExceptionObject reference in thread context
  12. // data (which GC scans). After the exception is caught, call GetAndClear()
  13. // to retrieve the wrapped JavascriptExceptionObject and clear it from thread
  14. // context data.
  15. //
  16. class JavascriptException : public ExceptionBase
  17. {
  18. private:
  19. Field(JavascriptExceptionObject*)* const addressOfException;
  20. public:
  21. // Caller should have stored the JavascriptExceptionObject reference in
  22. // thread context data. addressOfException should be the thread context data
  23. // address.
  24. JavascriptException(Field(JavascriptExceptionObject*)* addressOfException)
  25. : addressOfException(addressOfException)
  26. {
  27. Assert(addressOfException && *addressOfException);
  28. }
  29. JavascriptExceptionObject* GetAndClear() const
  30. {
  31. Assert(*addressOfException);
  32. JavascriptExceptionObject* exceptionObject = *addressOfException;
  33. *addressOfException = nullptr;
  34. return exceptionObject;
  35. }
  36. };
  37. } // namespace Js