Results 1 to 8 of 8

Thread: QtScript: how to reload current script?

  1. #1
    Join Date
    Oct 2009
    Posts
    36
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QtScript: how to reload current script?

    Hello.

    QScriptEngine has evaluate() method that can be used to load script, execute it and to run a specified function from already loaded script. But how to clear current script and load a new one? For example, i use evaluate() to load a script from file and then evaluate() to get a script functions and call them. But what can i do to clear current script and load a new one from a different file? Deleting and creating QScriptEngine seems like a solution, but it likes to be created in GUI thread (due to QScriptEngineDebugger) while all script operations are performed in separate thread. So is it any way to clear current script without re-creating QScriptEngine object?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QtScript: how to reload current script?

    There is no such thing as "current script". You pass a string to evaluate() so it's enough to pass a different one. Remember that QScriptEngine is not thread-safe! You have to create and use the script engine in the same thread.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2009
    Posts
    36
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QtScript: how to reload current script?

    There is no such thing as "current script". You pass a string to evaluate() so it's enough to pass a different one
    If next call to evaluate() will erase previous one, how following can work:

    Qt Code:
    1. evaluate( "function foo() { this.DoSomething(); }" );
    2. evaluate( "foo" );
    To copy to clipboard, switch view to plain text mode 

    Second call will return an object and evoking call() method on it will call "foo" function (with DoSomething() etc). How this can be possible if evaluate( "foo" ) erases previous call to evaluate() with function body?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QtScript: how to reload current script?

    Quote Originally Posted by eyeofhell View Post
    If next call to evaluate() will erase previous one, how following can work:

    Qt Code:
    1. evaluate( "function foo() { this.DoSomething(); }" );
    2. evaluate( "foo" );
    To copy to clipboard, switch view to plain text mode 
    It works because the first script makes changes to the script context present in the script engine.

    You can always push a new context prior to executing a script and then pop it back to make sure all changes made to the context by previous execution are removed. Just make sure you use activationObject() instead of globalObject() to modify the environment of the script.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2009
    Posts
    36
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QtScript: how to reload current script?

    It seems i'm a bit lost. "There is no such thing as 'current script'" and "first script makes changes to the script context present in the script engine" seems totally opposite to me O_O. What's the difference between "script context" and "current script"? Why evaluating "function foo() {}" will change script context but evaluating "foo" will not? Is it any rules available how evaluation changes script context?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QtScript: how to reload current script?

    Script context is just like a "stack frame" for regular computer applications. Functions and variables are local to the context (stack frame) they were declared in. When a function is executed, a new context is created for it and when it ends, the context is removed. So all local changes made inside a function will be gone when the function ends because the context of the function is removed. The context is not removed between evaluate() calls thus the top-level context remains the same and changes made to it are persistant. You need to explicitely push a context before calling evaluate() and pop it afterwards.

    See the example (not tested):
    Qt Code:
    1. QScriptEngine engine;
    2. QScriptContext *context = engine.context();
    3. context->activationObject().setProperty("x", 7);
    4. contet = engine.pushContext(); // new context
    5. context->activationObject().setProperty("y", 8);
    6. engine.evaluate("x+y");
    7. engine.popContext();
    8. engine.evaluate("x+y"); // "y" is undefined
    To copy to clipboard, switch view to plain text mode 

    The same happens if you call a function. But the function itself is defined in the context surrounding the function itself.

    If you wish to have a clean context for every evaluate() call, do this (indentation of the code below should help you understand):
    Qt Code:
    1. QScriptEngine engine;
    2. engine.pushContext();
    3. engine.evaluate("...");
    4. engine.popContext();
    5. engine.pushContext();
    6. engine.evaluate("...");
    7. engine.popContext();
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Oct 2009
    Posts
    36
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QtScript: how to reload current script?

    So, basically, it's a 'context' that accumulate functions and variables defined inside evaluate() and this context can be called "current script". Context does not change between evaluate, so following code:
    Qt Code:
    1. evaluate( "var a = 1;" );
    2. evaluate( "var b = 2;" );
    3. evaluate( "a + b" );
    To copy to clipboard, switch view to plain text mode 
    Will evaluate to "3". Am i correct?

    Also, what do you means by
    But the function itself is defined in the context surrounding the function itself
    Sorry for boring you with this stupid questions, but Qt documentation about scripting is a kind of limited and it's very important for me to know how it works .

    So, as i understand for now, my original task will be like following:
    1. push empty context
    2. evaluate script from file.
    3. As application executes, call script functions from time to time using evaluate( "function name" ).call()
    4. If another script must be loaded, pop context, push empty one and goto step 2.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QtScript: how to reload current script?

    Quote Originally Posted by eyeofhell View Post
    So, basically, it's a 'context' that accumulate functions and variables defined inside evaluate() and this context can be called "current script".
    No, it can't. The following uses three different contexts and that's just a single script:
    javascript Code:
    1. function x() { return 7; }
    2. function y() { return x()+5; }
    3. y();
    To copy to clipboard, switch view to plain text mode 

    Context does not change between evaluate, so following code:
    Qt Code:
    1. evaluate( "var a = 1;" );
    2. evaluate( "var b = 2;" );
    3. evaluate( "a + b" );
    To copy to clipboard, switch view to plain text mode 
    Will evaluate to "3". Am i correct?
    Correct.

    Also, what do you means by

    Quote Originally Posted by wysota
    But the function itself is defined in the context surrounding the function itself
    It means this script will not be valid...

    javascript Code:
    1. function x() {
    2. function y() { return 7; }
    3. return 5;
    4. }
    5.  
    6. y();
    To copy to clipboard, switch view to plain text mode 

    ... because function "y" exists only in the context of execution of function "x".

    Sorry for boring you with this stupid questions, but Qt documentation about scripting is a kind of limited and it's very important for me to know how it works .
    It has nothing to do with Qt. It's all about how today's computers work. The same concept applies to every C/C++ application as well.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 4
    Last Post: 11th May 2010, 17:33
  2. Reload a Qt Plugin
    By bunjee in forum Qt Programming
    Replies: 3
    Last Post: 30th December 2009, 15:36
  3. Replies: 0
    Last Post: 25th November 2009, 07:46
  4. Replies: 1
    Last Post: 11th November 2009, 12:22
  5. Can I include a script from script?
    By yycking in forum Qt Programming
    Replies: 1
    Last Post: 24th April 2009, 03:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.