Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / dom / base / DOMPoint.cpp
blob510bb76b0713e5a350600db8ea80f62d13549c4a
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 "mozilla/dom/DOMPoint.h"
9 #include <cstdint>
10 #include "js/StructuredClone.h"
11 #include "mozilla/Casting.h"
12 #include "mozilla/ErrorResult.h"
13 #include "mozilla/MacroForEach.h"
14 #include "mozilla/RefPtr.h"
15 #include "mozilla/dom/BindingDeclarations.h"
16 #include "mozilla/dom/DOMMatrix.h"
17 #include "mozilla/dom/DOMPointBinding.h"
18 #include "nsIGlobalObject.h"
20 using namespace mozilla;
21 using namespace mozilla::dom;
23 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMPointReadOnly, mParent)
25 already_AddRefed<DOMPointReadOnly> DOMPointReadOnly::FromPoint(
26 const GlobalObject& aGlobal, const DOMPointInit& aParams) {
27 RefPtr<DOMPointReadOnly> obj = new DOMPointReadOnly(
28 aGlobal.GetAsSupports(), aParams.mX, aParams.mY, aParams.mZ, aParams.mW);
29 return obj.forget();
32 already_AddRefed<DOMPointReadOnly> DOMPointReadOnly::Constructor(
33 const GlobalObject& aGlobal, double aX, double aY, double aZ, double aW) {
34 RefPtr<DOMPointReadOnly> obj =
35 new DOMPointReadOnly(aGlobal.GetAsSupports(), aX, aY, aZ, aW);
36 return obj.forget();
39 JSObject* DOMPointReadOnly::WrapObject(JSContext* aCx,
40 JS::Handle<JSObject*> aGivenProto) {
41 return DOMPointReadOnly_Binding::Wrap(aCx, this, aGivenProto);
44 already_AddRefed<DOMPoint> DOMPointReadOnly::MatrixTransform(
45 const DOMMatrixInit& aInit, ErrorResult& aRv) {
46 RefPtr<DOMMatrixReadOnly> matrix =
47 DOMMatrixReadOnly::FromMatrix(mParent, aInit, aRv);
48 if (aRv.Failed()) {
49 return nullptr;
51 DOMPointInit init;
52 init.mX = this->mX;
53 init.mY = this->mY;
54 init.mZ = this->mZ;
55 init.mW = this->mW;
56 RefPtr<DOMPoint> point = matrix->TransformPoint(init);
57 return point.forget();
60 // https://drafts.fxtf.org/geometry/#structured-serialization
61 bool DOMPointReadOnly::WriteStructuredClone(
62 JSContext* aCx, JSStructuredCloneWriter* aWriter) const {
63 #define WriteDouble(d) \
64 JS_WriteUint32Pair(aWriter, (BitwiseCast<uint64_t>(d) >> 32) & 0xffffffff, \
65 BitwiseCast<uint64_t>(d) & 0xffffffff)
67 return WriteDouble(mX) && WriteDouble(mY) && WriteDouble(mZ) &&
68 WriteDouble(mW);
70 #undef WriteDouble
73 // static
74 already_AddRefed<DOMPointReadOnly> DOMPointReadOnly::ReadStructuredClone(
75 JSContext* aCx, nsIGlobalObject* aGlobal,
76 JSStructuredCloneReader* aReader) {
77 RefPtr<DOMPointReadOnly> retval = new DOMPointReadOnly(aGlobal);
78 if (!retval->ReadStructuredClone(aReader)) {
79 return nullptr;
81 return retval.forget();
85 bool DOMPointReadOnly::ReadStructuredClone(JSStructuredCloneReader* aReader) {
86 uint32_t high;
87 uint32_t low;
89 #define ReadDouble(d) \
90 if (!JS_ReadUint32Pair(aReader, &high, &low)) { \
91 return false; \
92 } \
93 (*(d) = BitwiseCast<double>(static_cast<uint64_t>(high) << 32 | low))
95 ReadDouble(&mX);
96 ReadDouble(&mY);
97 ReadDouble(&mZ);
98 ReadDouble(&mW);
100 return true;
101 #undef ReadDouble
104 already_AddRefed<DOMPoint> DOMPoint::FromPoint(const GlobalObject& aGlobal,
105 const DOMPointInit& aParams) {
106 RefPtr<DOMPoint> obj = new DOMPoint(aGlobal.GetAsSupports(), aParams.mX,
107 aParams.mY, aParams.mZ, aParams.mW);
108 return obj.forget();
111 already_AddRefed<DOMPoint> DOMPoint::Constructor(const GlobalObject& aGlobal,
112 double aX, double aY,
113 double aZ, double aW) {
114 RefPtr<DOMPoint> obj = new DOMPoint(aGlobal.GetAsSupports(), aX, aY, aZ, aW);
115 return obj.forget();
118 JSObject* DOMPoint::WrapObject(JSContext* aCx,
119 JS::Handle<JSObject*> aGivenProto) {
120 return DOMPoint_Binding::Wrap(aCx, this, aGivenProto);
123 // static
124 already_AddRefed<DOMPoint> DOMPoint::ReadStructuredClone(
125 JSContext* aCx, nsIGlobalObject* aGlobal,
126 JSStructuredCloneReader* aReader) {
127 RefPtr<DOMPoint> retval = new DOMPoint(aGlobal);
128 if (!retval->ReadStructuredClone(aReader)) {
129 return nullptr;
131 return retval.forget();