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).
source share