/*! Calls the function \a function with the given \a arguments. The arguments are first converted into Qt Script datatypes. Functions which were passed to evaluate() in previous calls or which are defined in the current project, can be called from this function. If \a context is 0 (the default), the function is called in the global scope. If a \a context is given, the function is called in the scope of that object. Interpreters that belong to a project are subject to re-evaluation, since the code which has been passed previously into evaluate() gets lost when calling one of these functions. This happens when the project or the scripts in it are modified. */ QSArgument QSInterpreter::call(const QString &function, const QSArgumentList &arguments, QObject *context) { #if defined (QT_THREAD_SUPPORT) && QT_VERSION >= 0x030300 if (context && context->inherits("QWidget") && qt_get_application_thread_id() != QThread::currentThread()) { qWarning("QSInterpreter::evaluate(), GUI object (%s [%s]) not allowed in non GUI thread", context->name(), context->className()); return QSArgument(); } #endif running = TRUE; startInterpreter(); if (function.isEmpty()) { #if defined QT_RANGE_CHECK qWarning("QSInterpreter::call(), function name is empty"); #endif return QSArgument(); } QSArgument v = d->interpreter->call(context, function, arguments); running = FALSE; stopInterpreter(); return v; }
bool QuickDispatchObjectFactory::createInstance(const QString &className, const QValueList<QVariant> &args, QPtrVector<QObject> *result) { if (!d->objectsCache.contains(className)) return FALSE; QSArgumentList qsArgs; for (QValueList<QVariant>::ConstIterator it = args.begin(); it != args.end(); ++it) { if ((*it).type() == QVariant::String) { static const int length_of_Pointer = 7; static const QString pointer_header = QString::fromLatin1("Pointer"); QString s = (*it).toString(); if (s.left(length_of_Pointer) == pointer_header) { QStringList l = QStringList::split(':', s); if (l.count() == 3) { if (l[2] != QString::fromLatin1("QObject")) { ulong lng = l[1].toULong(); void *ptr = (void *)lng; qsArgs.append(QSArgument(ptr)); } else { ulong lng = l[1].toULong(); QObject *o = (QObject *)lng; qsArgs.append(QSArgument(o)); } continue; } } } qsArgs.append(QSArgument(*it)); } QSObjectFactory *factory = d->objectsCache[ className ]; QObject *ctx = 0; QSObject obj = interpreter()->env()->currentScope(); if (obj.isA(interpreter()->wrapperClass())) ctx = interpreter()->wrapperClass()->shared(&obj)->objects[0]; QObject *o = factory->create(className, qsArgs, ctx); if (o) { addObject(o, result); return TRUE; } return FALSE; }
QSArgument QuickInterpreter::convertToArgument( const QSObject &o ) { if( !o.isValid() ) return QSArgument(); const QSClass *cl = o.objectType(); if( cl->name() == QString::fromLatin1("QObject") ) { QSWrapperShared *shared = (QSWrapperShared*) o.shVal(); if (shared->objects.isEmpty()) return QSArgument(); return QSArgument( shared->objects[0] ); } else if( cl == ptrClass ) { Q_ASSERT( ptrClass->pointer( &o ) ); return QSArgument( ptrClass->pointer( &o ) ); } else { return QSArgument( o.toVariant( QVariant::Invalid ) ); } }
/*! Returns the value of the \a variableName in the scope of \a context. If the \a context is 0 (the default) the global scope is used. The variable name can be fully qualified in the form: \code a.b.c \endcode */ QSArgument QSInterpreter::variable(const QString &variableName, QObject *context) const { if (variableName.isEmpty()) { #if defined( QT_CHECK_STATE ) qWarning("QSInterpreter::variable(): variable name is empty"); #endif return QSArgument(); } return d->interpreter->variable(context, variableName); }
/*! Executes the string of Qt Script in \a code and returns any value produced by that \a code. This function executes the code passed in as \a code. The code can use and reference code (functions, classes, variables, etc.) which have been passed to this function previously or which are defined in the current project, if present. Also, application objects which have been added via addObject() can be accessed. If \a context is 0 (the default), the code is executed as global code. If a \a context is given, the code is executed in the context of that object. Interpreters that belong to a project are subject to re-evaluation, since the code which has been passed previously into evaluate() gets lost when calling one of these functions. This happens when the project or the scripts in it are modified. \a scriptName is used for error reporting and debugging. */ QSArgument QSInterpreter::evaluate(const QString &code, QObject *context, const QString &scriptName) { #if defined (QT_THREAD_SUPPORT) && QT_VERSION >= 0x030300 if (context && context->inherits("QWidget") && qt_get_application_thread_id() != QThread::currentThread()) { qWarning("QSInterpreter::evaluate(), GUI object %s [%s] not allowed in non GUI thread", context->name(), context->className()); return QSArgument(); } #endif running = TRUE; startInterpreter(); QSArgument v = d->interpreter->execute(context, code, scriptName); running = FALSE; stopInterpreter(); return v; }