LeaveScriptObject.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. namespace Js
  7. {
  8. EnterScriptObject::EnterScriptObject(ScriptContext* scriptContext, ScriptEntryExitRecord* entryExitRecord,
  9. void * returnAddress, void * addrOfReturnAddress, bool doCleanup, bool isCallRoot, bool hasCaller)
  10. {
  11. Assert(scriptContext);
  12. #ifdef PROFILE_EXEC
  13. scriptContext->ProfileBegin(Js::RunPhase);
  14. #endif
  15. if (scriptContext->GetThreadContext() &&
  16. scriptContext->GetThreadContext()->IsNoScriptScope())
  17. {
  18. FromDOM_NoScriptScope_unrecoverable_error();
  19. }
  20. // Keep a copy locally so the optimizer can just copy prop it to the dtor
  21. this->scriptContext = scriptContext;
  22. this->entryExitRecord = entryExitRecord;
  23. this->doCleanup = doCleanup;
  24. this->isCallRoot = isCallRoot;
  25. this->hr = NOERROR;
  26. this->hasForcedEnter =
  27. #ifdef ENABLE_SCRIPT_DEBUGGING
  28. scriptContext->GetDebugContext() != nullptr ?
  29. scriptContext->GetDebugContext()->GetProbeContainer()->isForcedToEnterScriptStart :
  30. #endif
  31. false;
  32. // Initialize the entry exit record
  33. entryExitRecord->returnAddrOfScriptEntryFunction = returnAddress;
  34. entryExitRecord->addrOfReturnAddrOfScriptEntryFunction = addrOfReturnAddress;
  35. entryExitRecord->hasCaller = hasCaller;
  36. entryExitRecord->scriptContext = scriptContext;
  37. #ifdef EXCEPTION_CHECK
  38. entryExitRecord->handledExceptionType = ExceptionCheck::ClearHandledExceptionType();
  39. #endif
  40. #if DBG_DUMP
  41. entryExitRecord->isCallRoot = isCallRoot;
  42. #endif
  43. if (!scriptContext->IsClosed())
  44. {
  45. library = scriptContext->GetLibrary();
  46. }
  47. try
  48. {
  49. AUTO_NESTED_HANDLED_EXCEPTION_TYPE(ExceptionType_OutOfMemory);
  50. scriptContext->GetThreadContext()->PushHostScriptContext(scriptContext->GetHostScriptContext());
  51. }
  52. catch (Js::OutOfMemoryException)
  53. {
  54. this->hr = E_OUTOFMEMORY;
  55. }
  56. BEGIN_NO_EXCEPTION
  57. {
  58. // We can not have any exception in the constructor, otherwise the destructor will
  59. // not run and we might be in an inconsistent state
  60. // Put any code that may raise an exception in OnScriptStart
  61. scriptContext->GetThreadContext()->EnterScriptStart(entryExitRecord, doCleanup);
  62. }
  63. END_NO_EXCEPTION
  64. }
  65. void EnterScriptObject::VerifyEnterScript()
  66. {
  67. if (FAILED(hr))
  68. {
  69. Assert(hr == E_OUTOFMEMORY);
  70. throw Js::OutOfMemoryException();
  71. }
  72. }
  73. EnterScriptObject::~EnterScriptObject()
  74. {
  75. scriptContext->OnScriptEnd(isCallRoot, hasForcedEnter);
  76. if (SUCCEEDED(hr))
  77. {
  78. scriptContext->GetThreadContext()->PopHostScriptContext();
  79. }
  80. scriptContext->GetThreadContext()->EnterScriptEnd(entryExitRecord, doCleanup);
  81. #ifdef PROFILE_EXEC
  82. scriptContext->ProfileEnd(Js::RunPhase);
  83. #endif
  84. }
  85. };