コード例 #1
0
ファイル: variant_binding.cpp プロジェクト: KDE/kjsembed
JavaScriptArrayType checkArray(KJS::ExecState *exec, KJS::JSValue *val)
{
    KJS::JSObject *obj = val->toObject(exec);
    if (toQString(obj->className()) == "Array") {
        if (!obj->hasProperty(exec, KJS::Identifier("length"))) {
            return Map;
        }
        KJS::JSValue *jslen = obj->get(exec, KJS::Identifier("length"));
        const int len = jslen->toNumber(exec);
        if (len > 0) {
            QByteArray buff;
            buff.setNum(len - 1);
            if (!obj->hasProperty(exec, KJS::Identifier(buff.data()))) {
                return Map;
            }
        }
        return List;
    } else {
        return None;
    }
}
コード例 #2
0
ファイル: slotproxy.cpp プロジェクト: KDE/kjsembed
KJS::List SlotProxy::convertArguments(KJS::ExecState *exec, void **_a)
{
    KJS::List args;
    int offset = metaObject()->indexOfMethod(m_signature.constData());
    QMetaMethod method = metaObject()->method(offset);
    QList<QByteArray> params = method.parameterTypes();
    int idx = 1;
#ifdef DEBUG_SLOTPROXY
    qDebug() << "SlotProxy::convertArguments(): obj=" << this << " m_signature=" << m_signature << " offset=" << offset << " params=" << params;
#endif
    foreach (const QByteArray &param, params) {
#ifdef DEBUG_SLOTPROXY
        int type = QMetaType::type(param.constData());
        qDebug("\tGot a %d - %s - _a[%d] = %p", type, param.data(), idx, _a[idx]);
        qDebug("\t QMetaType::type()=%d", QMetaType::type(QByteArray("Pinya::") + param.constData()));
#endif
        int tp = QVariant::nameToType(param.constData());
        switch (tp) {
        case QVariant::Int:
            args.append(KJS::jsNumber(*(int *)_a[idx]));
            break;
        case QVariant::UInt:
            args.append(KJS::jsNumber(*(uint *)_a[idx]));
            break;
        case QVariant::LongLong:
            args.append(KJS::jsNumber(*(qlonglong *)_a[idx]));
            break;
        case QVariant::ULongLong:
            args.append(KJS::jsNumber(*(qulonglong *)_a[idx]));
            break;
        case QVariant::Double:
            args.append(KJS::jsNumber(*(double *)_a[idx]));
            break;
        case QVariant::Bool:
            args.append(KJS::jsBoolean(*(bool *)_a[idx]));
            break;
        case QVariant::String:
            args.append(KJS::jsString((*reinterpret_cast<QString(*)>(_a[idx]))));
            break;
        case QVariant::UserType: {
            KJS::JSObject *returnValue;
            KJS::JSObject *parent = exec->dynamicInterpreter()->globalObject();
            QByteArray typeName = param.constData();
            bool isPtr = typeName.contains("*");
            if (isPtr) {
                typeName.replace("*", "");    //krazy:exclude=doublequote_chars
            }
#ifdef DEBUG_SLOTPROXY
            qDebug() << "\tQVariant::UserType: typeName=" << typeName << " param=" << param.constData() << " isPtr" << isPtr;
#endif
            if (parent->hasProperty(exec, KJS::Identifier(toUString(typeName)))) {
                QObject *qObj;
                if (isPtr &&
                        ((qObj = *reinterpret_cast<QObject **>(_a[idx])) != 0)) {
#ifdef DEBUG_SLOTPROXY
                    qDebug() << "qObj=" << qObj;
#endif
                    Pointer<QObject> pov(*reinterpret_cast<QObject*(*)>(_a[idx]));
                    returnValue = StaticConstructor::bind(exec, typeName, pov);
                    if (returnValue) {
                        args.append(returnValue);
                        break;
                    } else {
#ifdef DEBUG_SLOTPROXY
                        qDebug("\t\tNo binding retrieved");
#endif
                        returnValue = StaticConstructor::construct(exec, parent, toUString(typeName));
                        if (returnValue) {
                            if (QObjectBinding *objImp = KJSEmbed::extractBindingImp<QObjectBinding>(exec, returnValue)) {
#ifdef DEBUG_SLOTPROXY
                                qDebug() << "\t\t\tFound QObjectBinding";
#endif
                                objImp->setOwnership(KJSEmbed::ObjectBinding::JSOwned);
                                objImp->setObject(qObj);
                                if (qObj->parent() != 0) {
                                    objImp->setOwnership(KJSEmbed::ObjectBinding::QObjOwned);
                                } else {
                                    objImp->setOwnership(KJSEmbed::ObjectBinding::CPPOwned);
                                }
                                args.append(returnValue);
                                break;
                            }
                        }
                    }
                }
            } else {
#ifdef DEBUG_SLOTPROXY
                qDebug("\t\tNo binding registered");
#endif
                KJS::JSObject *returnValue = 0;
                const int metaTypeId = QMetaType::type(param.constData());
                if (QMetaType::typeFlags(metaTypeId) & QMetaType::PointerToQObject) {
                    QObject *obj = (*reinterpret_cast< QObject*(*)>(_a[idx]));
                    returnValue = KJSEmbed::createQObject(exec, obj, KJSEmbed::ObjectBinding::QObjOwned);
                }
                if (returnValue) {
                    args.append(returnValue);
                    break;
                }
            }
        }
        case QVariant::StringList:
        case QVariant::List:
        case QVariant::Map:
        default:
            //qDebug("\t\tconverting to variant");
            QVariant variant(tp, _a[idx]);
            args.append(KJSEmbed::convertToValue(exec, variant));
            break;
        }
        ++idx;
    }