Fix alias warnings in QV8QObjectWrapper
[profile/ivi/qtdeclarative.git] / src / declarative / qml / v8 / qv8engine_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 QDECLARATIVEV8ENGINE_P_H
43 #define QDECLARATIVEV8ENGINE_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/qvariant.h>
58 #include <QtCore/qset.h>
59 #include <QtCore/qmutex.h>
60 #include <private/qv8_p.h>
61
62 #include <private/qdeclarativepropertycache_p.h>
63
64 #include "qv8contextwrapper_p.h"
65 #include "qv8qobjectwrapper_p.h"
66 #include "qv8stringwrapper_p.h"
67 #include "qv8typewrapper_p.h"
68 #include "qv8listwrapper_p.h"
69 #include "qv8variantwrapper_p.h"
70 #include "qv8valuetypewrapper_p.h"
71
72 QT_BEGIN_NAMESPACE
73
74
75 // Uncomment the following line to enable global handle debugging.  When enabled, all the persistent
76 // handles allocated using qPersistentNew() (or registered with qPersistentRegsiter()) and disposed
77 // with qPersistentDispose() are tracked.  If you try and do something illegal, like double disposing
78 // a handle, qFatal() is called.
79 // #define QML_GLOBAL_HANDLE_DEBUGGING
80
81 #define V8_RESOURCE_TYPE(resourcetype) \
82 public: \
83     enum { V8ResourceType = QV8ObjectResource:: resourcetype }; \
84     virtual QV8ObjectResource::ResourceType resourceType() const { return QV8ObjectResource:: resourcetype; } \
85 private: 
86
87 #define V8ENGINE() ((QV8Engine *)v8::External::Unwrap(args.Data()))
88 #define V8FUNCTION(function, engine) v8::FunctionTemplate::New(function, v8::External::Wrap((QV8Engine*)engine))->GetFunction()
89 #define V8THROW_ERROR(string) { \
90     v8::ThrowException(v8::Exception::Error(v8::String::New(string))); \
91     return v8::Handle<v8::Value>(); \
92 }
93 #define V8THROW_TYPE(string) { \
94     v8::ThrowException(v8::Exception::TypeError(v8::String::New(string))); \
95     return v8::Handle<v8::Value>(); \
96 }
97 #define V8ENGINE_ACCESSOR() ((QV8Engine *)v8::External::Unwrap(info.Data()));
98 #define V8THROW_ERROR_SETTER(string) { \
99     v8::ThrowException(v8::Exception::Error(v8::String::New(string))); \
100     return; \
101 }
102
103 #define V8_DEFINE_EXTENSION(dataclass, datafunction) \
104     inline dataclass *datafunction(QV8Engine *engine) \
105     { \
106         static int extensionId = -1; \
107         if (extensionId == -1) { \
108             QV8Engine::registrationMutex()->lock(); \
109             if (extensionId == -1) \
110                 extensionId = QV8Engine::registerExtension(); \
111             QV8Engine::registrationMutex()->unlock(); \
112         } \
113         dataclass *rv = (dataclass *)engine->extensionData(extensionId); \
114         if (!rv) { \
115             rv = new dataclass(engine); \
116             engine->setExtensionData(extensionId, rv); \
117         } \
118         return rv; \
119     } \
120
121 class QV8Engine;
122 class QV8ObjectResource : public v8::Object::ExternalResource
123 {
124 public:
125     QV8ObjectResource(QV8Engine *engine) : engine(engine) { Q_ASSERT(engine); }
126     enum ResourceType { ContextType, QObjectType, TypeType, ListType, VariantType, 
127                         ValueTypeType, XMLHttpRequestType, DOMNodeType, SQLDatabaseType,
128                         ListModelType, Context2DType, ParticleDataType };
129     virtual ResourceType resourceType() const = 0;
130
131     QV8Engine *engine;
132 };
133
134 template<class T>
135 inline T *v8_resource_cast(v8::Handle<v8::Object> object) {
136     QV8ObjectResource *resource = static_cast<QV8ObjectResource *>(object->GetExternalResource());
137     return (resource && (quint32)resource->resourceType() == (quint32)T::V8ResourceType)?static_cast<T *>(resource):0;
138 }
139
140 template<class T>
141 inline T *v8_resource_check(v8::Handle<v8::Object> object) {
142     T *resource = static_cast<T *>(object->GetExternalResource());
143     Q_ASSERT(resource && resource->resourceType() == (quint32)T::V8ResourceType);
144     return resource;
145 }
146
147 // Used to allow a QObject method take and return raw V8 handles without having to expose
148 // v8 in the public API.
149 // Use like this:
150 //     class MyClass : public QObject {
151 //         Q_OBJECT
152 //         ...
153 //         Q_INVOKABLE void myMethod(QDeclarativeV8Function*);
154 //     };
155 // The QDeclarativeV8Function - and consequently the arguments and return value - only remains 
156 // valid during the call.  If the return value isn't set within myMethod(), the will return
157 // undefined.
158 class QV8Engine;
159 class QDeclarativeV8Function
160 {
161 public:
162     int Length() const { return _ac; }
163     v8::Local<v8::Value> operator[](int idx) { return (*_a)->Get(idx); }
164     QDeclarativeContextData *context() { return _c; }
165     v8::Handle<v8::Object> qmlGlobal() { return *_g; }
166     void returnValue(v8::Handle<v8::Value> rv) { *_r = rv; }
167     QV8Engine *engine() const { return _e; }
168 private:
169     friend class QV8QObjectWrapper;
170     QDeclarativeV8Function();
171     QDeclarativeV8Function(const QDeclarativeV8Function &);
172     QDeclarativeV8Function &operator=(const QDeclarativeV8Function &);
173
174     QDeclarativeV8Function(int length, v8::Handle<v8::Object> &args, 
175                            v8::Handle<v8::Value> &rv, v8::Handle<v8::Object> &global,
176                            QDeclarativeContextData *c, QV8Engine *e)
177     : _ac(length), _a(&args), _r(&rv), _g(&global), _c(c), _e(e) {}
178
179     int _ac;
180     v8::Handle<v8::Object> *_a;
181     v8::Handle<v8::Value> *_r;
182     v8::Handle<v8::Object> *_g;
183     QDeclarativeContextData *_c;
184     QV8Engine *_e;
185 };
186
187 class QDeclarativeV8Handle
188 {
189 public:
190     QDeclarativeV8Handle() : d(0) {}
191     QDeclarativeV8Handle(const QDeclarativeV8Handle &other) : d(other.d) {}
192     QDeclarativeV8Handle &operator=(const QDeclarativeV8Handle &other) { d = other.d; return *this; }
193
194     static QDeclarativeV8Handle fromHandle(v8::Handle<v8::Value> h) {
195         return QDeclarativeV8Handle(*h);
196     }
197     v8::Handle<v8::Value> toHandle() const {
198         return v8::Handle<v8::Value>((v8::Value *)d);
199     }
200 private:
201     QDeclarativeV8Handle(void *d) : d(d) {}
202     void *d;
203 };
204
205 class QObject;
206 class QDeclarativeEngine;
207 class QDeclarativeValueType;
208 class QNetworkAccessManager;
209 class QDeclarativeContextData;
210 class Q_DECLARATIVE_EXPORT QV8Engine
211 {
212 public:
213     QV8Engine();
214     ~QV8Engine();
215
216     struct Deletable {
217         virtual ~Deletable() {}
218     };
219
220     void init(QDeclarativeEngine *);
221
222     QDeclarativeEngine *engine() { return m_engine; }
223     v8::Local<v8::Object> global() { return m_context->Global(); }
224     v8::Handle<v8::Context> context() { return m_context; }
225     QV8ContextWrapper *contextWrapper() { return &m_contextWrapper; }
226     QV8QObjectWrapper *qobjectWrapper() { return &m_qobjectWrapper; }
227     QV8TypeWrapper *typeWrapper() { return &m_typeWrapper; }
228     QV8ListWrapper *listWrapper() { return &m_listWrapper; }
229     QV8VariantWrapper *variantWrapper() { return &m_variantWrapper; }
230
231     void *xmlHttpRequestData() { return m_xmlHttpRequestData; }
232     void *sqlDatabaseData() { return m_sqlDatabaseData; }
233
234     Deletable *listModelData() { return m_listModelData; }
235     void setListModelData(Deletable *d) { if (m_listModelData) delete m_listModelData; m_listModelData = d; }
236
237     QDeclarativeContextData *callingContext();
238
239     v8::Local<v8::Array> getOwnPropertyNames(v8::Handle<v8::Object>);
240     void freezeObject(v8::Handle<v8::Value>);
241
242     inline QString toString(v8::Handle<v8::Value> string);
243     inline QString toString(v8::Handle<v8::String> string);
244     static QString toStringStatic(v8::Handle<v8::Value>);
245     static QString toStringStatic(v8::Handle<v8::String>);
246     static inline bool startsWithUpper(v8::Handle<v8::String>);
247
248     QVariant toVariant(v8::Handle<v8::Value>, int typeHint);
249     v8::Handle<v8::Value> fromVariant(const QVariant &);
250     inline bool isVariant(v8::Handle<v8::Value>);
251
252     // Compile \a source (from \a fileName at \a lineNumber) in QML mode
253     v8::Local<v8::Script> qmlModeCompile(const QString &source, 
254                                          const QString &fileName = QString(), 
255                                          int lineNumber = 1);
256
257     // Return the QML global "scope" object for the \a ctxt context and \a scope object.
258     inline v8::Local<v8::Object> qmlScope(QDeclarativeContextData *ctxt, QObject *scope);
259
260     // Return a JS wrapper for the given QObject \a object
261     inline v8::Handle<v8::Value> newQObject(QObject *object);
262     inline bool isQObject(v8::Handle<v8::Value>);
263     inline QObject *toQObject(v8::Handle<v8::Value>);
264
265     // Return a JS string for the given QString \a string
266     inline v8::Local<v8::String> toString(const QString &string);
267
268     // Create a new value type object
269     inline v8::Handle<v8::Value> newValueType(QObject *, int coreIndex, QDeclarativeValueType *);
270     inline v8::Handle<v8::Value> newValueType(const QVariant &, QDeclarativeValueType *);
271
272     // Create a new QVariant object.  This doesn't examine the type of the variant, but always returns
273     // a QVariant wrapper
274     inline v8::Handle<v8::Value> newQVariant(const QVariant &);
275
276     // Return the network access manager for this engine.  By default this returns the network
277     // access manager of the QDeclarativeEngine.  It is overridden in the case of a threaded v8
278     // instance (like in WorkerScript).
279     virtual QNetworkAccessManager *networkAccessManager();
280
281     // Return the list of illegal id names (the names of the properties on the global object)
282     const QSet<QString> &illegalNames() const;
283
284     static void gc();
285
286 #ifdef QML_GLOBAL_HANDLE_DEBUGGING
287     // Used for handle debugging
288     static void registerHandle(void *);
289     static void releaseHandle(void *);
290 #endif
291
292     static QMutex *registrationMutex();
293     static int registerExtension();
294
295     inline Deletable *extensionData(int) const;
296     void setExtensionData(int, Deletable *);
297
298 private:
299     QDeclarativeEngine *m_engine;
300     v8::Persistent<v8::Context> m_context;
301
302     QV8StringWrapper m_stringWrapper;
303     QV8ContextWrapper m_contextWrapper;
304     QV8QObjectWrapper m_qobjectWrapper;
305     QV8TypeWrapper m_typeWrapper;
306     QV8ListWrapper m_listWrapper;
307     QV8VariantWrapper m_variantWrapper;
308     QV8ValueTypeWrapper m_valueTypeWrapper;
309
310     v8::Persistent<v8::Function> m_getOwnPropertyNames;
311     v8::Persistent<v8::Function> m_freezeObject;
312
313     void *m_xmlHttpRequestData;
314     void *m_sqlDatabaseData;
315
316     QVector<Deletable *> m_extensionData;
317     Deletable *m_listModelData;
318
319     QSet<QString> m_illegalNames;
320
321     QVariant toBasicVariant(v8::Handle<v8::Value>);
322
323     void initializeGlobal(v8::Handle<v8::Object>);
324
325     static v8::Handle<v8::Value> gc(const v8::Arguments &args);
326     static v8::Handle<v8::Value> print(const v8::Arguments &args);
327     static v8::Handle<v8::Value> isQtObject(const v8::Arguments &args);
328     static v8::Handle<v8::Value> rgba(const v8::Arguments &args);
329     static v8::Handle<v8::Value> hsla(const v8::Arguments &args);
330     static v8::Handle<v8::Value> rect(const v8::Arguments &args);
331     static v8::Handle<v8::Value> point(const v8::Arguments &args);
332     static v8::Handle<v8::Value> size(const v8::Arguments &args);
333     static v8::Handle<v8::Value> vector3d(const v8::Arguments &args);
334     static v8::Handle<v8::Value> lighter(const v8::Arguments &args);
335     static v8::Handle<v8::Value> darker(const v8::Arguments &args);
336     static v8::Handle<v8::Value> tint(const v8::Arguments &args);
337     static v8::Handle<v8::Value> formatDate(const v8::Arguments &args);
338     static v8::Handle<v8::Value> formatTime(const v8::Arguments &args);
339     static v8::Handle<v8::Value> formatDateTime(const v8::Arguments &args);
340     static v8::Handle<v8::Value> openUrlExternally(const v8::Arguments &args);
341     static v8::Handle<v8::Value> fontFamilies(const v8::Arguments &args);
342     static v8::Handle<v8::Value> md5(const v8::Arguments &args);
343     static v8::Handle<v8::Value> btoa(const v8::Arguments &args);
344     static v8::Handle<v8::Value> atob(const v8::Arguments &args);
345     static v8::Handle<v8::Value> quit(const v8::Arguments &args);
346     static v8::Handle<v8::Value> resolvedUrl(const v8::Arguments &args);
347     static v8::Handle<v8::Value> createQmlObject(const v8::Arguments &args);
348     static v8::Handle<v8::Value> createComponent(const v8::Arguments &args);
349     static v8::Handle<v8::Value> qsTranslate(const v8::Arguments &args);
350     static v8::Handle<v8::Value> qsTranslateNoOp(const v8::Arguments &args);
351     static v8::Handle<v8::Value> qsTr(const v8::Arguments &args);
352     static v8::Handle<v8::Value> qsTrNoOp(const v8::Arguments &args);
353     static v8::Handle<v8::Value> qsTrId(const v8::Arguments &args);
354     static v8::Handle<v8::Value> qsTrIdNoOp(const v8::Arguments &args);
355     static v8::Handle<v8::Value> stringArg(const v8::Arguments &args);
356
357     double qtDateTimeToJsDate(const QDateTime &dt);
358     QDateTime qtDateTimeFromJsDate(double jsDate);
359 };
360
361 // Allocate a new Persistent handle.  *ALL* persistent handles in QML must be allocated
362 // using this method.
363 template<class T>
364 v8::Persistent<T> qPersistentNew(v8::Handle<T> that)
365 {
366     v8::Persistent<T> rv = v8::Persistent<T>::New(that);
367 #ifdef QML_GLOBAL_HANDLE_DEBUGGING
368     QV8Engine::registerHandle(*rv);
369 #endif
370     return rv;
371 }
372
373 // Register a Persistent handle that was returned to you by V8 (such as by
374 // v8::Context::New). This allows us to do handle tracking on these handles too.
375 template<class T>
376 void qPersistentRegister(v8::Persistent<T> handle)
377 {
378 #ifdef QML_GLOBAL_HANDLE_DEBUGGING
379     QV8Engine::registerHandle(*handle);
380 #else
381     Q_UNUSED(handle);
382 #endif
383 }
384
385 // Dispose and clear a persistent handle.  *ALL* persistent handles in QML must be
386 // disposed using this method.
387 template<class T>
388 void qPersistentDispose(v8::Persistent<T> &that)
389 {
390 #ifdef QML_GLOBAL_HANDLE_DEBUGGING
391     QV8Engine::releaseHandle(*that);
392 #endif
393     that.Dispose();
394     that.Clear();
395 }
396
397 QString QV8Engine::toString(v8::Handle<v8::Value> string)
398 {
399     return m_stringWrapper.toString(string->ToString());
400 }
401
402 QString QV8Engine::toString(v8::Handle<v8::String> string)
403 {
404     return m_stringWrapper.toString(string);
405 }
406
407 bool QV8Engine::isVariant(v8::Handle<v8::Value> value)
408 {
409     return m_variantWrapper.isVariant(value);
410 }
411
412 v8::Local<v8::Object> QV8Engine::qmlScope(QDeclarativeContextData *ctxt, QObject *scope)
413 {
414     return m_contextWrapper.qmlScope(ctxt, scope);
415 }
416
417 bool QV8Engine::isQObject(v8::Handle<v8::Value> obj)
418 {
419     return obj->IsObject()?m_qobjectWrapper.isQObject(v8::Handle<v8::Object>::Cast(obj)):false;
420 }
421
422 QObject *QV8Engine::toQObject(v8::Handle<v8::Value> obj)
423 {
424     return obj->IsObject()?m_qobjectWrapper.toQObject(v8::Handle<v8::Object>::Cast(obj)):0;
425 }
426
427 v8::Handle<v8::Value> QV8Engine::newQObject(QObject *object)
428 {
429     return m_qobjectWrapper.newQObject(object);
430 }
431
432 v8::Local<v8::String> QV8Engine::toString(const QString &string)
433 {
434     return m_stringWrapper.toString(string);
435 }
436
437 v8::Handle<v8::Value> QV8Engine::newValueType(QObject *object, int property, QDeclarativeValueType *type)
438 {
439     return m_valueTypeWrapper.newValueType(object, property, type);
440 }
441
442 v8::Handle<v8::Value> QV8Engine::newValueType(const QVariant &value, QDeclarativeValueType *type)
443 {
444     return m_valueTypeWrapper.newValueType(value, type);
445 }
446
447 // XXX Can this be made more optimal?  It is called prior to resolving each and every 
448 // unqualified name in QV8ContextWrapper.
449 bool QV8Engine::startsWithUpper(v8::Handle<v8::String> string)
450 {
451     uint16_t c = string->GetCharacter(0);
452     return (c >= 'A' && c <= 'Z') || 
453            (c > 127 && QChar::category(c) == QChar::Letter_Uppercase);
454 }
455
456 QV8Engine::Deletable *QV8Engine::extensionData(int index) const
457 {
458     if (index < m_extensionData.count())
459         return m_extensionData[index];
460     else
461         return 0;
462 }
463
464 QT_END_NAMESPACE
465
466 #endif // QDECLARATIVEV8ENGINE_P_H