How to compile script in QScriptEngine without calling evaluate function
Hi ,
I am using QscriptEngine to provide scripting in my application. QScriptEngine will compile javascript script while evaluation. it works. but i need to compile the script, save it and evaluate after sometime. Compilation is mandatory before evaluation because when i evaluate script my application is in running state and any error from script is not acceptable. How can i achieve this?
Re: How to compile script in QScriptEngine without calling evaluate function
Use the QScriptProgram.
http://doc.qt.io/qt-4.8/QScriptProgram
Quote:
QScriptProgram retains the compiled representation of the script if possible. Thus, QScriptProgram can be used to evaluate the same script multiple times more efficiently.
You can also use the QScriptEngine::checkSyntax() to make sure your code is well formatted. But you won't know if an error will happen in your code until you run simply because that's how JavaScript works. E.G.: if you write in JS "console.print(messageString);", it will compile even if there is no such thing as the "console". This is because the access to console actually compiles to a dictionary access on the global namespace just like "global["console"].methods["print"](messageString)".
- Maxime
Re: How to compile script in QScriptEngine without calling evaluate function
Quote:
Originally Posted by
sheetalw
Compilation is mandatory before evaluation because when i evaluate script my application is in running state and any error from script is not acceptable. How can i achieve this?
You can't. QScriptProgram won't help. And before you ask your next question -- no, Qt is not to blame, it's not possible to do what you require because of the nature of Javascript.
Re: How to compile script in QScriptEngine without calling evaluate function
thanks. actually not just javascript, scripts are meant to compile and run at the same time. and i am not going to blame Qt, I just tried, may be i could get some other tool, i am a beginner don't know much abt qt.
Re: How to compile script in QScriptEngine without calling evaluate function
I'm not talking about other languages, I'm talking about JavaScript. The same code can be valid now but be invalid next time you call it (or vice versa). There are no guarantees that can be made upfront. You can only do a syntax check which captures only the most trivial problems.