From ee565b3db656b5b9bfd57d98d53b898c1a62e1f0 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Thu, 30 Aug 2018 12:58:55 +0200 Subject: Correctly create methods for functions in object literals Methods behave slightly different than normal functions as they have a home object and define how super property access is being done. To implement this correctly, we need to create these methods during object initialization time. Change-Id: Ib3f670c8790b882c6472de786938ca4f0b73f66f Reviewed-by: Simon Hausmann --- src/qml/compiler/qv4codegen.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'src/qml/compiler/qv4codegen.cpp') diff --git a/src/qml/compiler/qv4codegen.cpp b/src/qml/compiler/qv4codegen.cpp index 412d787d11..b09deb1eaf 100644 --- a/src/qml/compiler/qv4codegen.cpp +++ b/src/qml/compiler/qv4codegen.cpp @@ -2450,7 +2450,7 @@ bool Codegen::visit(ObjectPattern *ast) for (; it; it = it->next) { PatternProperty *p = it->property; AST::ComputedPropertyName *cname = AST::cast(p->name); - if (cname || p->type == PatternProperty::Getter || p->type == PatternProperty::Setter) + if (cname || p->type != PatternProperty::Literal) break; QString name = p->name->asString(); uint arrayIndex = QV4::String::toArrayIndex(name); @@ -2477,7 +2477,9 @@ bool Codegen::visit(ObjectPattern *ast) PatternProperty *p = it->property; AST::ComputedPropertyName *cname = AST::cast(p->name); ObjectLiteralArgument argType = ObjectLiteralArgument::Value; - if (p->type == PatternProperty::Getter) + if (p->type == PatternProperty::Method) + argType = ObjectLiteralArgument::Method; + else if (p->type == PatternProperty::Getter) argType = ObjectLiteralArgument::Getter; else if (p->type == PatternProperty::Setter) argType = ObjectLiteralArgument::Setter; @@ -2508,10 +2510,20 @@ bool Codegen::visit(ObjectPattern *ast) push(Reference::fromAccumulator(this)); { RegisterScope innerScope(this); - Reference value = expression(p->initializer); - if (hasError) - return false; - value.loadInAccumulator(); + if (p->type != PatternProperty::Literal) { + // need to get the closure id for the method + FunctionExpression *f = p->initializer->asFunctionDefinition(); + Q_ASSERT(f); + int function = defineFunction(f->name.toString(), f, f->formals, f->body); + if (hasError) + return false; + Reference::fromConst(this, Encode(function)).loadInAccumulator(); + } else { + Reference value = expression(p->initializer); + if (hasError) + return false; + value.loadInAccumulator(); + } } push(Reference::fromAccumulator(this)); } -- cgit v1.2.3