src/script/bridge/qscriptqobject_p.h
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 QtScript 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 QSCRIPTQOBJECT_P_H
       
    43 #define QSCRIPTQOBJECT_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 "qscriptobject_p.h"
       
    57 
       
    58 #include "qscriptengine.h"
       
    59 #include <QtCore/qpointer.h>
       
    60 
       
    61 #include "InternalFunction.h"
       
    62 
       
    63 QT_BEGIN_NAMESPACE
       
    64 
       
    65 namespace QScript
       
    66 {
       
    67 
       
    68 enum AttributeExtension {
       
    69     // ### Make sure there's no conflict with JSC::Attribute
       
    70     QObjectMemberAttribute = 1 << 12
       
    71 };
       
    72 
       
    73 class QObjectDelegate : public QScriptObjectDelegate
       
    74 {
       
    75 public:
       
    76     struct Data
       
    77     {
       
    78         QPointer<QObject> value;
       
    79         QScriptEngine::ValueOwnership ownership;
       
    80         QScriptEngine::QObjectWrapOptions options;
       
    81 
       
    82         QHash<QByteArray, JSC::JSValue> cachedMembers;
       
    83 
       
    84         Data(QObject *o, QScriptEngine::ValueOwnership own,
       
    85              QScriptEngine::QObjectWrapOptions opt)
       
    86             : value(o), ownership(own), options(opt) {}
       
    87     };
       
    88 
       
    89     QObjectDelegate(
       
    90         QObject *object, QScriptEngine::ValueOwnership ownership,
       
    91         const QScriptEngine::QObjectWrapOptions &options);
       
    92     ~QObjectDelegate();
       
    93 
       
    94     virtual Type type() const;
       
    95 
       
    96     virtual bool getOwnPropertySlot(QScriptObject*, JSC::ExecState*,
       
    97                                     const JSC::Identifier& propertyName,
       
    98                                     JSC::PropertySlot&);
       
    99     virtual void put(QScriptObject*, JSC::ExecState* exec,
       
   100                      const JSC::Identifier& propertyName,
       
   101                      JSC::JSValue, JSC::PutPropertySlot&);
       
   102     virtual bool deleteProperty(QScriptObject*, JSC::ExecState*,
       
   103                                 const JSC::Identifier& propertyName,
       
   104                                 bool checkDontDelete = true);
       
   105     virtual bool getPropertyAttributes(const QScriptObject*, JSC::ExecState*,
       
   106                                        const JSC::Identifier&,
       
   107                                        unsigned&) const;
       
   108     virtual void getOwnPropertyNames(QScriptObject*, JSC::ExecState*,
       
   109                                      JSC::PropertyNameArray&,
       
   110                                      bool includeNonEnumerable = false);
       
   111     virtual void markChildren(QScriptObject*, JSC::MarkStack& markStack);
       
   112     virtual bool compareToObject(QScriptObject*, JSC::ExecState*, JSC::JSObject*);
       
   113 
       
   114     inline QObject *value() const { return data->value; }
       
   115     inline void setValue(QObject* value) { data->value = value; }
       
   116 
       
   117     inline QScriptEngine::ValueOwnership ownership() const
       
   118         { return data->ownership; }
       
   119     inline void setOwnership(QScriptEngine::ValueOwnership ownership)
       
   120         { data->ownership = ownership; }
       
   121 
       
   122     inline QScriptEngine::QObjectWrapOptions options() const
       
   123         { return data->options; }
       
   124     inline void setOptions(QScriptEngine::QObjectWrapOptions options)
       
   125         { data->options = options; }
       
   126 
       
   127 protected:
       
   128     Data *data;
       
   129 };
       
   130 
       
   131 class QObjectPrototypeObject : public QObject
       
   132 {
       
   133     Q_OBJECT
       
   134 public:
       
   135     QObjectPrototypeObject(QObject *parent = 0)
       
   136         : QObject(parent) { }
       
   137     ~QObjectPrototypeObject() { }
       
   138 };
       
   139 
       
   140 class QObjectPrototype : public QScriptObject
       
   141 {
       
   142 public:
       
   143     QObjectPrototype(JSC::ExecState*, WTF::PassRefPtr<JSC::Structure>,
       
   144                      JSC::Structure* prototypeFunctionStructure);
       
   145 };
       
   146 
       
   147 class QObjectConnectionManager;
       
   148 
       
   149 struct QObjectWrapperInfo
       
   150 {
       
   151     QObjectWrapperInfo(QScriptObject *obj,
       
   152                        QScriptEngine::ValueOwnership own,
       
   153                        const QScriptEngine::QObjectWrapOptions &opt)
       
   154         : object(obj), ownership(own), options(opt) {}
       
   155 
       
   156     QScriptObject *object;
       
   157     QScriptEngine::ValueOwnership ownership;
       
   158     QScriptEngine::QObjectWrapOptions options;
       
   159 };
       
   160 
       
   161 class QObjectData // : public QObjectUserData
       
   162 {
       
   163 public:
       
   164     QObjectData(QScriptEnginePrivate *engine);
       
   165     ~QObjectData();
       
   166 
       
   167     bool addSignalHandler(QObject *sender,
       
   168                           int signalIndex,
       
   169                           JSC::JSValue receiver,
       
   170                           JSC::JSValue slot,
       
   171                           JSC::JSValue senderWrapper,
       
   172                           Qt::ConnectionType type);
       
   173     bool removeSignalHandler(QObject *sender,
       
   174                              int signalIndex,
       
   175                              JSC::JSValue receiver,
       
   176                              JSC::JSValue slot);
       
   177 
       
   178     QScriptObject *findWrapper(QScriptEngine::ValueOwnership ownership,
       
   179                                const QScriptEngine::QObjectWrapOptions &options) const;
       
   180     void registerWrapper(QScriptObject *wrapper,
       
   181                          QScriptEngine::ValueOwnership ownership,
       
   182                          const QScriptEngine::QObjectWrapOptions &options);
       
   183 
       
   184     void mark(JSC::MarkStack&);
       
   185 
       
   186 private:
       
   187     QScriptEnginePrivate *engine;
       
   188     QScript::QObjectConnectionManager *connectionManager;
       
   189     QList<QScript::QObjectWrapperInfo> wrappers;
       
   190 };
       
   191 
       
   192 class QtFunction: public JSC::InternalFunction
       
   193 {
       
   194 public:
       
   195     // work around CELL_SIZE limitation
       
   196     struct Data
       
   197     {
       
   198         JSC::JSValue object;
       
   199         int initialIndex;
       
   200         bool maybeOverloaded;
       
   201 
       
   202         Data(JSC::JSValue o, int ii, bool mo)
       
   203             : object(o), initialIndex(ii), maybeOverloaded(mo) {}
       
   204     };
       
   205 
       
   206     QtFunction(JSC::JSValue object, int initialIndex, bool maybeOverloaded,
       
   207                JSC::JSGlobalData*, WTF::PassRefPtr<JSC::Structure>, const JSC::Identifier&);
       
   208     virtual ~QtFunction();
       
   209 
       
   210     virtual JSC::CallType getCallData(JSC::CallData&);
       
   211     virtual void markChildren(JSC::MarkStack&);
       
   212 
       
   213     virtual const JSC::ClassInfo* classInfo() const { return &info; }
       
   214     static const JSC::ClassInfo info;
       
   215 
       
   216     static JSC::JSValue JSC_HOST_CALL call(JSC::ExecState*, JSC::JSObject*,
       
   217                                            JSC::JSValue, const JSC::ArgList&);
       
   218 
       
   219     JSC::JSValue execute(JSC::ExecState *exec, JSC::JSValue thisValue,
       
   220                          const JSC::ArgList &args);
       
   221 
       
   222     QScriptObject *wrapperObject() const;
       
   223     QObject *qobject() const;
       
   224     const QMetaObject *metaObject() const;
       
   225     int initialIndex() const;
       
   226     bool maybeOverloaded() const;
       
   227     int mostGeneralMethod(QMetaMethod *out = 0) const;
       
   228     QList<int> overloadedIndexes() const;
       
   229     QString functionName() const;
       
   230 
       
   231 private:
       
   232     Data *data;
       
   233 };
       
   234 
       
   235 class QtPropertyFunction: public JSC::InternalFunction
       
   236 {
       
   237 public:
       
   238     // work around CELL_SIZE limitation
       
   239     struct Data
       
   240     {
       
   241         const QMetaObject *meta;
       
   242         int index;
       
   243 
       
   244         Data(const QMetaObject *m, int i)
       
   245             : meta(m), index(i) {}
       
   246     };
       
   247 
       
   248     QtPropertyFunction(const QMetaObject *meta, int index,
       
   249                        JSC::JSGlobalData*, WTF::PassRefPtr<JSC::Structure>,
       
   250                        const JSC::Identifier&);
       
   251     virtual ~QtPropertyFunction();
       
   252 
       
   253     virtual JSC::CallType getCallData(JSC::CallData&);
       
   254 
       
   255     virtual const JSC::ClassInfo* classInfo() const { return &info; }
       
   256     static const JSC::ClassInfo info;
       
   257 
       
   258     static JSC::JSValue JSC_HOST_CALL call(JSC::ExecState*, JSC::JSObject*,
       
   259                                            JSC::JSValue, const JSC::ArgList&);
       
   260 
       
   261     JSC::JSValue execute(JSC::ExecState *exec, JSC::JSValue thisValue,
       
   262                          const JSC::ArgList &args);
       
   263 
       
   264     const QMetaObject *metaObject() const;
       
   265     int propertyIndex() const;
       
   266 
       
   267 private:
       
   268     Data *data;
       
   269 };
       
   270 
       
   271 class QMetaObjectWrapperObject : public JSC::JSObject
       
   272 {
       
   273 public:
       
   274     // work around CELL_SIZE limitation
       
   275     struct Data
       
   276     {
       
   277         const QMetaObject *value;
       
   278         JSC::JSValue ctor;
       
   279         JSC::JSValue prototype;
       
   280 
       
   281         Data(const QMetaObject *mo, JSC::JSValue c)
       
   282             : value(mo), ctor(c) {}
       
   283     };
       
   284 
       
   285     explicit QMetaObjectWrapperObject(
       
   286         JSC::ExecState *, const QMetaObject *metaobject, JSC::JSValue ctor,
       
   287         WTF::PassRefPtr<JSC::Structure> sid);
       
   288     ~QMetaObjectWrapperObject();
       
   289 
       
   290     virtual bool getOwnPropertySlot(JSC::ExecState*,
       
   291                                     const JSC::Identifier& propertyName,
       
   292                                     JSC::PropertySlot&);
       
   293     virtual void put(JSC::ExecState* exec, const JSC::Identifier& propertyName,
       
   294                      JSC::JSValue, JSC::PutPropertySlot&);
       
   295     virtual bool deleteProperty(JSC::ExecState*,
       
   296                                 const JSC::Identifier& propertyName,
       
   297                                 bool checkDontDelete = true);
       
   298     virtual bool getPropertyAttributes(JSC::ExecState*, const JSC::Identifier&,
       
   299                                        unsigned&) const;
       
   300     virtual void getOwnPropertyNames(JSC::ExecState*, JSC::PropertyNameArray&,
       
   301                                      bool includeNonEnumerable = false);
       
   302     virtual void markChildren(JSC::MarkStack& markStack);
       
   303 
       
   304     virtual JSC::CallType getCallData(JSC::CallData&);
       
   305     virtual JSC::ConstructType getConstructData(JSC::ConstructData&);
       
   306 
       
   307     virtual const JSC::ClassInfo* classInfo() const { return &info; }
       
   308     static const JSC::ClassInfo info;
       
   309 
       
   310     static JSC::JSValue JSC_HOST_CALL call(JSC::ExecState*, JSC::JSObject*,
       
   311                                            JSC::JSValue, const JSC::ArgList&);
       
   312     static JSC::JSObject* construct(JSC::ExecState *, JSC::JSObject *, const JSC::ArgList &);
       
   313 
       
   314     JSC::JSValue execute(JSC::ExecState *exec, const JSC::ArgList &args);
       
   315 
       
   316     inline const QMetaObject *value() const { return data->value; }
       
   317     inline void setValue(const QMetaObject* value) { data->value = value; }
       
   318 
       
   319     static WTF::PassRefPtr<JSC::Structure> createStructure(JSC::JSValue prototype)
       
   320     {
       
   321         return JSC::Structure::create(prototype, JSC::TypeInfo(JSC::ObjectType));
       
   322     }
       
   323 
       
   324 protected:
       
   325     Data *data;
       
   326 };
       
   327 
       
   328 class QMetaObjectPrototype : public QMetaObjectWrapperObject
       
   329 {
       
   330 public:
       
   331     QMetaObjectPrototype(JSC::ExecState*, WTF::PassRefPtr<JSC::Structure>,
       
   332                          JSC::Structure* prototypeFunctionStructure);
       
   333 };
       
   334 
       
   335 } // namespace QScript
       
   336 
       
   337 QT_END_NAMESPACE
       
   338 
       
   339 #endif