aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2018-09-03 09:31:47 +0200
committerLars Knoll <lars.knoll@qt.io>2018-09-05 12:57:41 +0000
commitfa74444ed06e4db21b0e9829a5832b886b39d372 (patch)
tree0bc352c539e164f15ccfe7fbb1db1dd7226a5bec /src
parent76f410452dd09489cc48197a0dd4d0bf95699647 (diff)
fix accesses to this in arrow functions
Change-Id: I4c0cfc3a120fc0b246760886b576e92d3f7623ff Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/qml/compiler/qv4codegen.cpp6
-rw-r--r--src/qml/compiler/qv4compilercontext.cpp7
-rw-r--r--src/qml/compiler/qv4compilercontext_p.h1
-rw-r--r--src/qml/compiler/qv4compilerscanfunctions.cpp26
4 files changed, 33 insertions, 7 deletions
diff --git a/src/qml/compiler/qv4codegen.cpp b/src/qml/compiler/qv4codegen.cpp
index 786753ab80..d61f79f0fc 100644
--- a/src/qml/compiler/qv4codegen.cpp
+++ b/src/qml/compiler/qv4codegen.cpp
@@ -2717,6 +2717,12 @@ bool Codegen::visit(ThisExpression *)
if (hasError)
return false;
+ if (_context->isArrowFunction) {
+ Reference r = referenceForName(QStringLiteral("this"), false);
+ r.isReadonly = true;
+ _expr.setResult(r);
+ return false;
+ }
_expr.setResult(Reference::fromThis(this));
return false;
}
diff --git a/src/qml/compiler/qv4compilercontext.cpp b/src/qml/compiler/qv4compilercontext.cpp
index 4ee6d2c179..1b4c084ab4 100644
--- a/src/qml/compiler/qv4compilercontext.cpp
+++ b/src/qml/compiler/qv4compilercontext.cpp
@@ -241,6 +241,13 @@ void Context::emitBlockHeader(Codegen *codegen)
Instruction::ConvertThisToObject convert;
bytecodeGenerator->addInstruction(convert);
}
+ if (innerFunctionAccessesThis) {
+ Instruction::LoadReg load;
+ load.reg = CallData::This;
+ bytecodeGenerator->addInstruction(load);
+ Codegen::Reference r = codegen->referenceForName(QStringLiteral("this"), true);
+ r.storeConsumeAccumulator();
+ }
if (contextType == ContextType::Global || (contextType == ContextType::Eval && !isStrict)) {
// variables in global code are properties of the global context object, not locals as with other functions.
diff --git a/src/qml/compiler/qv4compilercontext_p.h b/src/qml/compiler/qv4compilercontext_p.h
index 3df0aa6b3a..8713b0a188 100644
--- a/src/qml/compiler/qv4compilercontext_p.h
+++ b/src/qml/compiler/qv4compilercontext_p.h
@@ -201,6 +201,7 @@ struct Context {
bool isArrowFunction = false;
bool isGenerator = false;
bool usesThis = false;
+ bool innerFunctionAccessesThis = false;
bool hasTry = false;
bool returnsClosure = false;
mutable bool argumentsCanEscape = false;
diff --git a/src/qml/compiler/qv4compilerscanfunctions.cpp b/src/qml/compiler/qv4compilerscanfunctions.cpp
index e0a745ebb1..dafb1c360d 100644
--- a/src/qml/compiler/qv4compilerscanfunctions.cpp
+++ b/src/qml/compiler/qv4compilerscanfunctions.cpp
@@ -742,6 +742,8 @@ void ScanFunctions::calcEscapingVariables()
}
Q_ASSERT(c);
c->hasDirectEval = true;
+ if (!c->isStrict)
+ c->innerFunctionAccessesThis = true;
}
Context *c = inner;
while (c) {
@@ -751,17 +753,26 @@ void ScanFunctions::calcEscapingVariables()
}
if (inner->usesThis) {
inner->usesThis = false;
- if (!inner->isStrict) {
- Context *c = inner;
- while (c->contextType == ContextType::Block) {
- c = c->parent;
- }
- Q_ASSERT(c);
- c->usesThis = true;
+ bool innerFunctionAccessesThis = false;
+ Context *c = inner;
+ while (c->contextType == ContextType::Block || c->isArrowFunction) {
+ innerFunctionAccessesThis |= c->isArrowFunction;
+ c = c->parent;
}
+ Q_ASSERT(c);
+ if (!inner->isStrict)
+ c->usesThis = true;
+ c->innerFunctionAccessesThis |= innerFunctionAccessesThis;
}
}
for (Context *c : qAsConst(m->contextMap)) {
+ if (c->innerFunctionAccessesThis) {
+ // add an escaping 'this' variable
+ c->addLocalVar(QStringLiteral("this"), Context::VariableDefinition, VariableScope::Let);
+ c->requiresExecutionContext = true;
+ auto m = c->members.find(QStringLiteral("this"));
+ m->canEscape = true;
+ }
if (c->allVarsEscape && c->contextType == ContextType::Block && c->members.isEmpty())
c->allVarsEscape = false;
if (c->contextType == ContextType::Global || (!c->isStrict && c->contextType == ContextType::Eval) || m->debugMode)
@@ -801,6 +812,7 @@ void ScanFunctions::calcEscapingVariables()
qDebug() << "==== escaping variables ====";
for (Context *c : qAsConst(m->contextMap)) {
qDebug() << "Context" << c << c->name << "requiresExecutionContext" << c->requiresExecutionContext << "isStrict" << c->isStrict;
+ qDebug() << " isArrowFunction" << c->isArrowFunction << "innerFunctionAccessesThis" << c->innerFunctionAccessesThis;
qDebug() << " parent:" << c->parent;
if (c->argumentsCanEscape)
qDebug() << " Arguments escape";