[Qt-interest] Access current script context in QScriptEngineAgent

Jonas Gehring jonas.gehring at boolsoft.org
Thu Nov 24 12:08:58 CET 2011


Hi all,

I'm trying to inspect the current script context whenever it is about to
be popped. I've reimplemented QScriptEngineAgent::contextPop() in a
subclass, which is being added to the QScriptEngine instance. However,
inside contextPop(), engine()->currentContext() always returns the
initial global context.

Here's a minimal example (I'm working with Qt-4.7.4):

> #include <QCoreApplication>
> #include <QtScript>
> 
> const char *script = \ 
>     "function g() {"
>     "  print('g called');"
>     "}" 
>     "function f() {"
>     "  print('f started');"
>     "  g();"
>     "  print('f finished');"
>     "}" 
>     "f()";
> 
> class MyAgent : public QScriptEngineAgent
> {
> public:
>     MyAgent(QScriptEngine *engine) : QScriptEngineAgent(engine) { } 
> 
>     void contextPop() {
>         qDebug("popped:  %p (%s)",
>             engine()->currentContext(),
>             qPrintable(engine()->currentContext()->toString()));
>     }
> };
> 
> int main(int argc, char **argv)
> {
>     QCoreApplication app(argc, argv);
>     QScriptEngine engine;
>     engine.setAgent(new MyAgent(&engine));
> 
>     qDebug("initial: %p (%s)",
>         engine.currentContext(),
>         qPrintable(engine.currentContext()->toString()));
>     engine.evaluate(script);
>     return 0;
> }

When I run this, I get the following output:

> initial: 0x6a3788 (<global>() at -1)
> f started
> g called
> popped:  0x6a3788 (<global>() at -1)
> f finished
> popped:  0x6a3788 (<global>() at -1)

Can someone tell me what I'm doing wrong?

Regards,
Jonas

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20111124/4f76c701/attachment.bin 


More information about the Qt-interest-old mailing list