在评估之前设置QScriptValue局部值 [英] Setting QScriptValue-local values prior to evaluating
问题描述
在Qt 4.8中,可以从C ++端设置 QScriptEngine
QScriptValue >?
例如,假设我有:
QScriptEngine发动机;
engine.globalObject()。setProperty(glob,1000);
// ???:将loc设置为,例如42.
QScriptValue expr1 = engine.evaluate(glob + loc);
qDebug()<< expr1.toNumber();
// ???:将loc设置为,例如99.
QScriptValue expr2 = engine.evaluate(glob + loc);
qDebug()<< expr2.toNumber();
我想要的输出是:
1042
1099
上面的例子的明显的解决方案是使loc全局和设置+相应地重置它,但是为了分散这个问题,这不是我正在寻找的原因。我想要:
- loc为每个
的本地 / li>
- 不要通过例如人工修改脚本字符串 cb> c> c> c> 单个
QScriptEngine
。
- 不要通过例如人工修改脚本字符串 cb> c> c> c> 单个
我真正想到的只有尝试was:
QScriptValue expr1 = engine.evaluate(glob + loc);
expr1.setProperty(loc,42);
qDebug()<< expr1.toNumber();
但是似乎该值已经由 evaluate code>因此
setProperty()
没有效果(没有惊喜)。
查看 QScriptProgram
希望它提供了一个程序本地环境,但唉。
我查看文档,并将继续查看他们我发布这,但这是我第一次使用 QScriptEngine
,我不得不承认我的大脑变得糊涂,所以我道歉,如果它是在那里,完全明显。我会接受RTFM作为一个有效的答案。
我想出来,至少我认为这是最好的办法。关键是 QScriptEngine#pushContext()
和 #popContext()
:
QScriptEngine发动机;
engine.globalObject()。setProperty(glob,1000);
QScriptContext * local;
local = engine.pushContext();
local-> activationObject()。setProperty(loc,42);
QScriptValue expr1 = engine.evaluate(glob + loc);
engine.popContext();
qDebug()<< expr1.toNumber();
local = engine.pushContext();
local-> activationObject()。setProperty(loc,99);
QScriptValue expr2 = engine.evaluate(glob + loc);
engine.popContext();
qDebug()<< expr2.toNumber();
只要给定的 QScriptContext
活动的堆栈,所有 QScriptValue
评估将使用它。相同名称的预先存在的变量将被后续上下文覆盖。
注意,我想你必须使 push→所有评价pop atomic,而不是单独的评估,如果你要去多线程和一个引擎。我希望有一个方法可以传递一个上下文到 evaluate()
。
Is it possible, in Qt 4.8, from the C++ side, to set QScriptValue
-local values in a QScriptEngine
?
For example, let's say I have:
QScriptEngine engine;
engine.globalObject().setProperty("glob", 1000);
// ???: Set loc to, say, 42.
QScriptValue expr1 = engine.evaluate("glob + loc");
qDebug() << expr1.toNumber();
// ???: Set loc to, say, 99.
QScriptValue expr2 = engine.evaluate("glob + loc");
qDebug() << expr2.toNumber();
And I'd like the output of that to be:
1042
1099
The obvious solution to the contrived example above is to just make "loc" global and set + reset it accordingly, but for reasons that distract from this question that's not what I'm looking for. I'd like:
- "loc" to be local to each
QScriptValue
- To not artificially modify the script strings by e.g. prepending
QString().sprintf("loc = %d;", 42)
or whatever to the scripts. - To only use a single
QScriptEngine
.
The only thing I could really think of to try was:
QScriptValue expr1 = engine.evaluate("glob + loc");
expr1.setProperty("loc", 42);
qDebug() << expr1.toNumber();
But it seems the value is already fully evaluated by evaluate()
and so setProperty()
has no effect (no surprise there).
I also had a peek at QScriptProgram
hoping it provided a program-local environment but alas.
I am looking at the docs, and will continue to look at them after I post this, but this is the first time I'm using QScriptEngine
and I have to admit my brain is turning to mush, so I apologize if it's right there and totally obvious. I will accept RTFM as a valid answer.
I figured it out, at least I think this is the best way. The key is QScriptEngine#pushContext()
and #popContext()
:
QScriptEngine engine;
engine.globalObject().setProperty("glob", 1000);
QScriptContext *local;
local = engine.pushContext();
local->activationObject().setProperty("loc", 42);
QScriptValue expr1 = engine.evaluate("glob + loc");
engine.popContext();
qDebug() << expr1.toNumber();
local = engine.pushContext();
local->activationObject().setProperty("loc", 99);
QScriptValue expr2 = engine.evaluate("glob + loc");
engine.popContext();
qDebug() << expr2.toNumber();
And as long as a given QScriptContext
is active on the stack, all QScriptValue
evaluations will use it. Pre-existing variables of the same name will be overridden by subsequent contexts.
The caveat I guess is you have to make push → all evaluations → pop atomic, rather than individual evaluations, if you're going for multiple threads and one engine. I wish there were a way to pass a context to evaluate()
.
这篇关于在评估之前设置QScriptValue局部值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!