summaryrefslogtreecommitdiffstats
path: root/src/script/api/qscriptqobject_p.h
blob: 369cf6c93de124b873ea31dba59160a15c956a01 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** 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
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QSCRIPTQOBJECT_P_H
#define QSCRIPTQOBJECT_P_H

#include <QtCore/qmetaobject.h>
#include <QtCore/qsharedpointer.h>
#include "qscripttools_p.h"
#include "qscriptv8objectwrapper_p.h"
#include <v8.h>

QT_BEGIN_NAMESPACE

class QScriptEnginePrivate;
class QScriptable;

class QtDataBase : public QScriptLinkedNode
{
public:
    // QtData use virtual destructor for deleting "unspecified" data from QSEP::dealllocateAddtionalData.
    inline QtDataBase(QScriptEnginePrivate *engine);
    inline virtual ~QtDataBase();
    inline QScriptEnginePrivate *engine() const;
private:
    QScriptEnginePrivate *m_engine;
};

template<class T>
class QtData : public QtDataBase
{
public:
    inline QtData(QScriptEnginePrivate *engine);
    static T *get(v8::Handle<v8::Object> object);
    static void set(v8::Handle<v8::Object> object, T* data);
};

// Data associated with a QObject JS wrapper object.
//
// There can exist an arbitrary number of JS wrappers per C++ object,
// in the same script engine or different ones.
//
// - cppObject: The C++ object that is being wrapped.
//
// - ownership: Determines whether the C++ object is destroyed
//   when the JS wrapper is garbage-collected.
//
// - options: Flags that configure the binding
//   (e.g. exclude super-class contents, skip methods in enumeration)
//
class QScriptQObjectData : public QtData<QScriptQObjectData>
{
public:
    enum Mode {RaiseException, IgnoreException};

    inline QScriptQObjectData(QScriptEnginePrivate *, QObject *, QScriptEngine::ValueOwnership, const QScriptEngine::QObjectWrapOptions &);
    inline ~QScriptQObjectData();
    inline QObject *cppObject(v8::Local<v8::Value> *error = 0) const;
    inline QObject *cppObject(const Mode mode) const;
    inline QScriptEngine::ValueOwnership ownership() const;
    inline QScriptEngine::QObjectWrapOptions options() const;
    inline QScriptable *toQScriptable();

private:
    QWeakPointer<QObject> m_cppObject;
    QScriptEngine::ValueOwnership m_own;
    QScriptEngine::QObjectWrapOptions m_opt;
};

// Data associated with a QMetaObject JS wrapper object.
//
class QtMetaObjectData : public QtData<QtMetaObjectData>
{
public:
    inline QtMetaObjectData(QScriptEnginePrivate *engine, const QMetaObject *mo, v8::Handle<v8::Value> ctor);
    inline ~QtMetaObjectData();
    inline const QMetaObject *metaObject() const;
    inline v8::Handle<v8::Value> constructor() const;
private:
    QScriptEnginePrivate *m_engine;
    const QMetaObject *m_metaObject;
    v8::Persistent<v8::Value> m_ctor;
};

// Data associated with a QVariant JS wrapper object.
//
// When converting a QVariant to JS, QtScript will attempt
// to convert the QVariant to a "real" JS value, but in case
// it can't (for example, the type is a custom type with no
// conversion functions registered), the QVariant is wrapped
// in a custom JS object.
//
// It's also possible to explicitly create a QVariant wrapper
// object by calling QScriptEngine::newVariant().
//
class QtVariantData : public QtData<QtVariantData>
{
public:
    inline QtVariantData(QScriptEnginePrivate *engine, const QVariant &value);
    inline QVariant &value();
    inline void setValue(const QVariant &value);
private:
    QVariant m_value;
};

v8::Handle<v8::FunctionTemplate> createQtClassTemplate(QScriptEnginePrivate *, const QMetaObject *, const QScriptEngine::QObjectWrapOptions &options);

v8::Handle<v8::Value> QtDynamicPropertyGetter(v8::Local<v8::String> property,
                                                     const v8::AccessorInfo& info);
void QtDynamicPropertySetter(v8::Local<v8::String> property,
                                    v8::Local<v8::Value> value,
                                    const v8::AccessorInfo& info);

v8::Handle<v8::Value> QtLazyPropertyGetter(v8::Local<v8::String> property,
                                                  const v8::AccessorInfo& info);
v8::Handle<v8::Value> QtLazyPropertySetter(v8::Local<v8::String> property,
                                                  v8::Local<v8::Value> value,
                                                  const v8::AccessorInfo& info);

v8::Handle<v8::Value> QtMetaObjectCallback(const v8::Arguments& args);
v8::Handle<v8::Value> QtMetaObjectPropertyGetter(v8::Local<v8::String> property,
                                                 const v8::AccessorInfo& info);
v8::Handle<v8::Array> QtMetaObjectEnumerator(const v8::AccessorInfo& info);
QObject *toQtObject(QScriptEnginePrivate *engine, const v8::Handle<v8::Object> &object);

union QScriptMetaMethodInfo {
    QScriptMetaMethodInfo(): intData(0)
    { }

    uint32_t intData;
    struct {
        uint index: 28;
        uint resolveMode: 1;
        uint overloaded: 1;
        uint voidvoid: 1;
        uint padding: 1; // Make sure the struct fits in an SMI
    };
};

template <typename T, v8::Persistent<v8::FunctionTemplate> QScriptEnginePrivate::*functionTemplate>
class QScriptGenericMetaMethodData : public QScriptV8ObjectWrapper<T, functionTemplate> {
public:
    enum ResolveMode {
        ResolvedByName = 0,
        ResolvedBySignature = 1
    };

    QScriptGenericMetaMethodData(QScriptEnginePrivate *eng, v8::Handle<v8::Object> object,
                                 QScriptMetaMethodInfo info)
        : m_object(v8::Persistent<v8::Object>::New(object)), m_info(info)
    {
        this->engine = eng;
        // We cannot keep a persistant reference to the object, else it would never be garbage collected.
        //  (the object also reference us,  and persistent object are automatically marked.
        //  A reference is kept in the second internal field of the v8 method object.
        m_object.MakeWeak(this, objectDestroyed);
    }
    ~QScriptGenericMetaMethodData()
    {
        m_object.Dispose();
    }

    // The QObject wrapper object that this signal is bound to.
    v8::Handle<v8::Object> object() const
    { return m_object; }

    int index() const
    { return m_info.index; }

    ResolveMode resolveMode() const
    { return ResolveMode(m_info.resolveMode); }

    v8::Handle<v8::Value> call();

    v8::Persistent<v8::Object> m_object;
    QScriptMetaMethodInfo m_info;
private:
    static void objectDestroyed(v8::Persistent<v8::Value> object, void *data) {
        QScriptGenericMetaMethodData *that = static_cast<QScriptGenericMetaMethodData *>(data);
        Q_ASSERT(that->m_object == object);
        that->m_object.Clear();
        object.Dispose();
        // Note that since the method keep a reference to the object in its internal field,
        // this is only called when the QScriptGenericMetaMethodData is about to be garbage collected as well.
    }
};

class QScriptMetaMethodData : public QScriptGenericMetaMethodData<QScriptMetaMethodData, &QScriptEnginePrivate::metaMethodTemplate>
{
    typedef QScriptGenericMetaMethodData<QScriptMetaMethodData, &QScriptEnginePrivate::metaMethodTemplate> Base;
public:
    QScriptMetaMethodData(QScriptEnginePrivate *engine, v8::Handle<v8::Object> object, QScriptMetaMethodInfo info)
    : Base(engine, object, info)
    { }

    static v8::Handle<v8::FunctionTemplate> createFunctionTemplate(QScriptEnginePrivate *engine);
};

// Data associated with a signal JS wrapper object.
//
// A signal wrapper is bound to the particular Qt wrapper object
// where it was looked up as a member, i.e. signal wrappers are
// _per instance_, not per class (prototype). This is in order
// to support the connect() and disconnect() syntax:
//
// button1.clicked.connect(...);
// button2.clicked.connect(...);
//
// When connect() is called, the this-object will be the signal
// wrapper, not the QObject. Hence, in order to know which object's
// clicked() signal to connect to, the signal must be bound to
// that object.
//
// - object: The Qt wrapper object that this signal is bound to.
//
// - index: The index of the C++ signal.
//
// - resolve mode: How the signal was resolved; by name or signature.
//   If it was resolved by name, there's a chance the signal might have overloads.
//

class QScriptConnection;
class QScriptSignalData : public QScriptGenericMetaMethodData<QScriptSignalData, &QScriptEnginePrivate::signalTemplate>
{
    typedef QScriptGenericMetaMethodData<QScriptSignalData, &QScriptEnginePrivate::signalTemplate> Base;
public:

    QScriptSignalData(QScriptEnginePrivate *engine, v8::Handle<v8::Object> object, QScriptMetaMethodInfo info)
        : Base(engine, object, info)
    { }

    ~QScriptSignalData();

    static v8::Handle<v8::FunctionTemplate> createFunctionTemplate(QScriptEnginePrivate *engine);

    v8::Handle<v8::Value> connect(v8::Handle<v8::Object> receiver,
                                  v8::Handle<v8::Object> slot,
                                  Qt::ConnectionType type = Qt::AutoConnection);
    v8::Handle<v8::Value> disconnect(v8::Handle<v8::Function> callback);

    static QScriptSignalData *get(v8::Handle<v8::Object> object)
    {
        void *ptr = object->GetPointerFromInternalField(0);
        Q_ASSERT(ptr != 0);
        return static_cast<QScriptSignalData*>(ptr);
    }

    void unregisterQScriptConnection(QScriptConnection *connection) { m_connections.removeAll(connection); }
private:
    static v8::Handle<v8::Value> QtConnectCallback(const v8::Arguments& args);
    static v8::Handle<v8::Value> QtDisconnectCallback(const v8::Arguments& args);
    QList<QScriptConnection*> m_connections;
};

QT_END_NAMESPACE

#endif