void tst_QScriptContext::qobjectAsActivationObject() { QScriptEngine eng; QObject object; QScriptValue scriptObject = eng.newQObject(&object); QScriptContext *ctx = eng.pushContext(); ctx->setActivationObject(scriptObject); QVERIFY(ctx->activationObject().equals(scriptObject)); QVERIFY(!scriptObject.property("foo").isValid()); eng.evaluate("function foo() { return 123; }"); { QScriptValue val = scriptObject.property("foo"); QVERIFY(val.isValid()); QVERIFY(val.isFunction()); } QVERIFY(!eng.globalObject().property("foo").isValid()); QVERIFY(!scriptObject.property("bar").isValid()); eng.evaluate("var bar = 123"); { QScriptValue val = scriptObject.property("bar"); QVERIFY(val.isValid()); QVERIFY(val.isNumber()); QCOMPARE(val.toInt32(), 123); } QVERIFY(!eng.globalObject().property("bar").isValid()); { QScriptValue val = eng.evaluate("delete foo"); QVERIFY(val.isBool()); QVERIFY(val.toBool()); QVERIFY(!scriptObject.property("foo").isValid()); } }
bool ConditionalTransition::eventTest(QEvent* e) { if (e->type() != NamedEvent::type) { return false; } NamedEvent* namedEvent = static_cast<NamedEvent*>(e); if (namedEvent->getEventName() != eventName) { return false; } if (!condition.isEmpty()) { QScriptEngine* scriptEngine = stateMachine->getScriptEngine(); QScriptContext* context = scriptEngine->pushContext(); context->activationObject().setProperty("input", ValueScriptBinding::create(scriptEngine, &sourceState->getInput())); // TODO performance? context->activationObject().setProperty("output", ValueScriptBinding::create(scriptEngine, &sourceState->getOutput())); // TODO performance? bool result = scriptEngine->evaluate(condition).toBool(); scriptEngine->popContext(); return result; } return true; }
void QFormScriptRunner::QFormScriptRunnerPrivate::initializeEngine(QWidget *w, const WidgetList &children, QScriptEngine &scriptEngine) { // Populate the script variables. This pushes a context which must be popped. QScriptContext *ctx = scriptEngine.pushContext(); QScriptValue widgetObject = scriptEngine.newQObject(w); QScriptValue childrenArray = scriptEngine.newArray (children.size()); for(int i = 0; i < children.size(); i++) { childrenArray.setProperty(i, scriptEngine.newQObject(children[i])); } const QFormBuilderStrings &strings = QFormBuilderStrings::instance(); ctx ->activationObject().setProperty(strings.scriptWidgetVariable, widgetObject); ctx ->activationObject().setProperty(strings.scriptChildWidgetsVariable, childrenArray); }
void Cluster::setup () { QScriptEngine* engine = scene->scriptEngine(); QScriptContext* ctx = engine->pushContext(); QScriptValue ao = ctx->activationObject(); prepGlobalObject(ao); ao.setProperty("emit", engine->newFunction(emitFun)); /// @todo is this the best way to get access to the cluster? QVariant var = qVariantFromValue(this); ao.setProperty("self", engine->newVariant(var)); engine->evaluate(d->shellProgram); engine->popContext(); }
void tst_QScriptContext::popNativeContextScope() { QScriptEngine eng; QScriptContext *ctx = eng.pushContext(); QVERIFY(ctx->popScope().isObject()); // the activation object QCOMPARE(ctx->scopeChain().size(), 1); QVERIFY(ctx->scopeChain().at(0).strictlyEquals(eng.globalObject())); // This was different in 4.5: scope and activation were decoupled QVERIFY(ctx->activationObject().strictlyEquals(eng.globalObject())); QVERIFY(!eng.evaluate("var foo = 123; function bar() {}").isError()); QVERIFY(eng.globalObject().property("foo").isNumber()); QVERIFY(eng.globalObject().property("bar").isFunction()); QScriptValue customScope = eng.newObject(); ctx->pushScope(customScope); QCOMPARE(ctx->scopeChain().size(), 2); QVERIFY(ctx->scopeChain().at(0).strictlyEquals(customScope)); QVERIFY(ctx->scopeChain().at(1).strictlyEquals(eng.globalObject())); QVERIFY(ctx->activationObject().strictlyEquals(eng.globalObject())); ctx->setActivationObject(customScope); QVERIFY(ctx->activationObject().strictlyEquals(customScope)); QCOMPARE(ctx->scopeChain().size(), 2); QVERIFY(ctx->scopeChain().at(0).strictlyEquals(customScope)); QEXPECT_FAIL("", "QTBUG-11012", Continue); QVERIFY(ctx->scopeChain().at(1).strictlyEquals(eng.globalObject())); QVERIFY(!eng.evaluate("baz = 456; var foo = 789; function barbar() {}").isError()); QEXPECT_FAIL("", "QTBUG-11012", Continue); QVERIFY(eng.globalObject().property("baz").isNumber()); QVERIFY(customScope.property("foo").isNumber()); QVERIFY(customScope.property("barbar").isFunction()); QVERIFY(ctx->popScope().strictlyEquals(customScope)); QCOMPARE(ctx->scopeChain().size(), 1); QEXPECT_FAIL("", "QTBUG-11012", Continue); QVERIFY(ctx->scopeChain().at(0).strictlyEquals(eng.globalObject())); // Need to push another object, otherwise we crash in popContext() (QTBUG-11012) ctx->pushScope(customScope); eng.popContext(); }
bool HttpHandlerQtScriptFile::handleRequest(Pillow::HttpConnection *request) { if (!_lastModified.isValid() || (_autoReload && QFileInfo(_fileName).lastModified() > _lastModified)) { // Time to (re)load the script. qDebug() << "HttpHandlerQtScriptFile::handleRequest: (re)loading" << _fileName; QFile file(_fileName); if (!file.open(QIODevice::ReadOnly)) { request->writeResponseString(500, HttpHeaderCollection(), QString("HttpHandlerQtScriptFile::handleRequest: Could not read file %1").arg(_fileName)); return true; } QScriptEngine* engine = _scriptObject.engine(); _scriptObject = engine->newObject(); QScriptContext* context = engine->pushContext(); context->setActivationObject(_scriptObject); context->setThisObject(_scriptObject); QScriptValue result = engine->evaluate(file.readAll(), _fileName); engine->popContext(); if (result.isError()) { request->writeResponseString(500, HttpHeaderCollection(), QString("HttpHandlerQtScriptFile::handleRequest: Error while evaluating script %1: %2").arg(_fileName).arg(objectToString(result))); return true; } QScriptValue scriptFunction = _scriptObject.property(_functionName); if (!scriptFunction.isFunction()) { request->writeResponseString(500, HttpHeaderCollection(), QString("HttpHandlerQtScriptFile::handleRequest: Error while evaluating script %1: '%2' is not a function defined in the script").arg(_fileName).arg(_functionName)); return true; } _lastModified = QFileInfo(_fileName).lastModified(); setScriptFunction(scriptFunction); } return HttpHandlerQtScript::handleRequest(request); }
void tst_QScriptContext::pushAndPopContext() { QScriptEngine eng; QScriptContext *topLevel = eng.currentContext(); QCOMPARE(topLevel->engine(), &eng); QScriptContext *ctx = eng.pushContext(); QVERIFY(ctx != 0); QCOMPARE(ctx->parentContext(), topLevel); QCOMPARE(eng.currentContext(), ctx); QCOMPARE(ctx->engine(), &eng); QCOMPARE(ctx->state(), QScriptContext::NormalState); QCOMPARE(ctx->isCalledAsConstructor(), false); QCOMPARE(ctx->argumentCount(), 0); QCOMPARE(ctx->argument(0).isUndefined(), true); QVERIFY(!ctx->argument(-1).isValid()); QCOMPARE(ctx->argumentsObject().isObject(), true); QCOMPARE(ctx->activationObject().isObject(), true); QCOMPARE(ctx->callee().isValid(), false); QCOMPARE(ctx->thisObject().strictlyEquals(eng.globalObject()), true); QCOMPARE(ctx->scopeChain().size(), 2); QVERIFY(ctx->scopeChain().at(0).equals(ctx->activationObject())); QVERIFY(ctx->scopeChain().at(1).equals(eng.globalObject())); QScriptContext *ctx2 = eng.pushContext(); QCOMPARE(ctx2->parentContext(), ctx); QCOMPARE(eng.currentContext(), ctx2); eng.popContext(); QCOMPARE(eng.currentContext(), ctx); eng.popContext(); QCOMPARE(eng.currentContext(), topLevel); // popping the top-level context is not allowed QTest::ignoreMessage(QtWarningMsg, "QScriptEngine::popContext() doesn't match with pushContext()"); eng.popContext(); QCOMPARE(eng.currentContext(), topLevel); { QScriptContext *ctx3 = eng.pushContext(); ctx3->activationObject().setProperty("foo", QScriptValue(&eng, 123)); QVERIFY(eng.evaluate("foo").strictlyEquals(QScriptValue(&eng, 123))); eng.evaluate("var bar = 'ciao'"); QVERIFY(ctx3->activationObject().property("bar", QScriptValue::ResolveLocal).strictlyEquals(QScriptValue(&eng, "ciao"))); eng.popContext(); } { QScriptContext *ctx4 = eng.pushContext(); QScriptValue obj = eng.newObject(); obj.setProperty("prop", QScriptValue(&eng, 456)); ctx4->setThisObject(obj); QScriptValue ret = eng.evaluate("var tmp = this.prop; tmp + 1"); QCOMPARE(eng.currentContext(), ctx4); QVERIFY(ret.strictlyEquals(QScriptValue(&eng, 457))); eng.popContext(); } // throwing an exception { QScriptContext *ctx5 = eng.pushContext(); QScriptValue ret = eng.evaluate("throw new Error('oops')"); QVERIFY(ret.isError()); QVERIFY(eng.hasUncaughtException()); QCOMPARE(eng.currentContext(), ctx5); eng.popContext(); } }
QScriptValue Person_ctor(QScriptContext *ctx, QScriptEngine *eng) { QScriptValue object; if (ctx->isCalledAsConstructor()) { object = ctx->thisObject(); } else { object = eng->newObject(); object.setPrototype(ctx->callee().property("prototype")); } object.setProperty("name", ctx->argument(0)); return object; } //! [76] //! [77] QScriptContext *ctx = eng.pushContext(); QScriptValue act = ctx->activationObject(); act.setProperty("digit", 7); qDebug() << eng.evaluate("digit + 1").toNumber(); // 8 eng.popContext(); //! [77] //! [78] QScriptValue getSet(QScriptContext *ctx, QScriptEngine *eng) { QScriptValue obj = ctx->thisObject(); QScriptValue data = obj.data(); if (!data.isValid()) { data = eng->newObject();