Script context is just like a "stack frame" for regular computer applications. Functions and variables are local to the context (stack frame) they were declared in. When a function is executed, a new context is created for it and when it ends, the context is removed. So all local changes made inside a function will be gone when the function ends because the context of the function is removed. The context is not removed between evaluate() calls thus the top-level context remains the same and changes made to it are persistant. You need to explicitely push a context before calling evaluate() and pop it afterwards.
See the example (not tested):
QScriptEngine engine;
QScriptContext *context = engine.context();
context->activationObject().setProperty("x", 7);
contet = engine.pushContext(); // new context
context->activationObject().setProperty("y", 8);
engine.evaluate("x+y");
engine.popContext();
engine.evaluate("x+y"); // "y" is undefined
QScriptEngine engine;
QScriptContext *context = engine.context();
context->activationObject().setProperty("x", 7);
contet = engine.pushContext(); // new context
context->activationObject().setProperty("y", 8);
engine.evaluate("x+y");
engine.popContext();
engine.evaluate("x+y"); // "y" is undefined
To copy to clipboard, switch view to plain text mode
The same happens if you call a function. But the function itself is defined in the context surrounding the function itself.
If you wish to have a clean context for every evaluate() call, do this (indentation of the code below should help you understand):
QScriptEngine engine;
engine.pushContext();
engine.evaluate("...");
engine.popContext();
engine.pushContext();
engine.evaluate("...");
engine.popContext();
QScriptEngine engine;
engine.pushContext();
engine.evaluate("...");
engine.popContext();
engine.pushContext();
engine.evaluate("...");
engine.popContext();
To copy to clipboard, switch view to plain text mode
Bookmarks