Results 1 to 4 of 4

Thread: QtScript nesting with include/imports or spawned script engines

  1. #1
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QtScript nesting with include/imports or spawned script engines

    Has anyone ever tried to implement a form of script nesting within a QtScript/QtScriptEngine design. I'm trying to create a unit testing platform in which the entire system is driven via QtScript. Ideally i would create a global 'TestDriver' object and have all of the includes (individual tests) register themself with the global TestDriver before calling TestDriver.executeTests();

    Imagine you have a test.qs and init.qs
    init.qs :
    Qt Code:
    1. var TestDriver = new TestDriver();
    2.  
    3. //include actually pauses interpretation of this QtScript and
    4. // begins execution of test.qs -- making it like a C++ header
    5. include(test.qs);
    6.  
    7. var w = test.somemethod(); // execute individual test
    8. ...
    9. TestDriver.executeTests(); // execute all registered tests
    To copy to clipboard, switch view to plain text mode 

    I'm thinking the only way to create the above is to either have a QtScript preprocessor which basically does the file inclusion before sending it to the ScriptEngine.evaluate method. However that is kind of messy.

    Another idea is to nest a ScriptEngine within a script to allow a script to execute other scripts (i hope there is a better way to do this).

  2. #2
    Join Date
    Sep 2008
    Location
    Munich
    Posts
    32
    Thanked 8 Times in 6 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QtScript nesting with include/imports or spawned script engines

    Don't really understand the situation. It's my lack of imagination. But sounds like eval() might be something you want to look into: (http://www.w3schools.com/jsref/jsref_eval.asp). To read other .js files, I gues you need to add a custom function aka include("filename") // return content of file

  3. #3
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtScript nesting with include/imports or spawned script engines

    I solved the first idea by implementing the C++ preparser, basically before I pass a QScript to QScriptEngine.evaluate() I do the following:
    1) Begin reading from source script.
    2) I look for any instances of 'include("somefile.js");
    3) If an include exists I open the include file, and stream the contents into the current TextStream -- this is recursive, if it isn't an include I just stream it as normal
    4) Once I've processed the entire script I'm able to do the following

    Qt Code:
    1. //IncludedScript.js
    2. function magicalFunction()
    3. {
    4. print ("Executed included function");
    5. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //Init.js
    2.  
    3. include("IncludedScript.js");
    4.  
    5. function init()
    6. {
    7. print ("Calling Included Script");
    8. magicalFunction();
    9. print ("End Call");
    10. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //C++
    2. preprocess("init.js");
    3. engine.evaluate(processedResults);
    4. engine.evaluate("init();");
    5.  
    6. ////Output
    7. Calling Included Script
    8. Executed included function
    9. End Call
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    May 2009
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtScript nesting with include/imports or spawned script engines

    I was having this issue as well. A related thread addresses it perfectly. You just need to tweak the context a bit before you call evaluate:

    Qt Code:
    1. QScriptContext *context = engine->currentContext();
    2. QScriptContext *parent=context->parentContext();
    3.  
    4. if(parent!=0)
    5. {
    6. context->setActivationObject(context->parentContext()->activationObject());
    7. context->setThisObject(context->parentContext()->thisObject());
    8. }
    To copy to clipboard, switch view to plain text mode 

    Original thread: http://www.qtcentre.org/threads/2043...pt-from-script

Similar Threads

  1. Replies: 1
    Last Post: 27th February 2010, 11:33
  2. QtScript. How to add created object to script?
    By sergey_85 in forum Qt Programming
    Replies: 1
    Last Post: 18th February 2010, 15:24
  3. QtScript: how to reload current script?
    By eyeofhell in forum Qt Programming
    Replies: 7
    Last Post: 12th January 2010, 11:28
  4. Can I include a script from script?
    By yycking in forum Qt Programming
    Replies: 1
    Last Post: 24th April 2009, 04:01
  5. #include <QtScript>
    By stefan in forum Qt Programming
    Replies: 3
    Last Post: 25th February 2009, 21:04

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.