Qt 4.8
Classes | Public Functions | Protected Variables | List of all members
QScript::QObjectDelegate Class Reference

#include <qscriptqobject_p.h>

Inheritance diagram for QScript::QObjectDelegate:
QScriptObjectDelegate

Classes

struct  Data
 

Public Functions

virtual bool compareToObject (QScriptObject *, JSC::ExecState *, JSC::JSObject *)
 
virtual bool deleteProperty (QScriptObject *, JSC::ExecState *, const JSC::Identifier &propertyName)
 
virtual bool getOwnPropertyDescriptor (QScriptObject *, JSC::ExecState *, const JSC::Identifier &propertyName, JSC::PropertyDescriptor &)
 
virtual void getOwnPropertyNames (QScriptObject *, JSC::ExecState *, JSC::PropertyNameArray &, JSC::EnumerationMode mode=JSC::ExcludeDontEnumProperties)
 
virtual bool getOwnPropertySlot (QScriptObject *, JSC::ExecState *, const JSC::Identifier &propertyName, JSC::PropertySlot &)
 
virtual void markChildren (QScriptObject *, JSC::MarkStack &markStack)
 
QScriptEngine::QObjectWrapOptions options () const
 
QScriptEngine::ValueOwnership ownership () const
 
virtual void put (QScriptObject *, JSC::ExecState *exec, const JSC::Identifier &propertyName, JSC::JSValue, JSC::PutPropertySlot &)
 
 QObjectDelegate (QObject *object, QScriptEngine::ValueOwnership ownership, const QScriptEngine::QObjectWrapOptions &options)
 
void setOptions (QScriptEngine::QObjectWrapOptions options)
 
void setOwnership (QScriptEngine::ValueOwnership ownership)
 
void setValue (QObject *value)
 
virtual Type type () const
 
QObjectvalue () const
 
 ~QObjectDelegate ()
 
- Public Functions inherited from QScriptObjectDelegate
virtual JSC::CallType getCallData (QScriptObject *, JSC::CallData &)
 
virtual JSC::ConstructType getConstructData (QScriptObject *, JSC::ConstructData &)
 
virtual bool hasInstance (QScriptObject *, JSC::ExecState *, JSC::JSValue value, JSC::JSValue proto)
 
 QScriptObjectDelegate ()
 
virtual ~QScriptObjectDelegate ()
 

Protected Variables

Datadata
 

Additional Inherited Members

- Public Types inherited from QScriptObjectDelegate
enum  Type { QtObject, Variant, ClassObject, DeclarativeClassObject }
 

Detailed Description

Definition at line 55 of file qscriptqobject_p.h.

Constructors and Destructors

◆ QObjectDelegate()

QScript::QObjectDelegate::QObjectDelegate ( QObject object,
QScriptEngine::ValueOwnership  ownership,
const QScriptEngine::QObjectWrapOptions &  options 
)

Definition at line 1163 of file qscriptqobject.cpp.

Referenced by QScript::QObjectDelegate::Data::Data().

1166  : data(new Data(object, ownership, options))
1167 {
1168 }
QScriptEngine::QObjectWrapOptions options() const
QScriptEngine::ValueOwnership ownership() const

◆ ~QObjectDelegate()

QScript::QObjectDelegate::~QObjectDelegate ( )

Definition at line 1170 of file qscriptqobject.cpp.

Referenced by QScript::QObjectDelegate::Data::Data().

1171 {
1172  switch (data->ownership) {
1174  break;
1176  if (data->value)
1177  delete data->value; // ### fixme
1178 // eng->disposeQObject(value);
1179  break;
1181  if (data->value && !data->value->parent())
1182  delete data->value; // ### fixme
1183 // eng->disposeQObject(value);
1184  break;
1185  }
1186  delete data;
1187 }
QScriptEngine::ValueOwnership ownership
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273

Functions

◆ compareToObject()

bool QScript::QObjectDelegate::compareToObject ( QScriptObject ,
JSC::ExecState *  exec,
JSC::JSObject *  o2 
)
virtual

Reimplemented from QScriptObjectDelegate.

Definition at line 1654 of file qscriptqobject.cpp.

Referenced by QScript::QObjectDelegate::Data::Data().

1655 {
1656  if (!o2->inherits(&QScriptObject::info))
1657  return false;
1658  QScriptObject *object = static_cast<QScriptObject*>(o2);
1659  QScriptObjectDelegate *delegate = object->delegate();
1660  if (!delegate || (delegate->type() != QScriptObjectDelegate::QtObject))
1661  return false;
1662  return value() == static_cast<QObjectDelegate *>(delegate)->value();
1663 }
virtual Type type() const =0
QObjectDelegate(QObject *object, QScriptEngine::ValueOwnership ownership, const QScriptEngine::QObjectWrapOptions &options)
static const JSC::ClassInfo info
QObject * value() const

◆ deleteProperty()

bool QScript::QObjectDelegate::deleteProperty ( QScriptObject object,
JSC::ExecState *  exec,
const JSC::Identifier &  propertyName 
)
virtual

Reimplemented from QScriptObjectDelegate.

Definition at line 1544 of file qscriptqobject.cpp.

Referenced by QScript::QObjectDelegate::Data::Data(), and QScript::QMetaObjectWrapperObject::Data::Data().

1546 {
1547 #ifndef QT_NO_PROPERTIES
1548  QByteArray name = convertToLatin1(propertyName.ustring());
1549  QObject *qobject = data->value;
1550  if (!qobject) {
1551  QString message = QString::fromLatin1("cannot access member `%0' of deleted QObject")
1552  .arg(QString::fromLatin1(name));
1553  JSC::throwError(exec, JSC::GeneralError, message);
1554  return false;
1555  }
1556 
1557  const QMetaObject *meta = qobject->metaObject();
1558  {
1560  if (it != data->cachedMembers.end()) {
1561  if (GeneratePropertyFunctions && (meta->indexOfProperty(name) != -1))
1562  return false;
1563  data->cachedMembers.erase(it);
1564  return true;
1565  }
1566  }
1567 
1568  const QScriptEngine::QObjectWrapOptions &opt = data->options;
1569  int index = meta->indexOfProperty(name);
1570  if (index != -1) {
1571  QMetaProperty prop = meta->property(index);
1572  if (prop.isScriptable() &&
1574  || (index >= meta->propertyOffset()))) {
1575  return false;
1576  }
1577  }
1578 
1579  index = qobject->dynamicPropertyNames().indexOf(name);
1580  if (index != -1) {
1581  (void)qobject->setProperty(name, QVariant());
1582  return true;
1583  }
1584 
1585  return QScriptObjectDelegate::deleteProperty(object, exec, propertyName);
1586 #else //QT_NO_PROPERTIES
1587  return false;
1588 #endif //QT_NO_PROPERTIES
1589 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QMetaObject class contains meta-information about Qt objects.
Definition: qobjectdefs.h:304
virtual bool deleteProperty(QScriptObject *, JSC::ExecState *, const JSC::Identifier &propertyName)
bool isScriptable(const QObject *obj=0) const
Returns true if the property is scriptable for the given object; otherwise returns false...
#define it(className, varName)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
int indexOfProperty(const char *name) const
Finds property name and returns its index; otherwise returns -1.
const char * name
QHash< QByteArray, JSC::JSValue > cachedMembers
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
static const bool GeneratePropertyFunctions
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
quint16 index
QScriptEngine::QObjectWrapOptions options
The QMetaProperty class provides meta-data about a property.
Definition: qmetaobject.h:176
int propertyOffset() const
Returns the property offset for this class; i.e.
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition: qhash.h:865
iterator erase(iterator it)
Removes the (key, value) pair associated with the iterator pos from the hash, and returns an iterator...
Definition: qhash.h:827
QByteArray convertToLatin1(const JSC::UString &str)
QMetaProperty property(int index) const
Returns the meta-data for the property with the given index.

◆ getOwnPropertyDescriptor()

bool QScript::QObjectDelegate::getOwnPropertyDescriptor ( QScriptObject object,
JSC::ExecState *  exec,
const JSC::Identifier &  propertyName,
JSC::PropertyDescriptor &  descriptor 
)
virtual

Reimplemented from QScriptObjectDelegate.

Definition at line 1311 of file qscriptqobject.cpp.

Referenced by QScript::QObjectDelegate::Data::Data(), and QScript::QMetaObjectWrapperObject::Data::Data().

1314 {
1315  //Note: this has to be kept in sync with getOwnPropertySlot
1316 #ifndef QT_NO_PROPERTIES
1317  QByteArray name = convertToLatin1(propertyName.ustring());
1318  QObject *qobject = data->value;
1319  if (!qobject) {
1320  QString message = QString::fromLatin1("cannot access member `%0' of deleted QObject")
1321  .arg(QString::fromLatin1(name));
1322  descriptor.setValue(JSC::throwError(exec, JSC::GeneralError, message));
1323  return true;
1324  }
1325 
1326  const QScriptEngine::QObjectWrapOptions &opt = data->options;
1327 
1328  const QMetaObject *meta = qobject->metaObject();
1329  {
1331  if (it != data->cachedMembers.constEnd()) {
1332  int index;
1333  if (GeneratePropertyFunctions && ((index = meta->indexOfProperty(name)) != -1)) {
1334  QMetaProperty prop = meta->property(index);
1335  descriptor.setAccessorDescriptor(it.value(), it.value(), flagsForMetaProperty(prop));
1336  if (!prop.isWritable())
1337  descriptor.setWritable(false);
1338  } else {
1339  unsigned attributes = QObjectMemberAttribute;
1341  attributes |= JSC::DontEnum;
1342  descriptor.setDescriptor(it.value(), attributes);
1343  }
1344  return true;
1345  }
1346  }
1347 
1349  int index = -1;
1350  if (name.contains('(')) {
1351  QByteArray normalized = QMetaObject::normalizedSignature(name);
1352  if (-1 != (index = meta->indexOfMethod(normalized))) {
1353  QMetaMethod method = meta->method(index);
1354  if (hasMethodAccess(method, index, opt)) {
1356  || (index >= meta->methodOffset())) {
1357  QtFunction *fun = new (exec)QtFunction(
1358  object, index, /*maybeOverloaded=*/false,
1359  &exec->globalData(), eng->originalGlobalObject()->functionStructure(),
1360  propertyName);
1361  data->cachedMembers.insert(name, fun);
1362  unsigned attributes = QObjectMemberAttribute;
1364  attributes |= JSC::DontEnum;
1365  descriptor.setDescriptor(fun, attributes);
1366  return true;
1367  }
1368  }
1369  }
1370  }
1371 
1372  index = meta->indexOfProperty(name);
1373  if (index != -1) {
1374  QMetaProperty prop = meta->property(index);
1375  if (prop.isScriptable()) {
1377  || (index >= meta->propertyOffset())) {
1378  unsigned attributes = flagsForMetaProperty(prop);
1380  QtPropertyFunction *fun = new (exec)QtPropertyFunction(
1381  meta, index, &exec->globalData(),
1382  eng->originalGlobalObject()->functionStructure(),
1383  propertyName);
1384  data->cachedMembers.insert(name, fun);
1385  descriptor.setAccessorDescriptor(fun, fun, attributes);
1386  if (attributes & JSC::ReadOnly)
1387  descriptor.setWritable(false);
1388  } else {
1389  JSC::JSValue val;
1390  if (!prop.isValid())
1391  val = JSC::jsUndefined();
1392  else
1393  val = QScriptEnginePrivate::jscValueFromVariant(exec, prop.read(qobject));
1394  descriptor.setDescriptor(val, attributes);
1395  }
1396  return true;
1397  }
1398  }
1399  }
1400 
1401  index = qobject->dynamicPropertyNames().indexOf(name);
1402  if (index != -1) {
1403  JSC::JSValue val = QScriptEnginePrivate::jscValueFromVariant(exec, qobject->property(name));
1404  descriptor.setDescriptor(val, QObjectMemberAttribute);
1405  return true;
1406  }
1407 
1408  const int offset = (opt & QScriptEngine::ExcludeSuperClassMethods)
1409  ? meta->methodOffset() : 0;
1410  for (index = meta->methodCount() - 1; index >= offset; --index) {
1411  QMetaMethod method = meta->method(index);
1412  if (hasMethodAccess(method, index, opt)
1413  && methodNameEquals(method, name.constData(), name.length())) {
1414  QtFunction *fun = new (exec)QtFunction(
1415  object, index, /*maybeOverloaded=*/true,
1416  &exec->globalData(), eng->originalGlobalObject()->functionStructure(),
1417  propertyName);
1418  unsigned attributes = QObjectMemberAttribute;
1420  attributes |= JSC::DontEnum;
1421  descriptor.setDescriptor(fun, attributes);
1422  data->cachedMembers.insert(name, fun);
1423  return true;
1424  }
1425  }
1426 
1427  if (!(opt & QScriptEngine::ExcludeChildObjects)) {
1428  QList<QObject*> children = qobject->children();
1429  for (index = 0; index < children.count(); ++index) {
1430  QObject *child = children.at(index);
1431  if (child->objectName() == QString(propertyName.ustring())) {
1432  QScriptEngine::QObjectWrapOptions opt = QScriptEngine::PreferExistingWrapperObject;
1433  descriptor.setDescriptor(eng->newQObject(child, QScriptEngine::QtOwnership, opt),
1434  JSC::ReadOnly | JSC::DontDelete | JSC::DontEnum);
1435  return true;
1436  }
1437  }
1438  }
1439 
1440  return QScriptObjectDelegate::getOwnPropertyDescriptor(object, exec, propertyName, descriptor);
1441 #else //QT_NO_PROPERTIES
1442  return false;
1443 #endif //QT_NO_PROPERTIES
1444 }
The QMetaObject class contains meta-information about Qt objects.
Definition: qobjectdefs.h:304
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
bool isScriptable(const QObject *obj=0) const
Returns true if the property is scriptable for the given object; otherwise returns false...
bool isWritable() const
Returns true if this property is writable; otherwise returns false.
static JSC::JSValue jscValueFromVariant(JSC::ExecState *, const QVariant &value)
static QByteArray normalizedSignature(const char *method)
Normalizes the signature of the given method.
#define it(className, varName)
QString objectName
the name of this object
Definition: qobject.h:114
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
virtual bool getOwnPropertyDescriptor(QScriptObject *, JSC::ExecState *, const JSC::Identifier &propertyName, JSC::PropertyDescriptor &)
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
bool isValid() const
Returns true if this property is valid (readable); otherwise returns false.
Definition: qmetaobject.h:213
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
int indexOfProperty(const char *name) const
Finds property name and returns its index; otherwise returns -1.
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
int methodOffset() const
Returns the method offset for this class; i.e.
JSC::JSGlobalObject * originalGlobalObject() const
const char * name
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
static bool methodNameEquals(const QMetaMethod &method, const char *signature, int nameLength)
Returns true if the name of the given method is the same as that specified by the (signature...
const_iterator constFind(const Key &key) const
Returns an iterator pointing to the item with the key in the hash.
Definition: qhash.h:859
QHash< QByteArray, JSC::JSValue > cachedMembers
int length() const
Same as size().
Definition: qbytearray.h:356
static bool hasMethodAccess(const QMetaMethod &method, int index, const QScriptEngine::QObjectWrapOptions &opt)
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
static unsigned flagsForMetaProperty(const QMetaProperty &prop)
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:469
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
static const bool GeneratePropertyFunctions
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
int indexOfMethod(const char *method) const
Finds method and returns its index; otherwise returns -1.
QVariant read(const QObject *obj) const
Reads the property&#39;s value from the given object.
JSC::JSValue newQObject(QObject *object, QScriptEngine::ValueOwnership ownership=QScriptEngine::QtOwnership, const QScriptEngine::QObjectWrapOptions &options=0)
quint16 index
QScriptEngine::QObjectWrapOptions options
QMetaMethod method(int index) const
Returns the meta-data for the method with the given index.
The QMetaProperty class provides meta-data about a property.
Definition: qmetaobject.h:176
int propertyOffset() const
Returns the property offset for this class; i.e.
int methodCount() const
Returns the number of methods known to the meta-object system in this class, including the number of ...
QByteArray convertToLatin1(const JSC::UString &str)
The QMetaMethod class provides meta-data about a member function.
Definition: qmetaobject.h:56
QMetaProperty property(int index) const
Returns the meta-data for the property with the given index.
QBool contains(char c) const
Returns true if the byte array contains the character ch; otherwise returns false.
Definition: qbytearray.h:525
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ getOwnPropertyNames()

void QScript::QObjectDelegate::getOwnPropertyNames ( QScriptObject object,
JSC::ExecState *  exec,
JSC::PropertyNameArray &  propertyNames,
JSC::EnumerationMode  mode = JSC::ExcludeDontEnumProperties 
)
virtual

Reimplemented from QScriptObjectDelegate.

Definition at line 1591 of file qscriptqobject.cpp.

Referenced by QScript::QObjectDelegate::Data::Data(), and QScript::QMetaObjectWrapperObject::Data::Data().

1594 {
1595 #ifndef QT_NO_PROPERTIES
1596  QObject *qobject = data->value;
1597  if (!qobject) {
1598  QString message = QString::fromLatin1("cannot get property names of deleted QObject");
1599  JSC::throwError(exec, JSC::GeneralError, message);
1600  return;
1601  }
1602 
1603  const QScriptEngine::QObjectWrapOptions &opt = data->options;
1604  const QMetaObject *meta = qobject->metaObject();
1605  {
1607  ? meta->propertyOffset() : 0;
1608  for ( ; i < meta->propertyCount(); ++i) {
1609  QMetaProperty prop = meta->property(i);
1610  if (isEnumerableMetaProperty(prop, meta, i)) {
1612  propertyNames.add(JSC::Identifier(exec, name));
1613  }
1614  }
1615  }
1616 
1617  {
1618  QList<QByteArray> dpNames = qobject->dynamicPropertyNames();
1619  for (int i = 0; i < dpNames.size(); ++i) {
1620  QString name = QString::fromLatin1(dpNames.at(i));
1621  propertyNames.add(JSC::Identifier(exec, name));
1622  }
1623  }
1624 
1627  ? meta->methodOffset() : 0;
1628  for ( ; i < meta->methodCount(); ++i) {
1629  QMetaMethod method = meta->method(i);
1630  if (hasMethodAccess(method, i, opt)) {
1631  QMetaMethod method = meta->method(i);
1632  QString sig = QString::fromLatin1(method.signature());
1633  propertyNames.add(JSC::Identifier(exec, sig));
1634  }
1635  }
1636  }
1637 
1638  QScriptObjectDelegate::getOwnPropertyNames(object, exec, propertyNames, mode);
1639 #endif //QT_NO_PROPERTIES
1640 }
The QMetaObject class contains meta-information about Qt objects.
Definition: qobjectdefs.h:304
virtual void getOwnPropertyNames(QScriptObject *, JSC::ExecState *, JSC::PropertyNameArray &, JSC::EnumerationMode mode=JSC::ExcludeDontEnumProperties)
int propertyCount() const
Returns the number of properties in this class, including the number of properties provided by each b...
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
int methodOffset() const
Returns the method offset for this class; i.e.
const char * name
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
static bool hasMethodAccess(const QMetaMethod &method, int index, const QScriptEngine::QObjectWrapOptions &opt)
const char * name() const
Returns this property&#39;s name.
static bool isEnumerableMetaProperty(const QMetaProperty &prop, const QMetaObject *mo, int index)
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
QList< QByteArray > dynamicPropertyNames() const
Returns the names of all properties that were dynamically added to the object using setProperty()...
Definition: qobject.cpp:3839
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
QScriptEngine::QObjectWrapOptions options
QMetaMethod method(int index) const
Returns the meta-data for the method with the given index.
The QMetaProperty class provides meta-data about a property.
Definition: qmetaobject.h:176
int propertyOffset() const
Returns the property offset for this class; i.e.
const char * signature() const
Returns the signature of this method (e.g., setValue(double)).
int methodCount() const
Returns the number of methods known to the meta-object system in this class, including the number of ...
The QMetaMethod class provides meta-data about a member function.
Definition: qmetaobject.h:56
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.
QMetaProperty property(int index) const
Returns the meta-data for the property with the given index.

◆ getOwnPropertySlot()

bool QScript::QObjectDelegate::getOwnPropertySlot ( QScriptObject object,
JSC::ExecState *  exec,
const JSC::Identifier &  propertyName,
JSC::PropertySlot &  slot 
)
virtual

Reimplemented from QScriptObjectDelegate.

Definition at line 1194 of file qscriptqobject.cpp.

Referenced by QScript::QObjectDelegate::Data::Data(), and QScript::QMetaObjectWrapperObject::Data::Data().

1197 {
1198  //Note: this has to be kept in sync with getOwnPropertyDescriptor
1199 #ifndef QT_NO_PROPERTIES
1200  QByteArray name = convertToLatin1(propertyName.ustring());
1201  QObject *qobject = data->value;
1202  if (!qobject) {
1203  QString message = QString::fromLatin1("cannot access member `%0' of deleted QObject")
1204  .arg(QString::fromLatin1(name));
1205  slot.setValue(JSC::throwError(exec, JSC::GeneralError, message));
1206  return true;
1207  }
1208 
1209  const QMetaObject *meta = qobject->metaObject();
1210  {
1212  if (it != data->cachedMembers.constEnd()) {
1213  if (GeneratePropertyFunctions && (meta->indexOfProperty(name) != -1))
1214  slot.setGetterSlot(JSC::asObject(it.value()));
1215  else
1216  slot.setValue(it.value());
1217  return true;
1218  }
1219  }
1220 
1221  const QScriptEngine::QObjectWrapOptions &opt = data->options;
1223  int index = -1;
1224  if (name.contains('(')) {
1225  QByteArray normalized = QMetaObject::normalizedSignature(name);
1226  if (-1 != (index = meta->indexOfMethod(normalized))) {
1227  QMetaMethod method = meta->method(index);
1228  if (hasMethodAccess(method, index, opt)) {
1230  || (index >= meta->methodOffset())) {
1231  QtFunction *fun = new (exec)QtFunction(
1232  object, index, /*maybeOverloaded=*/false,
1233  &exec->globalData(), eng->originalGlobalObject()->functionStructure(),
1234  propertyName);
1235  slot.setValue(fun);
1236  data->cachedMembers.insert(name, fun);
1237  return true;
1238  }
1239  }
1240  }
1241  }
1242 
1243  index = meta->indexOfProperty(name);
1244  if (index != -1) {
1245  QMetaProperty prop = meta->property(index);
1246  if (prop.isScriptable()) {
1248  || (index >= meta->propertyOffset())) {
1250  QtPropertyFunction *fun = new (exec)QtPropertyFunction(
1251  meta, index, &exec->globalData(),
1252  eng->originalGlobalObject()->functionStructure(),
1253  propertyName);
1254  data->cachedMembers.insert(name, fun);
1255  slot.setGetterSlot(fun);
1256  } else {
1257  JSC::JSValue val;
1258  if (!prop.isValid())
1259  val = JSC::jsUndefined();
1260  else
1261  val = QScriptEnginePrivate::jscValueFromVariant(exec, prop.read(qobject));
1262  slot.setValue(val);
1263  }
1264  return true;
1265  }
1266  }
1267  }
1268 
1269  index = qobject->dynamicPropertyNames().indexOf(name);
1270  if (index != -1) {
1271  JSC::JSValue val = QScriptEnginePrivate::jscValueFromVariant(exec, qobject->property(name));
1272  slot.setValue(val);
1273  return true;
1274  }
1275 
1276  const int offset = (opt & QScriptEngine::ExcludeSuperClassMethods)
1277  ? meta->methodOffset() : 0;
1278  for (index = meta->methodCount() - 1; index >= offset; --index) {
1279  QMetaMethod method = meta->method(index);
1280  if (hasMethodAccess(method, index, opt)
1281  && methodNameEquals(method, name.constData(), name.length())) {
1282  QtFunction *fun = new (exec)QtFunction(
1283  object, index, /*maybeOverloaded=*/true,
1284  &exec->globalData(), eng->originalGlobalObject()->functionStructure(),
1285  propertyName);
1286  slot.setValue(fun);
1287  data->cachedMembers.insert(name, fun);
1288  return true;
1289  }
1290  }
1291 
1292  if (!(opt & QScriptEngine::ExcludeChildObjects)) {
1293  QList<QObject*> children = qobject->children();
1294  for (index = 0; index < children.count(); ++index) {
1295  QObject *child = children.at(index);
1296  if (child->objectName() == QString(propertyName.ustring())) {
1297  QScriptEngine::QObjectWrapOptions opt = QScriptEngine::PreferExistingWrapperObject;
1298  slot.setValue(eng->newQObject(child, QScriptEngine::QtOwnership, opt));
1299  return true;
1300  }
1301  }
1302  }
1303 
1304  return QScriptObjectDelegate::getOwnPropertySlot(object, exec, propertyName, slot);
1305 #else //QT_NO_PROPERTIES
1306  return false;
1307 #endif //QT_NO_PROPERTIES
1308 }
The QMetaObject class contains meta-information about Qt objects.
Definition: qobjectdefs.h:304
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
bool isScriptable(const QObject *obj=0) const
Returns true if the property is scriptable for the given object; otherwise returns false...
static JSC::JSValue jscValueFromVariant(JSC::ExecState *, const QVariant &value)
static QByteArray normalizedSignature(const char *method)
Normalizes the signature of the given method.
#define it(className, varName)
QString objectName
the name of this object
Definition: qobject.h:114
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
bool isValid() const
Returns true if this property is valid (readable); otherwise returns false.
Definition: qmetaobject.h:213
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
int indexOfProperty(const char *name) const
Finds property name and returns its index; otherwise returns -1.
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
int methodOffset() const
Returns the method offset for this class; i.e.
JSC::JSGlobalObject * originalGlobalObject() const
const char * name
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
static bool methodNameEquals(const QMetaMethod &method, const char *signature, int nameLength)
Returns true if the name of the given method is the same as that specified by the (signature...
const_iterator constFind(const Key &key) const
Returns an iterator pointing to the item with the key in the hash.
Definition: qhash.h:859
QHash< QByteArray, JSC::JSValue > cachedMembers
int length() const
Same as size().
Definition: qbytearray.h:356
static bool hasMethodAccess(const QMetaMethod &method, int index, const QScriptEngine::QObjectWrapOptions &opt)
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:469
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
static const bool GeneratePropertyFunctions
virtual bool getOwnPropertySlot(QScriptObject *, JSC::ExecState *, const JSC::Identifier &propertyName, JSC::PropertySlot &)
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
int indexOfMethod(const char *method) const
Finds method and returns its index; otherwise returns -1.
QVariant read(const QObject *obj) const
Reads the property&#39;s value from the given object.
JSC::JSValue newQObject(QObject *object, QScriptEngine::ValueOwnership ownership=QScriptEngine::QtOwnership, const QScriptEngine::QObjectWrapOptions &options=0)
quint16 index
QScriptEngine::QObjectWrapOptions options
QMetaMethod method(int index) const
Returns the meta-data for the method with the given index.
The QMetaProperty class provides meta-data about a property.
Definition: qmetaobject.h:176
int propertyOffset() const
Returns the property offset for this class; i.e.
int methodCount() const
Returns the number of methods known to the meta-object system in this class, including the number of ...
QByteArray convertToLatin1(const JSC::UString &str)
The QMetaMethod class provides meta-data about a member function.
Definition: qmetaobject.h:56
QMetaProperty property(int index) const
Returns the meta-data for the property with the given index.
QBool contains(char c) const
Returns true if the byte array contains the character ch; otherwise returns false.
Definition: qbytearray.h:525
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ markChildren()

void QScript::QObjectDelegate::markChildren ( QScriptObject object,
JSC::MarkStack &  markStack 
)
virtual

Reimplemented from QScriptObjectDelegate.

Definition at line 1642 of file qscriptqobject.cpp.

Referenced by QScript::QObjectDelegate::Data::Data(), QScript::QtFunction::Data::Data(), and QScript::QMetaObjectWrapperObject::Data::Data().

1643 {
1645  for (it = data->cachedMembers.constBegin(); it != data->cachedMembers.constEnd(); ++it) {
1646  JSC::JSValue val = it.value();
1647  if (val)
1648  markStack.append(val);
1649  }
1650 
1651  QScriptObjectDelegate::markChildren(object, markStack);
1652 }
#define it(className, varName)
virtual void markChildren(QScriptObject *, JSC::MarkStack &markStack)
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
QHash< QByteArray, JSC::JSValue > cachedMembers
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the hash.
Definition: qhash.h:466
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:469

◆ options()

QScriptEngine::QObjectWrapOptions QScript::QObjectDelegate::options ( ) const
inline

Definition at line 104 of file qscriptqobject_p.h.

105  { return data->options; }
QScriptEngine::QObjectWrapOptions options

◆ ownership()

QScriptEngine::ValueOwnership QScript::QObjectDelegate::ownership ( ) const
inline

Definition at line 99 of file qscriptqobject_p.h.

Referenced by QScript::QObjectConnection::mark().

100  { return data->ownership; }
QScriptEngine::ValueOwnership ownership

◆ put()

void QScript::QObjectDelegate::put ( QScriptObject object,
JSC::ExecState *  exec,
const JSC::Identifier &  propertyName,
JSC::JSValue  value,
JSC::PutPropertySlot &  slot 
)
virtual

Reimplemented from QScriptObjectDelegate.

Definition at line 1446 of file qscriptqobject.cpp.

Referenced by QScript::QObjectDelegate::Data::Data(), and QScript::QMetaObjectWrapperObject::Data::Data().

1449 {
1450 #ifndef QT_NO_PROPERTIES
1451  QByteArray name = convertToLatin1(propertyName.ustring());
1452  QObject *qobject = data->value;
1453  if (!qobject) {
1454  QString message = QString::fromLatin1("cannot access member `%0' of deleted QObject")
1455  .arg(QString::fromLatin1(name));
1456  JSC::throwError(exec, JSC::GeneralError, message);
1457  return;
1458  }
1459 
1460  const QScriptEngine::QObjectWrapOptions &opt = data->options;
1461  const QMetaObject *meta = qobject->metaObject();
1463  int index = -1;
1464  if (name.contains('(')) {
1465  QByteArray normalized = QMetaObject::normalizedSignature(name);
1466  if (-1 != (index = meta->indexOfMethod(normalized))) {
1467  QMetaMethod method = meta->method(index);
1468  if (hasMethodAccess(method, index, opt)) {
1470  || (index >= meta->methodOffset())) {
1471  data->cachedMembers.insert(name, value);
1472  return;
1473  }
1474  }
1475  }
1476  }
1477 
1478  index = meta->indexOfProperty(name);
1479  if (index != -1) {
1480  QMetaProperty prop = meta->property(index);
1481  if (prop.isScriptable()) {
1483  || (index >= meta->propertyOffset())) {
1485  // ### ideally JSC would do this for us already, i.e. find out
1486  // that the property is a setter and call the setter.
1487  // Maybe QtPropertyFunction needs to inherit JSC::GetterSetter.
1488  JSC::JSValue fun;
1490  it = data->cachedMembers.constFind(name);
1491  if (it != data->cachedMembers.constEnd()) {
1492  fun = it.value();
1493  } else {
1494  fun = new (exec)QtPropertyFunction(
1495  meta, index, &exec->globalData(),
1496  eng->originalGlobalObject()->functionStructure(),
1497  propertyName);
1498  data->cachedMembers.insert(name, fun);
1499  }
1500  JSC::CallData callData;
1501  JSC::CallType callType = fun.getCallData(callData);
1502  JSC::JSValue argv[1] = { value };
1503  JSC::ArgList args(argv, 1);
1504  (void)JSC::call(exec, fun, callType, callData, object, args);
1505  } else {
1506  QVariant v;
1507  if (prop.isEnumType() && value.isString()
1508  && !eng->hasDemarshalFunction(prop.userType())) {
1509  // give QMetaProperty::write() a chance to convert from
1510  // string to enum value
1511  v = (QString)value.toString(exec);
1512  } else {
1514  }
1515  (void)prop.write(qobject, v);
1516  }
1517  return;
1518  }
1519  }
1520  }
1521 
1522  const int offset = (opt & QScriptEngine::ExcludeSuperClassMethods)
1523  ? meta->methodOffset() : 0;
1524  for (index = meta->methodCount() - 1; index >= offset; --index) {
1525  QMetaMethod method = meta->method(index);
1526  if (hasMethodAccess(method, index, opt)
1527  && methodNameEquals(method, name.constData(), name.length())) {
1528  data->cachedMembers.insert(name, value);
1529  return;
1530  }
1531  }
1532 
1533  index = qobject->dynamicPropertyNames().indexOf(name);
1534  if ((index != -1) || (opt & QScriptEngine::AutoCreateDynamicProperties)) {
1536  (void)qobject->setProperty(name, v);
1537  return;
1538  }
1539 
1540  QScriptObjectDelegate::put(object, exec, propertyName, value, slot);
1541 #endif //QT_NO_PROPERTIES
1542 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QMetaObject class contains meta-information about Qt objects.
Definition: qobjectdefs.h:304
QScriptEnginePrivate * scriptEngineFromExec(const JSC::ExecState *exec)
bool isScriptable(const QObject *obj=0) const
Returns true if the property is scriptable for the given object; otherwise returns false...
static QByteArray normalizedSignature(const char *method)
Normalizes the signature of the given method.
#define it(className, varName)
static QVariant toVariant(JSC::ExecState *, JSC::JSValue)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
bool hasDemarshalFunction(int type) const
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual void put(QScriptObject *, JSC::ExecState *exec, const JSC::Identifier &propertyName, JSC::JSValue, JSC::PutPropertySlot &)
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
QObject * value() const
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
int indexOfProperty(const char *name) const
Finds property name and returns its index; otherwise returns -1.
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
bool isEnumType() const
Returns true if the property&#39;s type is an enumeration value; otherwise returns false.
int methodOffset() const
Returns the method offset for this class; i.e.
int userType() const
Returns this property&#39;s user type.
JSC::JSGlobalObject * originalGlobalObject() const
const char * name
static bool methodNameEquals(const QMetaMethod &method, const char *signature, int nameLength)
Returns true if the name of the given method is the same as that specified by the (signature...
const_iterator constFind(const Key &key) const
Returns an iterator pointing to the item with the key in the hash.
Definition: qhash.h:859
QHash< QByteArray, JSC::JSValue > cachedMembers
int length() const
Same as size().
Definition: qbytearray.h:356
static bool hasMethodAccess(const QMetaMethod &method, int index, const QScriptEngine::QObjectWrapOptions &opt)
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:469
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
static const bool GeneratePropertyFunctions
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
int indexOfMethod(const char *method) const
Finds method and returns its index; otherwise returns -1.
quint16 index
QScriptEngine::QObjectWrapOptions options
QMetaMethod method(int index) const
Returns the meta-data for the method with the given index.
The QMetaProperty class provides meta-data about a property.
Definition: qmetaobject.h:176
int propertyOffset() const
Returns the property offset for this class; i.e.
bool write(QObject *obj, const QVariant &value) const
Writes value as the property&#39;s value to the given object.
int methodCount() const
Returns the number of methods known to the meta-object system in this class, including the number of ...
static QVariant jscValueToVariant(JSC::ExecState *, JSC::JSValue value, int targetType)
QByteArray convertToLatin1(const JSC::UString &str)
The QMetaMethod class provides meta-data about a member function.
Definition: qmetaobject.h:56
QMetaProperty property(int index) const
Returns the meta-data for the property with the given index.
QBool contains(char c) const
Returns true if the byte array contains the character ch; otherwise returns false.
Definition: qbytearray.h:525

◆ setOptions()

void QScript::QObjectDelegate::setOptions ( QScriptEngine::QObjectWrapOptions  options)
inline

Definition at line 106 of file qscriptqobject_p.h.

Referenced by QScriptEngine::newQObject().

107  { data->options = options; }
QScriptEngine::QObjectWrapOptions options() const
QScriptEngine::QObjectWrapOptions options

◆ setOwnership()

void QScript::QObjectDelegate::setOwnership ( QScriptEngine::ValueOwnership  ownership)
inline

Definition at line 101 of file qscriptqobject_p.h.

Referenced by QScript::QMetaObjectWrapperObject::execute(), and QScriptEngine::newQObject().

102  { data->ownership = ownership; }
QScriptEngine::ValueOwnership ownership() const
QScriptEngine::ValueOwnership ownership

◆ setValue()

void QScript::QObjectDelegate::setValue ( QObject value)
inline

Definition at line 97 of file qscriptqobject_p.h.

Referenced by QScriptEngine::newQObject().

◆ type()

QScriptObjectDelegate::Type QScript::QObjectDelegate::type ( ) const
virtual

Implements QScriptObjectDelegate.

Definition at line 1189 of file qscriptqobject.cpp.

Referenced by QScript::QObjectDelegate::Data::Data().

1190 {
1191  return QtObject;
1192 }

◆ value()

QObject* QScript::QObjectDelegate::value ( ) const
inline

Properties

◆ data

Data* QScript::QObjectDelegate::data
protected

The documentation for this class was generated from the following files: