ThrowErrorObject.cpp 3.5 KB

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