1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
/*
* Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "config.h"
#include "qt_class.h"
#include "APICast.h"
#include "Identifier.h"
#include "qt_instance.h"
#include "qt_runtime.h"
#include <qdebug.h>
#include <qmetaobject.h>
namespace JSC {
namespace Bindings {
QtClass::QtClass(const QMetaObject* mo)
: m_metaObject(mo)
{
}
QtClass::~QtClass()
{
}
typedef HashMap<const QMetaObject*, QtClass*> ClassesByMetaObject;
static ClassesByMetaObject* classesByMetaObject = 0;
QtClass* QtClass::classForObject(QObject* o)
{
if (!classesByMetaObject)
classesByMetaObject = new ClassesByMetaObject;
const QMetaObject* mo = o->metaObject();
QtClass* aClass = classesByMetaObject->get(mo);
if (!aClass) {
aClass = new QtClass(mo);
classesByMetaObject->set(mo, aClass);
}
return aClass;
}
const char* QtClass::name() const
{
return m_metaObject->className();
}
// We use this to get at signals (so we can return a proper function object,
// and not get wrapped in RuntimeMethod). Also, use this for methods,
// so we can cache the object and return the same object for the same
// identifier.
JSValue QtClass::fallbackObject(ExecState* exec, Instance* inst, PropertyName identifier)
{
QtInstance* qtinst = static_cast<QtInstance*>(inst);
JSContextRef context = toRef(exec);
JSValueRef exception = 0;
String ustring(identifier.publicName());
const QByteArray name = QString(reinterpret_cast<const QChar*>(ustring.characters()), ustring.length()).toLatin1();
// First see if we have a cache hit
if (QtRuntimeMethod* method = qtinst->m_methods.value(name)) {
JSValue obj = toJS(method->jsObjectRef(context, &exception));
if (exception)
return throwError(exec, toJS(exec, exception));
return obj;
}
// Nope, create an entry
const QByteArray normal = QMetaObject::normalizedSignature(name.constData());
// See if there is an exact match
int index = -1;
QMetaMethod metaMethod;
if (normal.contains('(') && (index = m_metaObject->indexOfMethod(normal)) != -1) {
metaMethod = m_metaObject->method(index);
if (metaMethod.access() == QMetaMethod::Private)
index = -1;
}
// Nope.. try a basename match
if (index == -1) {
const int count = m_metaObject->methodCount();
for (index = count - 1; index >= 0; --index) {
metaMethod = m_metaObject->method(index);
if (metaMethod.access() == QMetaMethod::Private)
continue;
if (metaMethod.name() == normal)
break;
}
}
if (index == -1)
return jsUndefined();
int flags = metaMethod.methodType() == QMetaMethod::Signal ? QtRuntimeMethod::MethodIsSignal : 0;
QtRuntimeMethod* method = new QtRuntimeMethod(context, static_cast<QtInstance*>(inst)->getObject(), normal, index, flags, qtinst);
qtinst->m_methods.insert(name, method);
JSValue obj = toJS(method->jsObjectRef(context, &exception));
if (exception)
return throwError(exec, toJS(exec, exception));
return obj;
}
// This functionality is handled by the fallback case above...
Method* QtClass::methodNamed(PropertyName, Instance*) const
{
return 0;
}
// ### we may end up with a different search order than QtScript by not
// folding this code into the fallbackMethod above, but Fields propagate out
// of the binding code
Field* QtClass::fieldNamed(PropertyName identifier, Instance* instance) const
{
// Check static properties first
QtInstance* qtinst = static_cast<QtInstance*>(instance);
QObject* obj = qtinst->getObject();
String ustring(identifier.publicName());
const QString name(reinterpret_cast<const QChar*>(ustring.characters()), ustring.length());
const QByteArray ascii = name.toLatin1();
// First check for a cached field
QtField* f = qtinst->m_fields.value(name);
if (obj) {
if (f) {
// We only cache real metaproperties, but we do store the
// other types so we can delete them later
if (f->fieldType() == QtField::MetaProperty)
return f;
#ifndef QT_NO_PROPERTIES
if (f->fieldType() == QtField::DynamicProperty) {
if (obj->dynamicPropertyNames().indexOf(ascii) >= 0)
return f;
// Dynamic property that disappeared
qtinst->m_fields.remove(name);
delete f;
}
#endif
else {
const QList<QObject*>& children = obj->children();
const int count = children.size();
for (int index = 0; index < count; ++index) {
QObject* child = children.at(index);
if (child->objectName() == name)
return f;
}
// Didn't find it, delete it from the cache
qtinst->m_fields.remove(name);
delete f;
}
}
int index = m_metaObject->indexOfProperty(ascii);
if (index >= 0) {
const QMetaProperty prop = m_metaObject->property(index);
if (prop.isScriptable(obj)) {
f = new QtField(prop);
qtinst->m_fields.insert(name, f);
return f;
}
}
#ifndef QT_NO_PROPERTIES
// Dynamic properties
index = obj->dynamicPropertyNames().indexOf(ascii);
if (index >= 0) {
f = new QtField(ascii);
qtinst->m_fields.insert(name, f);
return f;
}
#endif
// Child objects
const QList<QObject*>& children = obj->children();
const int count = children.count();
for (index = 0; index < count; ++index) {
QObject* child = children.at(index);
if (child->objectName() == name) {
f = new QtField(child);
qtinst->m_fields.insert(name, f);
return f;
}
}
// Nothing named this
return 0;
}
// For compatibility with qtscript, cached methods don't cause
// errors until they are accessed, so don't blindly create an error
// here.
if (qtinst->m_methods.contains(ascii))
return 0;
#ifndef QT_NO_PROPERTIES
// deleted qobject, but can't throw an error from here (no exec)
// create a fake QtField that will throw upon access
if (!f) {
f = new QtField(ascii);
qtinst->m_fields.insert(name, f);
}
#endif
return f;
}
}
}
|