Merge branch 'v8'
[profile/ivi/qtdeclarative.git] / src / declarative / qml / v8 / qv8qobjectwrapper_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 QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QV8QOBJECTWRAPPER_P_H
43 #define QV8QOBJECTWRAPPER_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include <QtCore/qglobal.h>
57 #include <QtCore/qmetatype.h>
58 #include <QtCore/qpair.h>
59 #include <QtCore/qhash.h>
60 #include <private/qv8_p.h>
61 #include <private/qhashedstring_p.h>
62 #include <private/qdeclarativedata_p.h>
63 #include <private/qdeclarativepropertycache_p.h>
64
65 QT_BEGIN_NAMESPACE
66
67 class QObject;
68 class QV8Engine;
69 class QDeclarativeData;
70 class QV8ObjectResource;
71 class QV8QObjectInstance;
72 class QV8QObjectConnectionList;
73 class QDeclarativePropertyCache;
74 class Q_AUTOTEST_EXPORT QV8QObjectWrapper 
75 {
76 public:
77     QV8QObjectWrapper();
78     ~QV8QObjectWrapper();
79
80     void init(QV8Engine *);
81     void destroy();
82
83     v8::Handle<v8::Value> newQObject(QObject *object);
84     bool isQObject(v8::Handle<v8::Object>);
85     QObject *toQObject(v8::Handle<v8::Object>);
86     QObject *toQObject(QV8ObjectResource *);
87
88     enum RevisionMode { IgnoreRevision, CheckRevision };
89     inline v8::Handle<v8::Value> getProperty(QObject *, const QHashedV8String &, RevisionMode);
90     inline bool setProperty(QObject *, const QHashedV8String &, v8::Handle<v8::Value>, RevisionMode);
91
92 private:
93     friend class QDeclarativePropertyCache;
94     friend class QV8QObjectConnectionList;
95     friend class QV8QObjectInstance;
96
97     v8::Local<v8::Object> newQObject(QObject *, QDeclarativeData *, QV8Engine *);
98     static v8::Handle<v8::Value> GetProperty(QV8Engine *, QObject *, v8::Handle<v8::Value> *, 
99                                              const QHashedV8String &, QV8QObjectWrapper::RevisionMode);
100     static bool SetProperty(QV8Engine *, QObject *, const QHashedV8String &,
101                             v8::Handle<v8::Value>, QV8QObjectWrapper::RevisionMode);
102     static v8::Handle<v8::Value> Getter(v8::Local<v8::String> property, 
103                                         const v8::AccessorInfo &info);
104     static v8::Handle<v8::Value> Setter(v8::Local<v8::String> property, 
105                                         v8::Local<v8::Value> value,
106                                         const v8::AccessorInfo &info);
107     static v8::Handle<v8::Integer> Query(v8::Local<v8::String> property,
108                                          const v8::AccessorInfo &info);
109     static v8::Handle<v8::Array> Enumerator(const v8::AccessorInfo &info);
110     static v8::Handle<v8::Value> Connect(const v8::Arguments &args);
111     static v8::Handle<v8::Value> Disconnect(const v8::Arguments &args);
112     static v8::Handle<v8::Value> Invoke(const v8::Arguments &args);
113     static QPair<QObject *, int> ExtractQtMethod(QV8Engine *, v8::Handle<v8::Function>);
114
115     QV8Engine *m_engine;
116     quint32 m_id;
117     v8::Persistent<v8::Function> m_constructor;
118     v8::Persistent<v8::Function> m_methodConstructor;
119     v8::Persistent<v8::String> m_toStringSymbol;
120     v8::Persistent<v8::String> m_destroySymbol;
121     QHashedV8String m_toStringString;
122     QHashedV8String m_destroyString;
123     v8::Persistent<v8::Object> m_hiddenObject;
124     QHash<QObject *, QV8QObjectConnectionList *> m_connections;
125     typedef QHash<QObject *, QV8QObjectInstance *> TaintedHash;
126     TaintedHash m_taintedObjects;
127 };
128
129 v8::Handle<v8::Value> QV8QObjectWrapper::getProperty(QObject *object, const QHashedV8String &string,  
130                                                      RevisionMode mode)
131 {
132     QDeclarativeData *dd = QDeclarativeData::get(object, false);
133     if (!dd || !dd->propertyCache || m_toStringString == string || m_destroyString == string ||
134         dd->propertyCache->property(string)) {
135         return GetProperty(m_engine, object, 0, string, mode);
136     } else {
137         return v8::Handle<v8::Value>();
138     }
139 }
140
141 bool QV8QObjectWrapper::setProperty(QObject *object, const QHashedV8String &string, 
142                                     v8::Handle<v8::Value> value, RevisionMode mode)
143 {
144     QDeclarativeData *dd = QDeclarativeData::get(object, false);
145     if (!dd || !dd->propertyCache || m_toStringString == string || m_destroyString == string ||
146         dd->propertyCache->property(string)) {
147         return SetProperty(m_engine, object, string, value, mode);
148     } else {
149         return false;
150     }
151 }
152
153 QT_END_NAMESPACE
154
155 #endif // QV8QOBJECTWRAPPER_P_H
156
157