DOMPoint.cpp (4479B)
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/. */ 6 7 #include "mozilla/dom/DOMPoint.h" 8 9 #include <cstdint> 10 11 #include "js/StructuredClone.h" 12 #include "mozilla/Casting.h" 13 #include "mozilla/ErrorResult.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" 19 20 using namespace mozilla; 21 using namespace mozilla::dom; 22 23 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMPointReadOnly, mParent) 24 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(); 30 } 31 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(); 37 } 38 39 JSObject* DOMPointReadOnly::WrapObject(JSContext* aCx, 40 JS::Handle<JSObject*> aGivenProto) { 41 return DOMPointReadOnly_Binding::Wrap(aCx, this, aGivenProto); 42 } 43 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; 50 } 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(); 58 } 59 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) 66 67 return WriteDouble(mX) && WriteDouble(mY) && WriteDouble(mZ) && 68 WriteDouble(mW); 69 70 #undef WriteDouble 71 } 72 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; 80 } 81 return retval.forget(); 82 ; 83 } 84 85 bool DOMPointReadOnly::ReadStructuredClone(JSStructuredCloneReader* aReader) { 86 uint32_t high; 87 uint32_t low; 88 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)) 94 95 ReadDouble(&mX); 96 ReadDouble(&mY); 97 ReadDouble(&mZ); 98 ReadDouble(&mW); 99 100 return true; 101 #undef ReadDouble 102 } 103 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(); 109 } 110 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(); 116 } 117 118 JSObject* DOMPoint::WrapObject(JSContext* aCx, 119 JS::Handle<JSObject*> aGivenProto) { 120 return DOMPoint_Binding::Wrap(aCx, this, aGivenProto); 121 } 122 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; 130 } 131 return retval.forget(); 132 ; 133 }