PDA

View Full Version : Interpretting javascript using QScriptEngine



Saranakumardb
26th July 2008, 02:37
Hi there,

I'm trying to use use QScriptEngine to call a java script method.

Please refer below code, And I mentioned return values of each statement in comments.
Please correct me if my usage is wrong... My requirement is simple, I need to execute a method(which returns a string) present in javascript file and need to use returned string in my Qt program.

Thanks in advance...


int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QScriptEngine engine;
QScriptValue global = engine.globalObject();
QString str, str1, str2; int i=0;
QFile scriptFile("javascript.js");
// QFile scriptFile("New.html");

bool bRes = scriptFile.open(QIODevice::ReadOnly); // returns true
str = scriptFile.readAll(); // returns correct file content /script
bRes = engine.canEvaluate(str); // returns true
QScriptValue ctor = engine.evaluate(str);
scriptFile.close();
bRes = ctor.isError(); // returns false
str1 = ctor.toString(); // returns "undefined"
bRes = ctor.isString();// returns false

ctor = engine.evaluate("Hello");
bRes = ctor.isString(); // returns false
bRes = ctor.isValid();// returns true
bRes = ctor.isUndefined();// returns false
bRes = ctor.isError();// returns true
str2 = ctor.toString();// returns "ReferenceError: Hello is not defined"
i = ctor.toInt32(); // returns 0

return app.exec();

}

javascript.js:
function hello()
{
var testStr = "Hello";
return testStr;
}

Regards,
Saravana

wysota
26th July 2008, 08:53
Could you print the contents of "str" in your program? If you are under Windows, maybe you should open the file in text mode as well? Does evaluating "1+2" return 3 properly?

Saranakumardb
28th July 2008, 20:21
I added tex mode flag to file open,
bool bRes = scriptFile.open(QIODevice::ReadOnly|QIODevice::Tex t);
still facing same problem.
And please refer my answers to your questions inline.


Could you print the contents of "str" in your program?
>>>str = {"function hello()
{
var testStr = "Hello";
return testStr;
}" size=67}

If you are under Windows, maybe you should open the file in text mode as well?
>>> bool bRes = scriptFile.open(QIODevice::ReadOnly|QIODevice::Tex t);

Does evaluating "1+2" return 3 properly?
>>> returns correct value and works fine.
ctor = engine.evaluate("2+1");
bRes = ctor.isString();// returns false
bRes = ctor.isNumber();// returns true
bRes = ctor.isValid();// returns true
bRes = ctor.isUndefined();// returns false
bRes = ctor.isError();// returns false
i = ctor.toInt32();// returns 3, correct value

Thanks in advance...

Thanks,
Saravana.

Saranakumardb
28th July 2008, 20:29
Thanks a lot for your response.

Also I've tried directly assigning the code to "str" as follows,

QString str = QString("function hello(){var testStr=\"Hello\"; return testStr; }");

Please suggest me any other way you know to make it work.


Thanks,
Saravana.

wysota
28th July 2008, 23:48
What do isValid() and isObject() return?

Saranakumardb
29th July 2008, 14:05
I tried as per your suggestion, and IsObject() returns false for the pgm evaluation() but true for function evaluation.

Please refer code below and my comment inline.

Thanks in advance...



QFile scriptFile("javascript.js");

bool bRes = scriptFile.open(QIODevice::ReadOnly|QIODevice::Tex t);
str = scriptFile.readAll();
bRes = engine.canEvaluate(str);
QScriptValue ctor = engine.evaluate(str);
bRes = ctor.isValid(); // returns true
bRes = ctor.isObject(); // returns false

ctor = engine.evaluate("hello");
bRes = ctor.isValid(); // returns true
bRes = ctor.isObject(); // returns true



str = QString("function hello(){var testStr=\"Hello\"; return testStr; }");
bRes = engine.canEvaluate(str);
QScriptValue ctor = engine.evaluate(str);
bRes = ctor.isValid(); // returns true
bRes = ctor.isObject(); // returns false

ctor = engine.evaluate("hello");
bRes = ctor.isValid(); // returns true
bRes = ctor.isObject(); // returns true

Saranakumardb
30th July 2008, 16:45
Please suggest a way to overcome this issue. Hope some/one of you would faced and solved the problem already.

Thanks in advance.

wysota
31st July 2008, 08:42
I'm writing a script interpreter based application right now and I admit I'm not having such problems. Type conversion works fine and evaluation of scripts read from file works as expected as well. Your problem might be that the function is defined in script, so the type can't be cast to QString because it was never a variant based variable. I don't know if this is the case (and I doubt it), but it could be. Are you using the newest Qt release?

Saranakumardb
31st July 2008, 18:56
Hi, Thanks for your reply.

I use Qt 4.3.4...windows platform.

I tried below script... still facing the same problem. could you please check the script below for any error?


function hello()
{
var testStr = "Hello";
return 22;
}

could you please send me a working sample(java script + qt code) to my email muruga.db@gmail.com?
(windows version)

Thanks,
Saravanan.