Exception.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. #include "RuntimeBasePch.h"
  6. #include "Exception.h"
  7. namespace Js
  8. {
  9. bool Exception::RaiseIfScriptActive(ScriptContext *scriptContext, unsigned kind, PVOID returnAddress)
  10. {
  11. ThreadContext *threadContext = ThreadContext::GetContextForCurrentThread();
  12. if (threadContext != nullptr)
  13. {
  14. if (kind == ExceptionKind_OutOfMemory &&
  15. CONFIG_FLAG(EnableFatalErrorOnOOM) && !threadContext->TestThreadContextFlag(ThreadContextFlagDisableFatalOnOOM))
  16. {
  17. OutOfMemory_unrecoverable_error();
  18. }
  19. if (threadContext->IsScriptActive())
  20. {
  21. switch (kind) {
  22. case ExceptionKind_OutOfMemory:
  23. AssertMsg(returnAddress == NULL, "should not have returnAddress passed in");
  24. JavascriptError::ThrowOutOfMemoryError(scriptContext);
  25. case ExceptionKind_StackOverflow:
  26. JavascriptError::ThrowStackOverflowError(scriptContext, returnAddress);
  27. default:
  28. AssertMsg(false, "Invalid ExceptionKind");
  29. }
  30. }
  31. }
  32. return false;
  33. }
  34. // Recover/Release unused memory and give it back to OS.
  35. // The function doesn't throw if the attempt to recover memory fails, in which case it simply does nothing.
  36. // Useful when running out of memory e.g. for Arena but there is some recycler memory which has been committed but is unused.
  37. void Exception::RecoverUnusedMemory()
  38. {
  39. ThreadContext* threadContext = ThreadContext::GetContextForCurrentThread();
  40. if (threadContext)
  41. {
  42. Recycler* threadRecycler = threadContext->GetRecycler();
  43. if (threadRecycler)
  44. {
  45. try
  46. {
  47. threadRecycler->CollectNow<CollectOnRecoverFromOutOfMemory>();
  48. }
  49. catch (...)
  50. {
  51. // Technically, exception is a valid scenario: we asked to recover mem, and it couldn't.
  52. // Do not let the exception leak out.
  53. }
  54. }
  55. }
  56. }
  57. }