[Development] QtScript vs Qml/QJsEngine ?

Kent Hansen kent.hansen at nokia.com
Thu May 31 08:41:29 CEST 2012


Den 30. mai 2012 15:23, skrev ext Diego Iastrubni:
> On Wed, May 30, 2012 at 4:11 PM, Olivier Goffart <olivier at woboq.com 
> <mailto:olivier at woboq.com>> wrote:
>     On Saturday 26 May 2012 12:51:23 Stephen Kelly wrote:
>     > Hi,
>     >
>     > There is a discussion on a kde list touching on whether there is a
>     > replacement for QtScript in Qt 5.
>     >
>     >
>     http://thread.gmane.org/gmane.comp.kde.devel.kwrite/32993/focus=75079
>     >
>     > Is the QJSEngine the start of public API providing a replacement for
>     > QtScript?
> So as an application developer, which wants to add JS scripting 
> capabilities to JavaScript code (so user can extend my application). 
> What are my options?
> How can I load JS files, and call some specific functions defined 
> internally in those JS? I will also need to expose some API of mine to 
> those JS files.
> (Note: I am not talking about QML at all)

The QJSEngine/Value API already supports that use case. The code would 
be 99% the same as with QtScript, if you're familiar with that.

QJSEngine engine;
engine.evaluate("function add(a, b) { return a+ b; }"); // contents of 
some file
QJSValue addFun = engine.globalObject().property("add"); // get the 
function from the script
qDebug() << addFun.call(QJSValueList() << 1 << 2).toNumber();

Exposing a QObject is also similar to QtScript:

MyQObject *obj = ...;
engine.globalObject().setProperty("myQObject", engine.newQObject(obj));

What's missing is the more low-level API, like QScriptContext that 
Olivier mentioned. There's currently no API for exposing a "plain C 
callback" with QJS; everything has to go through the QObject binding. 
You can provide individual functions to JS by only exposing a particular 
QObject wrapper function, e.g.:


There's currently no API for dealing with exceptions. QtScript had 
uncaughtExceptionBacktrace() and friends; in QJS there is only 
QJSValue::isError() as of yet. You can get error messages, stack trace 
etc. by querying properties of error objects.

QJS has no debugging API or tools. V8 provides a public debugging API, 
but we'd have to be very careful if QJS were to wrap that (again, to 
avoid leaking back-end details).

Also worth mentioning is the quirky 
QScriptEngine::setProcessEventsInterval(), which was annoying to support 
with both JSC and V8 (and didn't ever work reliably even with the 
original back-end, because the engine could potentially call into a 
long-running C++ function, which invalidated the promise of respecting 
the user-specified interval). If you need to execute potentially 
long-running JavaScript, you'll need to do it from a separate thread 
with QJS.

Best regards,

More information about the Development mailing list