PDA

View Full Version : How to interrupt QScriptValu::call()



QtIsCute
20th July 2012, 16:49
Hello,

I have an application, in which a thread is executing a qscript function



ExecuteThread::ExecuteThread(QString code, QObject *parent) :
QThread(parent)
{
this->code = code;
this->result = 0;
ExecuteEngine = new QScriptEngine();
}

void ExecuteThread::run(){
ExecuteEngine = new QScriptEngine();
QScriptValue fun = ExecuteEngine->evaluate("(" + code + ")");
QScriptValue val = fun.call();
result = val.toInteger();
delete ExecuteEngine;
}

int ExecuteThread::getResult(){
return result;
}

void ExecuteThread::stop(){
ExecuteEngine->abortEvaluation(QScriptValue(-100));
exit(0);
}


Everything works fine, until there is an infinte loop in the QScript-function, then the thread stops at fun.call() and I can't delete the thread any more neither after calling stop() nor calling terminate(). Could you please tell me what I have to do in this case?

wysota
20th July 2012, 16:53
Search the forum, please. There has been a similar discussion few months ago.

QtIsCute
20th July 2012, 16:57
Yes I know, I have seen it and it worked, but I wasn't able to get my return statement by the method they used in this thread: http://www.qtcentre.org/threads/50105-How-to-interrupt-QScriptValu-call()?p=225108

QtIsCute
21st July 2012, 10:19
I meant this discussion of course: http://www.qtcentre.org/threads/47692-Is-there-any-way-to-abort-QScriptValue-call()

Please help me

wysota
21st July 2012, 11:00
If I knew another method, I would have suggested it in that thread. And I don't quite understand what you mean by that you couldn't get your return statement.

QtIsCute
21st July 2012, 11:14
When I have for example the following qscript function:


function(var xy){
if(xy == 15){
xy = xy*2;
}
var xx = xy*3;
return xx;
}

I can get the returned value by


QScriptValue fun = ExecuteEngine->evaluate("(" + code + ")");
QScriptValue val = fun.call();
result = val.toInteger();

But how can I do it without using the call function and is that possible at all?

wysota
21st July 2012, 11:23
My suggestion from the other thread was not using call(). If you use evaluate(), it returns the result of the code it evaluates. Thus if you evaluate a function, it will return a result of that function.

QtIsCute
21st July 2012, 11:41
Why won't it work then, if I use this code? result is 0 instead of 115



ExecuteEngine = new QScriptEngine();
QScriptContext *context = ExecuteEngine->pushContext();
QScriptValue v = context->activationObject();
v.setProperty("fun", "fun(){return 115;}");
result = ExecuteEngine->evaluate("fun()").toInteger();
ExecuteEngine->popContext();
delete ExecuteEngine;

wysota
21st July 2012, 11:51
This is what your code returns:


"TypeError: Result of expression 'fun' [fun(){return 115;}] is not a function."

so your function code is plain wrong :)

Here is the proper code:


#include <QtGui>
#include <QtScript>

int main(int argc, char **argv) {
QApplication app(argc, argv);
QScriptEngine engine;
QScriptContext *context = engine.pushContext();
QScriptValue v = context->activationObject();
v.setProperty("fun", engine.evaluate("(function(){return 115;})"));
QScriptValue result = engine.evaluate("fun()");
engine.popContext();
qDebug() << result.toString();
return 0;
}

QtIsCute
21st July 2012, 12:05
Ah now I understand and it's working too.

Thanks a lot :)