Ejemplo n.º 1
0
/*!
  Adds the Qt Script function \a qtscriptFunction (fully qualified)
  as a transient signal handler for the C++ signal \a signal of the
  object \a sender.

  Example:
  \code
  interpreter->addTransientSignalHandler( myButton, SIGNAL( clicked() ), "classA.obj.calculate" );
  \endcode

  \sa removeTransientSignalHandler()
*/
void QSInterpreter::addTransientSignalHandler(QObject *sender,
                                              const char *signal,
                                              const char *qtscriptFunction)
{
  QuickInterpreter *ip = interpreter();
  QString func = QString::fromLatin1(qtscriptFunction);
  func.left(func.find('('));
  QSObject senderObj = ip->wrap(sender);

  QSObject obj = ip->object(func);
  if (!obj.isFunction()) {
    qDebug("QSInterpreter::addTransientSignalHandler(): '%s' not a function",
           qtscriptFunction);
    return;
  }

  QSObject base = QSFuncRefClass::refBase(obj);
  QSMember member = QSFuncRefClass::refMember(obj);
  QSWrapperShared *sh = ip->wrapperClass()->shared(&senderObj);
  if (!sh->setEventHandler(ip, QString::fromLatin1(signal + 1), 0, member.name(), base)) {
#if defined( QT_CHECK_STATE )
    qWarning("QSInterpreter::addTransientSignalHandler(), "
             "failed to add signal handler: '%s' to '%s'", signal + 1, qtscriptFunction);
#endif
  }
}
Ejemplo n.º 2
0
ScopeChain QSFuncRefClass::refScope( const QSObject &ref )
{
    if( ref.isFunction() ) {
	return ( (QSReferenceData*) ref.shVal() )->context;
    }
    qWarning( "QSFuncRefClass::refScope() - not a reference" );
    return ScopeChain();
}
Ejemplo n.º 3
0
QSObject QSFuncRefClass::refBase( const QSObject &ref )
{
    if( ref.isFunction() ) {
	return ( (QSReferenceData*) ref.shVal() )->base;
    }
    qWarning( "QSFuncRefClass::refBase() - not a reference" );
    return ref.env()->createUndefined();
}
Ejemplo n.º 4
0
QSMember QSFuncRefClass::refMember( const QSObject &ref )
{
    if( ref.isFunction() ) {
	return ( (QSReferenceData*) ref.shVal() )->member;
    }
    qWarning( "QSFuncRefClass::refMember() - not a reference" );
    return QSMember();
}
Ejemplo n.º 5
0
bool QSEngineImp::call( QSObject *scope, const QString &func,
                        const QSList &args )
{
    init();
    QSObject t;
    if ( !scope || !scope->isValid() ) {
        t = env()->globalObject();
        scope = &t;
    }

    QSObject v = scope->getQualified(func);
    Q_ASSERT(v.isValid());

    if ( !v.isDefined() ) {
        if (func != QString::fromLatin1("main")) {
            errType = ReferenceError;
            errMsgs.append( QString::fromLatin1("Unknown function: ") + func );
            errLines.append( 0 );
        }
        return false;
    }

    if ( !v.isFunction() ) {
        errType = TypeError;
        errMsgs.append( func + QString::fromLatin1(" is not a function. Call failed.") );
        errLines.append( 0 );
        return false;
    }
    QSObject res = v.invoke( QSMember(), args );

    if ( env()->isExceptionMode() ) {
        QSObject err = env()->exception();
        errType = 99; /* TODO */
        errLines.append(QSErrorClass::errorLine(&err));
        errMsgs.append(QSErrorClass::errorName(&err) + QString::fromLatin1(". ") + QSErrorClass::errorMessage(&err));
#ifdef QSDEBUGGER
        if (dbg)
            dbg->setSourceId(QSErrorClass::errorSourceId(&err));
#endif
        env()->clearException();
        return false;
    } else {
        errType = 0;
        errLines.clear();
        errMsgs.clear();

        // return value
        retVal = res;
        return true;
    }
}
Ejemplo n.º 6
0
void QuickInterpreter::timerEvent(QTimerEvent *e)
{
    int id = e->timerId();
    QSObject function = m_timers[id];

    Q_ASSERT(function.isValid());
    Q_ASSERT(function.isFunction());

    QSList arguments;
    arguments.append(env()->createNumber(id));

    QSObject func_base = QSFuncRefClass::refBase(function);
    QString func_name = QSFuncRefClass::refMember(function).name();

   call(func_base, func_name, arguments);
}
Ejemplo n.º 7
0
/*******************************************************************************
 * Timers
 */
QSObject qsStartTimer( QSEnv *env )
{
    QSObject hnd = env->arg( 1 );
    if ( hnd.isFunction() ) {
        QuickInterpreter *ip = QuickInterpreter::fromEnv(env);

        int interval = (int) env->arg(0).toNumber();
        int id = ip->startTimer(interval);

        QHash<int, QSObject> *timers = ip->timers();
        timers->insert(id, hnd);

        return env->createNumber(id);
    }

    QString msg = QString::fromLatin1("Can only install functions as event handler");
    return env->throwError( TypeError, msg );
}