[Development] Porting code from QScriptEngine (Qt4) to QJSEngine (Qt5)

a.grandi at gmail.com a.grandi at gmail.com
Mon Jun 25 09:14:24 CEST 2012


Hi,

On 21 June 2012 17:18, Stephen Kelly <stephen.kelly at kdab.com> wrote:
> On Thursday, June 21, 2012 15:33:19 a.grandi at gmail.com wrote:
>> I've started porting some code from QScriptEngine to QJSEngine, but
>> there are some missing parts I don't know how to implement.
>
> My understanding is that QJS* is not a replacement for QScriptEngine:
>
> http://thread.gmane.org/gmane.comp.lib.qt.devel/4185
>
> There's nothing for you to port to yet. Maybe there will be later in Qt 5
> development.

I've just read all the thread, maybe it's better if I provide more
details about what I'm doing.

My current task is experimenting the compatibility of Qt5/QML
JavaScript engine with the most popular JavaScript libraries (for
example: jQuery, BackBoneJs, etc...).
All these libraries need browser objects like "window", "document"
etc... to work properly.

I found the EnvJs library and this useful blog article written by Kent
Hansen http://labs.qt.nokia.com/2011/03/10/say-hello-to-envjs-for-qtscript/

the only problem is that he doesn't provide a complete (ready to be
built) code example and he probably tested this on Qt4 (while I have
to use Qt5), so I wrote a Qt5 example from scratch, trying to follow
the article advices, but it's not working. I've tried to use both
QtScriptEngine and QJSEngine but it doesn't work.

Basically:

1) I load the env.js content and I evaluate it: qse.evaluate(js,
"js_errors.txt");

2) I call the initializeEnvjsNatives(&qse);

3) from my main.qml I import the env.js file (I should not import it
since I've already evaluated it with qse.evaluate(....) right? Anyway
it doesn't work in both ways...

import "env.js" as EnvJs

4) I try to use it in this way:

console.log("document:", EnvJs.document);
EnvJs.document.write("<p>Hello, JavaScript in QtScript!</p>");

and this is the error I always get:

document: undefined
file:///home/andrea/Documents/EnvjsTest/qml/EnvjsTest/main.qml:24:
TypeError: Cannot call method 'write' of undefined

If it can be useful for you to understand better, I think I can
provide the complete source code (it's just a basic example, but at
least it will save you lot of time if you want to give it a try).

Best regards,

-- 
Andrea Grandi - Nokia-DXM/Tampere / Qt Ambassador
Ubuntu Member: https://launchpad.net/~andreagrandi
website: http://www.andreagrandi.it



More information about the Development mailing list