nsScriptErrorWithStack.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. * nsScriptErrorWithStack implementation.
  7. * a main-thread-only, cycle-collected subclass of nsScriptErrorBase
  8. * that can store a SavedFrame stack trace object.
  9. */
  10. #include "nsScriptError.h"
  11. #include "MainThreadUtils.h"
  12. #include "mozilla/Assertions.h"
  13. #include "mozilla/dom/ScriptSettings.h"
  14. #include "nsGlobalWindow.h"
  15. #include "nsCycleCollectionParticipant.h"
  16. using namespace mozilla::dom;
  17. namespace {
  18. static nsCString
  19. FormatStackString(JSContext* cx, JS::HandleObject aStack) {
  20. JS::RootedString formattedStack(cx);
  21. if (!JS::BuildStackString(cx, aStack, &formattedStack)) {
  22. return nsCString();
  23. }
  24. nsAutoJSString stackJSString;
  25. if (!stackJSString.init(cx, formattedStack)) {
  26. return nsCString();
  27. }
  28. return NS_ConvertUTF16toUTF8(stackJSString.get());
  29. }
  30. }
  31. NS_IMPL_CYCLE_COLLECTION_CLASS(nsScriptErrorWithStack)
  32. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsScriptErrorWithStack)
  33. tmp->mStack = nullptr;
  34. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  35. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsScriptErrorWithStack)
  36. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  37. NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsScriptErrorWithStack)
  38. NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mStack)
  39. NS_IMPL_CYCLE_COLLECTION_TRACE_END
  40. NS_IMPL_CYCLE_COLLECTING_ADDREF(nsScriptErrorWithStack)
  41. NS_IMPL_CYCLE_COLLECTING_RELEASE(nsScriptErrorWithStack)
  42. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsScriptErrorWithStack)
  43. NS_INTERFACE_MAP_ENTRY(nsISupports)
  44. NS_INTERFACE_MAP_ENTRY(nsIConsoleMessage)
  45. NS_INTERFACE_MAP_ENTRY(nsIScriptError)
  46. NS_INTERFACE_MAP_END
  47. nsScriptErrorWithStack::nsScriptErrorWithStack(JS::HandleObject aStack)
  48. : mStack(aStack)
  49. {
  50. MOZ_ASSERT(NS_IsMainThread(), "You can't use this class on workers.");
  51. mozilla::HoldJSObjects(this);
  52. }
  53. nsScriptErrorWithStack::~nsScriptErrorWithStack() {
  54. mozilla::DropJSObjects(this);
  55. }
  56. NS_IMETHODIMP
  57. nsScriptErrorWithStack::Init(const nsAString& message,
  58. const nsAString& sourceName,
  59. const nsAString& sourceLine,
  60. uint32_t lineNumber,
  61. uint32_t columnNumber,
  62. uint32_t flags,
  63. const char* category)
  64. {
  65. MOZ_CRASH("nsScriptErrorWithStack requires to be initialized with a document, by using InitWithWindowID");
  66. }
  67. NS_IMETHODIMP
  68. nsScriptErrorWithStack::GetStack(JS::MutableHandleValue aStack) {
  69. aStack.setObjectOrNull(mStack);
  70. return NS_OK;
  71. }
  72. NS_IMETHODIMP
  73. nsScriptErrorWithStack::ToString(nsACString& /*UTF8*/ aResult)
  74. {
  75. MOZ_ASSERT(NS_IsMainThread());
  76. nsCString message;
  77. nsresult rv = nsScriptErrorBase::ToString(message);
  78. NS_ENSURE_SUCCESS(rv, rv);
  79. if (!mStack) {
  80. aResult.Assign(message);
  81. return NS_OK;
  82. }
  83. AutoJSAPI jsapi;
  84. if (!jsapi.Init(mStack)) {
  85. return NS_ERROR_FAILURE;
  86. }
  87. JSContext* cx = jsapi.cx();
  88. JS::RootedObject stack(cx, mStack);
  89. nsCString stackString = FormatStackString(cx, stack);
  90. nsCString combined = message + NS_LITERAL_CSTRING("\n") + stackString;
  91. aResult.Assign(combined);
  92. return NS_OK;
  93. }