/* THIS FILE IS AUTOGENERATED FROM IntersectionObserver.webidl BY Codegen.py - DO NOT EDIT */ #include #include "AtomList.h" #include "DOMIntersectionObserver.h" #include "IntersectionObserverBinding.h" #include "MainThreadUtils.h" #include "WrapperFactory.h" #include "XrayWrapper.h" #include "js/Array.h" #include "js/CallAndConstruct.h" #include "js/Exception.h" #include "js/ForOfIterator.h" #include "js/MapAndSet.h" #include "js/Object.h" #include "js/PropertyAndElement.h" #include "js/PropertyDescriptor.h" #include "js/experimental/JitInfo.h" #include "mozilla/Atomics.h" #include "mozilla/FloatingPoint.h" #include "mozilla/OwningNonNull.h" #include "mozilla/ProfilerLabels.h" #include "mozilla/StaticPrefs_dom.h" #include "mozilla/dom/BindingCallContext.h" #include "mozilla/dom/BindingUtils.h" #include "mozilla/dom/DOMIntersectionObserver.h" #include "mozilla/dom/DOMJSClass.h" #include "mozilla/dom/DOMRect.h" #include "mozilla/dom/Document.h" #include "mozilla/dom/Element.h" #include "mozilla/dom/NonRefcountedDOMObject.h" #include "mozilla/dom/Nullable.h" #include "mozilla/dom/PrimitiveConversions.h" #include "mozilla/dom/ScriptSettings.h" #include "mozilla/dom/WebIDLPrefs.h" #include "mozilla/dom/XrayExpandoClass.h" #include "nsIContent.h" namespace mozilla::dom { namespace binding_detail {}; // Just to make sure it's known as a namespace using namespace mozilla::dom::binding_detail; void ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback, OwningElementOrDocument& aUnion, const char* aName, uint32_t aFlags) { if (aUnion.IsElement()) { ImplCycleCollectionTraverse(aCallback, aUnion.GetAsElement(), "mElement", aFlags); } else if (aUnion.IsDocument()) { ImplCycleCollectionTraverse(aCallback, aUnion.GetAsDocument(), "mDocument", aFlags); } } void ImplCycleCollectionUnlink(OwningElementOrDocument& aUnion) { aUnion.Uninit(); } bool DoubleOrDoubleSequence::TrySetToDouble(BindingCallContext& cx, JS::Handle value, bool& tryNext, bool passedToJSImpl) { tryNext = false; { // scope for memberSlot double& memberSlot = RawSetAsDouble(); if (!ValueToPrimitive(cx, value, "Double branch of (double or sequence)", &memberSlot)) { return false; } else if (!std::isfinite(memberSlot)) { cx.ThrowErrorMessage("Double branch of (double or sequence)"); return false; } } return true; } bool DoubleOrDoubleSequence::TrySetToDouble(JSContext* cx_, JS::Handle value, bool& tryNext, bool passedToJSImpl) { BindingCallContext cx(cx_, nullptr); return TrySetToDouble(cx, value, tryNext, passedToJSImpl); } bool DoubleOrDoubleSequence::TrySetToDoubleSequence(BindingCallContext& cx, JS::Handle value, bool& tryNext, bool passedToJSImpl) { tryNext = false; { // scope for memberSlot binding_detail::AutoSequence& memberSlot = RawSetAsDoubleSequence(); JS::ForOfIterator iter(cx); if (!iter.init(value, JS::ForOfIterator::AllowNonIterable)) { return false; } if (!iter.valueIsIterable()) { DestroyDoubleSequence(); tryNext = true; return true; } binding_detail::AutoSequence &arr = memberSlot; if (mozilla::Maybe sizeHint = iter.sizeHint()) { if (!arr.SetCapacity(*sizeHint, mozilla::fallible)) { JS_ReportOutOfMemory(cx); return false; } } JS::Rooted temp(cx); while (true) { bool done; if (!iter.next(&temp, &done)) { return false; } if (done) { break; } double* slotPtr = arr.AppendElement(mozilla::fallible); if (!slotPtr) { JS_ReportOutOfMemory(cx); return false; } double& slot = *slotPtr; if (!ValueToPrimitive(cx, temp, "Element of sequence branch of (double or sequence)", &slot)) { return false; } else if (!std::isfinite(slot)) { cx.ThrowErrorMessage("Element of sequence branch of (double or sequence)"); return false; } } } return true; } bool DoubleOrDoubleSequence::TrySetToDoubleSequence(JSContext* cx_, JS::Handle value, bool& tryNext, bool passedToJSImpl) { BindingCallContext cx(cx_, nullptr); return TrySetToDoubleSequence(cx, value, tryNext, passedToJSImpl); } bool DoubleOrDoubleSequence::Init(BindingCallContext& cx, JS::Handle value, const char* sourceDescription, bool passedToJSImpl) { MOZ_ASSERT(mType == eUninitialized); bool done = false, failed = false, tryNext; if (value.isObject()) { done = (failed = !TrySetToDoubleSequence(cx, value, tryNext, passedToJSImpl)) || !tryNext; } if (!done) { do { done = (failed = !TrySetToDouble(cx, value, tryNext)) || !tryNext; break; } while (false); } if (failed) { return false; } if (!done) { cx.ThrowErrorMessage(sourceDescription, "sequence"); return false; } return true; } bool DoubleOrDoubleSequence::Init(JSContext* cx_, JS::Handle value, const char* sourceDescription, bool passedToJSImpl) { BindingCallContext cx(cx_, nullptr); return Init(cx, value, sourceDescription, passedToJSImpl); } bool DoubleOrDoubleSequence::ToJSVal(JSContext* cx, JS::Handle scopeObj, JS::MutableHandle rval) const { switch (mType) { case eUninitialized: { return false; } case eDouble: { rval.set(JS::NumberValue(double(mValue.mDouble.Value()))); return true; } case eDoubleSequence: { uint32_t length = mValue.mDoubleSequence.Value().Length(); JS::Rooted returnArray(cx, JS::NewArrayObject(cx, length)); if (!returnArray) { return false; } // Scope for 'tmp' { JS::Rooted tmp(cx); for (uint32_t sequenceIdx0 = 0; sequenceIdx0 < length; ++sequenceIdx0) { // Control block to let us common up the JS_DefineElement calls when there // are different ways to succeed at wrapping the object. do { tmp.set(JS::NumberValue(double(mValue.mDoubleSequence.Value()[sequenceIdx0]))); break; } while (false); if (!JS_DefineElement(cx, returnArray, sequenceIdx0, tmp, JSPROP_ENUMERATE)) { return false; } } } rval.setObject(*returnArray); return true; } default: { return false; } } } OwningDoubleOrDoubleSequence::OwningDoubleOrDoubleSequence(OwningDoubleOrDoubleSequence&& aOther) : mType(eUninitialized) { switch (aOther.mType) { case eUninitialized: { MOZ_ASSERT(mType == eUninitialized, "We need to destroy ourselves?"); break; } case eDouble: { mType = eDouble; mValue.mDouble.SetValue(std::move(aOther.mValue.mDouble.Value())); break; } case eDoubleSequence: { mType = eDoubleSequence; mValue.mDoubleSequence.SetValue(std::move(aOther.mValue.mDoubleSequence.Value())); break; } } } bool OwningDoubleOrDoubleSequence::TrySetToDouble(BindingCallContext& cx, JS::Handle value, bool& tryNext, bool passedToJSImpl) { tryNext = false; { // scope for memberSlot double& memberSlot = RawSetAsDouble(); if (!ValueToPrimitive(cx, value, "Double branch of (double or sequence)", &memberSlot)) { return false; } else if (!std::isfinite(memberSlot)) { cx.ThrowErrorMessage("Double branch of (double or sequence)"); return false; } } return true; } bool OwningDoubleOrDoubleSequence::TrySetToDouble(JSContext* cx_, JS::Handle value, bool& tryNext, bool passedToJSImpl) { BindingCallContext cx(cx_, nullptr); return TrySetToDouble(cx, value, tryNext, passedToJSImpl); } [[nodiscard]] double& OwningDoubleOrDoubleSequence::RawSetAsDouble() { if (mType == eDouble) { return mValue.mDouble.Value(); } MOZ_ASSERT(mType == eUninitialized); mType = eDouble; return mValue.mDouble.SetValue(); } [[nodiscard]] double& OwningDoubleOrDoubleSequence::SetAsDouble() { if (mType == eDouble) { return mValue.mDouble.Value(); } Uninit(); mType = eDouble; return mValue.mDouble.SetValue(); } void OwningDoubleOrDoubleSequence::DestroyDouble() { MOZ_RELEASE_ASSERT(IsDouble(), "Wrong type!"); mValue.mDouble.Destroy(); mType = eUninitialized; } bool OwningDoubleOrDoubleSequence::TrySetToDoubleSequence(BindingCallContext& cx, JS::Handle value, bool& tryNext, bool passedToJSImpl) { tryNext = false; { // scope for memberSlot Sequence& memberSlot = RawSetAsDoubleSequence(); JS::ForOfIterator iter(cx); if (!iter.init(value, JS::ForOfIterator::AllowNonIterable)) { return false; } if (!iter.valueIsIterable()) { DestroyDoubleSequence(); tryNext = true; return true; } Sequence &arr = memberSlot; if (mozilla::Maybe sizeHint = iter.sizeHint()) { if (!arr.SetCapacity(*sizeHint, mozilla::fallible)) { JS_ReportOutOfMemory(cx); return false; } } JS::Rooted temp(cx); while (true) { bool done; if (!iter.next(&temp, &done)) { return false; } if (done) { break; } double* slotPtr = arr.AppendElement(mozilla::fallible); if (!slotPtr) { JS_ReportOutOfMemory(cx); return false; } double& slot = *slotPtr; if (!ValueToPrimitive(cx, temp, "Element of sequence branch of (double or sequence)", &slot)) { return false; } else if (!std::isfinite(slot)) { cx.ThrowErrorMessage("Element of sequence branch of (double or sequence)"); return false; } } } return true; } bool OwningDoubleOrDoubleSequence::TrySetToDoubleSequence(JSContext* cx_, JS::Handle value, bool& tryNext, bool passedToJSImpl) { BindingCallContext cx(cx_, nullptr); return TrySetToDoubleSequence(cx, value, tryNext, passedToJSImpl); } [[nodiscard]] Sequence& OwningDoubleOrDoubleSequence::RawSetAsDoubleSequence() { if (mType == eDoubleSequence) { return mValue.mDoubleSequence.Value(); } MOZ_ASSERT(mType == eUninitialized); mType = eDoubleSequence; return mValue.mDoubleSequence.SetValue(); } [[nodiscard]] Sequence& OwningDoubleOrDoubleSequence::SetAsDoubleSequence() { if (mType == eDoubleSequence) { return mValue.mDoubleSequence.Value(); } Uninit(); mType = eDoubleSequence; return mValue.mDoubleSequence.SetValue(); } void OwningDoubleOrDoubleSequence::DestroyDoubleSequence() { MOZ_RELEASE_ASSERT(IsDoubleSequence(), "Wrong type!"); mValue.mDoubleSequence.Destroy(); mType = eUninitialized; } bool OwningDoubleOrDoubleSequence::Init(BindingCallContext& cx, JS::Handle value, const char* sourceDescription, bool passedToJSImpl) { MOZ_ASSERT(mType == eUninitialized); bool done = false, failed = false, tryNext; if (value.isObject()) { done = (failed = !TrySetToDoubleSequence(cx, value, tryNext, passedToJSImpl)) || !tryNext; } if (!done) { do { done = (failed = !TrySetToDouble(cx, value, tryNext)) || !tryNext; break; } while (false); } if (failed) { return false; } if (!done) { cx.ThrowErrorMessage(sourceDescription, "sequence"); return false; } return true; } bool OwningDoubleOrDoubleSequence::Init(JSContext* cx_, JS::Handle value, const char* sourceDescription, bool passedToJSImpl) { BindingCallContext cx(cx_, nullptr); return Init(cx, value, sourceDescription, passedToJSImpl); } void OwningDoubleOrDoubleSequence::Uninit() { switch (mType) { case eUninitialized: { break; } case eDouble: { DestroyDouble(); break; } case eDoubleSequence: { DestroyDoubleSequence(); break; } } } bool OwningDoubleOrDoubleSequence::ToJSVal(JSContext* cx, JS::Handle scopeObj, JS::MutableHandle rval) const { switch (mType) { case eUninitialized: { return false; } case eDouble: { rval.set(JS::NumberValue(double(mValue.mDouble.Value()))); return true; } case eDoubleSequence: { uint32_t length = mValue.mDoubleSequence.Value().Length(); JS::Rooted returnArray(cx, JS::NewArrayObject(cx, length)); if (!returnArray) { return false; } // Scope for 'tmp' { JS::Rooted tmp(cx); for (uint32_t sequenceIdx0 = 0; sequenceIdx0 < length; ++sequenceIdx0) { // Control block to let us common up the JS_DefineElement calls when there // are different ways to succeed at wrapping the object. do { tmp.set(JS::NumberValue(double(mValue.mDoubleSequence.Value()[sequenceIdx0]))); break; } while (false); if (!JS_DefineElement(cx, returnArray, sequenceIdx0, tmp, JSPROP_ENUMERATE)) { return false; } } } rval.setObject(*returnArray); return true; } default: { return false; } } } OwningDoubleOrDoubleSequence& OwningDoubleOrDoubleSequence::operator=(OwningDoubleOrDoubleSequence&& aOther) { this->~OwningDoubleOrDoubleSequence(); new (this) OwningDoubleOrDoubleSequence (std::move(aOther)); return *this; } OwningDoubleOrDoubleSequence& OwningDoubleOrDoubleSequence::operator=(const OwningDoubleOrDoubleSequence& aOther) { switch (aOther.mType) { case eUninitialized: { MOZ_ASSERT(mType == eUninitialized, "We need to destroy ourselves?"); break; } case eDouble: { SetAsDouble() = aOther.GetAsDouble(); break; } case eDoubleSequence: { SetAsDoubleSequence() = aOther.GetAsDoubleSequence(); break; } } return *this; } bool ElementOrDocument::TrySetToElement(BindingCallContext& cx, JS::Handle value, bool& tryNext, bool passedToJSImpl) { tryNext = false; { // scope for memberSlot NonNull& memberSlot = RawSetAsElement(); { // Our JSContext should be in the right global to do unwrapping in. nsresult rv = UnwrapObject(value, memberSlot, cx); if (NS_FAILED(rv)) { DestroyElement(); tryNext = true; return true; } } } return true; } bool ElementOrDocument::TrySetToElement(JSContext* cx_, JS::Handle value, bool& tryNext, bool passedToJSImpl) { BindingCallContext cx(cx_, nullptr); return TrySetToElement(cx, value, tryNext, passedToJSImpl); } bool ElementOrDocument::TrySetToDocument(BindingCallContext& cx, JS::Handle value, bool& tryNext, bool passedToJSImpl) { tryNext = false; { // scope for memberSlot NonNull& memberSlot = RawSetAsDocument(); { // Our JSContext should be in the right global to do unwrapping in. nsresult rv = UnwrapObject(value, memberSlot, cx); if (NS_FAILED(rv)) { DestroyDocument(); tryNext = true; return true; } } } return true; } bool ElementOrDocument::TrySetToDocument(JSContext* cx_, JS::Handle value, bool& tryNext, bool passedToJSImpl) { BindingCallContext cx(cx_, nullptr); return TrySetToDocument(cx, value, tryNext, passedToJSImpl); } bool ElementOrDocument::Init(BindingCallContext& cx, JS::Handle value, const char* sourceDescription, bool passedToJSImpl) { MOZ_ASSERT(mType == eUninitialized); bool done = false, failed = false, tryNext; if (value.isObject()) { done = (failed = !TrySetToElement(cx, value, tryNext, passedToJSImpl)) || !tryNext || (failed = !TrySetToDocument(cx, value, tryNext, passedToJSImpl)) || !tryNext; } if (failed) { return false; } if (!done) { cx.ThrowErrorMessage(sourceDescription, "Element, Document"); return false; } return true; } bool ElementOrDocument::Init(JSContext* cx_, JS::Handle value, const char* sourceDescription, bool passedToJSImpl) { BindingCallContext cx(cx_, nullptr); return Init(cx, value, sourceDescription, passedToJSImpl); } bool ElementOrDocument::ToJSVal(JSContext* cx, JS::Handle scopeObj, JS::MutableHandle rval) const { switch (mType) { case eUninitialized: { return false; } case eElement: { if (!GetOrCreateDOMReflector(cx, mValue.mElement.Value(), rval)) { MOZ_ASSERT(JS_IsExceptionPending(cx)); return false; } return true; } case eDocument: { if (!GetOrCreateDOMReflector(cx, mValue.mDocument.Value(), rval)) { MOZ_ASSERT(JS_IsExceptionPending(cx)); return false; } return true; } default: { return false; } } } OwningElementOrDocument::OwningElementOrDocument(OwningElementOrDocument&& aOther) : mType(eUninitialized) { switch (aOther.mType) { case eUninitialized: { MOZ_ASSERT(mType == eUninitialized, "We need to destroy ourselves?"); break; } case eElement: { mType = eElement; mValue.mElement.SetValue(std::move(aOther.mValue.mElement.Value())); break; } case eDocument: { mType = eDocument; mValue.mDocument.SetValue(std::move(aOther.mValue.mDocument.Value())); break; } } } bool OwningElementOrDocument::TrySetToElement(BindingCallContext& cx, JS::Handle value, bool& tryNext, bool passedToJSImpl) { tryNext = false; { // scope for memberSlot OwningNonNull& memberSlot = RawSetAsElement(); static_assert(IsRefcounted::value, "We can only store refcounted classes."); { // Our JSContext should be in the right global to do unwrapping in. nsresult rv = UnwrapObject(value, memberSlot, cx); if (NS_FAILED(rv)) { DestroyElement(); tryNext = true; return true; } } } return true; } bool OwningElementOrDocument::TrySetToElement(JSContext* cx_, JS::Handle value, bool& tryNext, bool passedToJSImpl) { BindingCallContext cx(cx_, nullptr); return TrySetToElement(cx, value, tryNext, passedToJSImpl); } [[nodiscard]] OwningNonNull& OwningElementOrDocument::RawSetAsElement() { if (mType == eElement) { return mValue.mElement.Value(); } MOZ_ASSERT(mType == eUninitialized); mType = eElement; return mValue.mElement.SetValue(); } [[nodiscard]] OwningNonNull& OwningElementOrDocument::SetAsElement() { if (mType == eElement) { return mValue.mElement.Value(); } Uninit(); mType = eElement; return mValue.mElement.SetValue(); } void OwningElementOrDocument::DestroyElement() { MOZ_RELEASE_ASSERT(IsElement(), "Wrong type!"); mValue.mElement.Destroy(); mType = eUninitialized; } bool OwningElementOrDocument::TrySetToDocument(BindingCallContext& cx, JS::Handle value, bool& tryNext, bool passedToJSImpl) { tryNext = false; { // scope for memberSlot OwningNonNull& memberSlot = RawSetAsDocument(); static_assert(IsRefcounted::value, "We can only store refcounted classes."); { // Our JSContext should be in the right global to do unwrapping in. nsresult rv = UnwrapObject(value, memberSlot, cx); if (NS_FAILED(rv)) { DestroyDocument(); tryNext = true; return true; } } } return true; } bool OwningElementOrDocument::TrySetToDocument(JSContext* cx_, JS::Handle value, bool& tryNext, bool passedToJSImpl) { BindingCallContext cx(cx_, nullptr); return TrySetToDocument(cx, value, tryNext, passedToJSImpl); } [[nodiscard]] OwningNonNull& OwningElementOrDocument::RawSetAsDocument() { if (mType == eDocument) { return mValue.mDocument.Value(); } MOZ_ASSERT(mType == eUninitialized); mType = eDocument; return mValue.mDocument.SetValue(); } [[nodiscard]] OwningNonNull& OwningElementOrDocument::SetAsDocument() { if (mType == eDocument) { return mValue.mDocument.Value(); } Uninit(); mType = eDocument; return mValue.mDocument.SetValue(); } void OwningElementOrDocument::DestroyDocument() { MOZ_RELEASE_ASSERT(IsDocument(), "Wrong type!"); mValue.mDocument.Destroy(); mType = eUninitialized; } bool OwningElementOrDocument::Init(BindingCallContext& cx, JS::Handle value, const char* sourceDescription, bool passedToJSImpl) { MOZ_ASSERT(mType == eUninitialized); bool done = false, failed = false, tryNext; if (value.isObject()) { done = (failed = !TrySetToElement(cx, value, tryNext, passedToJSImpl)) || !tryNext || (failed = !TrySetToDocument(cx, value, tryNext, passedToJSImpl)) || !tryNext; } if (failed) { return false; } if (!done) { cx.ThrowErrorMessage(sourceDescription, "Element, Document"); return false; } return true; } bool OwningElementOrDocument::Init(JSContext* cx_, JS::Handle value, const char* sourceDescription, bool passedToJSImpl) { BindingCallContext cx(cx_, nullptr); return Init(cx, value, sourceDescription, passedToJSImpl); } void OwningElementOrDocument::Uninit() { switch (mType) { case eUninitialized: { break; } case eElement: { DestroyElement(); break; } case eDocument: { DestroyDocument(); break; } } } bool OwningElementOrDocument::ToJSVal(JSContext* cx, JS::Handle scopeObj, JS::MutableHandle rval) const { switch (mType) { case eUninitialized: { return false; } case eElement: { if (!GetOrCreateDOMReflector(cx, mValue.mElement.Value(), rval)) { MOZ_ASSERT(JS_IsExceptionPending(cx)); return false; } return true; } case eDocument: { if (!GetOrCreateDOMReflector(cx, mValue.mDocument.Value(), rval)) { MOZ_ASSERT(JS_IsExceptionPending(cx)); return false; } return true; } default: { return false; } } } OwningElementOrDocument& OwningElementOrDocument::operator=(OwningElementOrDocument&& aOther) { this->~OwningElementOrDocument(); new (this) OwningElementOrDocument (std::move(aOther)); return *this; } OwningElementOrDocument& OwningElementOrDocument::operator=(const OwningElementOrDocument& aOther) { switch (aOther.mType) { case eUninitialized: { MOZ_ASSERT(mType == eUninitialized, "We need to destroy ourselves?"); break; } case eElement: { SetAsElement() = aOther.GetAsElement(); break; } case eDocument: { SetAsDocument() = aOther.GetAsDocument(); break; } } return *this; } MOZ_CAN_RUN_SCRIPT void IntersectionCallback::Call(BindingCallContext& cx, JS::Handle aThisVal, const Sequence>& entries, DOMIntersectionObserver& observer, ErrorResult& aRv) { JS::Rooted rval(cx); JS::RootedVector argv(cx); if (!argv.resize(2)) { // That threw an exception on the JSContext, and our CallSetup will do // the right thing with that. return; } unsigned argc = 2; do { if (!GetOrCreateDOMReflector(cx, observer, argv[1])) { MOZ_ASSERT(JS_IsExceptionPending(cx)); aRv.Throw(NS_ERROR_UNEXPECTED); return; } break; } while (false); do { uint32_t length = entries.Length(); JS::Rooted returnArray(cx, JS::NewArrayObject(cx, length)); if (!returnArray) { aRv.Throw(NS_ERROR_UNEXPECTED); return; } // Scope for 'tmp' { JS::Rooted tmp(cx); for (uint32_t sequenceIdx0 = 0; sequenceIdx0 < length; ++sequenceIdx0) { // Control block to let us common up the JS_DefineElement calls when there // are different ways to succeed at wrapping the object. do { if (!GetOrCreateDOMReflector(cx, entries[sequenceIdx0], &tmp)) { MOZ_ASSERT(JS_IsExceptionPending(cx)); aRv.Throw(NS_ERROR_UNEXPECTED); return; } break; } while (false); if (!JS_DefineElement(cx, returnArray, sequenceIdx0, tmp, JSPROP_ENUMERATE)) { aRv.Throw(NS_ERROR_UNEXPECTED); return; } } } argv[0].setObject(*returnArray); break; } while (false); JS::Rooted callable(cx, JS::ObjectValue(*mCallback)); if (!JS::Call(cx, aThisVal, callable, JS::HandleValueArray::subarray(argv, 0, argc), &rval)) { aRv.NoteJSContextException(cx); return; } } IntersectionObserverEntryInit::IntersectionObserverEntryInit() : mBoundingClientRect(FastDictionaryInitializer()), mIntersectionRect(FastDictionaryInitializer()), mRootBounds(FastDictionaryInitializer()) { // Safe to pass a null context if we pass a null value Init(); } bool IntersectionObserverEntryInit::InitIds(JSContext* cx, IntersectionObserverEntryInitAtoms* atomsCache) { MOZ_ASSERT(reinterpret_cast(atomsCache)->isVoid()); // Initialize these in reverse order so that any failure leaves the first one // uninitialized. if (!atomsCache->time_id.init(cx, "time") || !atomsCache->target_id.init(cx, "target") || !atomsCache->rootBounds_id.init(cx, "rootBounds") || !atomsCache->intersectionRect_id.init(cx, "intersectionRect") || !atomsCache->boundingClientRect_id.init(cx, "boundingClientRect")) { return false; } return true; } bool IntersectionObserverEntryInit::Init(const char* sourceDescription, bool passedToJSImpl) { return true; } void IntersectionObserverEntryInit::TraceDictionary(JSTracer* trc) { } IntersectionObserverEntryInit& IntersectionObserverEntryInit::operator=(const IntersectionObserverEntryInit& aOther) { DictionaryBase::operator=(aOther); mBoundingClientRect = aOther.mBoundingClientRect; mIntersectionRect = aOther.mIntersectionRect; mRootBounds = aOther.mRootBounds; mTarget = aOther.mTarget; mTime = aOther.mTime; return *this; } IntersectionObserverInit::IntersectionObserverInit() { // Safe to pass a null context if we pass a null value Init(nullptr, JS::NullHandleValue); } bool IntersectionObserverInit::InitIds(JSContext* cx, IntersectionObserverInitAtoms* atomsCache) { MOZ_ASSERT(reinterpret_cast(atomsCache)->isVoid()); // Initialize these in reverse order so that any failure leaves the first one // uninitialized. if (!atomsCache->threshold_id.init(cx, "threshold") || !atomsCache->scrollMargin_id.init(cx, "scrollMargin") || !atomsCache->rootMargin_id.init(cx, "rootMargin") || !atomsCache->root_id.init(cx, "root")) { return false; } return true; } bool IntersectionObserverInit::Init(BindingCallContext& cx, JS::Handle val, const char* sourceDescription, bool passedToJSImpl) { // Passing a null JSContext is OK only if we're initing from null, // Since in that case we will not have to do any property gets // Also evaluate isNullOrUndefined in order to avoid false-positive // checkers by static analysis tools MOZ_ASSERT_IF(!cx, val.isNull() && val.isNullOrUndefined()); IntersectionObserverInitAtoms* atomsCache = nullptr; if (cx) { atomsCache = GetAtomCache(cx); if (reinterpret_cast(atomsCache)->isVoid() && !InitIds(cx, atomsCache)) { return false; } } if (!IsConvertibleToDictionary(val)) { return cx.ThrowErrorMessage(sourceDescription, "dictionary"); } bool isNull = val.isNullOrUndefined(); // We only need these if !isNull, in which case we have |cx|. Maybe > object; Maybe > temp; if (!isNull) { MOZ_ASSERT(cx); object.emplace(cx, &val.toObject()); temp.emplace(cx); } if (!isNull) { if (!JS_GetPropertyById(cx, *object, atomsCache->root_id, temp.ptr())) { return false; } } if (!(!isNull && !temp->isUndefined()) || temp.ref().isNullOrUndefined()) { mRoot.SetNull(); } else { if (!mRoot.SetValue().Init(cx, temp.ref(), "'root' member of IntersectionObserverInit", passedToJSImpl)) { return false; } } mIsAnyMemberPresent = true; if (!isNull) { if (!JS_GetPropertyById(cx, *object, atomsCache->rootMargin_id, temp.ptr())) { return false; } } if (!isNull && !temp->isUndefined()) { if (!ConvertJSValueToString(cx, temp.ref(), eStringify, eStringify, mRootMargin)) { return false; } } else { mRootMargin.AssignLiteral("0px"); } mIsAnyMemberPresent = true; if (!isNull) { if (StaticPrefs::dom_intersection_observer_scroll_margin_enabled()) { if (!JS_GetPropertyById(cx, *object, atomsCache->scrollMargin_id, temp.ptr())) { return false; } } else { temp->setUndefined(); } } if (!isNull && !temp->isUndefined()) { if (!ConvertJSValueToString(cx, temp.ref(), eStringify, eStringify, mScrollMargin)) { return false; } } else { mScrollMargin.AssignLiteral("0px"); } mIsAnyMemberPresent = true; if (!isNull) { if (!JS_GetPropertyById(cx, *object, atomsCache->threshold_id, temp.ptr())) { return false; } } mThreshold.Uninit(); if (!(!isNull && !temp->isUndefined())) { mThreshold.RawSetAsDouble() = 0.0; } else { if (!mThreshold.Init(cx, temp.ref(), "'threshold' member of IntersectionObserverInit", passedToJSImpl)) { return false; } } mIsAnyMemberPresent = true; return true; } bool IntersectionObserverInit::Init(JSContext* cx_, JS::Handle val, const char* sourceDescription, bool passedToJSImpl) { // We don't want to use sourceDescription for our context here; // that's not really what it's formatted for. BindingCallContext cx(cx_, nullptr); return Init(cx, val, sourceDescription, passedToJSImpl); } void IntersectionObserverInit::TraceDictionary(JSTracer* trc) { } IntersectionObserverInit& IntersectionObserverInit::operator=(const IntersectionObserverInit& aOther) { MaybeEmptyDictionaryBase::operator=(aOther); mRoot = aOther.mRoot; mRootMargin = aOther.mRootMargin; mScrollMargin = aOther.mScrollMargin; mThreshold = aOther.mThreshold; return *this; } namespace IntersectionObserver_Binding { MOZ_CAN_RUN_SCRIPT static bool get_root(JSContext* cx, JS::Handle obj, void* void_self, JSJitGetterCallArgs args) { AUTO_PROFILER_LABEL_DYNAMIC_FAST( "IntersectionObserver", "root", DOM, cx, uint32_t(js::ProfilingStackFrame::Flags::STRING_TEMPLATE_GETTER) | uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS)); auto* self = static_cast(void_self); auto result(StrongOrRawPtr(MOZ_KnownLive(self)->GetRoot())); MOZ_ASSERT(!JS_IsExceptionPending(cx)); if (!result) { args.rval().setNull(); return true; } if (!GetOrCreateDOMReflector(cx, result, args.rval())) { MOZ_ASSERT(JS_IsExceptionPending(cx)); return false; } return true; } static const JSJitInfo root_getterinfo = { { get_root }, { prototypes::id::IntersectionObserver }, { PrototypeTraits::Depth }, JSJitInfo::Getter, JSJitInfo::AliasNone, /* aliasSet. Not relevant for setters. */ JSVAL_TYPE_UNKNOWN, /* returnType. Not relevant for setters. */ false, /* isInfallible. False in setters. */ true, /* isMovable. Not relevant for setters. */ true, /* isEliminatable. Not relevant for setters. */ false, /* isAlwaysInSlot. Only relevant for getters. */ false, /* isLazilyCachedInSlot. Only relevant for getters. */ false, /* isTypedMethod. Only relevant for methods. */ 0 /* Reserved slot index, if we're stored in a slot, else 0. */ }; MOZ_CAN_RUN_SCRIPT static bool get_rootMargin(JSContext* cx, JS::Handle obj, void* void_self, JSJitGetterCallArgs args) { AUTO_PROFILER_LABEL_DYNAMIC_FAST( "IntersectionObserver", "rootMargin", DOM, cx, uint32_t(js::ProfilingStackFrame::Flags::STRING_TEMPLATE_GETTER) | uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS)); auto* self = static_cast(void_self); nsAutoCString result; // NOTE: This assert does NOT call the function. static_assert(std::is_void_vGetRootMargin(result))>, "Should be returning void here"); MOZ_KnownLive(self)->GetRootMargin(result); MOZ_ASSERT(!JS_IsExceptionPending(cx)); if (!NonVoidUTF8StringToJsval(cx, result, args.rval())) { return false; } return true; } static const JSJitInfo rootMargin_getterinfo = { { get_rootMargin }, { prototypes::id::IntersectionObserver }, { PrototypeTraits::Depth }, JSJitInfo::Getter, JSJitInfo::AliasNone, /* aliasSet. Not relevant for setters. */ JSVAL_TYPE_STRING, /* returnType. Not relevant for setters. */ false, /* isInfallible. False in setters. */ true, /* isMovable. Not relevant for setters. */ true, /* isEliminatable. Not relevant for setters. */ false, /* isAlwaysInSlot. Only relevant for getters. */ false, /* isLazilyCachedInSlot. Only relevant for getters. */ false, /* isTypedMethod. Only relevant for methods. */ 0 /* Reserved slot index, if we're stored in a slot, else 0. */ }; MOZ_CAN_RUN_SCRIPT static bool get_scrollMargin(JSContext* cx, JS::Handle obj, void* void_self, JSJitGetterCallArgs args) { AUTO_PROFILER_LABEL_DYNAMIC_FAST( "IntersectionObserver", "scrollMargin", DOM, cx, uint32_t(js::ProfilingStackFrame::Flags::STRING_TEMPLATE_GETTER) | uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS)); auto* self = static_cast(void_self); // Have to either root across the getter call or reget after. bool isXray; JS::Rooted slotStorage(cx, GetCachedSlotStorageObject(cx, obj, &isXray)); if (!slotStorage) { return false; } const size_t slotIndex = isXray ? (DOM_EXPANDO_RESERVED_SLOTS + 0) : (DOM_INSTANCE_RESERVED_SLOTS + 0); MOZ_ASSERT(slotIndex < JSCLASS_RESERVED_SLOTS(JS::GetClass(slotStorage))); { // Scope for cachedVal JS::Value cachedVal = JS::GetNativeObjectReservedSlot(slotStorage, slotIndex); if (!cachedVal.isUndefined()) { args.rval().set(cachedVal); // The cached value is in the compartment of slotStorage, // so wrap into the caller compartment as needed. return MaybeWrapValue(cx, args.rval()); } } nsAutoCString result; // NOTE: This assert does NOT call the function. static_assert(std::is_void_vGetScrollMargin(result))>, "Should be returning void here"); MOZ_KnownLive(self)->GetScrollMargin(result); MOZ_ASSERT(!JS_IsExceptionPending(cx)); { JS::Rooted conversionScope(cx, isXray ? JS::CurrentGlobalOrNull(cx) : slotStorage); JSAutoRealm ar(cx, conversionScope); do { // block we break out of when done wrapping if (!NonVoidUTF8StringToJsval(cx, result, args.rval())) { return false; } break; } while (false); } { // And now store things in the realm of our slotStorage. JSAutoRealm ar(cx, slotStorage); // Make a copy so that we don't do unnecessary wrapping on args.rval(). JS::Rooted storedVal(cx, args.rval()); if (!MaybeWrapValue(cx, &storedVal)) { return false; } JS::SetNativeObjectReservedSlot(slotStorage, slotIndex, storedVal); if (!isXray) { // In the Xray case we don't need to do this, because getting the // expando object already preserved our wrapper. PreserveWrapper(self); } } // And now make sure args.rval() is in the caller realm. return MaybeWrapValue(cx, args.rval()); } static const JSJitInfo scrollMargin_getterinfo = { { get_scrollMargin }, { prototypes::id::IntersectionObserver }, { PrototypeTraits::Depth }, JSJitInfo::Getter, JSJitInfo::AliasNone, /* aliasSet. Not relevant for setters. */ JSVAL_TYPE_STRING, /* returnType. Not relevant for setters. */ false, /* isInfallible. False in setters. */ true, /* isMovable. Not relevant for setters. */ true, /* isEliminatable. Not relevant for setters. */ false, /* isAlwaysInSlot. Only relevant for getters. */ true, /* isLazilyCachedInSlot. Only relevant for getters. */ false, /* isTypedMethod. Only relevant for methods. */ (DOM_INSTANCE_RESERVED_SLOTS + 0) /* Reserved slot index, if we're stored in a slot, else 0. */ }; static_assert((DOM_INSTANCE_RESERVED_SLOTS + 0) <= JSJitInfo::maxSlotIndex, "We won't fit"); static_assert((DOM_INSTANCE_RESERVED_SLOTS + 0) < 3, "There is no slot for us"); MOZ_CAN_RUN_SCRIPT static bool get_thresholds(JSContext* cx, JS::Handle obj, void* void_self, JSJitGetterCallArgs args) { AUTO_PROFILER_LABEL_DYNAMIC_FAST( "IntersectionObserver", "thresholds", DOM, cx, uint32_t(js::ProfilingStackFrame::Flags::STRING_TEMPLATE_GETTER) | uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS)); auto* self = static_cast(void_self); // Have to either root across the getter call or reget after. bool isXray; JS::Rooted slotStorage(cx, GetCachedSlotStorageObject(cx, obj, &isXray)); if (!slotStorage) { return false; } const size_t slotIndex = isXray ? (DOM_EXPANDO_RESERVED_SLOTS + 1) : (DOM_INSTANCE_RESERVED_SLOTS + 1); MOZ_ASSERT(slotIndex < JSCLASS_RESERVED_SLOTS(JS::GetClass(slotStorage))); { // Scope for cachedVal JS::Value cachedVal = JS::GetNativeObjectReservedSlot(slotStorage, slotIndex); if (!cachedVal.isUndefined()) { args.rval().set(cachedVal); // The cached value is in the compartment of slotStorage, // so wrap into the caller compartment as needed. return MaybeWrapNonDOMObjectValue(cx, args.rval()); } } nsTArray result; // NOTE: This assert does NOT call the function. static_assert(std::is_void_vGetThresholds(result))>, "Should be returning void here"); MOZ_KnownLive(self)->GetThresholds(result); MOZ_ASSERT(!JS_IsExceptionPending(cx)); { JS::Rooted conversionScope(cx, isXray ? JS::CurrentGlobalOrNull(cx) : slotStorage); JSAutoRealm ar(cx, conversionScope); do { // block we break out of when done wrapping uint32_t length = result.Length(); JS::Rooted returnArray(cx, JS::NewArrayObject(cx, length)); if (!returnArray) { return false; } // Scope for 'tmp' { JS::Rooted tmp(cx); for (uint32_t sequenceIdx0 = 0; sequenceIdx0 < length; ++sequenceIdx0) { // Control block to let us common up the JS_DefineElement calls when there // are different ways to succeed at wrapping the object. do { tmp.set(JS::NumberValue(double(result[sequenceIdx0]))); break; } while (false); if (!JS_DefineElement(cx, returnArray, sequenceIdx0, tmp, JSPROP_ENUMERATE)) { return false; } } } args.rval().setObject(*returnArray); break; } while (false); } { // And now store things in the realm of our slotStorage. JSAutoRealm ar(cx, slotStorage); // Make a copy so that we don't do unnecessary wrapping on args.rval(). JS::Rooted storedVal(cx, args.rval()); if (!MaybeWrapNonDOMObjectValue(cx, &storedVal)) { return false; } JS::SetNativeObjectReservedSlot(slotStorage, slotIndex, storedVal); if (!isXray) { // In the Xray case we don't need to do this, because getting the // expando object already preserved our wrapper. PreserveWrapper(self); } } // And now make sure args.rval() is in the caller realm. return MaybeWrapNonDOMObjectValue(cx, args.rval()); } static const JSJitInfo thresholds_getterinfo = { { get_thresholds }, { prototypes::id::IntersectionObserver }, { PrototypeTraits::Depth }, JSJitInfo::Getter, JSJitInfo::AliasNone, /* aliasSet. Not relevant for setters. */ JSVAL_TYPE_OBJECT, /* returnType. Not relevant for setters. */ false, /* isInfallible. False in setters. */ true, /* isMovable. Not relevant for setters. */ true, /* isEliminatable. Not relevant for setters. */ false, /* isAlwaysInSlot. Only relevant for getters. */ true, /* isLazilyCachedInSlot. Only relevant for getters. */ false, /* isTypedMethod. Only relevant for methods. */ (DOM_INSTANCE_RESERVED_SLOTS + 1) /* Reserved slot index, if we're stored in a slot, else 0. */ }; static_assert((DOM_INSTANCE_RESERVED_SLOTS + 1) <= JSJitInfo::maxSlotIndex, "We won't fit"); static_assert((DOM_INSTANCE_RESERVED_SLOTS + 1) < 3, "There is no slot for us"); MOZ_CAN_RUN_SCRIPT static bool observe(JSContext* cx_, JS::Handle obj, void* void_self, const JSJitMethodCallArgs& args) { BindingCallContext cx(cx_, "IntersectionObserver.observe"); AUTO_PROFILER_LABEL_DYNAMIC_FAST( "IntersectionObserver", "observe", DOM, cx, uint32_t(js::ProfilingStackFrame::Flags::STRING_TEMPLATE_METHOD) | uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS)); auto* self = static_cast(void_self); if (!args.requireAtLeast(cx, "IntersectionObserver.observe", 1)) { return false; } NonNull arg0; if (args[0].isObject()) { { // Our JSContext should be in the right global to do unwrapping in. nsresult rv = UnwrapObject(args[0], arg0, cx); if (NS_FAILED(rv)) { cx.ThrowErrorMessage("Argument 1", "Element"); return false; } } } else { cx.ThrowErrorMessage("Argument 1"); return false; } // NOTE: This assert does NOT call the function. static_assert(std::is_void_vObserve(MOZ_KnownLive(NonNullHelper(arg0))))>, "Should be returning void here"); MOZ_KnownLive(self)->Observe(MOZ_KnownLive(NonNullHelper(arg0))); MOZ_ASSERT(!JS_IsExceptionPending(cx)); args.rval().setUndefined(); return true; } static const JSJitInfo observe_methodinfo = { { (JSJitGetterOp)observe }, { prototypes::id::IntersectionObserver }, { PrototypeTraits::Depth }, JSJitInfo::Method, JSJitInfo::AliasEverything, /* aliasSet. Not relevant for setters. */ JSVAL_TYPE_UNDEFINED, /* returnType. Not relevant for setters. */ false, /* isInfallible. False in setters. */ false, /* isMovable. Not relevant for setters. */ false, /* isEliminatable. Not relevant for setters. */ false, /* isAlwaysInSlot. Only relevant for getters. */ false, /* isLazilyCachedInSlot. Only relevant for getters. */ false, /* isTypedMethod. Only relevant for methods. */ 0 /* Reserved slot index, if we're stored in a slot, else 0. */ }; MOZ_CAN_RUN_SCRIPT static bool unobserve(JSContext* cx_, JS::Handle obj, void* void_self, const JSJitMethodCallArgs& args) { BindingCallContext cx(cx_, "IntersectionObserver.unobserve"); AUTO_PROFILER_LABEL_DYNAMIC_FAST( "IntersectionObserver", "unobserve", DOM, cx, uint32_t(js::ProfilingStackFrame::Flags::STRING_TEMPLATE_METHOD) | uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS)); auto* self = static_cast(void_self); if (!args.requireAtLeast(cx, "IntersectionObserver.unobserve", 1)) { return false; } NonNull arg0; if (args[0].isObject()) { { // Our JSContext should be in the right global to do unwrapping in. nsresult rv = UnwrapObject(args[0], arg0, cx); if (NS_FAILED(rv)) { cx.ThrowErrorMessage("Argument 1", "Element"); return false; } } } else { cx.ThrowErrorMessage("Argument 1"); return false; } // NOTE: This assert does NOT call the function. static_assert(std::is_void_vUnobserve(MOZ_KnownLive(NonNullHelper(arg0))))>, "Should be returning void here"); MOZ_KnownLive(self)->Unobserve(MOZ_KnownLive(NonNullHelper(arg0))); MOZ_ASSERT(!JS_IsExceptionPending(cx)); args.rval().setUndefined(); return true; } static const JSJitInfo unobserve_methodinfo = { { (JSJitGetterOp)unobserve }, { prototypes::id::IntersectionObserver }, { PrototypeTraits::Depth }, JSJitInfo::Method, JSJitInfo::AliasEverything, /* aliasSet. Not relevant for setters. */ JSVAL_TYPE_UNDEFINED, /* returnType. Not relevant for setters. */ false, /* isInfallible. False in setters. */ false, /* isMovable. Not relevant for setters. */ false, /* isEliminatable. Not relevant for setters. */ false, /* isAlwaysInSlot. Only relevant for getters. */ false, /* isLazilyCachedInSlot. Only relevant for getters. */ false, /* isTypedMethod. Only relevant for methods. */ 0 /* Reserved slot index, if we're stored in a slot, else 0. */ }; MOZ_CAN_RUN_SCRIPT static bool disconnect(JSContext* cx, JS::Handle obj, void* void_self, const JSJitMethodCallArgs& args) { AUTO_PROFILER_LABEL_DYNAMIC_FAST( "IntersectionObserver", "disconnect", DOM, cx, uint32_t(js::ProfilingStackFrame::Flags::STRING_TEMPLATE_METHOD) | uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS)); auto* self = static_cast(void_self); // NOTE: This assert does NOT call the function. static_assert(std::is_void_vDisconnect())>, "Should be returning void here"); MOZ_KnownLive(self)->Disconnect(); MOZ_ASSERT(!JS_IsExceptionPending(cx)); args.rval().setUndefined(); return true; } static const JSJitInfo disconnect_methodinfo = { { (JSJitGetterOp)disconnect }, { prototypes::id::IntersectionObserver }, { PrototypeTraits::Depth }, JSJitInfo::Method, JSJitInfo::AliasEverything, /* aliasSet. Not relevant for setters. */ JSVAL_TYPE_UNDEFINED, /* returnType. Not relevant for setters. */ true, /* isInfallible. False in setters. */ false, /* isMovable. Not relevant for setters. */ false, /* isEliminatable. Not relevant for setters. */ false, /* isAlwaysInSlot. Only relevant for getters. */ false, /* isLazilyCachedInSlot. Only relevant for getters. */ false, /* isTypedMethod. Only relevant for methods. */ 0 /* Reserved slot index, if we're stored in a slot, else 0. */ }; MOZ_CAN_RUN_SCRIPT static bool takeRecords(JSContext* cx, JS::Handle obj, void* void_self, const JSJitMethodCallArgs& args) { AUTO_PROFILER_LABEL_DYNAMIC_FAST( "IntersectionObserver", "takeRecords", DOM, cx, uint32_t(js::ProfilingStackFrame::Flags::STRING_TEMPLATE_METHOD) | uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS)); auto* self = static_cast(void_self); nsTArray> result; // NOTE: This assert does NOT call the function. static_assert(std::is_void_vTakeRecords(result))>, "Should be returning void here"); MOZ_KnownLive(self)->TakeRecords(result); MOZ_ASSERT(!JS_IsExceptionPending(cx)); uint32_t length = result.Length(); JS::Rooted returnArray(cx, JS::NewArrayObject(cx, length)); if (!returnArray) { return false; } // Scope for 'tmp' { JS::Rooted tmp(cx); for (uint32_t sequenceIdx0 = 0; sequenceIdx0 < length; ++sequenceIdx0) { // Control block to let us common up the JS_DefineElement calls when there // are different ways to succeed at wrapping the object. do { if (!GetOrCreateDOMReflector(cx, result[sequenceIdx0], &tmp)) { MOZ_ASSERT(JS_IsExceptionPending(cx)); return false; } break; } while (false); if (!JS_DefineElement(cx, returnArray, sequenceIdx0, tmp, JSPROP_ENUMERATE)) { return false; } } } args.rval().setObject(*returnArray); return true; } static const JSJitInfo takeRecords_methodinfo = { { (JSJitGetterOp)takeRecords }, { prototypes::id::IntersectionObserver }, { PrototypeTraits::Depth }, JSJitInfo::Method, JSJitInfo::AliasEverything, /* aliasSet. Not relevant for setters. */ JSVAL_TYPE_OBJECT, /* returnType. Not relevant for setters. */ false, /* isInfallible. False in setters. */ false, /* isMovable. Not relevant for setters. */ false, /* isEliminatable. Not relevant for setters. */ false, /* isAlwaysInSlot. Only relevant for getters. */ false, /* isLazilyCachedInSlot. Only relevant for getters. */ false, /* isTypedMethod. Only relevant for methods. */ 0 /* Reserved slot index, if we're stored in a slot, else 0. */ }; static void _finalize(JS::GCContext* gcx, JSObject* obj) { mozilla::dom::DOMIntersectionObserver* self = UnwrapPossiblyNotInitializedDOMObject(obj); if (self) { JS::SetNativeObjectReservedSlot(obj, DOM_OBJECT_SLOT, JS::UndefinedValue()); ClearWrapper(self, self, obj); if (size_t mallocBytes = BindingJSObjectMallocBytes(self)) { JS::RemoveAssociatedMemory(obj, mallocBytes, JS::MemoryUse::DOMBinding); } AddForDeferredFinalization(self); } } MOZ_GLOBINIT static const JSFunctionSpec sMethods_specs[] = { JS_FNSPEC("observe", (GenericMethod), reinterpret_cast(&observe_methodinfo), 1, JSPROP_ENUMERATE, nullptr), JS_FNSPEC("unobserve", (GenericMethod), reinterpret_cast(&unobserve_methodinfo), 1, JSPROP_ENUMERATE, nullptr), JS_FNSPEC("disconnect", (GenericMethod), reinterpret_cast(&disconnect_methodinfo), 0, JSPROP_ENUMERATE, nullptr), JS_FNSPEC("takeRecords", (GenericMethod), reinterpret_cast(&takeRecords_methodinfo), 0, JSPROP_ENUMERATE, nullptr), JS_FS_END }; static const Prefable sMethods[] = { { nullptr, &sMethods_specs[0] }, { nullptr, nullptr } }; static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_PREF_INDEX, "We have a prefable index that is >= (1 << NUM_BITS_PROPERTY_INFO_PREF_INDEX)"); static_assert(4 <= 1ull << NUM_BITS_PROPERTY_INFO_SPEC_INDEX, "We have a spec index that is >= (1 << NUM_BITS_PROPERTY_INFO_SPEC_INDEX)"); MOZ_GLOBINIT static const JSPropertySpec sAttributes_specs[] = { JSPropertySpec::nativeAccessors("root", JSPROP_ENUMERATE, GenericGetter, &root_getterinfo, nullptr, nullptr), JSPropertySpec::nativeAccessors("rootMargin", JSPROP_ENUMERATE, GenericGetter, &rootMargin_getterinfo, nullptr, nullptr), JS_PS_END, JSPropertySpec::nativeAccessors("scrollMargin", JSPROP_ENUMERATE, GenericGetter, &scrollMargin_getterinfo, nullptr, nullptr), JS_PS_END, JSPropertySpec::nativeAccessors("thresholds", JSPROP_ENUMERATE, GenericGetter, &thresholds_getterinfo, nullptr, nullptr), JS_PS_END }; static const PrefableDisablers sAttributes_disablers3 = { WebIDLPrefIndex::dom_intersection_observer_scroll_margin_enabled, 0, false, OriginTrial(0), nullptr }; static const Prefable sAttributes[] = { { nullptr, &sAttributes_specs[0] }, { &sAttributes_disablers3, &sAttributes_specs[3] }, { nullptr, &sAttributes_specs[5] }, { nullptr, nullptr } }; static_assert(3 <= 1ull << NUM_BITS_PROPERTY_INFO_PREF_INDEX, "We have a prefable index that is >= (1 << NUM_BITS_PROPERTY_INFO_PREF_INDEX)"); static_assert(2 <= 1ull << NUM_BITS_PROPERTY_INFO_SPEC_INDEX, "We have a spec index that is >= (1 << NUM_BITS_PROPERTY_INFO_SPEC_INDEX)"); static uint16_t sNativeProperties_sortedPropertyIndices[8]; static PropertyInfo sNativeProperties_propertyInfos[8]; static const NativePropertiesN<2> sNativeProperties = { false, 0, false, 0, true, 0 /* sMethods */, true, 1 /* sAttributes */, false, 0, false, 0, false, 0, -1, 8, sNativeProperties_sortedPropertyIndices, { { sMethods, &sNativeProperties_propertyInfos[0] }, { sAttributes, &sNativeProperties_propertyInfos[4] } } }; static_assert(8 < 1ull << (CHAR_BIT * sizeof(sNativeProperties.propertyInfoCount)), "We have a property info count that is oversized"); // This may allocate too many slots, because we only really need // slots for our non-interface-typed members that we cache. But // allocating slots only for those would make the slot index // computations much more complicated, so let's do this the simple // way for now. DEFINE_XRAY_EXPANDO_CLASS_WITH_OPS(static, sXrayExpandoObjectClass, 2, &xpc::XrayExpandoObjectClassOps); bool sNativePropertiesInited = false; const NativePropertyHooks sNativePropertyHooks = { nullptr, { sNativeProperties.Upcast(), nullptr, &sNativePropertiesInited }, prototypes::id::IntersectionObserver, constructors::id::IntersectionObserver, &sXrayExpandoObjectClass }; static bool _constructor(JSContext* cx_, unsigned argc, JS::Value* vp) { BindingCallContext cx(cx_, "IntersectionObserver constructor"); AUTO_PROFILER_LABEL_DYNAMIC_FAST( "IntersectionObserver", "constructor", DOM, cx, uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS)); JS::CallArgs args = JS::CallArgsFromVp(argc, vp); JS::Rooted obj(cx, &args.callee()); if (!args.isConstructing()) { return ThrowConstructorWithoutNew(cx, "IntersectionObserver"); } JS::Rooted desiredProto(cx); if (!GetDesiredProto(cx, args, prototypes::id::IntersectionObserver, CreateInterfaceObjects, &desiredProto)) { return false; } if (!args.requireAtLeast(cx, "IntersectionObserver constructor", 1)) { return false; } GlobalObject global(cx, obj); if (global.Failed()) { return false; } bool objIsXray = xpc::WrapperFactory::IsXrayWrapper(obj); RootedCallback> arg0(cx); if (args[0].isObject()) { if (JS::IsCallable(&args[0].toObject())) { { // scope for tempRoot and tempGlobalRoot if needed arg0 = new binding_detail::FastIntersectionCallback(&args[0].toObject(), JS::CurrentGlobalOrNull(cx)); } } else { cx.ThrowErrorMessage("Argument 1"); return false; } } else { cx.ThrowErrorMessage("Argument 1"); return false; } binding_detail::FastIntersectionObserverInit arg1; if (!arg1.Init(cx, (args.hasDefined(1)) ? args[1] : JS::NullHandleValue, "Argument 2", false)) { return false; } Maybe ar; if (objIsXray) { // Since our object is an Xray, we can just CheckedUnwrapStatic: // we know Xrays have no dynamic unwrap behavior. obj = js::CheckedUnwrapStatic(obj); if (!obj) { return false; } ar.emplace(cx, obj); if (!JS_WrapObject(cx, &desiredProto)) { return false; } } FastErrorResult rv; auto result(StrongOrRawPtr(mozilla::dom::DOMIntersectionObserver::Constructor(global, MOZ_KnownLive(NonNullHelper(arg0)), Constify(arg1), rv))); if (rv.MaybeSetPendingException(cx, "IntersectionObserver constructor")) [[unlikely]] { return false; } MOZ_ASSERT(!JS_IsExceptionPending(cx)); static_assert(!std::is_pointer_v, "NewObject implies that we need to keep the object alive with a strong reference."); if (!GetOrCreateDOMReflector(cx, result, args.rval(), desiredProto)) { MOZ_ASSERT(JS_IsExceptionPending(cx)); return false; } return true; } static const DOMInterfaceInfo sInterfaceObjectInfo = { { _constructor, &sNativePropertyHooks }, JS::GetRealmFunctionPrototypeHandle, PrototypeTraits::Depth, prototypes::id::IntersectionObserver, true, 1, "IntersectionObserver", }; static const DOMIfaceAndProtoJSClass sPrototypeClass = { { "IntersectionObserverPrototype", JSCLASS_IS_DOMIFACEANDPROTOJSCLASS | JSCLASS_HAS_RESERVED_SLOTS(DOM_INTERFACE_PROTO_SLOTS_BASE), JS_NULL_CLASS_OPS, JS_NULL_CLASS_SPEC, JS_NULL_CLASS_EXT, JS_NULL_OBJECT_OPS }, eInterfacePrototype, prototypes::id::IntersectionObserver, PrototypeTraits::Depth, &sNativePropertyHooks, JS::GetRealmObjectPrototype }; static JS::Handle GetProtoObjectHandle(JSContext* aCx); static const JSClassOps sClassOps = { nullptr, /* addProperty */ nullptr, /* delProperty */ nullptr, /* enumerate */ nullptr, /* newEnumerate */ nullptr, /* resolve */ nullptr, /* mayResolve */ _finalize, /* finalize */ nullptr, /* call */ nullptr, /* construct */ nullptr, /* trace */ }; static const DOMJSClass sClass = { { "IntersectionObserver", JSCLASS_IS_DOMJSCLASS | JSCLASS_FOREGROUND_FINALIZE | JSCLASS_HAS_RESERVED_SLOTS(3) | JSCLASS_PRESERVES_WRAPPER, &sClassOps, JS_NULL_CLASS_SPEC, &NativeTypeHelpers::sClassExtension, JS_NULL_OBJECT_OPS }, { prototypes::id::IntersectionObserver, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count }, std::is_base_of_v, &sNativePropertyHooks, GetProtoObjectHandle, GetCCParticipant::Get(), nullptr, NativeTypeHelpers::GetWrapperCache }; static_assert(1 == DOM_INSTANCE_RESERVED_SLOTS, "Must have the right minimal number of reserved slots."); static_assert(3 >= 3, "Must have enough reserved slots."); bool Wrap(JSContext* aCx, mozilla::dom::DOMIntersectionObserver* aObject, nsWrapperCache* aCache, JS::Handle aGivenProto, JS::MutableHandle aReflector) { static_assert(!std::is_base_of_v, "Shouldn't have wrappercached things that are not refcounted."); static_assert(std::is_same_v); MOZ_ASSERT(ToSupportsIsCorrect(aObject)); MOZ_ASSERT_IF(aGivenProto, js::IsObjectInContextCompartment(aGivenProto, aCx)); MOZ_ASSERT(!aCache->GetWrapper(), "You should probably not be using Wrap() directly; use " "GetOrCreateDOMReflector instead"); MOZ_ASSERT(ToSupportsIsOnPrimaryInheritanceChain(aObject, aCache), "nsISupports must be on our primary inheritance chain"); // If the wrapper cache contains a dead reflector then finalize that // now, ensuring that the finalizer for the old reflector always // runs before the new reflector is created and attached. This // avoids the awkward situation where there are multiple reflector // objects that contain pointers to the same native. if (JSObject* oldReflector = aCache->GetWrapperMaybeDead()) { _finalize(nullptr /* unused */, oldReflector); MOZ_ASSERT(!aCache->GetWrapperMaybeDead()); } JS::Rooted global(aCx, FindAssociatedGlobal(aCx, aObject->GetParentObject())); if (!global) { return false; } MOZ_ASSERT(JS_IsGlobalObject(global)); JS::AssertObjectIsNotGray(global); // That might have ended up wrapping us already, due to the wonders // of XBL. Check for that, and bail out as needed. aReflector.set(aCache->GetWrapper()); if (aReflector) { #ifdef DEBUG AssertReflectorHasGivenProto(aCx, aReflector, aGivenProto); #endif // DEBUG return true; } JSAutoRealm ar(aCx, global); JS::Handle canonicalProto = GetProtoObjectHandle(aCx); if (!canonicalProto) { return false; } JS::Rooted proto(aCx); if (aGivenProto) { proto = aGivenProto; // Unfortunately, while aGivenProto was in the compartment of aCx // coming in, we changed compartments to that of "parent" so may need // to wrap the proto here. if (js::GetContextCompartment(aCx) != JS::GetCompartment(proto)) { if (!JS_WrapObject(aCx, &proto)) { return false; } } } else { proto = canonicalProto; } BindingJSObjectCreator creator(aCx); creator.CreateObject(aCx, sClass.ToJSClass(), proto, aObject, aReflector); if (!aReflector) { return false; } aCache->SetWrapper(aReflector); creator.InitializationSucceeded(); MOZ_ASSERT(aCache->GetWrapperPreserveColor() && aCache->GetWrapperPreserveColor() == aReflector); // If proto != canonicalProto, we have to preserve our wrapper; // otherwise we won't be able to properly recreate it later, since // we won't know what proto to use. Note that we don't check // aGivenProto here, since it's entirely possible (and even // somewhat common) to have a non-null aGivenProto which is the // same as canonicalProto. if (proto != canonicalProto) { PreserveWrapper(aObject); } return true; } void CreateInterfaceObjects(JSContext* aCx, JS::Handle aGlobal, ProtoAndIfaceCache& aProtoAndIfaceCache, DefineInterfaceProperty aDefineOnGlobal) { JS::Heap* protoCache = &aProtoAndIfaceCache.EntrySlotOrCreate(prototypes::id::IntersectionObserver); JS::Heap* interfaceCache = &aProtoAndIfaceCache.EntrySlotOrCreate(constructors::id::IntersectionObserver); JS::Handle parentProto(JS::GetRealmObjectPrototypeHandle(aCx)); if (!parentProto) { return; } JS::Handle constructorProto(JS::GetRealmFunctionPrototypeHandle(aCx)); if (!constructorProto) { return; } dom::CreateInterfaceObjects(aCx, aGlobal, parentProto, &sPrototypeClass, protoCache, constructorProto, &sInterfaceObjectInfo, 1, false, Span{}, interfaceCache, sNativeProperties.Upcast(), nullptr, "IntersectionObserver", aDefineOnGlobal != DefineInterfaceProperty::No, nullptr, false, nullptr); } static JS::Handle GetProtoObjectHandle(JSContext* aCx) { /* Get the interface prototype object for this class. This will create the object as needed. */ return GetPerInterfaceObjectHandle(aCx, prototypes::id::IntersectionObserver, &CreateInterfaceObjects, DefineInterfaceProperty::CheckExposure); } JS::Handle GetConstructorObjectHandle(JSContext* aCx) { /* Get the interface object for this class. This will create the object as needed. */ return GetPerInterfaceObjectHandle(aCx, constructors::id::IntersectionObserver, &CreateInterfaceObjects, DefineInterfaceProperty::CheckExposure); } } // namespace IntersectionObserver_Binding namespace IntersectionObserverEntry_Binding { MOZ_CAN_RUN_SCRIPT static bool get_time(JSContext* cx, JS::Handle obj, void* void_self, JSJitGetterCallArgs args) { AUTO_PROFILER_LABEL_DYNAMIC_FAST( "IntersectionObserverEntry", "time", DOM, cx, uint32_t(js::ProfilingStackFrame::Flags::STRING_TEMPLATE_GETTER) | uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS)); auto* self = static_cast(void_self); double result(MOZ_KnownLive(self)->Time()); MOZ_ASSERT(!JS_IsExceptionPending(cx)); args.rval().set(JS::NumberValue(double(result))); return true; } static const JSJitInfo time_getterinfo = { { get_time }, { prototypes::id::IntersectionObserverEntry }, { PrototypeTraits::Depth }, JSJitInfo::Getter, JSJitInfo::AliasNone, /* aliasSet. Not relevant for setters. */ JSVAL_TYPE_DOUBLE, /* returnType. Not relevant for setters. */ true, /* isInfallible. False in setters. */ true, /* isMovable. Not relevant for setters. */ true, /* isEliminatable. Not relevant for setters. */ false, /* isAlwaysInSlot. Only relevant for getters. */ false, /* isLazilyCachedInSlot. Only relevant for getters. */ false, /* isTypedMethod. Only relevant for methods. */ 0 /* Reserved slot index, if we're stored in a slot, else 0. */ }; MOZ_CAN_RUN_SCRIPT static bool get_rootBounds(JSContext* cx, JS::Handle obj, void* void_self, JSJitGetterCallArgs args) { AUTO_PROFILER_LABEL_DYNAMIC_FAST( "IntersectionObserverEntry", "rootBounds", DOM, cx, uint32_t(js::ProfilingStackFrame::Flags::STRING_TEMPLATE_GETTER) | uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS)); auto* self = static_cast(void_self); auto result(StrongOrRawPtr(MOZ_KnownLive(self)->GetRootBounds())); MOZ_ASSERT(!JS_IsExceptionPending(cx)); if (!result) { args.rval().setNull(); return true; } if (!GetOrCreateDOMReflector(cx, result, args.rval())) { MOZ_ASSERT(JS_IsExceptionPending(cx)); return false; } return true; } static const JSJitInfo rootBounds_getterinfo = { { get_rootBounds }, { prototypes::id::IntersectionObserverEntry }, { PrototypeTraits::Depth }, JSJitInfo::Getter, JSJitInfo::AliasNone, /* aliasSet. Not relevant for setters. */ JSVAL_TYPE_UNKNOWN, /* returnType. Not relevant for setters. */ false, /* isInfallible. False in setters. */ true, /* isMovable. Not relevant for setters. */ true, /* isEliminatable. Not relevant for setters. */ false, /* isAlwaysInSlot. Only relevant for getters. */ false, /* isLazilyCachedInSlot. Only relevant for getters. */ false, /* isTypedMethod. Only relevant for methods. */ 0 /* Reserved slot index, if we're stored in a slot, else 0. */ }; MOZ_CAN_RUN_SCRIPT static bool get_boundingClientRect(JSContext* cx, JS::Handle obj, void* void_self, JSJitGetterCallArgs args) { AUTO_PROFILER_LABEL_DYNAMIC_FAST( "IntersectionObserverEntry", "boundingClientRect", DOM, cx, uint32_t(js::ProfilingStackFrame::Flags::STRING_TEMPLATE_GETTER) | uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS)); auto* self = static_cast(void_self); auto result(StrongOrRawPtr(MOZ_KnownLive(self)->BoundingClientRect())); MOZ_ASSERT(!JS_IsExceptionPending(cx)); if (!GetOrCreateDOMReflector(cx, result, args.rval())) { MOZ_ASSERT(JS_IsExceptionPending(cx)); return false; } return true; } static const JSJitInfo boundingClientRect_getterinfo = { { get_boundingClientRect }, { prototypes::id::IntersectionObserverEntry }, { PrototypeTraits::Depth }, JSJitInfo::Getter, JSJitInfo::AliasNone, /* aliasSet. Not relevant for setters. */ JSVAL_TYPE_OBJECT, /* returnType. Not relevant for setters. */ false, /* isInfallible. False in setters. */ true, /* isMovable. Not relevant for setters. */ true, /* isEliminatable. Not relevant for setters. */ false, /* isAlwaysInSlot. Only relevant for getters. */ false, /* isLazilyCachedInSlot. Only relevant for getters. */ false, /* isTypedMethod. Only relevant for methods. */ 0 /* Reserved slot index, if we're stored in a slot, else 0. */ }; MOZ_CAN_RUN_SCRIPT static bool get_intersectionRect(JSContext* cx, JS::Handle obj, void* void_self, JSJitGetterCallArgs args) { AUTO_PROFILER_LABEL_DYNAMIC_FAST( "IntersectionObserverEntry", "intersectionRect", DOM, cx, uint32_t(js::ProfilingStackFrame::Flags::STRING_TEMPLATE_GETTER) | uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS)); auto* self = static_cast(void_self); auto result(StrongOrRawPtr(MOZ_KnownLive(self)->IntersectionRect())); MOZ_ASSERT(!JS_IsExceptionPending(cx)); if (!GetOrCreateDOMReflector(cx, result, args.rval())) { MOZ_ASSERT(JS_IsExceptionPending(cx)); return false; } return true; } static const JSJitInfo intersectionRect_getterinfo = { { get_intersectionRect }, { prototypes::id::IntersectionObserverEntry }, { PrototypeTraits::Depth }, JSJitInfo::Getter, JSJitInfo::AliasNone, /* aliasSet. Not relevant for setters. */ JSVAL_TYPE_OBJECT, /* returnType. Not relevant for setters. */ false, /* isInfallible. False in setters. */ true, /* isMovable. Not relevant for setters. */ true, /* isEliminatable. Not relevant for setters. */ false, /* isAlwaysInSlot. Only relevant for getters. */ false, /* isLazilyCachedInSlot. Only relevant for getters. */ false, /* isTypedMethod. Only relevant for methods. */ 0 /* Reserved slot index, if we're stored in a slot, else 0. */ }; MOZ_CAN_RUN_SCRIPT static bool get_isIntersecting(JSContext* cx, JS::Handle obj, void* void_self, JSJitGetterCallArgs args) { AUTO_PROFILER_LABEL_DYNAMIC_FAST( "IntersectionObserverEntry", "isIntersecting", DOM, cx, uint32_t(js::ProfilingStackFrame::Flags::STRING_TEMPLATE_GETTER) | uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS)); auto* self = static_cast(void_self); bool result(MOZ_KnownLive(self)->IsIntersecting()); MOZ_ASSERT(!JS_IsExceptionPending(cx)); args.rval().setBoolean(result); return true; } static const JSJitInfo isIntersecting_getterinfo = { { get_isIntersecting }, { prototypes::id::IntersectionObserverEntry }, { PrototypeTraits::Depth }, JSJitInfo::Getter, JSJitInfo::AliasNone, /* aliasSet. Not relevant for setters. */ JSVAL_TYPE_BOOLEAN, /* returnType. Not relevant for setters. */ true, /* isInfallible. False in setters. */ true, /* isMovable. Not relevant for setters. */ true, /* isEliminatable. Not relevant for setters. */ false, /* isAlwaysInSlot. Only relevant for getters. */ false, /* isLazilyCachedInSlot. Only relevant for getters. */ false, /* isTypedMethod. Only relevant for methods. */ 0 /* Reserved slot index, if we're stored in a slot, else 0. */ }; MOZ_CAN_RUN_SCRIPT static bool get_intersectionRatio(JSContext* cx, JS::Handle obj, void* void_self, JSJitGetterCallArgs args) { AUTO_PROFILER_LABEL_DYNAMIC_FAST( "IntersectionObserverEntry", "intersectionRatio", DOM, cx, uint32_t(js::ProfilingStackFrame::Flags::STRING_TEMPLATE_GETTER) | uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS)); auto* self = static_cast(void_self); double result(MOZ_KnownLive(self)->IntersectionRatio()); MOZ_ASSERT(!JS_IsExceptionPending(cx)); args.rval().set(JS::NumberValue(double(result))); return true; } static const JSJitInfo intersectionRatio_getterinfo = { { get_intersectionRatio }, { prototypes::id::IntersectionObserverEntry }, { PrototypeTraits::Depth }, JSJitInfo::Getter, JSJitInfo::AliasNone, /* aliasSet. Not relevant for setters. */ JSVAL_TYPE_DOUBLE, /* returnType. Not relevant for setters. */ true, /* isInfallible. False in setters. */ true, /* isMovable. Not relevant for setters. */ true, /* isEliminatable. Not relevant for setters. */ false, /* isAlwaysInSlot. Only relevant for getters. */ false, /* isLazilyCachedInSlot. Only relevant for getters. */ false, /* isTypedMethod. Only relevant for methods. */ 0 /* Reserved slot index, if we're stored in a slot, else 0. */ }; MOZ_CAN_RUN_SCRIPT static bool get_target(JSContext* cx, JS::Handle obj, void* void_self, JSJitGetterCallArgs args) { AUTO_PROFILER_LABEL_DYNAMIC_FAST( "IntersectionObserverEntry", "target", DOM, cx, uint32_t(js::ProfilingStackFrame::Flags::STRING_TEMPLATE_GETTER) | uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS)); auto* self = static_cast(void_self); auto result(StrongOrRawPtr(MOZ_KnownLive(self)->Target())); MOZ_ASSERT(!JS_IsExceptionPending(cx)); if (!GetOrCreateDOMReflector(cx, result, args.rval())) { MOZ_ASSERT(JS_IsExceptionPending(cx)); return false; } return true; } static const JSJitInfo target_getterinfo = { { get_target }, { prototypes::id::IntersectionObserverEntry }, { PrototypeTraits::Depth }, JSJitInfo::Getter, JSJitInfo::AliasNone, /* aliasSet. Not relevant for setters. */ JSVAL_TYPE_OBJECT, /* returnType. Not relevant for setters. */ false, /* isInfallible. False in setters. */ true, /* isMovable. Not relevant for setters. */ true, /* isEliminatable. Not relevant for setters. */ false, /* isAlwaysInSlot. Only relevant for getters. */ false, /* isLazilyCachedInSlot. Only relevant for getters. */ false, /* isTypedMethod. Only relevant for methods. */ 0 /* Reserved slot index, if we're stored in a slot, else 0. */ }; static void _finalize(JS::GCContext* gcx, JSObject* obj) { mozilla::dom::DOMIntersectionObserverEntry* self = UnwrapPossiblyNotInitializedDOMObject(obj); if (self) { JS::SetNativeObjectReservedSlot(obj, DOM_OBJECT_SLOT, JS::UndefinedValue()); ClearWrapper(self, self, obj); if (size_t mallocBytes = BindingJSObjectMallocBytes(self)) { JS::RemoveAssociatedMemory(obj, mallocBytes, JS::MemoryUse::DOMBinding); } AddForDeferredFinalization(self); } } MOZ_GLOBINIT static const JSPropertySpec sAttributes_specs[] = { JSPropertySpec::nativeAccessors("time", JSPROP_ENUMERATE, GenericGetter, &time_getterinfo, nullptr, nullptr), JSPropertySpec::nativeAccessors("rootBounds", JSPROP_ENUMERATE, GenericGetter, &rootBounds_getterinfo, nullptr, nullptr), JSPropertySpec::nativeAccessors("boundingClientRect", JSPROP_ENUMERATE, GenericGetter, &boundingClientRect_getterinfo, nullptr, nullptr), JSPropertySpec::nativeAccessors("intersectionRect", JSPROP_ENUMERATE, GenericGetter, &intersectionRect_getterinfo, nullptr, nullptr), JSPropertySpec::nativeAccessors("isIntersecting", JSPROP_ENUMERATE, GenericGetter, &isIntersecting_getterinfo, nullptr, nullptr), JSPropertySpec::nativeAccessors("intersectionRatio", JSPROP_ENUMERATE, GenericGetter, &intersectionRatio_getterinfo, nullptr, nullptr), JSPropertySpec::nativeAccessors("target", JSPROP_ENUMERATE, GenericGetter, &target_getterinfo, nullptr, nullptr), JS_PS_END }; static const Prefable sAttributes[] = { { nullptr, &sAttributes_specs[0] }, { nullptr, nullptr } }; static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_PREF_INDEX, "We have a prefable index that is >= (1 << NUM_BITS_PROPERTY_INFO_PREF_INDEX)"); static_assert(7 <= 1ull << NUM_BITS_PROPERTY_INFO_SPEC_INDEX, "We have a spec index that is >= (1 << NUM_BITS_PROPERTY_INFO_SPEC_INDEX)"); static uint16_t sNativeProperties_sortedPropertyIndices[7]; static PropertyInfo sNativeProperties_propertyInfos[7]; static const NativePropertiesN<1> sNativeProperties = { false, 0, false, 0, false, 0, true, 0 /* sAttributes */, false, 0, false, 0, false, 0, -1, 7, sNativeProperties_sortedPropertyIndices, { { sAttributes, &sNativeProperties_propertyInfos[0] } } }; static_assert(7 < 1ull << (CHAR_BIT * sizeof(sNativeProperties.propertyInfoCount)), "We have a property info count that is oversized"); bool sNativePropertiesInited = false; const NativePropertyHooks sNativePropertyHooks = { nullptr, { sNativeProperties.Upcast(), nullptr, &sNativePropertiesInited }, prototypes::id::IntersectionObserverEntry, constructors::id::IntersectionObserverEntry, &DefaultXrayExpandoObjectClass }; static const DOMInterfaceInfo sInterfaceObjectInfo = { { ThrowingConstructor, &sNativePropertyHooks }, JS::GetRealmFunctionPrototypeHandle, PrototypeTraits::Depth, prototypes::id::IntersectionObserverEntry, true, 0, "IntersectionObserverEntry", }; static const DOMIfaceAndProtoJSClass sPrototypeClass = { { "IntersectionObserverEntryPrototype", JSCLASS_IS_DOMIFACEANDPROTOJSCLASS | JSCLASS_HAS_RESERVED_SLOTS(DOM_INTERFACE_PROTO_SLOTS_BASE), JS_NULL_CLASS_OPS, JS_NULL_CLASS_SPEC, JS_NULL_CLASS_EXT, JS_NULL_OBJECT_OPS }, eInterfacePrototype, prototypes::id::IntersectionObserverEntry, PrototypeTraits::Depth, &sNativePropertyHooks, JS::GetRealmObjectPrototype }; static JS::Handle GetProtoObjectHandle(JSContext* aCx); static const JSClassOps sClassOps = { nullptr, /* addProperty */ nullptr, /* delProperty */ nullptr, /* enumerate */ nullptr, /* newEnumerate */ nullptr, /* resolve */ nullptr, /* mayResolve */ _finalize, /* finalize */ nullptr, /* call */ nullptr, /* construct */ nullptr, /* trace */ }; static const DOMJSClass sClass = { { "IntersectionObserverEntry", JSCLASS_IS_DOMJSCLASS | JSCLASS_FOREGROUND_FINALIZE | JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_SKIP_NURSERY_FINALIZE | JSCLASS_PRESERVES_WRAPPER, &sClassOps, JS_NULL_CLASS_SPEC, &NativeTypeHelpers::sClassExtension, JS_NULL_OBJECT_OPS }, { prototypes::id::IntersectionObserverEntry, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count }, std::is_base_of_v, &sNativePropertyHooks, GetProtoObjectHandle, GetCCParticipant::Get(), nullptr, NativeTypeHelpers::GetWrapperCache }; static_assert(1 == DOM_INSTANCE_RESERVED_SLOTS, "Must have the right minimal number of reserved slots."); static_assert(1 >= 1, "Must have enough reserved slots."); bool Wrap(JSContext* aCx, mozilla::dom::DOMIntersectionObserverEntry* aObject, nsWrapperCache* aCache, JS::Handle aGivenProto, JS::MutableHandle aReflector) { static_assert(!std::is_base_of_v, "Shouldn't have wrappercached things that are not refcounted."); static_assert(std::is_same_v); MOZ_ASSERT(ToSupportsIsCorrect(aObject)); MOZ_ASSERT_IF(aGivenProto, js::IsObjectInContextCompartment(aGivenProto, aCx)); MOZ_ASSERT(!aCache->GetWrapper(), "You should probably not be using Wrap() directly; use " "GetOrCreateDOMReflector instead"); MOZ_ASSERT(ToSupportsIsOnPrimaryInheritanceChain(aObject, aCache), "nsISupports must be on our primary inheritance chain"); // If the wrapper cache contains a dead reflector then finalize that // now, ensuring that the finalizer for the old reflector always // runs before the new reflector is created and attached. This // avoids the awkward situation where there are multiple reflector // objects that contain pointers to the same native. if (JSObject* oldReflector = aCache->GetWrapperMaybeDead()) { _finalize(nullptr /* unused */, oldReflector); MOZ_ASSERT(!aCache->GetWrapperMaybeDead()); } JS::Rooted global(aCx, FindAssociatedGlobal(aCx, aObject->GetParentObject())); if (!global) { return false; } MOZ_ASSERT(JS_IsGlobalObject(global)); JS::AssertObjectIsNotGray(global); // That might have ended up wrapping us already, due to the wonders // of XBL. Check for that, and bail out as needed. aReflector.set(aCache->GetWrapper()); if (aReflector) { #ifdef DEBUG AssertReflectorHasGivenProto(aCx, aReflector, aGivenProto); #endif // DEBUG return true; } JSAutoRealm ar(aCx, global); JS::Handle canonicalProto = GetProtoObjectHandle(aCx); if (!canonicalProto) { return false; } JS::Rooted proto(aCx); if (aGivenProto) { proto = aGivenProto; // Unfortunately, while aGivenProto was in the compartment of aCx // coming in, we changed compartments to that of "parent" so may need // to wrap the proto here. if (js::GetContextCompartment(aCx) != JS::GetCompartment(proto)) { if (!JS_WrapObject(aCx, &proto)) { return false; } } } else { proto = canonicalProto; } BindingJSObjectCreator creator(aCx); creator.CreateObject(aCx, sClass.ToJSClass(), proto, aObject, aReflector); if (!aReflector) { return false; } aCache->SetWrapper(aReflector); creator.InitializationSucceeded(); MOZ_ASSERT(aCache->GetWrapperPreserveColor() && aCache->GetWrapperPreserveColor() == aReflector); // If proto != canonicalProto, we have to preserve our wrapper; // otherwise we won't be able to properly recreate it later, since // we won't know what proto to use. Note that we don't check // aGivenProto here, since it's entirely possible (and even // somewhat common) to have a non-null aGivenProto which is the // same as canonicalProto. if (proto != canonicalProto) { PreserveWrapper(aObject); } return true; } void CreateInterfaceObjects(JSContext* aCx, JS::Handle aGlobal, ProtoAndIfaceCache& aProtoAndIfaceCache, DefineInterfaceProperty aDefineOnGlobal) { JS::Heap* protoCache = &aProtoAndIfaceCache.EntrySlotOrCreate(prototypes::id::IntersectionObserverEntry); JS::Heap* interfaceCache = &aProtoAndIfaceCache.EntrySlotOrCreate(constructors::id::IntersectionObserverEntry); JS::Handle parentProto(JS::GetRealmObjectPrototypeHandle(aCx)); if (!parentProto) { return; } JS::Handle constructorProto(JS::GetRealmFunctionPrototypeHandle(aCx)); if (!constructorProto) { return; } dom::CreateInterfaceObjects(aCx, aGlobal, parentProto, &sPrototypeClass, protoCache, constructorProto, &sInterfaceObjectInfo, 0, false, Span{}, interfaceCache, sNativeProperties.Upcast(), nullptr, "IntersectionObserverEntry", aDefineOnGlobal != DefineInterfaceProperty::No, nullptr, false, nullptr); } static JS::Handle GetProtoObjectHandle(JSContext* aCx) { /* Get the interface prototype object for this class. This will create the object as needed. */ return GetPerInterfaceObjectHandle(aCx, prototypes::id::IntersectionObserverEntry, &CreateInterfaceObjects, DefineInterfaceProperty::CheckExposure); } JS::Handle GetConstructorObjectHandle(JSContext* aCx) { /* Get the interface object for this class. This will create the object as needed. */ return GetPerInterfaceObjectHandle(aCx, constructors::id::IntersectionObserverEntry, &CreateInterfaceObjects, DefineInterfaceProperty::CheckExposure); } } // namespace IntersectionObserverEntry_Binding } // namespace mozilla::dom