Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qv4variantobject.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6#include <private/qqmlvaluetypewrapper_p.h>
7#include <private/qv4qobjectwrapper_p.h>
8
10
11using namespace QV4;
12
14
16{
17 Object::init();
19}
20
22{
23 Object::init();
25 if (isScarce())
26 removeVmePropertyReference();
27}
28
29bool VariantObject::Data::isScarce() const
30{
31 int t = data().userType();
32 return t == QMetaType::QPixmap || t == QMetaType::QImage;
33}
34
36{
38 QV4::VariantObject *lv = static_cast<QV4::VariantObject *>(m);
39
41 return lv->d()->data() == rv->d()->data();
42
44 return v->isEqual(lv->d()->data());
45
46 return false;
47}
48
50{
51 if (d()->isScarce() && ++d()->vmePropertyReferenceCount == 1) {
52 // remove from the ep->scarceResources list
53 // since it is now no longer eligible to be
54 // released automatically by the engine.
56 }
57}
58
60{
61 if (d()->isScarce() && --d()->vmePropertyReferenceCount == 0) {
62 // and add to the ep->scarceResources list
63 // since it is now eligible to be released
64 // automatically by the engine.
65 d()->removeVmePropertyReference();
66 }
67}
68
69
71{
72 defineDefaultProperty(QStringLiteral("preserve"), method_preserve, 0);
73 defineDefaultProperty(QStringLiteral("destroy"), method_destroy, 0);
74 defineDefaultProperty(engine()->id_valueOf(), method_valueOf, 0);
75 defineDefaultProperty(engine()->id_toString(), method_toString, 0);
76}
77
79{
80 const VariantObject *o = thisObject->as<QV4::VariantObject>();
81 if (o && o->d()->isScarce())
82 o->d()->addVmePropertyReference();
84}
85
87{
88 const VariantObject *o = thisObject->as<QV4::VariantObject>();
89 if (o) {
90 if (o->d()->isScarce())
91 o->d()->addVmePropertyReference();
92 o->d()->data() = QVariant();
93 }
95}
96
98{
99 ExecutionEngine *v4 = b->engine();
100 const VariantObject *o = thisObject->as<QV4::VariantObject>();
101 if (!o)
103 const QVariant variant = o->d()->data();
105 if (result.isEmpty() && !variant.canConvert(QMetaType(QMetaType::QString))) {
106 QDebug dbg(&result);
107 dbg << variant;
108 // QDebug appends a space, we're not interested in continuing the stream so we chop it off.
109 // Can't use nospace() because it would affect the debug-stream operator of the variant.
110 result.chop(1);
111 }
112 return Encode(v4->newString(result));
113}
114
116{
117 const VariantObject *o = thisObject->as<QV4::VariantObject>();
118 if (o) {
119 QVariant v = o->d()->data();
120 switch (v.userType()) {
122 return Encode::undefined();
123 case QMetaType::QString:
124 return Encode(b->engine()->newString(v.toString()));
125 case QMetaType::Int:
126 return Encode(v.toInt());
127 case QMetaType::Double:
128 case QMetaType::UInt:
129 return Encode(v.toDouble());
130 case QMetaType::Bool:
131 return Encode(v.toBool());
132 default:
133 if (QMetaType(v.metaType()).flags() & QMetaType::IsEnumeration)
134 if (v.metaType().sizeOf() <= qsizetype(sizeof(int)))
135 return Encode(v.toInt());
136 if (v.canConvert<double>())
137 return Encode(v.toDouble());
138 if (v.canConvert<int>())
139 return Encode(v.toInt());
140 if (v.canConvert<uint>())
141 return Encode(v.toUInt());
142 if (v.canConvert<bool>())
143 return Encode(v.toBool());
144 if (v.canConvert<QString>())
145 return Encode(b->engine()->newString(v.toString()));
146 break;
147 }
148 }
149 return thisObject->asReturnedValue();
150}
151
\inmodule QtCore
\inmodule QtCore
Definition qmetatype.h:341
constexpr TypeFlags flags() const
Definition qmetatype.h:2658
@ IsEnumeration
Definition qmetatype.h:407
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qvariant.h:65
QString toString() const
Returns the variant as a QString if the variant has a userType() including, but not limited to:
bool canConvert(QMetaType targetType) const
Definition qvariant.h:345
Combined button and popup list for selecting options.
quint64 ReturnedValue
GLboolean GLboolean GLboolean b
GLsizei const GLfloat * v
[13]
const GLfloat * m
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum type
GLdouble GLdouble t
Definition qopenglext.h:243
GLuint64EXT * result
[6]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define QStringLiteral(str)
ptrdiff_t qsizetype
Definition qtypes.h:165
unsigned int uint
Definition qtypes.h:34
#define RETURN_UNDEFINED()
#define DEFINE_OBJECT_VTABLE(classname)
QVariant variant
[1]
QSharedPointer< T > other(t)
[5]
QJSEngine engine
[0]
static constexpr ReturnedValue undefined()
Heap::String * newString(char16_t c)
constexpr ReturnedValue asReturnedValue() const
static constexpr VTable::IsEqualTo virtualIsEqualTo
const T * as() const
Definition qv4value_p.h:132
V4_NEEDS_DESTROY void addVmePropertyReference() const
static ReturnedValue method_destroy(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_valueOf(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_preserve(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
static ReturnedValue method_toString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc)