/*! 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; }
/*! Makes the QObject \a object available to the scripting engine. All child named objects of \a object are also made available, recursivly. Transient objects added to the interpreter are not persistent. This means that when the interpreter is cleared, or when a project is re-evaluated, the transient objects are removed. Use QSProject::addObject() to add persistent objects to the interpreter. Note on threading; If the interpreter is running in the non-GUI thread, \a object cannot be a QWidget subclass. \warning Every object passed to this function must have a unique name. If you want to reuse names then you need to call clear() first \sa QSProject::addObject() */ void QSInterpreter::addTransientObject(QObject *object) { #if defined (QT_THREAD_SUPPORT) && QT_VERSION >= 0x030300 if (object && object->inherits("QWidget") && qt_get_application_thread_id() != QThread::currentThread()) { qWarning("QSInterpreter::evaluate(), GUI object %s [%s] not allowed in non GUI thread", object->name(), object->className()); return; } #endif d->interpreter->addTopLevelObject(object); }
/*! 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; }
QSObject QuickInterpreter::wrap( QObject *o ) { if( !o ) { QSList list; return env()->nullClass()->construct( list ); } #if defined (QT_THREAD_SUPPORT) && QT_VERSION >= 0x030300 if (qt_get_application_thread_id() != QThread::currentThread() && o->inherits("QWidget")) { qWarning("QuickInterpreter::wrap(), GUI object (%s [%s]) cannot be used in non GUI thread", o->name(), o->className()); return QSObject(); } #endif QSUserData *udata = (QSUserData*) o->userData( userDataId() ); // set user data in QObject if it's not there, yet if( !udata ) { udata = new QSUserData( 0 ); o->setUserData( userDataId(), udata ); } QSWrapperShared *shared = udata->data(); const QSWrapperClass *cl; if ( shared ) { // wrapper is already there, reuse it cl = shared->wrapperClass(); } else { // create & remember wrapper cl = new QSWrapperClass( wrapperClass() ); shared = cl->createShared( o ); shared->setObjectType( QSWrapperShared::GlobalObject ); env()->registerShared( shared ); wrapperShared->append( shared ); } shared->setUserData( udata ); udata->setData( shared ); shared->ref(); // additional ref by either QObject or QSObject QSObject obj( cl ); obj.setVal( shared ); // no ownership needs to be transferred anymore return obj; }
/*! \internal */ void QSInterpreter::runtimeError(const QString &message, const QString &scriptName, int lineNumber) { #ifdef QSDEBUGGER emit error(message, scriptName, lineNumber); QObject *ctx = d->interpreter->objectOfSourceId(d->interpreter->debuggerEngine()->sourceId()); emit error(message, ctx, scriptName, lineNumber); if (errorMode() == Notify) { if (qApp->type() == QApplication::Tty #if defined (QT_THREAD_SUPPORT) && QT_VERSION >= 0x030300 || qt_get_application_thread_id() != QThread::currentThread() #endif ) { qDebug("Error in script: '%s', line: %d\n %s\n", scriptName.latin1(), lineNumber, message.latin1()); } else { QMessageBox::critical(qApp->mainWidget(), QString::fromLatin1("Error"), QString::fromLatin1("The following error occurred in " "line <b>%1</b> of <b>%2</b> while executing " "the script:<pre><font color=red>%3</font></pre>") .arg(lineNumber).arg(scriptName).arg(message)); } } else if (errorMode() == AskForDebug) { // TODO: Add here code to debug the runtimeError... qDebug("Error in script: '%s', line: %d\n %s\n", scriptName.latin1(), lineNumber, message.latin1()); } #else QMessageBox::critical(qApp->mainWidget(), QString::fromLatin1("Error"), QString::fromLatin1("The following error occurred in " "line <b>%1</b> of <b>%2</b> while executing " "the script:<pre><font color=red>%3</font></pre>") .arg(lineNumber).arg(scriptName).arg(message)); #endif }