QtScript is also good for tiny things

Here's a quick example of why it's nice to have a script interpreter embedded in Qt: Plasma's KRunner has a calculator which used code borrowed from the KDE 3.x minicli. The old code started up the bc command line calculator then displayed the result - not exactly an efficient way to do things. I've just committed a change that makes it use QtScript and the code is trivial:

QString CalculatorRunner::calculate( const QString& term )
    QScriptEngine eng;
    QScriptValue result = eng.evaluate( term );
    return result.toString();

This replaces 23 lines of code and is quite a bit more powerful. Nice!


Won't this allow people to run arbitrary JS code from KRunner?

Granted, there isn't much you can do with a pure JS environment without any binding...

By Thiago Macieira at Mon, 10/15/2007 - 11:24

No, you can't run arbitrary code as the calculator KRunner is only activated when the message typed matches a certain regexp (ie. a whitelist). I am thinking of changing this so you can run arbitrary code however. This isn't a security problem as only the core JS objects are available so you have no way to escape the sandbox.

By Richard Moore at Mon, 10/15/2007 - 11:35