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"
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
);
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
);
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
);
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
) &&
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
)) {
81 return retval
.forget();
85 bool DOMPointReadOnly::ReadStructuredClone(JSStructuredCloneReader
* aReader
) {
89 #define ReadDouble(d) \
90 if (!JS_ReadUint32Pair(aReader, &high, &low)) { \
93 (*(d) = BitwiseCast<double>(static_cast<uint64_t>(high) << 32 | low))
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
);
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
);
118 JSObject
* DOMPoint::WrapObject(JSContext
* aCx
,
119 JS::Handle
<JSObject
*> aGivenProto
) {
120 return DOMPoint_Binding::Wrap(aCx
, this, aGivenProto
);
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
)) {
131 return retval
.forget();