7b2a001f97913e64a6df09419364aec6080af18e
[profile/ivi/qtdeclarative.git] / src / declarative / qml / v8 / qjsvalue_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtScript module of the Qt Toolkit.
8 **
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.
17 **
18 ** If you have questions regarding the use of this file, please contact
19 ** Nokia at qt-info@nokia.com.
20 ** $QT_END_LICENSE$
21 **
22 ****************************************************************************/
23
24 //
25 //  W A R N I N G
26 //  -------------
27 //
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.
31 //
32 // We mean it.
33 //
34
35 #ifndef QJSVALUE_P_H
36 #define QJSVALUE_P_H
37
38 #include <private/qv8_p.h>
39
40 #include <QtCore/qbytearray.h>
41 #include <QtCore/qdatetime.h>
42 #include <QtCore/qmath.h>
43 #include <QtCore/qvarlengtharray.h>
44 #include <qdebug.h>
45
46 #include "qscripttools_p.h"
47 #include "qscriptshareddata_p.h"
48 #include "qjsvalue.h"
49
50 QT_BEGIN_NAMESPACE
51
52 /*!
53   \internal
54   \class QJSValuePrivate
55 */
56 class QJSValuePrivate
57         : public QSharedData
58         , public QScriptLinkedNode
59 {
60 public:
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();
67
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);
74
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);
84
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;
98
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;
114
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;
120
121     inline QScriptPassPointer<QJSValuePrivate> prototype() const;
122     inline void setPrototype(QJSValuePrivate* prototype);
123
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;
130     template<typename T>
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;
135
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);
142
143     inline bool assignEngine(QV8Engine *engine);
144     inline QV8Engine *engine() const;
145
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);
149 private:
150     QV8Engine *m_engine;
151
152     // Please, update class documentation when you change the enum.
153     enum State {
154         Invalid = 0,
155         CString = 0x1000,
156         CNumber,
157         CBool,
158         CNull,
159         CUndefined,
160         JSValue = 0x2000, // V8 values are equal or higher then this value.
161         // JSPrimitive,
162         // JSObject
163     } m_state;
164
165     union CValue {
166         bool m_bool;
167         double m_number;
168         QString* m_string;
169
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) {}
176     } u;
177     // v8::Persistent is not a POD, so can't be part of the union.
178     v8::Persistent<v8::Value> m_value;
179
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;
185 };
186
187 // This template is used indirectly by the Q_GLOBAL_STATIC macro below
188 template<>
189 class QGlobalStaticDeleter<QJSValuePrivate>
190 {
191 public:
192     QGlobalStatic<QJSValuePrivate> &globalStatic;
193     QGlobalStaticDeleter(QGlobalStatic<QJSValuePrivate> &_globalStatic)
194         : globalStatic(_globalStatic)
195     {
196         globalStatic.pointer->ref.ref();
197     }
198
199     inline ~QGlobalStaticDeleter()
200     {
201         if (!globalStatic.pointer->ref.deref()) { // Logic copy & paste from SharedDataPointer
202             delete globalStatic.pointer;
203         }
204         globalStatic.pointer = 0;
205         globalStatic.destroyed = true;
206     }
207 };
208
209 Q_GLOBAL_STATIC(QJSValuePrivate, InvalidValue)
210
211 QT_END_NAMESPACE
212
213 #endif