Exemplo n.º 1
0
KJS::JSObject *convertEvent(KJS::ExecState *exec, const QCloseEvent *ev)
{
    KJS::JSObject *cev = convertEvent(exec, (QEvent *)ev);

    cev->put(exec, "isAccepted", KJS::jsBoolean(ev->isAccepted()));
    return cev;
}
Exemplo n.º 2
0
QVariant KJSObjectModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    Node *item = static_cast<Node*>(index.internalPointer());
    KJS::JSObject *instance = item->instance;

    if (role == Qt::DecorationRole )
    {
        if( instance->implementsConstruct() )
            return QPixmap(":/images/class.png");
        else if( instance->implementsCall() )
            return QPixmap(":/images/method.png");
        else
            return QPixmap(":/images/property.png");
    }
    if( role == Qt::TextColorRole )
    {
        if( instance->implementsConstruct() )
            return QColor("blue");
        else if( instance->implementsCall() )
            return QColor("green");
        else
            return QColor("black");
    }
    if (role == Qt::DisplayRole)
        return item->name;
    return QVariant();
}
Exemplo n.º 3
0
KJS::JSObject *convertEvent(KJS::ExecState *exec, const QResizeEvent *ev)
{
    KJS::JSObject *rev = convertEvent(exec, (QEvent *)ev);

    rev->put(exec, "size", convertToValue(exec, ev->size()));
    rev->put(exec, "oldSize", convertToValue(exec, ev->oldSize()));
    return rev;
}
Exemplo n.º 4
0
KJS::JSObject *convertEvent(KJS::ExecState *exec, const QPaintEvent *ev)
{
    KJS::JSObject *pev = convertEvent(exec, (QEvent *)ev);

    pev->put(exec, "rect", convertToValue(exec, ev->rect()));
//         pev->put( exec, "region", convertToValue( exec, ev->region() ) );
    return pev;
}
Exemplo n.º 5
0
KJS::JSObject *convertEvent(KJS::ExecState *exec, const QMoveEvent *ev)
{
    KJS::JSObject *mev = convertEvent(exec, (QEvent *)ev);

    mev->put(exec, "pos", convertToValue(exec, ev->pos()));
    mev->put(exec, "oldPos", convertToValue(exec, ev->oldPos()));
    return mev;
}
Exemplo n.º 6
0
KJS::JSObject *convertEvent(KJS::ExecState *exec, const QFocusEvent *ev)
{
    KJS::JSObject *dev = convertEvent(exec, (QEvent *)ev);
    dev->put(exec, "gotFocus", KJS::jsBoolean(ev->gotFocus()));
    dev->put(exec, "lostFocus", KJS::jsBoolean(ev->lostFocus()));
    //Gold star trolltech!
    dev->put(exec, "reason", KJS::jsNumber((int) const_cast<QFocusEvent *>(ev)->reason()));
    return dev;
}
Exemplo n.º 7
0
KJS::JSObject *convertEvent(KJS::ExecState *exec, const QInputMethodEvent *ev)
{
    KJS::JSObject *qev = convertEvent(exec, (QEvent *)ev);

    qev->put(exec, "commitString", KJS::jsString(ev->commitString()));
    qev->put(exec, "preeditString", KJS::jsString(ev->preeditString()));
    qev->put(exec, "replacementLength", KJS::jsNumber(ev->replacementLength()));
    qev->put(exec, "replacementStart", KJS::jsNumber(ev->replacementStart()));
    return qev;
}
Exemplo n.º 8
0
KJS::JSObject *convertEvent(KJS::ExecState *exec, const QKeyEvent *ev)
{
    KJS::JSObject *kev = convertEvent(exec, (QInputEvent *)ev);

    kev->put(exec, "key", KJS::jsNumber(ev->key()));
    kev->put(exec, "text", KJS::jsString(ev->text()));
    kev->put(exec, "isAutoRepeat", KJS::jsBoolean(ev->isAutoRepeat()));
    kev->put(exec, "count", KJS::jsNumber(ev->count()));
    return kev;
}
Exemplo n.º 9
0
NodeFilter* toNodeFilter(KJS::JSValue* val)
{
    if (!val || !val->isObject())
        return 0;

    if (val->isObject(&JSNodeFilter::info))
        return static_cast<JSNodeFilter*>(val)->impl();

    KJS::JSObject* o = static_cast<KJS::JSObject*>(val);
    if (o->implementsCall())
        return new NodeFilter(new JSNodeFilterCondition(o));

    return 0;
}
Exemplo n.º 10
0
QMap<QString, QVariant> KJSEmbed::convertArrayToMap(KJS::ExecState *exec, KJS::JSValue *value)
{
    QMap<QString, QVariant> returnMap;
    KJS::JSObject *obj = value->toObject(exec);
    KJS::PropertyNameArray lst;
    obj->getPropertyNames(exec, lst);
    KJS::PropertyNameArrayIterator idx = lst.begin();
    for (; idx != lst.end(); idx++) {
        KJS::Identifier id = *idx;
        KJS::JSValue *val  = obj->get(exec, id);
        returnMap[toQString(id)] = convertToVariant(exec, val);
    }
    return returnMap;
}
Exemplo n.º 11
0
bool EventProxy::callHandler( QEvent *e )
{
// Be careful enabling this as if there are a lot of events then the event loop times
// out and the app crashes with 'Alarm Clock'.
//    qDebug("JSObjectEventProxy::callHandler() event type %d" , e->type() );

    KJS::ExecState *exec = m_interpreter->globalExec();
    KJS::Identifier id = JSEventMapper::mapper()->findEventHandler( e->type() );

    KJS::JSObject *jsobj(m_watch);
    KJS::JSObject *fun = jsobj->get(exec, id )->toObject( exec );

    KJS::JSValue *retValue;
    if ( !fun->implementsCall() )
    {
        QString msg = i18n( "Bad event handler: Object %1 Identifier %2 Method %3 Type: %4.",
          jsobj->className().ascii(),
          id.ascii(),
          fun->className().ascii(),
          e->type());
        retValue = throwError(exec, KJS::TypeError, msg);
    }
    else
    {
        // Process args
        KJS::List args;
        args.append( JSEventUtils::event(exec, e) );
        
        // Call handler
        retValue = fun->call( exec, jsobj, args );
    }
    
    if ( exec->hadException() ) 
    {
        if (m_interpreter->shouldPrintExceptions())
        {
            KJS::JSLock lock;
            KJS::JSObject* exceptObj = retValue->toObject(exec);
            QString message = toQString(exceptObj->toString(exec));
            QString sourceURL = toQString(exceptObj->get(exec, "sourceURL")->toString(exec));
            int sourceId = exceptObj->get(exec, "sourceId")->toUInt32(exec);
            int line = exceptObj->get(exec, "line")->toUInt32(exec);
            (*KJSEmbed::conerr()) << i18n("Exception calling '%1' function from %2:%3:%4", id.ascii(), !sourceURL.isEmpty() ? sourceURL : QString::number(sourceId), line, message) << endl;
        }
        
        
        // clear it so it doesn't stop other things
        exec->clearException();
        return false;
    }
  
    return true;
}
Exemplo n.º 12
0
KJS::JSValue *SlotProxy::callMethod(const QByteArray &methodName, void **_a)
{
#ifdef DEBUG_SLOTPROXY
    qDebug() << "SlotProxy::callMethod(" << methodName << ",_a) obj=" << this;
#endif
    KJS::ExecState *exec = m_interpreter->globalExec();
    exec->clearException();

    // Crash
    // KJS::Interpreter::globalExec()->context().thisValue()
    KJS::List args = convertArguments(exec, _a);
    KJS::Identifier id = KJS::Identifier(KJS::UString(methodName.data()));
    KJS::JSObject *fun = m_object->get(exec, id)->toObject(exec);
    KJS::JSValue *retValue;
    if (!fun->implementsCall()) {
#ifdef DEBUG_SLOTPROXY
        qDebug() << "SlotProxy::callMethod got bad handler";
#endif
        QString msg = i18n("Bad slot handler: Object %1 Identifier %2 Method %3 Signature: %4.",
                           m_object->className().ascii(),
                           id.ascii(),
                           methodName.data(),
                           QString(m_signature));

        retValue = throwError(exec, KJS::TypeError, msg);
    } else {
        retValue = fun->call(exec, m_object, args);
    }

    if (exec->hadException()) {
#ifdef DEBUG_SLOTPROXY
        qDebug() << "SlotProxy::callMethod had exception";
#endif
        if (m_interpreter->shouldPrintExceptions()) {
            KJS::JSLock lock;
            KJS::JSObject *exceptObj = exec->exception()->toObject(exec);//retValue->toObject(exec);
            QString message = toQString(exceptObj->toString(exec));
            QString sourceURL = toQString(exceptObj->get(exec, "sourceURL")->toString(exec));
            int sourceId = exceptObj->get(exec, "sourceId")->toUInt32(exec);
            // would include the line number, but it's always last line of file
            int line = exceptObj->get(exec, "line")->toUInt32(exec);
            (*KJSEmbed::conerr()) << i18n("Exception calling '%1' slot from %2:%3:%4", QString(methodName), !sourceURL.isEmpty() ? sourceURL : QString::number(sourceId), line, message) << endl;
        }

        // clear it so it doesn't stop other things
        exec->clearException();

        return KJS::jsNull();
    } else {
        if (retValue->type() == 1 || retValue->type() == 0) {
            return KJS::jsNull();
        }
    }
    return retValue;
}
Exemplo n.º 13
0
QVariant KJSEmbed::convertToVariant(KJS::ExecState *exec, KJS::JSValue *value)
{
#ifdef KJSEMBED_VARIANT_DEBUG
    qDebug() << "KJSEmbed::convertToVariant";
#endif

    QVariant returnValue;
    switch (value->type()) {
    case KJS::UndefinedType:
    case KJS::NullType:
        break;
    case KJS::StringType:
        returnValue = toQString(value->toString(exec));
        break;
    case KJS::NumberType:
        returnValue = value->toNumber(exec);
        break;
    case KJS::BooleanType:
        returnValue = value->toBoolean(exec);
        break;
    case KJS::ObjectType: {
        KJS::JSObject *obj = value->toObject(exec);
        //qDebug() << "Object type: " << toQString(obj->className());
        if (toQString(obj->className()) == "Array") {
            if (checkArray(exec, value) == List) {
                returnValue = convertArrayToList(exec, value);
            } else {
                returnValue = convertArrayToMap(exec, value);
            }
        } else if (toQString(obj->className()) == "Date") {
            returnValue = convertDateToDateTime(exec, value);
        } else {
            returnValue = extractVariant(exec, value);
        }
        //if( returnValue.isNull() ) returnValue = toQString(value->toString(exec));
    } break;
    default:
        returnValue = extractVariant(exec, value);
        //if( returnValue.isNull() ) returnValue = toQString(value->toString(exec));
        break;
    }
    return returnValue;
}
Exemplo n.º 14
0
KJS::JSValue *Engine::callMethod(  KJS::JSObject *parent,
                                   const KJS::UString &methodName, const KJS::List &args )
{
    KJS::ExecState *exec = dptr->m_interpreter->globalExec();

    KJS::Identifier id = KJS::Identifier( methodName);
    KJS::JSObject *fun = parent->get( exec, id )->toObject( exec );
    KJS::JSValue *retValue;

    if ( !fun->implementsCall() ) {
        QString msg = i18n( "%1 is not a function and cannot be called.", toQString(methodName) );
        return throwError( exec, KJS::TypeError, msg );
    }

    retValue = fun->call( exec, parent, args );

    if( exec->hadException() )
        return exec->exception();

    return retValue;
}
Exemplo n.º 15
0
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;
    }
}
Exemplo n.º 16
0
KJS::JSObject *convertEvent(KJS::ExecState *exec, const QDropEvent *ev)
{
    KJS::JSObject *dev = convertEvent(exec, (QEvent *)ev);
    dev->put(exec, "pos", convertToValue(exec, ev->pos()));
    dev->put(exec, "source", new QObjectBinding(exec, (QObject *)ev->source()));
    dev->put(exec, "dropAction", KJS::jsNumber((int)(ev->dropAction())));
    dev->put(exec, "keyboardModifiers", KJS::jsNumber((int)(ev->keyboardModifiers())));
    dev->put(exec, "possibleActions", KJS::jsNumber((int)(ev->possibleActions())));
    dev->put(exec, "proposedAction", KJS::jsNumber((int)(ev->proposedAction())));
    return dev;
}
Exemplo n.º 17
0
KJS::JSObject *convertEvent(KJS::ExecState *exec, const QWheelEvent *ev)
{
    KJS::JSObject *wev = convertEvent(exec, (QInputEvent *)ev);

    wev->put(exec, "delta", KJS::jsNumber(ev->delta()));
    wev->put(exec, "pos", convertToValue(exec, ev->pos()));
    wev->put(exec, "globalPos", convertToValue(exec, ev->globalPos()));
    wev->put(exec, "x", KJS::jsNumber(ev->x()));
    wev->put(exec, "y", KJS::jsNumber(ev->y()));
    wev->put(exec, "globalX", KJS::jsNumber(ev->globalX()));
    wev->put(exec, "globalY", KJS::jsNumber(ev->globalY()));
    wev->put(exec, "orientation", KJS::jsNumber(ev->orientation()));
    wev->put(exec, "buttons", KJS::jsNumber(ev->buttons()));

    return wev;
}
Exemplo n.º 18
0
QModelIndex KJSObjectModel::index(int row, int column, const QModelIndex &parent ) const
{
    KJS::JSObject *parentInstance = 0;
    Node *childItem = 0;
    KJS::ExecState *exec = m_js->globalExec();

    if (!parent.isValid())
    {
        if (m_root)
            parentInstance = m_root;
        else
            return QModelIndex();
    }
    else
        parentInstance = static_cast<Node*>(parent.internalPointer())->instance;
    int idx = 0;
    KJS::PropertyNameArray props;
    parentInstance->getPropertyNames(exec, props);
    for( KJS::PropertyNameArrayIterator ref = props.begin(); ref != props.end(); ref++)
    {
        if( idx == row)
        {
                childItem = new Node;
                childItem->name = ref->ascii(); //### M.O.: this is wrong, can be unicode.
                childItem->instance = parentInstance->get( exec,
                        childItem->name.constData() )->toObject(exec);
                childItem->parent = static_cast<Node*>(parent.internalPointer());
                break;
        }
        ++idx;
    }
    if (childItem)
        return createIndex(row, column, childItem);

    return QModelIndex();
}
Exemplo n.º 19
0
KJS::JSObject *convertEvent(KJS::ExecState *exec, const QContextMenuEvent *ev)
{
    KJS::JSObject *cxev = convertEvent(exec, (QInputEvent *)ev);
    cxev->put(exec, "x", KJS::jsNumber(ev->x()));
    cxev->put(exec, "y", KJS::jsNumber(ev->y()));
    cxev->put(exec, "globalX", KJS::jsNumber(ev->globalX()));
    cxev->put(exec, "globalY", KJS::jsNumber(ev->globalY()));
    cxev->put(exec, "pos", convertToValue(exec, ev->pos()));
    cxev->put(exec, "globalPos", convertToValue(exec, ev->globalPos()));
    cxev->put(exec, "reason", KJS::jsNumber((int)ev->reason()));
    return cxev;
}
Exemplo n.º 20
0
KJS::JSObject *convertEvent(KJS::ExecState *exec, const QMouseEvent *ev)
{
    KJS::JSObject *mev = convertEvent(exec, (QInputEvent *)ev);

    mev->put(exec, "pos", convertToValue(exec, ev->pos()));
    mev->put(exec, "x", KJS::jsNumber(ev->x()));
    mev->put(exec, "y", KJS::jsNumber(ev->y()));

    mev->put(exec, "globalPos", convertToValue(exec, ev->globalPos()));
    mev->put(exec, "globalX", KJS::jsNumber(ev->globalX()));
    mev->put(exec, "globalY", KJS::jsNumber(ev->globalY()));

    mev->put(exec, "button", KJS::jsNumber(ev->button()));
    mev->put(exec, "buttons", KJS::jsNumber(ev->buttons()));

    return mev;
}
Exemplo n.º 21
0
QDateTime convertDateToDateTime(KJS::ExecState *exec, KJS::JSValue *value)
{
    KJS::List args;
    QDateTime returnDateTime;
    KJS::JSObject *obj = value->toObject(exec);
    if (toQString(obj->className()) == "Date") {
        int seconds = int(obj->get(exec, KJS::Identifier("getSeconds"))->toObject(exec)->call(exec, obj, args)->toInteger(exec));
        int minutes = int(obj->get(exec, KJS::Identifier("getMinutes"))->toObject(exec)->call(exec, obj, args)->toInteger(exec));
        int hours = int(obj->get(exec, KJS::Identifier("getHours"))->toObject(exec)->call(exec, obj, args)->toInteger(exec));
        int month = int(obj->get(exec, KJS::Identifier("getMonth"))->toObject(exec)->call(exec, obj, args)->toInteger(exec));
        int day = int(obj->get(exec, KJS::Identifier("getDate"))->toObject(exec)->call(exec, obj, args)->toInteger(exec));
        int year = int(obj->get(exec, KJS::Identifier("getFullYear"))->toObject(exec)->call(exec, obj, args)->toInteger(exec));
        returnDateTime.setDate(QDate(year, month + 1, day));
        returnDateTime.setTime(QTime(hours, minutes, seconds));
    } else {
        // Throw error
    }

    return returnDateTime;
}
Exemplo n.º 22
0
KJS::JSValue *KJSEmbed::convertToValue(KJS::ExecState *exec, const QVariant &value)
{
#ifdef KJSEMBED_VARIANT_DEBUG
    qDebug() << "KJSEmbed::convertToValue typeid=" << value.type() << "typename=" << value.typeName() << "toString=" << value.toString();
#endif

    KJS::JSValue *returnValue;
    switch (value.type()) {
    case QVariant::Invalid:
        returnValue = KJS::jsNull();
        break;
    case QVariant::Int:
        returnValue = KJS::jsNumber(value.value<int>());
        break;
    case QVariant::UInt:
        returnValue = KJS::jsNumber(value.value<unsigned int>());
        break;
    case QVariant::LongLong:
        returnValue = KJS::jsNumber(value.value<qlonglong>());
        break;
    case QVariant::ULongLong:
        returnValue = KJS::jsNumber(value.value<qulonglong>());
        break;
    case QVariant::Double:
        returnValue = KJS::jsNumber(value.value<double>());
        break;
    case QVariant::Bool:
        returnValue = KJS::jsBoolean(value.value<bool>());
        break;
    case QVariant::ByteArray:
        returnValue = KJS::jsString(QString(value.value<QByteArray>()));
        break;
    case QVariant::String:
        returnValue = KJS::jsString(value.value<QString>());
        break;
    case QVariant::StringList: {
        KJS::List items;
        QStringList lst = value.value<QStringList>();
        QStringList::Iterator idx = lst.begin();
        for (; idx != lst.end(); ++idx) {
            items.append(KJS::jsString((*idx)));
        }
        returnValue = exec->lexicalInterpreter()->builtinArray()->construct(exec, items);
        break;
    }
    case QVariant::Date: // fall through
    case QVariant::DateTime: // fall through
    case QVariant::Time: {
        QDateTime dt = QDateTime::currentDateTime();
        if (value.type() == QVariant::Date) {
            dt.setDate(value.toDate());
        } else if (value.type() == QVariant::Time) {
            dt.setTime(value.toTime());
        } else {
            dt = value.toDateTime();
        }

        KJS::List items;
        items.append(KJS::jsNumber(dt.date().year()));
        items.append(KJS::jsNumber(dt.date().month() - 1));
        items.append(KJS::jsNumber(dt.date().day()));
        items.append(KJS::jsNumber(dt.time().hour()));
        items.append(KJS::jsNumber(dt.time().minute()));
        items.append(KJS::jsNumber(dt.time().second()));
        items.append(KJS::jsNumber(dt.time().msec()));
        returnValue = exec->lexicalInterpreter()->builtinDate()->construct(exec, items);
        break;
    }
    case QVariant::List: {
        KJS::List items;
        QList<QVariant> lst = value.toList();
        foreach (const QVariant &item, lst) {
            items.append(convertToValue(exec, item));
        }
        returnValue = exec->lexicalInterpreter()->builtinArray()->construct(exec, items);
        break;
    }
    case QVariant::Map: {
        QMap<QString, QVariant> map = value.toMap();
        QMap<QString, QVariant>::Iterator idx = map.begin();
        KJS::JSObject *obj = exec->lexicalInterpreter()->builtinObject()->construct(exec, KJS::List());
        for (; idx != map.end(); ++idx) {
            obj->put(exec, KJS::Identifier(toUString(idx.key())), convertToValue(exec,  idx.value()));
        }
        returnValue =  obj;
        break;
    }
    default: {
        if (value.canConvert< QWidget * >()) {
            QWidget *widget = qvariant_cast< QWidget * >(value);
            returnValue = widget ? createQObject(exec, widget, KJSEmbed::ObjectBinding::CPPOwned) : KJS::jsNull();
        } else if (value.canConvert< QObject * >()) {
            QObject *object = qvariant_cast< QObject * >(value);
            returnValue = object ? createQObject(exec, object, KJSEmbed::ObjectBinding::CPPOwned) : KJS::jsNull();
        } else {
            returnValue = createVariant(exec, value.typeName(), value);
            if (returnValue->isNull()) {
                returnValue = KJS::jsString(value.value<QString>());
            }
        }
        break;
    }
    }
Exemplo n.º 23
0
KJS::JSObject *convertEvent(KJS::ExecState *exec, const QDragMoveEvent *ev)
{
    KJS::JSObject *mev = convertEvent(exec, (QDropEvent *)ev);
    mev->put(exec, "answerRect", convertToValue(exec, ev->answerRect()));
    return mev;
}
Exemplo n.º 24
0
KJS::JSObject *convertEvent(KJS::ExecState *exec, const QTimerEvent *ev)
{
    KJS::JSObject *dev = convertEvent(exec, (QEvent *)ev);
    dev->put(exec, "timerId", KJS::jsNumber(ev->timerId()));
    return dev;
}
Exemplo n.º 25
0
int main( int argc, char **argv )
{
    QTime time;
    time.start();

#ifdef _WIN32
#   ifdef CONSOLEIO
    RedirectIOToConsole();
#   endif
#endif

    // Handle arguments
    QString appName = argv[0];
    QStringList args;
    for (int i = 1; i < argc; i++ )
    {
        args << argv[i];
    }

    QString script;
    KJS::List scriptArgs;
    bool gui = true;
#ifndef QT_ONLY
    /*
    #ifdef __GNUC__
        #warning "KDE Support enabled"
    #endif
    */
    bool kde = true;
#else
    /*
    #ifdef __GNUC__
        #warning "KDE Support disabled"
    #endif
    */
#endif

    if (argc > 1)
    {
        while (!args.isEmpty())
        {
            QString arg = args.takeFirst();
            if (arg.contains('-'))
            {
                if ((arg == "--version") || (arg == "-v"))
		{
		    printf("Qt: %s\n", qVersion()); 
#ifndef QT_ONLY
		    printf("KDE: %s\n", KDE_VERSION_STRING); 
#endif
		     return 0;
		}
                if ((arg == "--exec") || (arg == "-e"))
		{
                    gui = false;
		}
                else if ((arg == "--interactive") || (arg == "-i"))
                    (*KJSEmbed::conout()) << "Interactive";
#ifndef QT_ONLY
                else if ((arg == "-n") || (arg == "--no-kde"))
		{
		    kde = false;
		}
#endif
		else
                {
                    printUsage(appName);
                    return 0;
                }
            }
            else
            {
                if (!script.isEmpty())
                    scriptArgs.append(KJS::jsString(arg));
                else
                    script = arg;
            }
        }
    }
    else
    {
        printUsage(appName);
        return 0;
    }

    // Setup QApplication
    QCoreApplication *app;

#ifndef QT_ONLY
    if (kde)
    {
        KAboutData aboutData( "kjscmd", 0, ki18n("KJSCmd"), "0.2",
            ki18n(""
            "Utility for running KJSEmbed scripts \n" ),
            KAboutData::License_LGPL,
            ki18n("(C) 2005-2006 The KJSEmbed Authors") );

        KCmdLineOptions options;
        options.add("e", ki18n("Execute script without gui support"));
        options.add("exec", ki18n("Execute script without gui support"));
        options.add("i", ki18n("start interactive kjs interpreter"));
        options.add("interactive", ki18n("start interactive kjs interpreter"));
        options.add("n", ki18n("start without KDE KApplication support."));
        options.add("no-kde", ki18n("start without KDE KApplication support."));
        options.add("!+command", ki18n("Script to execute"));

        KCmdLineArgs::addCmdLineOptions( options ); // Add our own options.
        KCmdLineArgs::init( argc, argv, &aboutData );

        app = new KApplication(gui);
    }
    else
#endif
    if (gui)
    {
	qDebug("no KDE");
        app = new QApplication( argc, argv );
        dynamic_cast<QApplication*>(app)->connect( app, SIGNAL(lastWindowClosed()), SLOT(quit()) );
    }
    else
    {
	qDebug("no GUI");
        app = new QCoreApplication(argc, argv);
    }
    qDebug(" New %s %dms", app->metaObject()->className(), time.elapsed());

    app->setApplicationName( appName );
    
    // Setup Interpreter
    time.restart();
    Engine kernel;
    qDebug(" New engine %dms", time.elapsed());
    time.restart();

    KJS::Interpreter *js = kernel.interpreter();
    js->setShouldPrintExceptions(true);
    KJS::ExecState *exec = js->globalExec();

    // Publish bindings
    KJS::JSObject *appObject = kernel.addObject( app, "Application" );
    KJS::JSObject *argObject = js->builtinArray()->construct( exec, scriptArgs );
    appObject->put( exec, "args", argObject );
    Engine::ExitStatus result = Engine::Failure;

    if (!script.isEmpty())
    {
        result = kernel.runFile(toUString(script));
    }
    else // exec shell
    {
        result = kernel.runFile( ":/console.js" );
    }

    if ( result != Engine::Success )
    {
        KJS::Completion jsres = kernel.completion();
       (*KJSEmbed::conerr()) << toQString(jsres.value()->toString(exec)) << endl;
    }
    return (int)result;
}
Exemplo n.º 26
0
// from khtml/ecma/debugger/debugwindow.cpp
static QString exceptionToString(KJS::ExecState* exec, KJS::JSValue* exceptionObj)
{
    QString exceptionMsg = valueToString(exceptionObj);

    // Since we purposefully bypass toString, we need to figure out
    // string serialization ourselves.
    //### might be easier to export class info for ErrorInstance ---

    KJS::JSObject* valueObj = exceptionObj->getObject();
    KJS::JSValue*  protoObj = valueObj ? valueObj->prototype() : 0;

    bool exception   = false;
    bool syntaxError = false;
    if (protoObj == exec->lexicalInterpreter()->builtinSyntaxErrorPrototype())
    {
        exception   = true;
        syntaxError = true;
    }

    if (protoObj == exec->lexicalInterpreter()->builtinErrorPrototype()          ||
        protoObj == exec->lexicalInterpreter()->builtinEvalErrorPrototype()      ||
        protoObj == exec->lexicalInterpreter()->builtinReferenceErrorPrototype() ||
        protoObj == exec->lexicalInterpreter()->builtinRangeErrorPrototype()     ||
        protoObj == exec->lexicalInterpreter()->builtinTypeErrorPrototype()      ||
        protoObj == exec->lexicalInterpreter()->builtinURIErrorPrototype())
    {
        exception = true;
    }

    if (!exception)
        return exceptionMsg;

    // Clear exceptions temporarily so we can get/call a few things.
    // We memorize the old exception first, of course. Note that
    // This is not always the same as exceptionObj since we may be
    //  asked to translate a non-active exception
    KJS::JSValue* oldExceptionObj = exec->exception();
    exec->clearException();

    // We want to serialize the syntax errors ourselves, to provide the line number.
    // The URL is in "sourceURL" and the line is in "line"
    // ### TODO: Perhaps we want to use 'sourceId' in case of eval contexts.
    if (syntaxError)
    {
        KJS::JSValue* lineValue = valueObj->get(exec, "line");
        KJS::JSValue* urlValue  = valueObj->get(exec, "sourceURL");

        int      line = lineValue->toNumber(exec);
        QString  url  = urlValue->toString(exec).qstring();
        exceptionMsg = i18n("Parse error at %1 line %2",
                            url, line + 1);
    }
    else
    {
        // ### it's still not 100% safe to call toString here, even on
        // native exception objects, since someone might have changed the toString property
        // of the exception prototype, but I'll punt on this case for now.
        exceptionMsg = exceptionObj->toString(exec).qstring();
    }
    exec->setException(oldExceptionObj);
    return exceptionMsg;
}
Exemplo n.º 27
0
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;
    }
Exemplo n.º 28
0
KJS::JSObject *convertEvent(KJS::ExecState *exec, const QInputEvent *ev)
{
    KJS::JSObject *iev = convertEvent(exec, (QEvent *)ev);
    iev->put(exec, "modifiers", KJS::jsNumber(ev->modifiers()));
    return iev;
}