PDA

View Full Version : How to get value from Script file?



superinman
30th July 2010, 17:52
Hi.

I have a problem about Qtscript.

My project is a com port tool and my idea is: The main project loads a script file and returns commands to the C++ code, then the commands from script file are sent to the peripheral equipment. In this way I just need to change the script file if I want to send different commands each time.

Now I did in this way:
this is the C++ code to interpret script file:


QScriptEngine engine;
QString scriptFileName(":/myscriptfile.js");// Change the script file according to the requirement.
QFile file(scriptFileName);
file.open(QIODevice::ReadOnly);
engine.evaluate(file.readAll(), scriptFileName);
file.close();
QScriptValue global = engine.globalObject();
QScriptValue GeneralCommand = global.property("myCommand");
QScriptValue ATCommand1 = global.property("Command1");
QScriptValue ATCommand2 = global.property("Command2");
QScriptValue ATCommand3 = global.property("Command3");
QScriptValue ATCommand4 = global.property("Command4");
QScriptValue ATCommand5 = global.property("Command5");

CommandString[0]=GeneralCommand.call(ATCommand1).toString();
CommandString[1]=GeneralCommand.call(ATCommand2).toString();
CommandString[2]=GeneralCommand.call(ATCommand3).toString();
CommandString[3]=GeneralCommand.call(ATCommand4).toString();
CommandString[4]=GeneralCommand.call(ATCommand5).toString();



this is the script file"myscriptfile.js":


//! [0]
function myCommand() { return this.Command;}
Command1 = { Command: 'test command 1'}
Command2 = { Command: ' test command 2'}
Command3 = { Command: ' test command 3}
Command4 = { Command: ' test command 4'}
Command5 = { Command: ' test command 5'}
//! [0]


Actually it works. However I want to do it in a better way. the script file can be like this:


SendCommand('test command 1');
SendCommand('test command 2');
SendCommand('test command 3');
SendCommand('test command 4');

It is simple and easy to understand. But I don't know how to configure script engine in order to write script code like this? Is there someone can tell me how to do? thanks a lot.

asvil
30th July 2010, 18:23
First way, declare array object in script environment:
C++


QString scriptFileName(":/myscriptfile.js");// Change the script file according to the requirement.
QFile file(scriptFileName);
file.open(QIODevice::ReadOnly);
QScriptValue jsArray = engine.evaluate(file.readAll(), scriptFileName);
file.close();
QStringList commands = jsArray.toVariant().toStringList();
// if function work with array
// sendArrayToPort(commands);
// else
// foreach(QString command, commands)
// sendToPort(command);

JS


["command1", "command2", "command3",...,"commandN"]


Second way, wrapping a native function:
C++


QScriptValue sendToPortWrapper(QScriptContext *context, QScriptEngine* engine)
{
if (context->argumentCount()) {
sendToPort(context->argument(0).toString());
}
return engine->undefinedValue();
}


{
............
engine->globalObject().setProperty("sendToPort", engine->newFunction(sendToPortWrapper));
............
}

JS


sendToPort('command1');
sendToPort('command2');
sendToPort('command3');
sendToPort('command4');

superinman
31st July 2010, 00:06
Thank you very much!!