diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2022-03-25 10:56:41 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2022-03-28 10:28:35 +0200 |
commit | 3fd49c82cf4eccd24c5141d9dcb8a41722e70fc2 (patch) | |
tree | 7df63f092d4cc256c78597316371bb89eacbf9b3 /src/qml/jsruntime/qv4qmlcontext.cpp | |
parent | 664aa0c1893e03d4de44959f188f06082bcf5c0c (diff) |
Respect invokable toString() methods
We should not invoke the base toString() method if there is an override
in a more specific type. This involves lowering the priority of generic
JS lookups when resolving scope properties, in favor of context and
scope lookup.
[ChangeLog][QtQml] You can now override the JavaScript toString() method
by providing a Q_INVOKABLE method of the same name in your QObject-based
C++ classes.
Fixes: QTBUG-87697
Change-Id: I6190111f4c28e54ce76c391c69c4a921e290e612
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/jsruntime/qv4qmlcontext.cpp')
-rw-r--r-- | src/qml/jsruntime/qv4qmlcontext.cpp | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/src/qml/jsruntime/qv4qmlcontext.cpp b/src/qml/jsruntime/qv4qmlcontext.cpp index e3069fdc06..d1b923fa36 100644 --- a/src/qml/jsruntime/qv4qmlcontext.cpp +++ b/src/qml/jsruntime/qv4qmlcontext.cpp @@ -150,13 +150,7 @@ ReturnedValue QQmlContextWrapper::getPropertyAndBase(const QQmlContextWrapper *r return Object::virtualGet(resource, id, receiver, hasProperty); } - bool hasProp = false; - ScopedValue result(scope, Object::virtualGet(resource, id, receiver, &hasProp)); - if (hasProp) { - if (hasProperty) - *hasProperty = hasProp; - return result->asReturnedValue(); - } + ScopedValue result(scope); // It's possible we could delay the calculation of the "actual" context (in the case // of sub contexts) until it is definitely needed. @@ -360,6 +354,16 @@ ReturnedValue QQmlContextWrapper::getPropertyAndBase(const QQmlContextWrapper *r lookup = nullptr; } + // Do the generic JS lookup late. + // The scope, context, types etc should be able to override it. + bool hasProp = false; + result = Object::virtualGet(resource, id, receiver, &hasProp); + if (hasProp) { + if (hasProperty) + *hasProperty = hasProp; + return result->asReturnedValue(); + } + // Do a lookup in the global object here to avoid expressionContext->unresolvedNames becoming // true if we access properties of the global object. if (originalLookup) { |