#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();
}
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();
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;
}
const QByteArray normal = QMetaObject::normalizedSignature(name.constData());
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;
}
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;
}
Method* QtClass::methodNamed(PropertyName, Instance*) const
{
return 0;
}
Field* QtClass::fieldNamed(PropertyName identifier, Instance* instance) const
{
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();
QtField* f = qtinst->m_fields.value(name);
if (obj) {
if (f) {
if (f->fieldType() == QtField::MetaProperty)
return f;
#ifndef QT_NO_PROPERTIES
if (f->fieldType() == QtField::DynamicProperty) {
if (obj->dynamicPropertyNames().indexOf(ascii) >= 0)
return f;
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;
}
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
index = obj->dynamicPropertyNames().indexOf(ascii);
if (index >= 0) {
f = new QtField(ascii);
qtinst->m_fields.insert(name, f);
return f;
}
#endif
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;
}
}
return 0;
}
if (qtinst->m_methods.contains(ascii))
return 0;
#ifndef QT_NO_PROPERTIES
if (!f) {
f = new QtField(ascii);
qtinst->m_fields.insert(name, f);
}
#endif
return f;
}
}
}