Merge branch 'master' into refactor
[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 <private/qintrusivelist_p.h>
47 #include "qscriptshareddata_p.h"
48 #include "qjsvalue.h"
49
50 QT_BEGIN_NAMESPACE
51
52 class QV8Engine;
53
54 /*!
55   \internal
56   \class QJSValuePrivate
57 */
58 class QJSValuePrivate
59         : public QSharedData
60 {
61 public:
62     inline QJSValuePrivate();
63     inline static QJSValuePrivate* get(const QJSValue& q);
64     inline static QJSValue get(const QJSValuePrivate* d);
65     inline static QJSValue get(QJSValuePrivate* d);
66     inline static QJSValue get(QScriptPassPointer<QJSValuePrivate> d);
67     inline ~QJSValuePrivate();
68
69     inline QJSValuePrivate(bool value);
70     inline QJSValuePrivate(int value);
71     inline QJSValuePrivate(uint value);
72     inline QJSValuePrivate(double value);
73     inline QJSValuePrivate(const QString& value);
74     inline QJSValuePrivate(QJSValue::SpecialValue value);
75
76     inline QJSValuePrivate(QV8Engine *engine, bool value);
77     inline QJSValuePrivate(QV8Engine *engine, int value);
78     inline QJSValuePrivate(QV8Engine *engine, uint value);
79     inline QJSValuePrivate(QV8Engine *engine, double value);
80     inline QJSValuePrivate(QV8Engine *engine, const QString& value);
81     inline QJSValuePrivate(QV8Engine *engine, QJSValue::SpecialValue value);
82     inline QJSValuePrivate(QV8Engine *engine, v8::Handle<v8::Value>);
83     inline void invalidate();
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     QIntrusiveListNode m_node;
151     QV8Engine *m_engine;
152
153     // Please, update class documentation when you change the enum.
154     enum State {
155         Invalid = 0,
156         CString = 0x1000,
157         CNumber,
158         CBool,
159         CNull,
160         CUndefined,
161         JSValue = 0x2000, // V8 values are equal or higher then this value.
162         // JSPrimitive,
163         // JSObject
164     } m_state;
165
166     union CValue {
167         bool m_bool;
168         double m_number;
169         QString* m_string;
170
171         CValue() : m_number(0) {}
172         CValue(bool value) : m_bool(value) {}
173         CValue(int number) : m_number(number) {}
174         CValue(uint number) : m_number(number) {}
175         CValue(double number) : m_number(number) {}
176         CValue(QString* string) : m_string(string) {}
177     } u;
178     // v8::Persistent is not a POD, so can't be part of the union.
179     v8::Persistent<v8::Value> m_value;
180
181     Q_DISABLE_COPY(QJSValuePrivate)
182     inline bool isJSBased() const;
183     inline bool isNumberBased() const;
184     inline bool isStringBased() const;
185     inline bool prepareArgumentsForCall(v8::Handle<v8::Value> argv[], const QJSValueList& arguments) const;
186
187     friend class QV8Engine;
188 };
189
190 QT_END_NAMESPACE
191
192 #endif