src/script/bridge/qscriptqobject.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the QtScript module of the Qt Toolkit.
     7 ** This file is part of the QtScript module of the Qt Toolkit.
     8 **
     8 **
     9 ** $QT_BEGIN_LICENSE:LGPL$
     9 ** $QT_BEGIN_LICENSE:LGPL-ONLY$
    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
    10 ** GNU Lesser General Public License Usage
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
    11 ** 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
    12 ** General Public License version 2.1 as published by the Free Software
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
    13 ** Foundation and appearing in the file LICENSE.LGPL included in the
    20 ** packaging of this file.  Please review the following information to
    14 ** packaging of this file.  Please review the following information to
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
    15 ** 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.
    16 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    23 **
    17 **
    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
    18 ** If you have questions regarding the use of this file, please contact
    29 ** Nokia at qt-info@nokia.com.
    19 ** Nokia at qt-info@nokia.com.
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
    20 ** $QT_END_LICENSE$
    39 **
    21 **
    40 ****************************************************************************/
    22 ****************************************************************************/
    41 
    23 
    42 #include "config.h"
    24 #include "config.h"
    99         return (s == slot);
    81         return (s == slot);
   100     }
    82     }
   101 
    83 
   102     void mark(JSC::MarkStack& markStack)
    84     void mark(JSC::MarkStack& markStack)
   103     {
    85     {
   104         // ### need to find out if senderWrapper is marked
       
   105         if (senderWrapper) {
    86         if (senderWrapper) {
   106             // see if the sender should be marked or not
    87             // see if the sender should be marked or not;
       
    88             // if the C++ object is owned by script, we don't want
       
    89             // it to stay alive due to a script connection.
   107             Q_ASSERT(senderWrapper.inherits(&QScriptObject::info));
    90             Q_ASSERT(senderWrapper.inherits(&QScriptObject::info));
   108             QScriptObject *scriptObject = static_cast<QScriptObject*>(JSC::asObject(senderWrapper));
    91             QScriptObject *scriptObject = static_cast<QScriptObject*>(JSC::asObject(senderWrapper));
   109             QScriptObjectDelegate *delegate = scriptObject->delegate();
    92             if (!JSC::Heap::isCellMarked(scriptObject)) {
   110             Q_ASSERT(delegate && (delegate->type() == QScriptObjectDelegate::QtObject));
    93                 QScriptObjectDelegate *delegate = scriptObject->delegate();
   111             QObjectDelegate *inst = static_cast<QObjectDelegate*>(delegate);
    94                 Q_ASSERT(delegate && (delegate->type() == QScriptObjectDelegate::QtObject));
   112             if ((inst->ownership() == QScriptEngine::ScriptOwnership)
    95                 QObjectDelegate *inst = static_cast<QObjectDelegate*>(delegate);
   113                 || ((inst->ownership() == QScriptEngine::AutoOwnership)
    96                 if ((inst->ownership() == QScriptEngine::ScriptOwnership)
   114                     && inst->value() && !inst->value()->parent())) {
    97                     || ((inst->ownership() == QScriptEngine::AutoOwnership)
   115                 // #### don't mark if not marked otherwise
    98                         && inst->value() && !inst->value()->parent())) {
   116                 //senderWrapper = JSC::JSValue();
    99                     senderWrapper = JSC::JSValue();
   117                 markStack.append(senderWrapper);
   100                 } else {
   118             } else {
   101                     markStack.append(senderWrapper);
   119                 markStack.append(senderWrapper);
   102                 }
   120             }
   103             }
   121         }
   104         }
   122         if (receiver)
   105         if (receiver)
   123             markStack.append(receiver);
   106             markStack.append(receiver);
   124         if (slot)
   107         if (slot)
  1188 
  1171 
  1189 bool QObjectDelegate::getOwnPropertySlot(QScriptObject *object, JSC::ExecState *exec,
  1172 bool QObjectDelegate::getOwnPropertySlot(QScriptObject *object, JSC::ExecState *exec,
  1190                                          const JSC::Identifier &propertyName,
  1173                                          const JSC::Identifier &propertyName,
  1191                                          JSC::PropertySlot &slot)
  1174                                          JSC::PropertySlot &slot)
  1192 {
  1175 {
       
  1176 #ifndef QT_NO_PROPERTIES
  1193     QByteArray name = QString(propertyName.ustring()).toLatin1();
  1177     QByteArray name = QString(propertyName.ustring()).toLatin1();
  1194     QObject *qobject = data->value;
  1178     QObject *qobject = data->value;
  1195     if (!qobject) {
  1179     if (!qobject) {
  1196         QString message = QString::fromLatin1("cannot access member `%0' of deleted QObject")
  1180         QString message = QString::fromLatin1("cannot access member `%0' of deleted QObject")
  1197                           .arg(QString::fromLatin1(name));
  1181                           .arg(QString::fromLatin1(name));
  1294             }
  1278             }
  1295         }
  1279         }
  1296     }
  1280     }
  1297 
  1281 
  1298     return QScriptObjectDelegate::getOwnPropertySlot(object, exec, propertyName, slot);
  1282     return QScriptObjectDelegate::getOwnPropertySlot(object, exec, propertyName, slot);
       
  1283 #else //QT_NO_PROPERTIES
       
  1284     return false;
       
  1285 #endif //QT_NO_PROPERTIES
  1299 }
  1286 }
  1300 
  1287 
  1301 void QObjectDelegate::put(QScriptObject *object, JSC::ExecState* exec,
  1288 void QObjectDelegate::put(QScriptObject *object, JSC::ExecState* exec,
  1302                           const JSC::Identifier& propertyName,
  1289                           const JSC::Identifier& propertyName,
  1303                           JSC::JSValue value, JSC::PutPropertySlot &slot)
  1290                           JSC::JSValue value, JSC::PutPropertySlot &slot)
  1304 {
  1291 {
       
  1292 #ifndef QT_NO_PROPERTIES
  1305     QByteArray name = ((QString)propertyName.ustring()).toLatin1();
  1293     QByteArray name = ((QString)propertyName.ustring()).toLatin1();
  1306     QObject *qobject = data->value;
  1294     QObject *qobject = data->value;
  1307     if (!qobject) {
  1295     if (!qobject) {
  1308         QString message = QString::fromLatin1("cannot access member `%0' of deleted QObject")
  1296         QString message = QString::fromLatin1("cannot access member `%0' of deleted QObject")
  1309                           .arg(QString::fromLatin1(name));
  1297                           .arg(QString::fromLatin1(name));
  1390         (void)qobject->setProperty(name, v);
  1378         (void)qobject->setProperty(name, v);
  1391         return;
  1379         return;
  1392     }
  1380     }
  1393 
  1381 
  1394     QScriptObjectDelegate::put(object, exec, propertyName, value, slot);
  1382     QScriptObjectDelegate::put(object, exec, propertyName, value, slot);
       
  1383 #endif //QT_NO_PROPERTIES
  1395 }
  1384 }
  1396 
  1385 
  1397 bool QObjectDelegate::deleteProperty(QScriptObject *object, JSC::ExecState *exec,
  1386 bool QObjectDelegate::deleteProperty(QScriptObject *object, JSC::ExecState *exec,
  1398                                      const JSC::Identifier& propertyName,
  1387                                      const JSC::Identifier& propertyName,
  1399                                      bool checkDontDelete)
  1388                                      bool checkDontDelete)
  1400 {
  1389 {
       
  1390 #ifndef QT_NO_PROPERTIES
  1401     QByteArray name = ((QString)propertyName.ustring()).toLatin1();
  1391     QByteArray name = ((QString)propertyName.ustring()).toLatin1();
  1402     QObject *qobject = data->value;
  1392     QObject *qobject = data->value;
  1403     if (!qobject) {
  1393     if (!qobject) {
  1404         QString message = QString::fromLatin1("cannot access member `%0' of deleted QObject")
  1394         QString message = QString::fromLatin1("cannot access member `%0' of deleted QObject")
  1405                           .arg(QString::fromLatin1(name));
  1395                           .arg(QString::fromLatin1(name));
  1434         (void)qobject->setProperty(name, QVariant());
  1424         (void)qobject->setProperty(name, QVariant());
  1435         return true;
  1425         return true;
  1436     }
  1426     }
  1437 
  1427 
  1438     return QScriptObjectDelegate::deleteProperty(object, exec, propertyName, checkDontDelete);
  1428     return QScriptObjectDelegate::deleteProperty(object, exec, propertyName, checkDontDelete);
       
  1429 #else //QT_NO_PROPERTIES
       
  1430     return false;
       
  1431 #endif //QT_NO_PROPERTIES
  1439 }
  1432 }
  1440 
  1433 
  1441 bool QObjectDelegate::getPropertyAttributes(const QScriptObject *object,
  1434 bool QObjectDelegate::getPropertyAttributes(const QScriptObject *object,
  1442                                             JSC::ExecState *exec,
  1435                                             JSC::ExecState *exec,
  1443                                             const JSC::Identifier &propertyName,
  1436                                             const JSC::Identifier &propertyName,
  1444                                             unsigned &attributes) const
  1437                                             unsigned &attributes) const
  1445 {
  1438 {
       
  1439 #ifndef QT_NO_PROPERTIES
  1446     // ### try to avoid duplicating logic from getOwnPropertySlot()
  1440     // ### try to avoid duplicating logic from getOwnPropertySlot()
  1447     QByteArray name = ((QString)propertyName.ustring()).toLatin1();
  1441     QByteArray name = ((QString)propertyName.ustring()).toLatin1();
  1448     QObject *qobject = data->value;
  1442     QObject *qobject = data->value;
  1449     if (!qobject)
  1443     if (!qobject)
  1450         return false;
  1444         return false;
  1509             }
  1503             }
  1510         }
  1504         }
  1511     }
  1505     }
  1512 
  1506 
  1513     return QScriptObjectDelegate::getPropertyAttributes(object, exec, propertyName, attributes);
  1507     return QScriptObjectDelegate::getPropertyAttributes(object, exec, propertyName, attributes);
       
  1508 #else //QT_NO_PROPERTIES
       
  1509     return false;
       
  1510 #endif //QT_NO_PROPERTIES
  1514 }
  1511 }
  1515 
  1512 
  1516 void QObjectDelegate::getOwnPropertyNames(QScriptObject *object, JSC::ExecState *exec,
  1513 void QObjectDelegate::getOwnPropertyNames(QScriptObject *object, JSC::ExecState *exec,
  1517                                           JSC::PropertyNameArray &propertyNames,
  1514                                           JSC::PropertyNameArray &propertyNames,
  1518                                           bool includeNonEnumerable)
  1515                                           bool includeNonEnumerable)
  1519 {
  1516 {
       
  1517 #ifndef QT_NO_PROPERTIES
  1520     QObject *qobject = data->value;
  1518     QObject *qobject = data->value;
  1521     if (!qobject) {
  1519     if (!qobject) {
  1522         QString message = QString::fromLatin1("cannot get property names of deleted QObject");
  1520         QString message = QString::fromLatin1("cannot get property names of deleted QObject");
  1523         JSC::throwError(exec, JSC::GeneralError, message);
  1521         JSC::throwError(exec, JSC::GeneralError, message);
  1524         return;
  1522         return;
  1558             }
  1556             }
  1559         }
  1557         }
  1560     }
  1558     }
  1561 
  1559 
  1562     QScriptObjectDelegate::getOwnPropertyNames(object, exec, propertyNames, includeNonEnumerable);
  1560     QScriptObjectDelegate::getOwnPropertyNames(object, exec, propertyNames, includeNonEnumerable);
       
  1561 #endif //QT_NO_PROPERTIES
  1563 }
  1562 }
  1564 
  1563 
  1565 void QObjectDelegate::markChildren(QScriptObject *object, JSC::MarkStack& markStack)
  1564 void QObjectDelegate::markChildren(QScriptObject *object, JSC::MarkStack& markStack)
  1566 {
  1565 {
  1567     QHash<QByteArray, JSC::JSValue>::const_iterator it;
  1566     QHash<QByteArray, JSC::JSValue>::const_iterator it;