How to interrupt QScriptValu::call()
Hello,
I have an application, in which a thread is executing a qscript function
Code:
{
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?
Re: How to interrupt QScriptValu::call()
Search the forum, please. There has been a similar discussion few months ago.
Re: How to interrupt QScriptValu::call()
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/5010...all()?p=225108
Re: How to interrupt QScriptValu::call()
Re: How to interrupt QScriptValu::call()
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.
Re: How to interrupt QScriptValu::call()
When I have for example the following qscript function:
Code:
function(var xy){
if(xy == 15){
xy = xy*2;
}
var xx = xy*3;
return xx;
}
I can get the returned value by
Code:
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?
Re: How to interrupt QScriptValu::call()
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.
Re: How to interrupt QScriptValu::call()
Why won't it work then, if I use this code? result is 0 instead of 115
Code:
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;
Re: How to interrupt QScriptValu::call()
This is what your code returns:
Quote:
"TypeError: Result of expression 'fun' [fun(){return 115;}] is not a function."
so your function code is plain wrong :)
Here is the proper code:
Code:
#include <QtGui>
#include <QtScript>
int main(int argc, char **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;
}
Re: How to interrupt QScriptValu::call()
Ah now I understand and it's working too.
Thanks a lot :)