Is there a way to maintain a Qt script contextual environment outside of QScriptEngine # pushContext / popContext?

In the Qt 4.8 scripting engine, โ€œlocalโ€ variables can be set by obtaining a QScriptContext from QScriptEngine::pushContext , and then setting the properties of its activation object . This can only be done in push / pop calls, since only the QScriptContext location is QScriptContext and AFAICT there is no equivalent to QScriptEngine#evaluate , which uses QScriptContext for use as an environment:

 QScriptEngine engine; QScriptContext *local; local = engine.pushContext(); local->activationObject().setProperty("value", 2); // set value=2 qDebug() << engine.evaluate("value").toNumber(); // outputs 2 engine.popContext(); 

Is there a way to maintain an environment for use with evaluations outside of push / pop calls? For example, I tried to create a QScriptValue to use as an activation object and then set it:

 QScriptEngine engine; QScriptContext *local; // Use ao as activation object, set variables here, prior to pushContext. QScriptValue ao; ao.setProperty("value", 1); // Test with ao: local = engine.pushContext(); local->setActivationObject(ao); qDebug() << engine.evaluate("value").toNumber(); engine.popContext(); 

But that does not work. It outputs nan instead of 1 , since value is undefined. For some reason, setActivationObject did not change the value.

My common goal:

  • Set up your local environment outside of the evaluation code.
  • Then use this pre-configured local environment when evaluating scripts between the pushContext and popContext , without having to re-set all the variables in that environment each time.

So:

  • Is there any way to do this?
  • Is it possible that I'm on the right track, but I configured ao inappropriately? For example, there is an undocumented QScriptEngine#newActivationObject() that gives a "unimplemented" error when using it, maybe this is a hint?

How to set up a local context, but basically you donโ€™t have to reconfigure it every time I click on a context (since it is essentially lost forever every time the context appears).

+6
source share
1 answer

You can use a global object. He will transfer the value of the property according to all estimates:

 #include <QCoreApplication> #include <QDebug> #include <QtScript/QScriptEngine> #include <QtScript/QScriptContext> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QScriptEngine engine; engine.globalObject().setProperty("value", 2); engine.globalObject().setProperty("value2", 3); qDebug() << engine.evaluate("value").toNumber(); // outputs 2 qDebug() << engine.evaluate("value2").toNumber(); // outputs 3 return a.exec(); } 

Or if you do not need a global scope:

 #include <QCoreApplication> #include <QDebug> #include <QtScript/QScriptEngine> #include <QtScript/QScriptContext> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QScriptEngine engine; QScriptContext *context; QScriptValue scope = engine.newObject(); scope.setProperty("value", 1); scope.setProperty("value2", 2); context = engine.pushContext(); context->pushScope(scope); qDebug() << engine.evaluate("value").toNumber(); // outputs 1 qDebug() << engine.evaluate("value2").toNumber(); // outputs 2 engine.popContext(); return a.exec(); } 
+2
source

All Articles