| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. |
| 4 | ** Contact: http://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtScript module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QSCRIPTQOBJECT_P_H |
| 41 | #define QSCRIPTQOBJECT_P_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists purely as an |
| 48 | // implementation detail. This header file may change from version to |
| 49 | // version without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | #include "qscriptobject_p.h" |
| 55 | |
| 56 | #include "qscriptengine.h" |
| 57 | #include <QtCore/qpointer.h> |
| 58 | |
| 59 | #include "InternalFunction.h" |
| 60 | |
| 61 | QT_BEGIN_NAMESPACE |
| 62 | |
| 63 | namespace QScript |
| 64 | { |
| 65 | |
| 66 | enum AttributeExtension { |
| 67 | // ### Make sure there's no conflict with JSC::Attribute |
| 68 | QObjectMemberAttribute = 1 << 12 |
| 69 | }; |
| 70 | |
| 71 | class QObjectDelegate : public QScriptObjectDelegate |
| 72 | { |
| 73 | public: |
| 74 | struct Data |
| 75 | { |
| 76 | QPointer<QObject> value; |
| 77 | QScriptEngine::ValueOwnership ownership; |
| 78 | QScriptEngine::QObjectWrapOptions options; |
| 79 | |
| 80 | QHash<QByteArray, JSC::JSValue> cachedMembers; |
| 81 | |
| 82 | Data(QObject *o, QScriptEngine::ValueOwnership own, |
| 83 | QScriptEngine::QObjectWrapOptions opt) |
| 84 | : value(o), ownership(own), options(opt) {} |
| 85 | }; |
| 86 | |
| 87 | QObjectDelegate( |
| 88 | QObject *object, QScriptEngine::ValueOwnership ownership, |
| 89 | const QScriptEngine::QObjectWrapOptions &options); |
| 90 | ~QObjectDelegate(); |
| 91 | |
| 92 | virtual Type type() const; |
| 93 | |
| 94 | virtual bool getOwnPropertySlot(QScriptObject*, JSC::ExecState*, |
| 95 | const JSC::Identifier& propertyName, |
| 96 | JSC::PropertySlot&); |
| 97 | virtual bool getOwnPropertyDescriptor(QScriptObject*, JSC::ExecState*, |
| 98 | const JSC::Identifier& propertyName, |
| 99 | JSC::PropertyDescriptor&); |
| 100 | |
| 101 | virtual void put(QScriptObject*, JSC::ExecState* exec, |
| 102 | const JSC::Identifier& propertyName, |
| 103 | JSC::JSValue, JSC::PutPropertySlot&); |
| 104 | virtual bool deleteProperty(QScriptObject*, JSC::ExecState*, |
| 105 | const JSC::Identifier& propertyName); |
| 106 | virtual void getOwnPropertyNames(QScriptObject*, JSC::ExecState*, |
| 107 | JSC::PropertyNameArray&, |
| 108 | JSC::EnumerationMode mode = JSC::ExcludeDontEnumProperties); |
| 109 | virtual void markChildren(QScriptObject*, JSC::MarkStack& markStack); |
| 110 | virtual bool compareToObject(QScriptObject*, JSC::ExecState*, JSC::JSObject*); |
| 111 | |
| 112 | inline QObject *value() const { return data->value; } |
| 113 | inline void setValue(QObject* value) { data->value = value; } |
| 114 | inline bool hasParent() const { return data->value && data->value->parent(); } |
| 115 | |
| 116 | inline QScriptEngine::ValueOwnership ownership() const |
| 117 | { return data->ownership; } |
| 118 | inline void setOwnership(QScriptEngine::ValueOwnership ownership) |
| 119 | { data->ownership = ownership; } |
| 120 | |
| 121 | inline QScriptEngine::QObjectWrapOptions options() const |
| 122 | { return data->options; } |
| 123 | inline void setOptions(QScriptEngine::QObjectWrapOptions options) |
| 124 | { data->options = options; } |
| 125 | |
| 126 | protected: |
| 127 | Data *data; |
| 128 | }; |
| 129 | |
| 130 | class QObjectPrototypeObject : public QObject |
| 131 | { |
| 132 | Q_OBJECT |
| 133 | public: |
| 134 | QObjectPrototypeObject(QObject *parent = 0) |
| 135 | : QObject(parent) { } |
| 136 | ~QObjectPrototypeObject() { } |
| 137 | }; |
| 138 | |
| 139 | class QObjectPrototype : public QScriptObject |
| 140 | { |
| 141 | public: |
| 142 | QObjectPrototype(JSC::ExecState*, WTF::PassRefPtr<JSC::Structure>, |
| 143 | JSC::Structure* prototypeFunctionStructure); |
| 144 | }; |
| 145 | |
| 146 | class QObjectConnectionManager; |
| 147 | |
| 148 | struct QObjectWrapperInfo |
| 149 | { |
| 150 | QObjectWrapperInfo(QScriptObject *obj, |
| 151 | QScriptEngine::ValueOwnership own, |
| 152 | const QScriptEngine::QObjectWrapOptions &opt) |
| 153 | : object(obj), ownership(own), options(opt) {} |
| 154 | |
| 155 | QScriptObject *object; |
| 156 | QScriptEngine::ValueOwnership ownership; |
| 157 | QScriptEngine::QObjectWrapOptions options; |
| 158 | |
| 159 | // Returns true if this wrapper can be garbage-collected when there are no |
| 160 | // other references to it in the JS environment (weak reference), otherwise |
| 161 | // returns false (should not be collected). |
| 162 | bool isCollectableWhenWeaklyReferenced() const |
| 163 | { |
| 164 | switch (ownership) { |
| 165 | case QScriptEngine::ScriptOwnership: |
| 166 | return true; |
| 167 | case QScriptEngine::AutoOwnership: { |
| 168 | QScriptObjectDelegate *delegate = object->delegate(); |
| 169 | Q_ASSERT(delegate && (delegate->type() == QScriptObjectDelegate::QtObject)); |
| 170 | return !static_cast<QObjectDelegate *>(delegate)->hasParent(); |
| 171 | } |
| 172 | default: |
| 173 | break; |
| 174 | } |
| 175 | return false; |
| 176 | } |
| 177 | }; |
| 178 | |
| 179 | class QObjectData // : public QObjectUserData |
| 180 | { |
| 181 | public: |
| 182 | QObjectData(QScriptEnginePrivate *engine); |
| 183 | ~QObjectData(); |
| 184 | |
| 185 | bool addSignalHandler(QObject *sender, |
| 186 | int signalIndex, |
| 187 | JSC::JSValue receiver, |
| 188 | JSC::JSValue slot, |
| 189 | JSC::JSValue senderWrapper, |
| 190 | Qt::ConnectionType type); |
| 191 | bool removeSignalHandler(QObject *sender, |
| 192 | int signalIndex, |
| 193 | JSC::JSValue receiver, |
| 194 | JSC::JSValue slot); |
| 195 | |
| 196 | QScriptObject *findWrapper(QScriptEngine::ValueOwnership ownership, |
| 197 | const QScriptEngine::QObjectWrapOptions &options) const; |
| 198 | void registerWrapper(QScriptObject *wrapper, |
| 199 | QScriptEngine::ValueOwnership ownership, |
| 200 | const QScriptEngine::QObjectWrapOptions &options); |
| 201 | |
| 202 | void clearConnectionMarkBits(); |
| 203 | int markConnections(JSC::MarkStack&); |
| 204 | void markWrappers(JSC::MarkStack&); |
| 205 | |
| 206 | private: |
| 207 | QScriptEnginePrivate *engine; |
| 208 | QScript::QObjectConnectionManager *connectionManager; |
| 209 | QList<QScript::QObjectWrapperInfo> wrappers; |
| 210 | }; |
| 211 | |
| 212 | class QtFunction: public JSC::InternalFunction |
| 213 | { |
| 214 | public: |
| 215 | // work around CELL_SIZE limitation |
| 216 | struct Data |
| 217 | { |
| 218 | JSC::JSValue object; |
| 219 | int initialIndex; |
| 220 | bool maybeOverloaded; |
| 221 | |
| 222 | Data(JSC::JSValue o, int ii, bool mo) |
| 223 | : object(o), initialIndex(ii), maybeOverloaded(mo) {} |
| 224 | }; |
| 225 | |
| 226 | QtFunction(JSC::JSValue object, int initialIndex, bool maybeOverloaded, |
| 227 | JSC::JSGlobalData*, WTF::PassRefPtr<JSC::Structure>, const JSC::Identifier&); |
| 228 | virtual ~QtFunction(); |
| 229 | |
| 230 | virtual JSC::CallType getCallData(JSC::CallData&); |
| 231 | virtual void markChildren(JSC::MarkStack&); |
| 232 | |
| 233 | virtual const JSC::ClassInfo* classInfo() const { return &info; } |
| 234 | static const JSC::ClassInfo info; |
| 235 | |
| 236 | static JSC::JSValue JSC_HOST_CALL call(JSC::ExecState*, JSC::JSObject*, |
| 237 | JSC::JSValue, const JSC::ArgList&); |
| 238 | |
| 239 | JSC::JSValue execute(JSC::ExecState *exec, JSC::JSValue thisValue, |
| 240 | const JSC::ArgList &args); |
| 241 | |
| 242 | QScriptObject *wrapperObject() const; |
| 243 | QObject *qobject() const; |
| 244 | const QMetaObject *metaObject() const; |
| 245 | int initialIndex() const; |
| 246 | int specificIndex(const QScriptContext *context) const; |
| 247 | bool maybeOverloaded() const; |
| 248 | int mostGeneralMethod(QMetaMethod *out = 0) const; |
| 249 | QList<int> overloadedIndexes() const; |
| 250 | |
| 251 | private: |
| 252 | Data *data; |
| 253 | }; |
| 254 | |
| 255 | class QtPropertyFunction: public JSC::InternalFunction |
| 256 | { |
| 257 | public: |
| 258 | // work around CELL_SIZE limitation |
| 259 | struct Data |
| 260 | { |
| 261 | const QMetaObject *meta; |
| 262 | int index; |
| 263 | |
| 264 | Data(const QMetaObject *m, int i) |
| 265 | : meta(m), index(i) {} |
| 266 | }; |
| 267 | |
| 268 | QtPropertyFunction(const QMetaObject *meta, int index, |
| 269 | JSC::JSGlobalData*, WTF::PassRefPtr<JSC::Structure>, |
| 270 | const JSC::Identifier&); |
| 271 | virtual ~QtPropertyFunction(); |
| 272 | |
| 273 | virtual JSC::CallType getCallData(JSC::CallData&); |
| 274 | |
| 275 | virtual const JSC::ClassInfo* classInfo() const { return &info; } |
| 276 | static const JSC::ClassInfo info; |
| 277 | |
| 278 | static JSC::JSValue JSC_HOST_CALL call(JSC::ExecState*, JSC::JSObject*, |
| 279 | JSC::JSValue, const JSC::ArgList&); |
| 280 | |
| 281 | JSC::JSValue execute(JSC::ExecState *exec, JSC::JSValue thisValue, |
| 282 | const JSC::ArgList &args); |
| 283 | |
| 284 | const QMetaObject *metaObject() const; |
| 285 | int propertyIndex() const; |
| 286 | |
| 287 | private: |
| 288 | Data *data; |
| 289 | }; |
| 290 | |
| 291 | class QMetaObjectWrapperObject : public JSC::JSObject |
| 292 | { |
| 293 | public: |
| 294 | // work around CELL_SIZE limitation |
| 295 | struct Data |
| 296 | { |
| 297 | const QMetaObject *value; |
| 298 | JSC::JSValue ctor; |
| 299 | JSC::JSValue prototype; |
| 300 | |
| 301 | Data(const QMetaObject *mo, JSC::JSValue c) |
| 302 | : value(mo), ctor(c) {} |
| 303 | }; |
| 304 | |
| 305 | explicit QMetaObjectWrapperObject( |
| 306 | JSC::ExecState *, const QMetaObject *metaobject, JSC::JSValue ctor, |
| 307 | WTF::PassRefPtr<JSC::Structure> sid); |
| 308 | ~QMetaObjectWrapperObject(); |
| 309 | |
| 310 | virtual bool getOwnPropertySlot(JSC::ExecState*, |
| 311 | const JSC::Identifier& propertyName, |
| 312 | JSC::PropertySlot&); |
| 313 | virtual bool getOwnPropertyDescriptor(JSC::ExecState*, |
| 314 | const JSC::Identifier& propertyName, |
| 315 | JSC::PropertyDescriptor&); |
| 316 | virtual void put(JSC::ExecState* exec, const JSC::Identifier& propertyName, |
| 317 | JSC::JSValue, JSC::PutPropertySlot&); |
| 318 | virtual bool deleteProperty(JSC::ExecState*, |
| 319 | const JSC::Identifier& propertyName); |
| 320 | virtual void getOwnPropertyNames(JSC::ExecState*, JSC::PropertyNameArray&, |
| 321 | JSC::EnumerationMode mode = JSC::ExcludeDontEnumProperties); |
| 322 | virtual void markChildren(JSC::MarkStack& markStack); |
| 323 | |
| 324 | virtual JSC::CallType getCallData(JSC::CallData&); |
| 325 | virtual JSC::ConstructType getConstructData(JSC::ConstructData&); |
| 326 | |
| 327 | virtual const JSC::ClassInfo* classInfo() const { return &info; } |
| 328 | static const JSC::ClassInfo info; |
| 329 | |
| 330 | static JSC::JSValue JSC_HOST_CALL call(JSC::ExecState*, JSC::JSObject*, |
| 331 | JSC::JSValue, const JSC::ArgList&); |
| 332 | static JSC::JSObject* construct(JSC::ExecState *, JSC::JSObject *, const JSC::ArgList &); |
| 333 | |
| 334 | JSC::JSValue execute(JSC::ExecState *exec, const JSC::ArgList &args); |
| 335 | |
| 336 | inline const QMetaObject *value() const { return data->value; } |
| 337 | inline void setValue(const QMetaObject* value) { data->value = value; } |
| 338 | |
| 339 | static WTF::PassRefPtr<JSC::Structure> createStructure(JSC::JSValue prototype) |
| 340 | { |
| 341 | return JSC::Structure::create(prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags)); |
| 342 | } |
| 343 | |
| 344 | protected: |
| 345 | static const unsigned StructureFlags = JSC::OverridesGetOwnPropertySlot | JSC::OverridesMarkChildren | JSC::OverridesGetPropertyNames | JSC::ImplementsHasInstance | JSObject::StructureFlags; |
| 346 | |
| 347 | Data *data; |
| 348 | }; |
| 349 | |
| 350 | class QMetaObjectPrototype : public QMetaObjectWrapperObject |
| 351 | { |
| 352 | public: |
| 353 | QMetaObjectPrototype(JSC::ExecState*, WTF::PassRefPtr<JSC::Structure>, |
| 354 | JSC::Structure* prototypeFunctionStructure); |
| 355 | }; |
| 356 | |
| 357 | } // namespace QScript |
| 358 | |
| 359 | QT_END_NAMESPACE |
| 360 | |
| 361 | #endif |
| 362 | |