ModuleScript.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  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. * A class that handles loading and evaluation of <script> elements.
  7. */
  8. #include "ModuleScript.h"
  9. #include "mozilla/HoldDropJSObjects.h"
  10. #include "ScriptLoader.h"
  11. namespace mozilla {
  12. namespace dom {
  13. // A single module script. May be used to satisfy multiple load requests.
  14. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ModuleScript)
  15. NS_INTERFACE_MAP_END
  16. NS_IMPL_CYCLE_COLLECTION_CLASS(ModuleScript)
  17. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ModuleScript)
  18. NS_IMPL_CYCLE_COLLECTION_UNLINK(mLoader)
  19. NS_IMPL_CYCLE_COLLECTION_UNLINK(mBaseURL)
  20. tmp->UnlinkModuleRecord();
  21. tmp->mParseError.setUndefined();
  22. tmp->mErrorToRethrow.setUndefined();
  23. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  24. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ModuleScript)
  25. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLoader)
  26. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  27. NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(ModuleScript)
  28. NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mModuleRecord)
  29. NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mParseError)
  30. NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mErrorToRethrow)
  31. NS_IMPL_CYCLE_COLLECTION_TRACE_END
  32. NS_IMPL_CYCLE_COLLECTING_ADDREF(ModuleScript)
  33. NS_IMPL_CYCLE_COLLECTING_RELEASE(ModuleScript)
  34. ModuleScript::ModuleScript(ScriptLoader *aLoader, nsIURI* aBaseURL)
  35. : mLoader(aLoader),
  36. mBaseURL(aBaseURL)
  37. {
  38. MOZ_ASSERT(mLoader);
  39. MOZ_ASSERT(mBaseURL);
  40. MOZ_ASSERT(!mModuleRecord);
  41. MOZ_ASSERT(!HasParseError());
  42. MOZ_ASSERT(!HasErrorToRethrow());
  43. }
  44. void
  45. ModuleScript::UnlinkModuleRecord()
  46. {
  47. // Remove module's back reference to this object request if present.
  48. if (mModuleRecord) {
  49. MOZ_ASSERT(JS::GetModuleHostDefinedField(mModuleRecord).toPrivate() ==
  50. this);
  51. JS::SetModuleHostDefinedField(mModuleRecord, JS::UndefinedValue());
  52. mModuleRecord = nullptr;
  53. }
  54. }
  55. ModuleScript::~ModuleScript()
  56. {
  57. // The object may be destroyed without being unlinked first.
  58. UnlinkModuleRecord();
  59. DropJSObjects(this);
  60. }
  61. void
  62. ModuleScript::SetModuleRecord(JS::Handle<JSObject*> aModuleRecord)
  63. {
  64. MOZ_ASSERT(!mModuleRecord);
  65. MOZ_ASSERT(!HasParseError());
  66. MOZ_ASSERT(!HasErrorToRethrow());
  67. mModuleRecord = aModuleRecord;
  68. // Make module's host defined field point to this module script object.
  69. // This is cleared in the UnlinkModuleRecord().
  70. JS::SetModuleHostDefinedField(mModuleRecord, JS::PrivateValue(this));
  71. HoldJSObjects(this);
  72. }
  73. void
  74. ModuleScript::SetParseError(const JS::Value& aError)
  75. {
  76. MOZ_ASSERT(!aError.isUndefined());
  77. MOZ_ASSERT(!HasParseError());
  78. MOZ_ASSERT(!HasErrorToRethrow());
  79. UnlinkModuleRecord();
  80. mParseError = aError;
  81. HoldJSObjects(this);
  82. }
  83. void
  84. ModuleScript::SetErrorToRethrow(const JS::Value& aError)
  85. {
  86. MOZ_ASSERT(!aError.isUndefined());
  87. MOZ_ASSERT(!HasErrorToRethrow());
  88. mErrorToRethrow = aError;
  89. }
  90. } // dom namespace
  91. } // mozilla namespace