From 29b6d2e45c7434fccf2e6878630e62d5dcce38db Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Tue, 10 Dec 2013 15:25:22 +0100 Subject: Fix broken Maroon game / regression in PropertyChanges {} element Commit 0aadcf8077840068eb182269e9ed9c31ad12f45e that pre-compiles the expressions in PropertyChanges {} introduced a regression in where the evaluation context was incorrect and thus bindings would not be able to access the correct properties. For example PropertyChanges { target: someObject y: height / 2 } Here height should be looked up in the context of "someObject", not of the PropertyChanges element. This patch introduces an auto-test that verifies that the lookup context is correct and fixes the bug by disabling accelerated compile time property lookups for binding expressions that are requested from a custom parser. Change-Id: I5cb607d07211b453ddfc9928ccbf5f9ecec85575 Reviewed-by: Lars Knoll --- src/qml/compiler/qqmlcodegenerator.cpp | 28 ++++++++++++++++++---------- src/qml/compiler/qqmlcodegenerator_p.h | 21 ++++++++++++++++++--- src/qml/qml/qqmlcompiler.cpp | 14 ++++++++++---- src/qml/qml/qqmlcompiler_p.h | 9 +++++---- src/qml/qml/qqmltypeloader.cpp | 3 +-- 5 files changed, 52 insertions(+), 23 deletions(-) (limited to 'src/qml') diff --git a/src/qml/compiler/qqmlcodegenerator.cpp b/src/qml/compiler/qqmlcodegenerator.cpp index 1b96bad94f..8809221abe 100644 --- a/src/qml/compiler/qqmlcodegenerator.cpp +++ b/src/qml/compiler/qqmlcodegenerator.cpp @@ -281,7 +281,7 @@ bool QQmlCodeGenerator::sanityCheckFunctionNames() { QSet functionNames; for (Function *f = _object->functions->first; f; f = f->next) { - AST::FunctionDeclaration *function = AST::cast(_functions.at(f->index)); + AST::FunctionDeclaration *function = AST::cast(_functions.at(f->index).node); Q_ASSERT(function); QString name = function->name.toString(); if (functionNames.contains(name)) @@ -1210,6 +1210,7 @@ JSCodeGen::JSCodeGen(const QString &fileName, const QString &sourceCode, V4IR::M , jsEngine(jsEngine) , qmlRoot(qmlRoot) , imports(imports) + , _disableAcceleratedLookups(false) , _contextObject(0) , _scopeObject(0) , _contextObjectTemp(-1) @@ -1234,23 +1235,23 @@ void JSCodeGen::beginObjectScope(QQmlPropertyCache *scopeObject) _scopeObject = scopeObject; } -QVector JSCodeGen::generateJSCodeForFunctionsAndBindings(const QList &functions, const QHash &functionNames) +QVector JSCodeGen::generateJSCodeForFunctionsAndBindings(const QList &functions) { QVector runtimeFunctionIndices(functions.size()); ScanFunctions scan(this, sourceCode, GlobalCode); scan.enterEnvironment(0, QmlBinding); scan.enterQmlScope(qmlRoot, QStringLiteral("context scope")); - foreach (AST::Node *node, functions) { - Q_ASSERT(node != qmlRoot); - AST::FunctionDeclaration *function = AST::cast(node); + foreach (const CompiledFunctionOrExpression &f, functions) { + Q_ASSERT(f.node != qmlRoot); + AST::FunctionDeclaration *function = AST::cast(f.node); if (function) scan.enterQmlFunction(function); else - scan.enterEnvironment(node, QmlBinding); + scan.enterEnvironment(f.node, QmlBinding); - scan(function ? function->body : node); + scan(function ? function->body : f.node); scan.leaveEnvironment(); } scan.leaveEnvironment(); @@ -1260,7 +1261,8 @@ QVector JSCodeGen::generateJSCodeForFunctionsAndBindings(const QListfunctions.at(defineFunction(QStringLiteral("context scope"), qmlRoot, 0, 0)); for (int i = 0; i < functions.count(); ++i) { - AST::Node *node = functions.at(i); + const CompiledFunctionOrExpression &qmlFunction = functions.at(i); + AST::Node *node = qmlFunction.node; Q_ASSERT(node != qmlRoot); AST::FunctionDeclaration *function = AST::cast(node); @@ -1268,8 +1270,10 @@ QVector JSCodeGen::generateJSCodeForFunctionsAndBindings(const QListname.toString(); + else if (!qmlFunction.name.isEmpty()) + name = qmlFunction.name; else - name = functionNames.value(i, QStringLiteral("%qml-expression-entry")); + name = QStringLiteral("%qml-expression-entry"); AST::SourceElements *body; if (function) @@ -1289,6 +1293,7 @@ QVector JSCodeGen::generateJSCodeForFunctionsAndBindings(const QListfinish(); } + _disableAcceleratedLookups = qmlFunction.disableAcceleratedLookups; int idx = defineFunction(name, node, function ? function->formals : 0, body); @@ -1530,6 +1535,9 @@ void JSCodeGen::beginFunctionBodyHook() V4IR::Expr *JSCodeGen::fallbackNameLookup(const QString &name, int line, int col) { + if (_disableAcceleratedLookups) + return 0; + Q_UNUSED(line) Q_UNUSED(col) // Implement QML lookup semantics in the current file context. @@ -1746,7 +1754,7 @@ bool SignalHandlerConverter::convertSignalHandlerExpressionsToFunctionDeclaratio if (paramList) paramList = paramList->finish(); - AST::Statement *statement = static_cast(parsedQML->functions[binding->value.compiledScriptIndex]); + AST::Statement *statement = static_cast(parsedQML->functions[binding->value.compiledScriptIndex].node); AST::SourceElement *sourceElement = new (pool) AST::StatementSourceElement(statement); AST::SourceElements *elements = new (pool) AST::SourceElements(sourceElement); elements = elements->finish(); diff --git a/src/qml/compiler/qqmlcodegenerator_p.h b/src/qml/compiler/qqmlcodegenerator_p.h index 348ef0a2cf..0a0e4f2d5b 100644 --- a/src/qml/compiler/qqmlcodegenerator_p.h +++ b/src/qml/compiler/qqmlcodegenerator_p.h @@ -166,6 +166,20 @@ struct Pragma QV4::CompiledData::Location location; }; +struct CompiledFunctionOrExpression +{ + CompiledFunctionOrExpression() + : disableAcceleratedLookups(false) + {} + CompiledFunctionOrExpression(AST::Node *n) + : node(n) + , disableAcceleratedLookups(false) + {} + AST::Node *node; // FunctionDeclaration, Statement or Expression + QString name; + bool disableAcceleratedLookups; +}; + struct ParsedQML { ParsedQML(bool debugMode) @@ -180,7 +194,7 @@ struct ParsedQML AST::UiProgram *program; int indexOfRootObject; QList objects; - QList functions; // FunctionDeclaration, Statement or Expression + QList functions; QV4::Compiler::JSUnitGenerator jsGenerator; QV4::CompiledData::TypeReferenceMap typeReferences; @@ -269,7 +283,7 @@ public: QList _imports; QList _pragmas; QList _objects; - QList _functions; + QList _functions; QV4::CompiledData::TypeReferenceMap _typeReferences; @@ -362,7 +376,7 @@ struct Q_QML_EXPORT JSCodeGen : public QQmlJS::Codegen void beginObjectScope(QQmlPropertyCache *scopeObject); // Returns mapping from input functions to index in V4IR::Module::functions / compiledData->runtimeFunctions - QVector generateJSCodeForFunctionsAndBindings(const QList &functions, const QHash &functionNames); + QVector generateJSCodeForFunctionsAndBindings(const QList &functions); protected: virtual void beginFunctionBodyHook(); @@ -376,6 +390,7 @@ private: AST::UiProgram *qmlRoot; QQmlTypeNameCache *imports; + bool _disableAcceleratedLookups; ObjectIdMapping _idObjects; QQmlPropertyCache *_contextObject; QQmlPropertyCache *_scopeObject; diff --git a/src/qml/qml/qqmlcompiler.cpp b/src/qml/qml/qqmlcompiler.cpp index 2b925b24ec..1e5f6bfa34 100644 --- a/src/qml/qml/qqmlcompiler.cpp +++ b/src/qml/qml/qqmlcompiler.cpp @@ -62,7 +62,6 @@ #include "qqmlglobal_p.h" #include "qqmlbinding_p.h" #include "qqmlabstracturlinterceptor_p.h" -#include "qqmlcodegenerator_p.h" #include #include @@ -2707,6 +2706,10 @@ int QQmlCompiler::bindingIdentifier(const QString &name, const Variant &value, c reference->value = 0; reference->bindingContext = ctxt; reference->bindingContext.owner++; + // Unfortunately this is required for example for PropertyChanges where the bindings + // will be executed in the dynamic scope of the target, so we can't resolve any lookups + // at run-time. + reference->disableLookupAcceleration = true; const int id = output->customParserBindings.count(); output->customParserBindings.append(0); // Filled in later. @@ -3684,9 +3687,12 @@ bool QQmlCompiler::completeComponentBuild() } ComponentCompileState::PerObjectCompileData *cd = &compileState->jsCompileData[b->bindingContext.object]; - cd->functionsToCompile.append(node); + QtQml::CompiledFunctionOrExpression f; + f.node = node; + f.name = binding.property->name().toString().prepend(QStringLiteral("expression for ")); + f.disableAcceleratedLookups = binding.disableLookupAcceleration; + cd->functionsToCompile.append(f); binding.compiledIndex = cd->functionsToCompile.count() - 1; - cd->expressionNames.insert(binding.compiledIndex, binding.property->name().toString().prepend(QStringLiteral("expression for "))); if (componentStats && b->value) componentStats->componentStat.scriptBindings.append(b->value->location); @@ -3719,7 +3725,7 @@ bool QQmlCompiler::completeComponentBuild() jsCodeGen.beginObjectScope(scopeObject->metatype); - cd->runtimeFunctionIndices = jsCodeGen.generateJSCodeForFunctionsAndBindings(cd->functionsToCompile, cd->expressionNames); + cd->runtimeFunctionIndices = jsCodeGen.generateJSCodeForFunctionsAndBindings(cd->functionsToCompile); QList errors = jsCodeGen.errors(); if (!errors.isEmpty()) { exceptions << errors; diff --git a/src/qml/qml/qqmlcompiler_p.h b/src/qml/qml/qqmlcompiler_p.h index 75f404a7d5..516f6653ca 100644 --- a/src/qml/qml/qqmlcompiler_p.h +++ b/src/qml/qml/qqmlcompiler_p.h @@ -62,6 +62,7 @@ #include "qqmlpropertycache_p.h" #include "qqmltypenamecache_p.h" #include "qqmltypeloader_p.h" +#include #include "private/qv4identifier_p.h" #include @@ -231,14 +232,15 @@ namespace QQmlCompilerTypes { struct JSBindingReference : public QQmlPool::Class, public BindingReference { - JSBindingReference() : nextReference(0) {} + JSBindingReference() : disableLookupAcceleration(false), nextReference(0) {} QQmlScript::Variant expression; QQmlScript::Property *property; QQmlScript::Value *value; int compiledIndex : 16; - int customParserBindingsIndex : 16; + int customParserBindingsIndex : 15; + int disableLookupAcceleration: 1; BindingContext bindingContext; @@ -319,10 +321,9 @@ namespace QQmlCompilerTypes { QList compiledMetaMethods; struct PerObjectCompileData { - QList functionsToCompile; + QList functionsToCompile; QVector runtimeFunctionIndices; QVector compiledMetaMethods; - QHash expressionNames; }; QHash jsCompileData; }; diff --git a/src/qml/qml/qqmltypeloader.cpp b/src/qml/qml/qqmltypeloader.cpp index 7d377af0f8..c9694da0df 100644 --- a/src/qml/qml/qqmltypeloader.cpp +++ b/src/qml/qml/qqmltypeloader.cpp @@ -2372,8 +2372,7 @@ void QQmlTypeData::compile() // Compile JS binding expressions and signal handlers JSCodeGen jsCodeGen(finalUrlString(), parsedQML->code, &parsedQML->jsModule, &parsedQML->jsParserEngine, parsedQML->program, m_compiledData->importCache); - QHash expressionNames; // ### TODO - const QVector runtimeFunctionIndices = jsCodeGen.generateJSCodeForFunctionsAndBindings(parsedQML->functions, expressionNames); + const QVector runtimeFunctionIndices = jsCodeGen.generateJSCodeForFunctionsAndBindings(parsedQML->functions); QV4::ExecutionEngine *v4 = QV8Engine::getV4(m_typeLoader->engine()); -- cgit v1.2.3