23aebc7822164b208ddfb878653e5cc6db3ead85
[profile/ivi/qtdeclarative.git] / src / declarative / qml / v8 / qjsvalue_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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 ** us via http://www.qt-project.org/.
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 bool hasProperty(const QString &name) const;
134     inline bool hasOwnProperty(const QString &name) const;
135     inline QJSValue::PropertyFlags propertyFlags(const QString& name) const;
136     inline QJSValue::PropertyFlags propertyFlags(v8::Handle<v8::String> name) const;
137
138     inline QScriptPassPointer<QJSValuePrivate> call(QJSValuePrivate* thisObject, const QJSValueList& args);
139     inline QScriptPassPointer<QJSValuePrivate> call(QJSValuePrivate* thisObject, const QJSValue& arguments);
140     inline QScriptPassPointer<QJSValuePrivate> call(QJSValuePrivate* thisObject, int argc, v8::Handle< v8::Value >* argv);
141     inline QScriptPassPointer<QJSValuePrivate> callAsConstructor(int argc, v8::Handle<v8::Value> *argv);
142     inline QScriptPassPointer<QJSValuePrivate> callAsConstructor(const QJSValueList& args);
143     inline QScriptPassPointer<QJSValuePrivate> callAsConstructor(const QJSValue& arguments);
144
145     inline bool assignEngine(QV8Engine *engine);
146     inline QV8Engine *engine() const;
147
148     inline operator v8::Handle<v8::Value>() const;
149     inline operator v8::Handle<v8::Object>() const;
150     inline v8::Handle<v8::Value> asV8Value(QV8Engine *engine);
151 private:
152     QIntrusiveListNode m_node;
153     QV8Engine *m_engine;
154
155     // Please, update class documentation when you change the enum.
156     enum State {
157         Invalid = 0,
158         CString = 0x1000,
159         CNumber,
160         CBool,
161         CNull,
162         CUndefined,
163         JSValue = 0x2000, // V8 values are equal or higher then this value.
164         // JSPrimitive,
165         // JSObject
166     } m_state;
167
168     union CValue {
169         bool m_bool;
170         double m_number;
171         QString* m_string;
172
173         CValue() : m_number(0) {}
174         CValue(bool value) : m_bool(value) {}
175         CValue(int number) : m_number(number) {}
176         CValue(uint number) : m_number(number) {}
177         CValue(double number) : m_number(number) {}
178         CValue(QString* string) : m_string(string) {}
179     } u;
180     // v8::Persistent is not a POD, so can't be part of the union.
181     v8::Persistent<v8::Value> m_value;
182
183     Q_DISABLE_COPY(QJSValuePrivate)
184     inline bool isJSBased() const;
185     inline bool isNumberBased() const;
186     inline bool isStringBased() const;
187     inline bool prepareArgumentsForCall(v8::Handle<v8::Value> argv[], const QJSValueList& arguments) const;
188
189     friend class QV8Engine;
190 };
191
192 QT_END_NAMESPACE
193
194 #endif