1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtScript module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL-ONLY$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser
12 ** General Public License version 2.1 as published by the Free Software
13 ** Foundation and appearing in the file LICENSE.LGPL included in the
14 ** packaging of this file. Please review the following information to
15 ** ensure the GNU Lesser General Public License version 2.1 requirements
16 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** If you have questions regarding the use of this file, please contact
19 ** Nokia at qt-info@nokia.com.
22 ****************************************************************************/
28 // This file is not part of the Qt API. It exists purely as an
29 // implementation detail. This header file may change from version to
30 // version without notice, or even be removed.
38 #include <private/qv8_p.h>
40 #include <QtCore/qbytearray.h>
41 #include <QtCore/qdatetime.h>
42 #include <QtCore/qmath.h>
43 #include <QtCore/qvarlengtharray.h>
46 #include "qscripttools_p.h"
47 #include "qscriptshareddata_p.h"
54 \class QJSValuePrivate
58 , public QScriptLinkedNode
61 inline QJSValuePrivate();
62 inline static QJSValuePrivate* get(const QJSValue& q);
63 inline static QJSValue get(const QJSValuePrivate* d);
64 inline static QJSValue get(QJSValuePrivate* d);
65 inline static QJSValue get(QScriptPassPointer<QJSValuePrivate> d);
66 inline ~QJSValuePrivate();
68 inline QJSValuePrivate(bool value);
69 inline QJSValuePrivate(int value);
70 inline QJSValuePrivate(uint value);
71 inline QJSValuePrivate(double value);
72 inline QJSValuePrivate(const QString& value);
73 inline QJSValuePrivate(QJSValue::SpecialValue value);
75 inline QJSValuePrivate(QV8Engine *engine, bool value);
76 inline QJSValuePrivate(QV8Engine *engine, int value);
77 inline QJSValuePrivate(QV8Engine *engine, uint value);
78 inline QJSValuePrivate(QV8Engine *engine, double value);
79 inline QJSValuePrivate(QV8Engine *engine, const QString& value);
80 inline QJSValuePrivate(QV8Engine *engine, QJSValue::SpecialValue value);
81 inline QJSValuePrivate(QV8Engine *engine, v8::Handle<v8::Value>);
82 inline void reinitialize();
83 inline void reinitialize(QV8Engine *engine, v8::Handle<v8::Value> value);
85 inline bool toBool() const;
86 inline double toNumber() const;
87 inline QScriptPassPointer<QJSValuePrivate> toObject() const;
88 inline QScriptPassPointer<QJSValuePrivate> toObject(QV8Engine *engine) const;
89 inline QString toString() const;
90 inline double toInteger() const;
91 inline qint32 toInt32() const;
92 inline quint32 toUInt32() const;
93 inline quint16 toUInt16() const;
94 inline QDateTime toDataTime() const;
95 inline QRegExp toRegExp() const;
96 inline QObject *toQObject() const;
97 inline QVariant toVariant() const;
99 inline bool isArray() const;
100 inline bool isBool() const;
101 inline bool isCallable() const;
102 inline bool isError() const;
103 inline bool isFunction() const;
104 inline bool isNull() const;
105 inline bool isNumber() const;
106 inline bool isObject() const;
107 inline bool isString() const;
108 inline bool isUndefined() const;
109 inline bool isValid() const;
110 inline bool isVariant() const;
111 inline bool isDate() const;
112 inline bool isRegExp() const;
113 inline bool isQObject() const;
115 inline bool equals(QJSValuePrivate* other);
116 inline bool strictlyEquals(QJSValuePrivate* other);
117 inline bool lessThan(QJSValuePrivate *other) const;
118 inline bool instanceOf(QJSValuePrivate*) const;
119 inline bool instanceOf(v8::Handle<v8::Object> other) const;
121 inline QScriptPassPointer<QJSValuePrivate> prototype() const;
122 inline void setPrototype(QJSValuePrivate* prototype);
124 inline void setProperty(const QString &name, QJSValuePrivate *value, uint attribs = 0);
125 inline void setProperty(v8::Handle<v8::String> name, QJSValuePrivate *value, uint attribs = 0);
126 inline void setProperty(quint32 index, QJSValuePrivate* value, uint attribs = 0);
127 inline QScriptPassPointer<QJSValuePrivate> property(const QString& name) const;
128 inline QScriptPassPointer<QJSValuePrivate> property(v8::Handle<v8::String> name) const;
129 inline QScriptPassPointer<QJSValuePrivate> property(quint32 index) const;
131 inline QScriptPassPointer<QJSValuePrivate> property(T name) const;
132 inline bool deleteProperty(const QString& name);
133 inline QJSValue::PropertyFlags propertyFlags(const QString& name) const;
134 inline QJSValue::PropertyFlags propertyFlags(v8::Handle<v8::String> name) const;
136 inline QScriptPassPointer<QJSValuePrivate> call(QJSValuePrivate* thisObject, const QJSValueList& args);
137 inline QScriptPassPointer<QJSValuePrivate> call(QJSValuePrivate* thisObject, const QJSValue& arguments);
138 inline QScriptPassPointer<QJSValuePrivate> call(QJSValuePrivate* thisObject, int argc, v8::Handle< v8::Value >* argv);
139 inline QScriptPassPointer<QJSValuePrivate> construct(int argc, v8::Handle<v8::Value> *argv);
140 inline QScriptPassPointer<QJSValuePrivate> construct(const QJSValueList& args);
141 inline QScriptPassPointer<QJSValuePrivate> construct(const QJSValue& arguments);
143 inline bool assignEngine(QV8Engine *engine);
144 inline QV8Engine *engine() const;
146 inline operator v8::Handle<v8::Value>() const;
147 inline operator v8::Handle<v8::Object>() const;
148 inline v8::Handle<v8::Value> asV8Value(QV8Engine *engine);
152 // Please, update class documentation when you change the enum.
160 JSValue = 0x2000, // V8 values are equal or higher then this value.
170 CValue() : m_number(0) {}
171 CValue(bool value) : m_bool(value) {}
172 CValue(int number) : m_number(number) {}
173 CValue(uint number) : m_number(number) {}
174 CValue(double number) : m_number(number) {}
175 CValue(QString* string) : m_string(string) {}
177 // v8::Persistent is not a POD, so can't be part of the union.
178 v8::Persistent<v8::Value> m_value;
180 Q_DISABLE_COPY(QJSValuePrivate)
181 inline bool isJSBased() const;
182 inline bool isNumberBased() const;
183 inline bool isStringBased() const;
184 inline bool prepareArgumentsForCall(v8::Handle<v8::Value> argv[], const QJSValueList& arguments) const;
187 // This template is used indirectly by the Q_GLOBAL_STATIC macro below
189 class QGlobalStaticDeleter<QJSValuePrivate>
192 QGlobalStatic<QJSValuePrivate> &globalStatic;
193 QGlobalStaticDeleter(QGlobalStatic<QJSValuePrivate> &_globalStatic)
194 : globalStatic(_globalStatic)
196 globalStatic.pointer->ref.ref();
199 inline ~QGlobalStaticDeleter()
201 if (!globalStatic.pointer->ref.deref()) { // Logic copy & paste from SharedDataPointer
202 delete globalStatic.pointer;
204 globalStatic.pointer = 0;
205 globalStatic.destroyed = true;
209 Q_GLOBAL_STATIC(QJSValuePrivate, InvalidValue)