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 :
Code:
var TestDriver = new TestDriver();
//include actually pauses interpretation of this QtScript and
// begins execution of test.qs -- making it like a C++ header
include(test.qs);
var w = test.somemethod(); // execute individual test
...
TestDriver.executeTests(); // execute all registered tests
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).
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
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
Code:
//IncludedScript.js
function magicalFunction()
{
print ("Executed included function");
}
Code:
//Init.js
include("IncludedScript.js");
function init()
{
print ("Calling Included Script");
magicalFunction();
print ("End Call");
}
Code:
//C++
preprocess("init.js");
engine.evaluate(processedResults);
engine.evaluate("init();");
////Output
Calling Included Script
Executed included function
End Call
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:
Code:
QScriptContext *context = engine->currentContext();
QScriptContext *parent=context->parentContext();
if(parent!=0)
{
context->setActivationObject(context->parentContext()->activationObject());
context->setThisObject(context->parentContext()->thisObject());
}
Original thread: http://www.qtcentre.org/threads/2043...pt-from-script