Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / dom / svg / DOMSVGPointList.cpp
blobe703a3202e851240ae5c0c554dea9d5c1f5c0307
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "DOMSVGPointList.h"
9 #include "nsContentUtils.h"
10 #include "DOMSVGPoint.h"
11 #include "nsError.h"
12 #include "SVGAnimatedPointList.h"
13 #include "SVGAttrTearoffTable.h"
14 #include "SVGPolyElement.h"
15 #include "mozilla/dom/SVGElement.h"
16 #include "mozilla/dom/SVGPointListBinding.h"
17 #include <algorithm>
19 // See the comment in this file's header.
21 // local helper functions
22 namespace {
24 void UpdateListIndicesFromIndex(
25 FallibleTArray<mozilla::dom::DOMSVGPoint*>& aItemsArray,
26 uint32_t aStartingIndex) {
27 uint32_t length = aItemsArray.Length();
29 for (uint32_t i = aStartingIndex; i < length; ++i) {
30 if (aItemsArray[i]) {
31 aItemsArray[i]->UpdateListIndex(i);
36 } // namespace
38 namespace mozilla::dom {
40 static inline SVGAttrTearoffTable<void, DOMSVGPointList>&
41 SVGPointListTearoffTable() {
42 static SVGAttrTearoffTable<void, DOMSVGPointList> sSVGPointListTearoffTable;
43 return sSVGPointListTearoffTable;
46 NS_IMPL_CYCLE_COLLECTION_CLASS(DOMSVGPointList)
48 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(DOMSVGPointList)
49 // No unlinking of mElement, we'd need to null out the value pointer (the
50 // object it points to is held by the element) and null-check it everywhere.
51 tmp->RemoveFromTearoffTable();
52 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
53 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
54 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(DOMSVGPointList)
55 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mElement)
56 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
57 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(DOMSVGPointList)
58 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
59 NS_IMPL_CYCLE_COLLECTION_TRACE_END
61 NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMSVGPointList)
62 NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMSVGPointList)
64 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMSVGPointList)
65 NS_INTERFACE_MAP_ENTRY_CONCRETE(DOMSVGPointList)
66 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
67 NS_INTERFACE_MAP_ENTRY(nsISupports)
68 NS_INTERFACE_MAP_END
70 /* static */
71 already_AddRefed<DOMSVGPointList> DOMSVGPointList::GetDOMWrapper(
72 void* aList, SVGPolyElement* aElement) {
73 RefPtr<DOMSVGPointList> wrapper =
74 SVGPointListTearoffTable().GetTearoff(aList);
75 if (!wrapper) {
76 wrapper = new DOMSVGPointList(
77 aElement, aElement->GetAnimatedPointList()->GetAnimValKey() == aList);
78 SVGPointListTearoffTable().AddTearoff(aList, wrapper);
80 return wrapper.forget();
83 /* static */
84 DOMSVGPointList* DOMSVGPointList::GetDOMWrapperIfExists(void* aList) {
85 return SVGPointListTearoffTable().GetTearoff(aList);
88 void DOMSVGPointList::RemoveFromTearoffTable() {
89 // Called from Unlink and the destructor.
91 // There are now no longer any references to us held by script or list items.
92 // Note we must use GetAnimValKey/GetBaseValKey here, NOT InternalList()!
93 void* key = mIsAnimValList ? InternalAList().GetAnimValKey()
94 : InternalAList().GetBaseValKey();
95 SVGPointListTearoffTable().RemoveTearoff(key);
98 DOMSVGPointList::~DOMSVGPointList() { RemoveFromTearoffTable(); }
100 JSObject* DOMSVGPointList::WrapObject(JSContext* cx,
101 JS::Handle<JSObject*> aGivenProto) {
102 return mozilla::dom::SVGPointList_Binding::Wrap(cx, this, aGivenProto);
105 void DOMSVGPointList::InternalListWillChangeTo(const SVGPointList& aNewValue) {
106 // When the number of items in our internal counterpart changes, we MUST stay
107 // in sync. Everything in the scary comment in
108 // DOMSVGLengthList::InternalBaseValListWillChangeTo applies here too!
110 uint32_t oldLength = mItems.Length();
112 uint32_t newLength = aNewValue.Length();
113 if (newLength > DOMSVGPoint::MaxListIndex()) {
114 // It's safe to get out of sync with our internal list as long as we have
115 // FEWER items than it does.
116 newLength = DOMSVGPoint::MaxListIndex();
119 RefPtr<DOMSVGPointList> kungFuDeathGrip;
120 if (newLength < oldLength) {
121 // RemovingFromList() might clear last reference to |this|.
122 // Retain a temporary reference to keep from dying before returning.
123 kungFuDeathGrip = this;
126 // If our length will decrease, notify the items that will be removed:
127 for (uint32_t i = newLength; i < oldLength; ++i) {
128 if (mItems[i]) {
129 mItems[i]->RemovingFromList();
133 if (!mItems.SetLength(newLength, fallible)) {
134 // We silently ignore SetLength OOM failure since being out of sync is safe
135 // so long as we have *fewer* items than our internal list.
136 mItems.Clear();
137 return;
140 // If our length has increased, null out the new pointers:
141 for (uint32_t i = oldLength; i < newLength; ++i) {
142 mItems[i] = nullptr;
146 bool DOMSVGPointList::AttrIsAnimating() const {
147 return InternalAList().IsAnimating();
150 bool DOMSVGPointList::AnimListMirrorsBaseList() const {
151 return GetDOMWrapperIfExists(InternalAList().GetAnimValKey()) &&
152 !AttrIsAnimating();
155 SVGPointList& DOMSVGPointList::InternalList() const {
156 SVGAnimatedPointList* alist = mElement->GetAnimatedPointList();
157 return mIsAnimValList && alist->IsAnimating() ? *alist->mAnimVal
158 : alist->mBaseVal;
161 SVGAnimatedPointList& DOMSVGPointList::InternalAList() const {
162 MOZ_ASSERT(mElement->GetAnimatedPointList(), "Internal error");
163 return *mElement->GetAnimatedPointList();
166 // ----------------------------------------------------------------------------
167 // nsIDOMSVGPointList implementation:
169 void DOMSVGPointList::Clear(ErrorResult& aError) {
170 if (IsAnimValList()) {
171 aError.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
172 return;
175 if (LengthNoFlush() > 0) {
176 AutoChangePointListNotifier notifier(this);
177 // DOM list items that are to be removed must be removed before we change
178 // the internal list, otherwise they wouldn't be able to copy their
179 // internal counterparts' values!
181 InternalListWillChangeTo(SVGPointList()); // clears mItems
183 if (!AttrIsAnimating()) {
184 // The anim val list is in sync with the base val list
185 DOMSVGPointList* animList =
186 GetDOMWrapperIfExists(InternalAList().GetAnimValKey());
187 if (animList) {
188 animList->InternalListWillChangeTo(
189 SVGPointList()); // clears its mItems
193 InternalList().Clear();
197 already_AddRefed<DOMSVGPoint> DOMSVGPointList::Initialize(DOMSVGPoint& aNewItem,
198 ErrorResult& aError) {
199 if (IsAnimValList()) {
200 aError.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
201 return nullptr;
204 // If aNewItem is already in a list we should insert a clone of aNewItem,
205 // and for consistency, this should happen even if *this* is the list that
206 // aNewItem is currently in. Note that in the case of aNewItem being in this
207 // list, the Clear() call before the InsertItemBefore() call would remove it
208 // from this list, and so the InsertItemBefore() call would not insert a
209 // clone of aNewItem, it would actually insert aNewItem. To prevent that
210 // from happening we have to do the clone here, if necessary.
212 RefPtr<DOMSVGPoint> domItem = &aNewItem;
213 if (domItem->HasOwner()) {
214 domItem = domItem->Copy(); // must do this before changing anything!
217 ErrorResult rv;
218 Clear(rv);
219 MOZ_ASSERT(!rv.Failed());
220 return InsertItemBefore(*domItem, 0, aError);
223 already_AddRefed<DOMSVGPoint> DOMSVGPointList::GetItem(uint32_t index,
224 ErrorResult& error) {
225 bool found;
226 RefPtr<DOMSVGPoint> item = IndexedGetter(index, found, error);
227 if (!found) {
228 error.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
230 return item.forget();
233 already_AddRefed<DOMSVGPoint> DOMSVGPointList::IndexedGetter(
234 uint32_t aIndex, bool& aFound, ErrorResult& aError) {
235 if (IsAnimValList()) {
236 Element()->FlushAnimations();
238 aFound = aIndex < LengthNoFlush();
239 if (aFound) {
240 return GetItemAt(aIndex);
242 return nullptr;
245 already_AddRefed<DOMSVGPoint> DOMSVGPointList::InsertItemBefore(
246 DOMSVGPoint& aNewItem, uint32_t aIndex, ErrorResult& aError) {
247 if (IsAnimValList()) {
248 aError.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
249 return nullptr;
252 aIndex = std::min(aIndex, LengthNoFlush());
253 if (aIndex >= DOMSVGPoint::MaxListIndex()) {
254 aError.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
255 return nullptr;
258 RefPtr<DOMSVGPoint> domItem = &aNewItem;
259 if (domItem->HasOwner()) {
260 domItem = domItem->Copy(); // must do this before changing anything!
263 // Ensure we have enough memory so we can avoid complex error handling below:
264 if (!mItems.SetCapacity(mItems.Length() + 1, fallible) ||
265 !InternalList().SetCapacity(InternalList().Length() + 1)) {
266 aError.Throw(NS_ERROR_OUT_OF_MEMORY);
267 return nullptr;
269 if (AnimListMirrorsBaseList()) {
270 DOMSVGPointList* animVal =
271 GetDOMWrapperIfExists(InternalAList().GetAnimValKey());
272 MOZ_ASSERT(animVal, "animVal must be a valid pointer");
273 if (!animVal->mItems.SetCapacity(animVal->mItems.Length() + 1, fallible)) {
274 aError.Throw(NS_ERROR_OUT_OF_MEMORY);
275 return nullptr;
279 AutoChangePointListNotifier notifier(this);
280 // Now that we know we're inserting, keep animVal list in sync as necessary.
281 MaybeInsertNullInAnimValListAt(aIndex);
283 InternalList().InsertItem(aIndex, domItem->ToSVGPoint());
284 MOZ_ALWAYS_TRUE(mItems.InsertElementAt(aIndex, domItem, fallible));
286 // This MUST come after the insertion into InternalList(), or else under the
287 // insertion into InternalList() the values read from domItem would be bad
288 // data from InternalList() itself!:
289 domItem->InsertingIntoList(this, aIndex, IsAnimValList());
291 UpdateListIndicesFromIndex(mItems, aIndex + 1);
293 return domItem.forget();
296 already_AddRefed<DOMSVGPoint> DOMSVGPointList::ReplaceItem(
297 DOMSVGPoint& aNewItem, uint32_t aIndex, ErrorResult& aError) {
298 if (IsAnimValList()) {
299 aError.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
300 return nullptr;
303 if (aIndex >= LengthNoFlush()) {
304 aError.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
305 return nullptr;
308 RefPtr<DOMSVGPoint> domItem = &aNewItem;
309 if (domItem->HasOwner()) {
310 domItem = domItem->Copy(); // must do this before changing anything!
313 AutoChangePointListNotifier notifier(this);
314 if (mItems[aIndex]) {
315 // Notify any existing DOM item of removal *before* modifying the lists so
316 // that the DOM item can copy the *old* value at its index:
317 mItems[aIndex]->RemovingFromList();
320 InternalList()[aIndex] = domItem->ToSVGPoint();
321 mItems[aIndex] = domItem;
323 // This MUST come after the ToSVGPoint() call, otherwise that call
324 // would end up reading bad data from InternalList()!
325 domItem->InsertingIntoList(this, aIndex, IsAnimValList());
327 return domItem.forget();
330 already_AddRefed<DOMSVGPoint> DOMSVGPointList::RemoveItem(uint32_t aIndex,
331 ErrorResult& aError) {
332 if (IsAnimValList()) {
333 aError.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
334 return nullptr;
337 if (aIndex >= LengthNoFlush()) {
338 aError.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
339 return nullptr;
342 AutoChangePointListNotifier notifier(this);
343 // Now that we know we're removing, keep animVal list in sync as necessary.
344 // Do this *before* touching InternalList() so the removed item can get its
345 // internal value.
346 MaybeRemoveItemFromAnimValListAt(aIndex);
348 // We have to return the removed item, so get it, creating it if necessary:
349 RefPtr<DOMSVGPoint> result = GetItemAt(aIndex);
351 // Notify the DOM item of removal *before* modifying the lists so that the
352 // DOM item can copy its *old* value:
353 mItems[aIndex]->RemovingFromList();
355 InternalList().RemoveItem(aIndex);
356 mItems.RemoveElementAt(aIndex);
358 UpdateListIndicesFromIndex(mItems, aIndex);
360 return result.forget();
363 already_AddRefed<DOMSVGPoint> DOMSVGPointList::GetItemAt(uint32_t aIndex) {
364 MOZ_ASSERT(aIndex < mItems.Length());
366 if (!mItems[aIndex]) {
367 mItems[aIndex] = new DOMSVGPoint(this, aIndex, IsAnimValList());
369 RefPtr<DOMSVGPoint> result = mItems[aIndex];
370 return result.forget();
373 void DOMSVGPointList::MaybeInsertNullInAnimValListAt(uint32_t aIndex) {
374 MOZ_ASSERT(!IsAnimValList(), "call from baseVal to animVal");
376 if (!AnimListMirrorsBaseList()) {
377 return;
380 // The anim val list is in sync with the base val list
381 DOMSVGPointList* animVal =
382 GetDOMWrapperIfExists(InternalAList().GetAnimValKey());
384 MOZ_ASSERT(animVal, "AnimListMirrorsBaseList() promised a non-null animVal");
385 MOZ_ASSERT(animVal->mItems.Length() == mItems.Length(),
386 "animVal list not in sync!");
387 MOZ_ALWAYS_TRUE(animVal->mItems.InsertElementAt(aIndex, nullptr, fallible));
389 UpdateListIndicesFromIndex(animVal->mItems, aIndex + 1);
392 void DOMSVGPointList::MaybeRemoveItemFromAnimValListAt(uint32_t aIndex) {
393 MOZ_ASSERT(!IsAnimValList(), "call from baseVal to animVal");
395 if (!AnimListMirrorsBaseList()) {
396 return;
399 // This needs to be a strong reference; otherwise, the RemovingFromList call
400 // below might drop the last reference to animVal before we're done with it.
401 RefPtr<DOMSVGPointList> animVal =
402 GetDOMWrapperIfExists(InternalAList().GetAnimValKey());
404 MOZ_ASSERT(animVal, "AnimListMirrorsBaseList() promised a non-null animVal");
405 MOZ_ASSERT(animVal->mItems.Length() == mItems.Length(),
406 "animVal list not in sync!");
408 if (animVal->mItems[aIndex]) {
409 animVal->mItems[aIndex]->RemovingFromList();
411 animVal->mItems.RemoveElementAt(aIndex);
413 UpdateListIndicesFromIndex(animVal->mItems, aIndex);
416 } // namespace mozilla::dom