2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-09-30 20:40:24 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef nsAttrValueInlines_h__
|
|
|
|
#define nsAttrValueInlines_h__
|
|
|
|
|
2013-07-30 18:25:31 +04:00
|
|
|
#include <stdint.h>
|
2012-10-02 12:57:57 +04:00
|
|
|
|
2013-09-06 10:40:03 +04:00
|
|
|
#include "nsAttrValue.h"
|
2018-01-30 21:29:14 +03:00
|
|
|
#include "mozilla/Atomics.h"
|
2015-02-26 16:21:58 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
2018-01-30 21:29:14 +03:00
|
|
|
#include "mozilla/ServoUtils.h"
|
2013-09-06 10:40:03 +04:00
|
|
|
|
2014-07-15 19:37:45 +04:00
|
|
|
struct MiscContainer;
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
struct MiscContainer final {
|
2012-09-30 20:40:24 +04:00
|
|
|
typedef nsAttrValue::ValueType ValueType;
|
|
|
|
|
|
|
|
ValueType mType;
|
2017-10-03 01:05:19 +03:00
|
|
|
// mStringBits points to either nsAtom* or nsStringBuffer* and is used when
|
2016-10-18 07:29:03 +03:00
|
|
|
// mType isn't eCSSDeclaration.
|
2012-09-30 20:40:24 +04:00
|
|
|
// Note eStringBase and eAtomBase is used also to handle the type of
|
|
|
|
// mStringBits.
|
2018-01-30 21:29:14 +03:00
|
|
|
//
|
2018-02-02 21:49:56 +03:00
|
|
|
// Note that we use an atomic here so that we can use Compare-And-Swap
|
2018-01-30 21:29:14 +03:00
|
|
|
// to cache the serialization during the parallel servo traversal. This case
|
|
|
|
// (which happens when the main thread is blocked) is the only case where
|
2018-02-02 21:49:56 +03:00
|
|
|
// mStringBits is mutated off-main-thread. The Atomic needs to be
|
|
|
|
// ReleaseAcquire so that the pointer to the serialization does not become
|
|
|
|
// observable to other threads before the initialization of the pointed-to
|
|
|
|
// memory is also observable.
|
|
|
|
mozilla::Atomic<uintptr_t, mozilla::ReleaseAcquire> mStringBits;
|
2012-09-30 20:40:24 +04:00
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
union {
|
|
|
|
int32_t mInteger;
|
|
|
|
nscolor mColor;
|
|
|
|
uint32_t mEnumValue;
|
|
|
|
int32_t mPercent;
|
2016-10-18 07:29:03 +03:00
|
|
|
mozilla::DeclarationBlock* mCSSDeclaration;
|
2018-04-10 17:54:57 +03:00
|
|
|
nsIURI* mURL;
|
2017-10-13 03:59:23 +03:00
|
|
|
mozilla::AtomArray* mAtomArray;
|
2012-09-30 20:40:24 +04:00
|
|
|
nsIntMargin* mIntMargin;
|
2019-01-02 00:01:19 +03:00
|
|
|
const mozilla::SVGAngle* mSVGAngle;
|
2019-01-06 11:22:55 +03:00
|
|
|
const mozilla::SVGIntegerPair* mSVGIntegerPair;
|
2012-09-30 20:40:24 +04:00
|
|
|
const nsSVGLength2* mSVGLength;
|
|
|
|
const mozilla::SVGLengthList* mSVGLengthList;
|
|
|
|
const mozilla::SVGNumberList* mSVGNumberList;
|
2019-01-06 11:22:55 +03:00
|
|
|
const mozilla::SVGNumberPair* mSVGNumberPair;
|
2012-09-30 20:40:24 +04:00
|
|
|
const mozilla::SVGPathData* mSVGPathData;
|
|
|
|
const mozilla::SVGPointList* mSVGPointList;
|
|
|
|
const mozilla::SVGAnimatedPreserveAspectRatio* mSVGPreserveAspectRatio;
|
|
|
|
const mozilla::SVGStringList* mSVGStringList;
|
|
|
|
const mozilla::SVGTransformList* mSVGTransformList;
|
2019-01-06 20:52:55 +03:00
|
|
|
const mozilla::SVGViewBox* mSVGViewBox;
|
2012-09-30 20:40:24 +04:00
|
|
|
};
|
|
|
|
uint32_t mRefCount : 31;
|
|
|
|
uint32_t mCached : 1;
|
|
|
|
} mValue;
|
|
|
|
double mDoubleValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
MiscContainer() : mType(nsAttrValue::eColor), mStringBits(0) {
|
|
|
|
MOZ_COUNT_CTOR(MiscContainer);
|
|
|
|
mValue.mColor = 0;
|
|
|
|
mValue.mRefCount = 0;
|
|
|
|
mValue.mCached = 0;
|
|
|
|
}
|
|
|
|
|
2015-02-26 16:21:58 +03:00
|
|
|
protected:
|
|
|
|
// Only nsAttrValue should be able to delete us.
|
|
|
|
friend class nsAttrValue;
|
|
|
|
|
2012-09-30 20:40:24 +04:00
|
|
|
~MiscContainer() {
|
|
|
|
if (IsRefCounted()) {
|
|
|
|
MOZ_ASSERT(mValue.mRefCount == 0);
|
|
|
|
MOZ_ASSERT(!mValue.mCached);
|
|
|
|
}
|
|
|
|
MOZ_COUNT_DTOR(MiscContainer);
|
|
|
|
}
|
|
|
|
|
2015-02-26 16:21:58 +03:00
|
|
|
public:
|
2013-01-05 00:31:50 +04:00
|
|
|
bool GetString(nsAString& aString) const;
|
2012-09-30 20:40:24 +04:00
|
|
|
|
2018-01-30 21:29:14 +03:00
|
|
|
void SetStringBitsMainThread(uintptr_t aBits) {
|
|
|
|
// mStringBits is atomic, but the callers of this function are
|
|
|
|
// single-threaded so they don't have to worry about it.
|
|
|
|
MOZ_ASSERT(!mozilla::IsInServoTraversal());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mStringBits = aBits;
|
|
|
|
}
|
|
|
|
|
2012-09-30 20:40:24 +04:00
|
|
|
inline bool IsRefCounted() const {
|
|
|
|
// Nothing stops us from refcounting (and sharing) other types of
|
|
|
|
// MiscContainer (except eDoubleValue types) but there's no compelling
|
2016-06-24 06:35:12 +03:00
|
|
|
// reason to.
|
2016-10-18 07:29:03 +03:00
|
|
|
return mType == nsAttrValue::eCSSDeclaration;
|
2012-09-30 20:40:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline int32_t AddRef() {
|
|
|
|
MOZ_ASSERT(IsRefCounted());
|
|
|
|
return ++mValue.mRefCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int32_t Release() {
|
|
|
|
MOZ_ASSERT(IsRefCounted());
|
|
|
|
return --mValue.mRefCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cache();
|
|
|
|
void Evict();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of inline methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
inline int32_t nsAttrValue::GetIntegerValue() const {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(Type() == eInteger, "wrong type");
|
2012-09-30 20:40:24 +04:00
|
|
|
return (BaseType() == eIntegerBase) ? GetIntInternal()
|
|
|
|
: GetMiscContainer()->mValue.mInteger;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int16_t nsAttrValue::GetEnumValue() const {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(Type() == eEnum, "wrong type");
|
2012-09-30 20:40:24 +04:00
|
|
|
// We don't need to worry about sign extension here since we're
|
|
|
|
// returning an int16_t which will cut away the top bits.
|
|
|
|
return static_cast<int16_t>(((BaseType() == eIntegerBase)
|
|
|
|
? static_cast<uint32_t>(GetIntInternal())
|
|
|
|
: GetMiscContainer()->mValue.mEnumValue) >>
|
|
|
|
NS_ATTRVALUE_ENUMTABLEINDEX_BITS);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline float nsAttrValue::GetPercentValue() const {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(Type() == ePercent, "wrong type");
|
2012-09-30 20:40:24 +04:00
|
|
|
return ((BaseType() == eIntegerBase) ? GetIntInternal()
|
|
|
|
: GetMiscContainer()->mValue.mPercent) /
|
|
|
|
100.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline mozilla::AtomArray* nsAttrValue::GetAtomArrayValue() const {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(Type() == eAtomArray, "wrong type");
|
2012-09-30 20:40:24 +04:00
|
|
|
return GetMiscContainer()->mValue.mAtomArray;
|
|
|
|
}
|
|
|
|
|
2016-10-18 07:29:03 +03:00
|
|
|
inline mozilla::DeclarationBlock* nsAttrValue::GetCSSDeclarationValue() const {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(Type() == eCSSDeclaration, "wrong type");
|
2016-10-18 07:29:03 +03:00
|
|
|
return GetMiscContainer()->mValue.mCSSDeclaration;
|
2016-06-24 06:35:12 +03:00
|
|
|
}
|
|
|
|
|
2012-09-30 20:40:24 +04:00
|
|
|
inline nsIURI* nsAttrValue::GetURLValue() const {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(Type() == eURL, "wrong type");
|
2012-09-30 20:40:24 +04:00
|
|
|
return GetMiscContainer()->mValue.mURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline double nsAttrValue::GetDoubleValue() const {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(Type() == eDoubleValue, "wrong type");
|
2012-09-30 20:40:24 +04:00
|
|
|
return GetMiscContainer()->mDoubleValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool nsAttrValue::GetIntMarginValue(nsIntMargin& aMargin) const {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(Type() == eIntMarginValue, "wrong type");
|
2012-09-30 20:40:24 +04:00
|
|
|
nsIntMargin* m = GetMiscContainer()->mValue.mIntMargin;
|
|
|
|
if (!m) return false;
|
|
|
|
aMargin = *m;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool nsAttrValue::IsSVGType(ValueType aType) const {
|
|
|
|
return aType >= eSVGTypesBegin && aType <= eSVGTypesEnd;
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:57:13 +03:00
|
|
|
inline bool nsAttrValue::StoresOwnData() const {
|
|
|
|
if (BaseType() != eOtherBase) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
ValueType t = Type();
|
2016-10-18 07:29:03 +03:00
|
|
|
return t != eCSSDeclaration && !IsSVGType(t);
|
2015-07-25 08:57:13 +03:00
|
|
|
}
|
|
|
|
|
2012-09-30 20:40:24 +04:00
|
|
|
inline void nsAttrValue::SetPtrValueAndType(void* aValue, ValueBaseType aType) {
|
|
|
|
NS_ASSERTION(!(NS_PTR_TO_INT32(aValue) & ~NS_ATTRVALUE_POINTERVALUE_MASK),
|
|
|
|
"pointer not properly aligned, this will crash");
|
2012-10-02 12:57:57 +04:00
|
|
|
mBits = reinterpret_cast<intptr_t>(aValue) | aType;
|
2012-09-30 20:40:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void nsAttrValue::ResetIfSet() {
|
|
|
|
if (mBits) {
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline MiscContainer* nsAttrValue::GetMiscContainer() const {
|
|
|
|
NS_ASSERTION(BaseType() == eOtherBase, "wrong type");
|
|
|
|
return static_cast<MiscContainer*>(GetPtr());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int32_t nsAttrValue::GetIntInternal() const {
|
|
|
|
NS_ASSERTION(BaseType() == eIntegerBase, "getting integer from non-integer");
|
|
|
|
// Make sure we get a signed value.
|
|
|
|
// Lets hope the optimizer optimizes this into a shift. Unfortunatly signed
|
|
|
|
// bitshift right is implementaion dependant.
|
|
|
|
return static_cast<int32_t>(mBits & ~NS_ATTRVALUE_INTEGERTYPE_MASK) /
|
|
|
|
NS_ATTRVALUE_INTEGERTYPE_MULTIPLIER;
|
|
|
|
}
|
|
|
|
|
2017-12-02 22:43:12 +03:00
|
|
|
inline nsAttrValue::ValueType nsAttrValue::Type() const {
|
|
|
|
switch (BaseType()) {
|
|
|
|
case eIntegerBase: {
|
|
|
|
return static_cast<ValueType>(mBits & NS_ATTRVALUE_INTEGERTYPE_MASK);
|
|
|
|
}
|
|
|
|
case eOtherBase: {
|
|
|
|
return GetMiscContainer()->mType;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return static_cast<ValueType>(static_cast<uint16_t>(BaseType()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsAtom* nsAttrValue::GetAtomValue() const {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(Type() == eAtom, "wrong type");
|
2017-12-02 22:43:12 +03:00
|
|
|
return reinterpret_cast<nsAtom*>(GetPtr());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void nsAttrValue::ToString(mozilla::dom::DOMString& aResult) const {
|
|
|
|
switch (Type()) {
|
|
|
|
case eString: {
|
|
|
|
nsStringBuffer* str = static_cast<nsStringBuffer*>(GetPtr());
|
|
|
|
if (str) {
|
2017-12-22 21:02:51 +03:00
|
|
|
aResult.SetKnownLiveStringBuffer(
|
|
|
|
str, str->StorageSize() / sizeof(char16_t) - 1);
|
2017-12-02 22:43:12 +03:00
|
|
|
}
|
|
|
|
// else aResult is already empty
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case eAtom: {
|
|
|
|
nsAtom* atom = static_cast<nsAtom*>(GetPtr());
|
2017-12-22 21:02:51 +03:00
|
|
|
aResult.SetKnownLiveAtom(atom, mozilla::dom::DOMString::eNullNotExpected);
|
2017-12-02 22:43:12 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: { ToString(aResult.AsAString()); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-30 20:40:24 +04:00
|
|
|
#endif
|