aboutsummaryrefslogtreecommitdiffstats
path: root/src/declarative/qml/v8/qjsvalue_p.h
blob: 3eccba64bd735fc6ceaf5ca076b69ef4a7054a06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtScript module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL-ONLY$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you have questions regarding the use of this file, please contact
** us via http://www.qt-project.org/.
**
** $QT_END_LICENSE$
**
****************************************************************************/

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#ifndef QJSVALUE_P_H
#define QJSVALUE_P_H

#include <private/qv8_p.h>

#include <QtCore/qbytearray.h>
#include <QtCore/qdatetime.h>
#include <QtCore/qmath.h>
#include <QtCore/qvarlengtharray.h>
#include <qdebug.h>

#include <private/qintrusivelist_p.h>
#include "qscriptshareddata_p.h"
#include "qjsvalue.h"

QT_BEGIN_NAMESPACE

class QV8Engine;

/*!
  \internal
  \class QJSValuePrivate
*/
class QJSValuePrivate
        : public QSharedData
{
public:
    enum PropertyFlag {
        ReadOnly            = 0x00000001,
        Undeletable         = 0x00000002,
        SkipInEnumeration   = 0x00000004
    };
    Q_DECLARE_FLAGS(PropertyFlags, PropertyFlag)

    inline static QJSValuePrivate* get(const QJSValue& q);
    inline static QJSValue get(const QJSValuePrivate* d);
    inline static QJSValue get(QJSValuePrivate* d);
    inline static QJSValue get(QScriptPassPointer<QJSValuePrivate> d);
    inline ~QJSValuePrivate();

    inline QJSValuePrivate(bool value);
    inline QJSValuePrivate(int value);
    inline QJSValuePrivate(uint value);
    inline QJSValuePrivate(double value);
    inline QJSValuePrivate(const QString& value);
    inline QJSValuePrivate(QJSValue::SpecialValue value = QJSValue::UndefinedValue);

    inline QJSValuePrivate(QV8Engine *engine, bool value);
    inline QJSValuePrivate(QV8Engine *engine, int value);
    inline QJSValuePrivate(QV8Engine *engine, uint value);
    inline QJSValuePrivate(QV8Engine *engine, double value);
    inline QJSValuePrivate(QV8Engine *engine, const QString& value);
    inline QJSValuePrivate(QV8Engine *engine, QJSValue::SpecialValue value = QJSValue::UndefinedValue);
    inline QJSValuePrivate(QV8Engine *engine, v8::Handle<v8::Value>);
    inline void invalidate();

    inline bool toBool() const;
    inline double toNumber() const;
    inline QString toString() const;
    inline double toInteger() const;
    inline qint32 toInt32() const;
    inline quint32 toUInt32() const;
    inline quint16 toUInt16() const;
    inline QDateTime toDataTime() const;
    inline QObject *toQObject() const;
    inline QVariant toVariant() const;

    inline bool isArray() const;
    inline bool isBool() const;
    inline bool isCallable() const;
    inline bool isError() const;
    inline bool isFunction() const;
    inline bool isNull() const;
    inline bool isNumber() const;
    inline bool isObject() const;
    inline bool isString() const;
    inline bool isUndefined() const;
    inline bool isVariant() const;
    inline bool isDate() const;
    inline bool isRegExp() const;
    inline bool isQObject() const;

    inline bool equals(QJSValuePrivate* other);
    inline bool strictlyEquals(QJSValuePrivate* other);
    inline bool lessThan(QJSValuePrivate *other) const;

    inline QScriptPassPointer<QJSValuePrivate> prototype() const;
    inline void setPrototype(QJSValuePrivate* prototype);

    inline void setProperty(const QString &name, QJSValuePrivate *value, uint attribs = 0);
    inline void setProperty(v8::Handle<v8::String> name, QJSValuePrivate *value, uint attribs = 0);
    inline void setProperty(quint32 index, QJSValuePrivate* value, uint attribs = 0);
    inline QScriptPassPointer<QJSValuePrivate> property(const QString& name) const;
    inline QScriptPassPointer<QJSValuePrivate> property(v8::Handle<v8::String> name) const;
    inline QScriptPassPointer<QJSValuePrivate> property(quint32 index) const;
    template<typename T>
    inline QScriptPassPointer<QJSValuePrivate> property(T name) const;
    inline bool deleteProperty(const QString& name);
    inline bool hasProperty(const QString &name) const;
    inline bool hasOwnProperty(const QString &name) const;
    inline PropertyFlags propertyFlags(const QString& name) const;
    inline PropertyFlags propertyFlags(v8::Handle<v8::String> name) const;

    inline QScriptPassPointer<QJSValuePrivate> call(QJSValuePrivate* thisObject, const QJSValueList& args);
    inline QScriptPassPointer<QJSValuePrivate> call(QJSValuePrivate* thisObject, const QJSValue& arguments);
    inline QScriptPassPointer<QJSValuePrivate> call(QJSValuePrivate* thisObject, int argc, v8::Handle< v8::Value >* argv);
    inline QScriptPassPointer<QJSValuePrivate> callAsConstructor(int argc, v8::Handle<v8::Value> *argv);
    inline QScriptPassPointer<QJSValuePrivate> callAsConstructor(const QJSValueList& args);
    inline QScriptPassPointer<QJSValuePrivate> callAsConstructor(const QJSValue& arguments);

    inline bool assignEngine(QV8Engine *engine);
    inline QV8Engine *engine() const;

    inline operator v8::Handle<v8::Value>() const;
    inline operator v8::Handle<v8::Object>() const;
    inline v8::Handle<v8::Value> asV8Value(QV8Engine *engine);
private:
    QIntrusiveListNode m_node;
    QV8Engine *m_engine;

    // Please, update class documentation when you change the enum.
    enum State {
        CString = 0x1000,
        CNumber,
        CBool,
        CNull,
        CUndefined,
        JSValue = 0x2000, // V8 values are equal or higher then this value.
        // JSPrimitive,
        // JSObject
    } m_state;

    union CValue {
        bool m_bool;
        double m_number;
        QString* m_string;

        CValue() : m_number(0) {}
        CValue(bool value) : m_bool(value) {}
        CValue(int number) : m_number(number) {}
        CValue(uint number) : m_number(number) {}
        CValue(double number) : m_number(number) {}
        CValue(QString* string) : m_string(string) {}
    } u;
    // v8::Persistent is not a POD, so can't be part of the union.
    v8::Persistent<v8::Value> m_value;

    Q_DISABLE_COPY(QJSValuePrivate)
    inline bool isJSBased() const;
    inline bool isNumberBased() const;
    inline bool isStringBased() const;
    inline bool prepareArgumentsForCall(v8::Handle<v8::Value> argv[], const QJSValueList& arguments) const;

    friend class QV8Engine;
};

Q_DECLARE_OPERATORS_FOR_FLAGS(QJSValuePrivate::PropertyFlags)

QT_END_NAMESPACE

#endif