Bug 1892041 - Part 1: Update test262 features. r=spidermonkey-reviewers,dminor
[gecko.git] / dom / canvas / ImageData.cpp
blobb826c7cdf0a0dc6563dd8d1dc6bdd392f35a374f
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 et tw=78: */
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/ImageData.h"
9 #include "ErrorList.h"
10 #include "js/StructuredClone.h"
11 #include "js/Value.h"
12 #include "jsapi.h"
13 #include "jsfriendapi.h"
14 #include "mozilla/CheckedInt.h"
15 #include "mozilla/ErrorResult.h"
16 #include "mozilla/HoldDropJSObjects.h"
17 #include "mozilla/RefPtr.h"
18 #include "mozilla/dom/BindingDeclarations.h"
19 #include "mozilla/dom/ImageDataBinding.h"
20 #include "nsCycleCollectionNoteChild.h"
22 namespace mozilla::dom {
24 NS_IMPL_CYCLE_COLLECTING_ADDREF(ImageData)
25 NS_IMPL_CYCLE_COLLECTING_RELEASE(ImageData)
27 NS_IMPL_CYCLE_COLLECTION_CLASS(ImageData)
29 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ImageData)
30 NS_INTERFACE_MAP_ENTRY(nsISupports)
31 NS_INTERFACE_MAP_END
33 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(ImageData)
34 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mData)
35 NS_IMPL_CYCLE_COLLECTION_TRACE_END
37 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ImageData)
38 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
40 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ImageData)
41 tmp->DropData();
42 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
44 // static
45 already_AddRefed<ImageData> ImageData::Constructor(const GlobalObject& aGlobal,
46 const uint32_t aWidth,
47 const uint32_t aHeight,
48 ErrorResult& aRv) {
49 if (aWidth == 0 || aHeight == 0) {
50 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
51 return nullptr;
54 // Restrict the typed array length to INT32_MAX because that's all we support.
55 CheckedInt<uint32_t> length = CheckedInt<uint32_t>(aWidth) * aHeight * 4;
56 if (!length.isValid() || length.value() > INT32_MAX) {
57 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
58 return nullptr;
60 js::AssertSameCompartment(aGlobal.Context(), aGlobal.Get());
61 JSObject* data =
62 Uint8ClampedArray::Create(aGlobal.Context(), length.value(), aRv);
63 if (aRv.Failed()) {
64 return nullptr;
66 RefPtr<ImageData> imageData = new ImageData(aWidth, aHeight, *data);
67 return imageData.forget();
70 // static
71 already_AddRefed<ImageData> ImageData::Constructor(
72 const GlobalObject& aGlobal, const Uint8ClampedArray& aData,
73 const uint32_t aWidth, const Optional<uint32_t>& aHeight,
74 ErrorResult& aRv) {
75 Maybe<uint32_t> maybeLength = aData.ProcessData(
76 [&](const Span<uint8_t>& aData, JS::AutoCheckCannotGC&& nogc) {
77 return Some(aData.Length());
78 });
79 uint32_t length = maybeLength.valueOr(0);
80 if (length == 0 || length % 4) {
81 aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
82 return nullptr;
84 length /= 4;
85 if (aWidth == 0) {
86 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
87 return nullptr;
89 uint32_t height = length / aWidth;
90 if (length != aWidth * height ||
91 (aHeight.WasPassed() && aHeight.Value() != height)) {
92 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
93 return nullptr;
95 RefPtr<ImageData> imageData = new ImageData(aWidth, height, *aData.Obj());
96 return imageData.forget();
99 void ImageData::HoldData() { mozilla::HoldJSObjects(this); }
101 void ImageData::DropData() {
102 if (mData) {
103 mozilla::DropJSObjects(this);
107 bool ImageData::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto,
108 JS::MutableHandle<JSObject*> aReflector) {
109 return ImageData_Binding::Wrap(aCx, this, aGivenProto, aReflector);
112 // static
113 already_AddRefed<ImageData> ImageData::ReadStructuredClone(
114 JSContext* aCx, nsIGlobalObject* aGlobal,
115 JSStructuredCloneReader* aReader) {
116 // Read the information out of the stream.
117 uint32_t width, height;
118 JS::Rooted<JS::Value> dataArray(aCx);
119 if (!JS_ReadUint32Pair(aReader, &width, &height) ||
120 !JS_ReadTypedArray(aReader, &dataArray)) {
121 return nullptr;
123 MOZ_ASSERT(dataArray.isObject());
125 RefPtr<ImageData> imageData =
126 new ImageData(width, height, dataArray.toObject());
127 return imageData.forget();
130 bool ImageData::WriteStructuredClone(JSContext* aCx,
131 JSStructuredCloneWriter* aWriter) const {
132 JS::Rooted<JS::Value> arrayValue(aCx, JS::ObjectValue(*GetDataObject()));
133 if (!JS_WrapValue(aCx, &arrayValue)) {
134 return false;
137 return JS_WriteUint32Pair(aWriter, Width(), Height()) &&
138 JS_WriteTypedArray(aWriter, arrayValue);
141 } // namespace mozilla::dom