ThrowErrorObject.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 "RuntimeLibraryPch.h"
  6. #ifdef ENABLE_SCRIPT_DEBUGGING
  7. #include "Debug/DiagHelperMethodWrapper.h"
  8. #endif
  9. #include "Library/ThrowErrorObject.h"
  10. namespace Js
  11. {
  12. // In some cases we delay throw from helper methods and return ThrowErrorObject instead which we call and throw later.
  13. // Then the exception is actually thrown when we call this method.
  14. Var ThrowErrorObject::DefaultEntryPoint(RecyclableObject* function, CallInfo callInfo, ...)
  15. {
  16. ARGUMENTS(args, callInfo);
  17. ScriptContext* scriptContext = function->GetScriptContext();
  18. ThrowErrorObject* throwErrorObject = VarTo<ThrowErrorObject>(function);
  19. #ifdef ENABLE_SCRIPT_DEBUGGING
  20. bool useExceptionWrapper =
  21. scriptContext->IsScriptContextInDebugMode() /* Check for script context is intentional as library code also uses exception wrapper */ &&
  22. (ScriptContext::IsExceptionWrapperForBuiltInsEnabled(scriptContext) || ScriptContext::IsExceptionWrapperForHelpersEnabled(scriptContext)) &&
  23. !AutoRegisterIgnoreExceptionWrapper::IsRegistered(scriptContext->GetThreadContext());
  24. if (useExceptionWrapper)
  25. {
  26. // Forward the throw via regular try-catch wrapper logic that we use for helper/library calls.
  27. AutoRegisterIgnoreExceptionWrapper autoWrapper(scriptContext->GetThreadContext());
  28. Var ret = HelperOrLibraryMethodWrapper<true>(scriptContext, [throwErrorObject, scriptContext]() -> Var {
  29. JavascriptExceptionOperators::Throw(throwErrorObject->m_error, scriptContext);
  30. });
  31. return ret;
  32. }
  33. else
  34. #endif
  35. {
  36. JavascriptExceptionOperators::Throw(throwErrorObject->m_error, scriptContext);
  37. }
  38. }
  39. ThrowErrorObject::ThrowErrorObject(StaticType* type, JavascriptError* error)
  40. : RecyclableObject(type), m_error(error)
  41. {
  42. }
  43. ThrowErrorObject* ThrowErrorObject::New(StaticType* type, JavascriptError* error, Recycler* recycler)
  44. {
  45. return RecyclerNew(recycler, ThrowErrorObject, type, error);
  46. }
  47. RecyclableObject* ThrowErrorObject::CreateThrowErrorObject(CreateErrorFunc createError, ScriptContext* scriptContext, int32 hCode, PCWSTR varName)
  48. {
  49. JavascriptLibrary* library = scriptContext->GetLibrary();
  50. JavascriptError *pError = (library->*createError)();
  51. JavascriptError::SetErrorMessage(pError, hCode, varName, scriptContext);
  52. return library->CreateThrowErrorObject(pError);
  53. }
  54. RecyclableObject* ThrowErrorObject::CreateThrowTypeErrorObject(ScriptContext* scriptContext, int32 hCode, PCWSTR varName)
  55. {
  56. return CreateThrowErrorObject(&JavascriptLibrary::CreateTypeError, scriptContext, hCode, varName);
  57. }
  58. RecyclableObject* ThrowErrorObject::CreateThrowTypeErrorObject(ScriptContext* scriptContext, int32 hCode, JavascriptString* varName)
  59. {
  60. return CreateThrowTypeErrorObject(scriptContext, hCode, varName->GetSz());
  61. }
  62. }