diff options
author | Andrei Golubev <andrei.golubev@qt.io> | 2021-02-16 12:52:41 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-12-17 14:39:09 +0000 |
commit | dbc134e207943b23edb90552a89d02b8f43812af (patch) | |
tree | dec2b4319ca6f2b9499d1ca77ee241f35c792866 /src | |
parent | 39433310aa3a25e2711ce426d456a8e78e3cd325 (diff) |
Use qmltc compiler prototype as a fallback implementation
Task-number: QTBUG-91927
Task-number: QTBUG-96041
Task-number: QTBUG-84368
Change-Id: I47320b5f3ed8efff6fb234778df5fae5be5b64f2
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit e7ce5abf24f04d1b071343f07ca28b6a5d9ad4b9)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/qml/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/qml/Qt6QmlBuildInternals.cmake | 2 | ||||
-rw-r--r-- | src/qml/Qt6QmlMacros.cmake | 23 | ||||
-rw-r--r-- | src/qml/qmltc/supportlibrary/qqmlcppbinding.cpp | 147 | ||||
-rw-r--r-- | src/qml/qmltc/supportlibrary/qqmlcppbinding_p.h | 88 | ||||
-rw-r--r-- | src/qml/qmltc/supportlibrary/qqmlcpponassignment.cpp | 56 | ||||
-rw-r--r-- | src/qml/qmltc/supportlibrary/qqmlcpponassignment_p.h | 82 |
7 files changed, 397 insertions, 5 deletions
diff --git a/src/qml/CMakeLists.txt b/src/qml/CMakeLists.txt index 65667b865d..046e29fdcd 100644 --- a/src/qml/CMakeLists.txt +++ b/src/qml/CMakeLists.txt @@ -345,6 +345,10 @@ qt_internal_add_qml_module(Qml types/qqmlconnections.cpp types/qqmlconnections_p.h util/qqmlpropertymap.cpp util/qqmlpropertymap.h qmltc/qqmltcobjectcreationhelper_p.h + qmltc/supportlibrary/qqmlcppbinding_p.h + qmltc/supportlibrary/qqmlcppbinding.cpp + qmltc/supportlibrary/qqmlcpponassignment_p.h + qmltc/supportlibrary/qqmlcpponassignment.cpp DEFINES BUILDING_QT__ ENABLE_ASSEMBLER_WX_EXCLUSIVE=1 diff --git a/src/qml/Qt6QmlBuildInternals.cmake b/src/qml/Qt6QmlBuildInternals.cmake index 788f950c9a..4b6a144b0e 100644 --- a/src/qml/Qt6QmlBuildInternals.cmake +++ b/src/qml/Qt6QmlBuildInternals.cmake @@ -439,7 +439,7 @@ endfunction() function(qt_internal_target_compile_qml_to_cpp target) set(option_args "") set(single_args NAMESPACE) - set(multi_args FILES) + set(multi_args FILES IMPORT_PATHS) qt_parse_all_arguments(arg "qt_internal_target_compile_qml_to_cpp" "${option_args}" "${single_args}" diff --git a/src/qml/Qt6QmlMacros.cmake b/src/qml/Qt6QmlMacros.cmake index f81364e481..a5cea6f01a 100644 --- a/src/qml/Qt6QmlMacros.cmake +++ b/src/qml/Qt6QmlMacros.cmake @@ -1028,10 +1028,7 @@ endfunction() function(qt6_target_compile_qml_to_cpp target) set(args_option "") set(args_single NAMESPACE) - set(args_multi FILES) - - # TODO: add qmldir argument - # TODO: add qml import path argument + set(args_multi FILES IMPORT_PATHS) cmake_parse_arguments(PARSE_ARGV 1 arg "${args_option}" "${args_single}" "${args_multi}" @@ -1074,6 +1071,23 @@ function(qt6_target_compile_qml_to_cpp target) list(APPEND common_args --namespace "${arg_NAMESPACE}") endif() + get_target_property(output_dir ${target} QT_QML_MODULE_OUTPUT_DIRECTORY) + set(qmldir_file ${output_dir}/qmldir) + list(APPEND common_args "-i" ${qmldir_file}) + + foreach(import_path IN LISTS arg_IMPORT_PATHS) + list(APPEND common_args -I "${import_path}") + endforeach() + + # we explicitly depend on qmldir (due to `-i ${qmldir_file}`) but also + # implicitly on the generated qmltypes file, which is a part of qmldir + set(qml_module_files) + list(APPEND qml_module_files ${qmldir_file}) + get_target_property(qmltypes_file ${target} QT_QML_MODULE_TYPEINFO) + if(qmltypes_file) + list(APPEND qml_module_files ${output_dir}/${qmltypes_file}) + endif() + foreach(qml_file_src IN LISTS arg_FILES) if(NOT qml_file_src MATCHES "\\.(qml)$") list(APPEND non_qml_files ${qml_file_src}) @@ -1120,6 +1134,7 @@ function(qt6_target_compile_qml_to_cpp target) DEPENDS ${qmltc_executable} "${file_absolute}" + ${qml_module_files} ) set_source_files_properties(${compiled_header} ${compiled_cpp} diff --git a/src/qml/qmltc/supportlibrary/qqmlcppbinding.cpp b/src/qml/qmltc/supportlibrary/qqmlcppbinding.cpp new file mode 100644 index 0000000000..142f9be024 --- /dev/null +++ b/src/qml/qmltc/supportlibrary/qqmlcppbinding.cpp @@ -0,0 +1,147 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qqmlcppbinding_p.h" + +#include <QtQml/qqmlengine.h> +#include <QtQml/qqmlcontext.h> +#include <QtCore/qmetaobject.h> + +#include <private/qqmltypedata_p.h> +#include <private/qqmlpropertybinding_p.h> +#include <private/qqmlbinding_p.h> +#include <private/qv4qmlcontext_p.h> +#include <private/qqmlproperty_p.h> +#include <private/qqmlbinding_p.h> + +QT_BEGIN_NAMESPACE + +template<typename CreateBinding> +inline decltype(auto) createBindingInScope(QObject *thisObject, CreateBinding create) +{ + QQmlEngine *qmlengine = qmlEngine(thisObject); + Q_ASSERT(qmlengine); + QV4::ExecutionEngine *v4 = qmlengine->handle(); + Q_ASSERT(v4); + + QQmlData *ddata = QQmlData::get(thisObject); + Q_ASSERT(ddata && ddata->outerContext); + QQmlRefPointer<QQmlContextData> ctxtdata = QQmlRefPointer<QQmlContextData>(ddata->outerContext); + + QV4::Scope scope(v4); + QV4::ExecutionContext *executionCtx = v4->scriptContext(); + QV4::Scoped<QV4::QmlContext> qmlContext( + scope, QV4::QmlContext::create(executionCtx, ctxtdata, thisObject)); + + return create(ctxtdata, qmlContext); +} + +QUntypedPropertyBinding +QQmlCppBinding::createBindingForBindable(const QV4::ExecutableCompilationUnit *unit, + QObject *thisObject, qsizetype functionIndex, + QObject *bindingTarget, int metaPropertyIndex, + int valueTypePropertyIndex, const QString &propertyName) +{ + Q_UNUSED(propertyName); + + QV4::Function *v4Function = unit->runtimeFunctions.value(functionIndex, nullptr); + if (!v4Function) { + // TODO: align with existing logging of such + qCritical() << "invalid JavaScript function index (internal error)"; + return QUntypedPropertyBinding(); + } + if (metaPropertyIndex < 0) { + // TODO: align with existing logging of such + qCritical() << "invalid meta property index (internal error)"; + return QUntypedPropertyBinding(); + } + + const QMetaObject *mo = bindingTarget->metaObject(); + Q_ASSERT(mo); + QMetaProperty property = mo->property(metaPropertyIndex); + Q_ASSERT(valueTypePropertyIndex == -1 || QString::fromUtf8(property.name()) == propertyName); + + return createBindingInScope( + thisObject, + [&](const QQmlRefPointer<QQmlContextData> &ctxt, QV4::ExecutionContext *scope) { + auto index = QQmlPropertyIndex(property.propertyIndex(), valueTypePropertyIndex); + return QQmlPropertyBinding::create(property.metaType(), v4Function, thisObject, + ctxt, scope, bindingTarget, index); + }); +} + +void QQmlCppBinding::createBindingForNonBindable(const QV4::ExecutableCompilationUnit *unit, + QObject *thisObject, qsizetype functionIndex, + QObject *bindingTarget, int metaPropertyIndex, + int valueTypePropertyIndex, + const QString &propertyName) +{ + Q_UNUSED(propertyName); + + QV4::Function *v4Function = unit->runtimeFunctions.value(functionIndex, nullptr); + if (!v4Function) { + // TODO: align with existing logging of such + qCritical() << "invalid JavaScript function index (internal error)"; + return; + } + if (metaPropertyIndex < 0) { + // TODO: align with existing logging of such + qCritical() << "invalid meta property index (internal error)"; + return; + } + + const QMetaObject *mo = bindingTarget->metaObject(); + Q_ASSERT(mo); + QMetaProperty property = mo->property(metaPropertyIndex); + Q_ASSERT(valueTypePropertyIndex != -1 || QString::fromUtf8(property.name()) == propertyName); + + createBindingInScope( + thisObject, + [&](const QQmlRefPointer<QQmlContextData> &ctxt, QV4::ExecutionContext *scope) -> void { + QQmlBinding *binding = QQmlBinding::create(property.metaType(), v4Function, + thisObject, ctxt, scope); + // almost as in qv4objectwrapper.cpp:535 + Q_ASSERT(!property.isAlias()); // we convert aliases to (almost) real properties + binding->setTarget(bindingTarget, property.propertyIndex(), false, + valueTypePropertyIndex); + QQmlPropertyPrivate::setBinding(binding); + }); +} + +QT_END_NAMESPACE diff --git a/src/qml/qmltc/supportlibrary/qqmlcppbinding_p.h b/src/qml/qmltc/supportlibrary/qqmlcppbinding_p.h new file mode 100644 index 0000000000..5c4a10e39d --- /dev/null +++ b/src/qml/qmltc/supportlibrary/qqmlcppbinding_p.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQMLCPPBINDING_P_H +#define QQMLCPPBINDING_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtCore/qobject.h> +#include <QtCore/qproperty.h> +#include <QtCore/qurl.h> + +#include <QtQml/qqmlengine.h> +#include <QtQml/qqmlcontext.h> +#include <QtCore/qmetaobject.h> + +#include <private/qqmltypedata_p.h> +#include <private/qqmlpropertybinding_p.h> +#include <private/qqmlbinding_p.h> +#include <private/qv4qmlcontext_p.h> +#include <private/qqmlproperty_p.h> +#include <private/qqmlbinding_p.h> + +QT_BEGIN_NAMESPACE + +struct Q_QML_PRIVATE_EXPORT QQmlCppBinding +{ + // TODO: this might instead be put into the QQmlEngine or QQmlAnyBinding? + static QUntypedPropertyBinding + createBindingForBindable(const QV4::ExecutableCompilationUnit *unit, QObject *thisObject, + qsizetype functionIndex, QObject *bindingTarget, int metaPropertyIndex, + int valueTypePropertyIndex, const QString &propertyName); + + static void createBindingForNonBindable(const QV4::ExecutableCompilationUnit *unit, + QObject *thisObject, qsizetype functionIndex, + QObject *bindingTarget, int metaPropertyIndex, + int valueTypePropertyIndex, + const QString &propertyName); +}; + +QT_END_NAMESPACE + +#endif // QQMLCPPBINDING_P_H diff --git a/src/qml/qmltc/supportlibrary/qqmlcpponassignment.cpp b/src/qml/qmltc/supportlibrary/qqmlcpponassignment.cpp new file mode 100644 index 0000000000..a471ef2594 --- /dev/null +++ b/src/qml/qmltc/supportlibrary/qqmlcpponassignment.cpp @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qqmlcpponassignment_p.h" + +QT_BEGIN_NAMESPACE + +void QQmlCppOnAssignmentHelper::set(QQmlPropertyValueInterceptor *interceptor, + const QQmlProperty &property) +{ + interceptor->setTarget(property); +} + +void QQmlCppOnAssignmentHelper::set(QQmlPropertyValueSource *valueSource, + const QQmlProperty &property) +{ + valueSource->setTarget(property); +} + +QT_END_NAMESPACE diff --git a/src/qml/qmltc/supportlibrary/qqmlcpponassignment_p.h b/src/qml/qmltc/supportlibrary/qqmlcpponassignment_p.h new file mode 100644 index 0000000000..5fb35aab97 --- /dev/null +++ b/src/qml/qmltc/supportlibrary/qqmlcpponassignment_p.h @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQMLCPPONASSIGNMENT_P_H +#define QQMLCPPONASSIGNMENT_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <private/qqmlpropertyvalueinterceptor_p.h> +#include <QtQml/qqmlpropertyvaluesource.h> + +QT_BEGIN_NAMESPACE + +/*! \internal + + Helper class that provides setTarget() functionality for both value + interceptors and value sources. + + Property value sources could be problematic because QQuickAbstractAnimation + changes access specifier of QQmlPropertyValueSource::setTarget() to private + (unintentionally?). This API allows to avoid manual casts to base types as + the C++ compiler would implicitly cast derived classes in this case. +*/ +struct Q_QML_PRIVATE_EXPORT QQmlCppOnAssignmentHelper +{ + // TODO: in theory, this API might just accept QObject * and int that would + // give the QMetaProperty. using the meta property, one could create + // QQmlProperty with a call to QQmlProperty::restore() (if there's an + // overload that takes QMetaProperty instead of QQmlPropertyData - which is + // also possible to add by using QQmlPropertyData::load()) + static void set(QQmlPropertyValueInterceptor *interceptor, const QQmlProperty &property); + static void set(QQmlPropertyValueSource *valueSource, const QQmlProperty &property); +}; + +QT_END_NAMESPACE + +#endif // QQMLCPPONASSIGNMENT_P_H |