PDA

View Full Version : QSriptEngine and more than one script



lukass
29th February 2012, 19:13
I'm creating program with loadable scripts. User should be able to load several scripts from files. But there is a problem. Can I use different instances of QScriptEngine per script or maybe is better way for this?

wysota
29th February 2012, 20:14
Yes, you can. However it's probably better to only have one engine.

lukass
1st March 2012, 09:51
Yes, you can. However it's probably better to only have one engine.
but how can I obtain clean environment for each script file? By QScriptEngine::pushContext() ? And what happens if I attach QScriptEngineDebugger to that one engine?

wysota
1st March 2012, 10:54
but how can I obtain clean environment for each script file? By QScriptEngine::pushContext() ?
Yes, that's one way of doing that.


And what happens if I attach QScriptEngineDebugger to that one engine?
I don't understand your question. What do you mean what happens?

lukass
1st March 2012, 11:05
Yes, that's one way of doing that.
If I use pushContext() then how can I "unload" particular script?
And are there other ways? If so, how?


I don't understand your question. What do you mean what happens?
Now I also do not understand. :D I have to check it out.

wysota
1st March 2012, 11:36
If I use pushContext() then how can I "unload" particular script?
I don't understand what you mean.


And are there other ways? If so, how?
For example you could set a new global object for the engine.

Let's make something clear -- one script engine only allows one script to be ran at the same time. If you want to run more (long running) scripts concurrently, you need separate engines however doing that in a single thread doesn't make much sense. Doing that in different threads requires you to synchronize access to all data shared between those scripts and also between your main program flow (aka "GUI thread").

lukass
1st March 2012, 12:10
I don't understand what you mean.
For example I loaded three scripts by:

engine.evaluate(script1);
engine.pushContext();
engine.evaluate(script2);
engine.pushContext();
engine.evaluate(script3);
I can unload last script by engine.popContext() but how can I unload first script?

wysota
1st March 2012, 13:18
The code you pasted doesn't make sense. The following should be fine:


engine.pushContext();
engine.evaluate(script1);
engine.popContext();
engine.pushContext();
engine.evaluate(script2);
engine.popContext();
engine.pushContext();
engine.evaluate(script3);
engine.popContext();

You don't "unload" scripts. When a script has finished executing you don't have to do anything more with it.