diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2022-05-25 15:46:13 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2022-06-24 14:47:58 +0200 |
commit | 23fdccf7f3d4ace5b0cb9f04d9b941a1235629b8 (patch) | |
tree | ae57cde18d0c43ff09e49fa449187b7cc4accde0 /src/qml | |
parent | 1b6069798aa7088457eb5e033aa2fa1d809ec3e4 (diff) |
QmlCompiler: Inline translation methods
We hardcode them into QQmlJSTypePropagator and QQmlJSCodegenerator for
now. This is OK for builtins.
Task-number: QTBUG-101387
Change-Id: Ifab46083b3a782f009859ce969c283d5bb2b4e8b
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml')
-rw-r--r-- | src/qml/qml/qqml.cpp | 44 | ||||
-rw-r--r-- | src/qml/qml/qqmlbuiltinfunctions.cpp | 77 | ||||
-rw-r--r-- | src/qml/qml/qqmlbuiltinfunctions_p.h | 1 | ||||
-rw-r--r-- | src/qml/qml/qqmlprivate.h | 2 |
4 files changed, 75 insertions, 49 deletions
diff --git a/src/qml/qml/qqml.cpp b/src/qml/qml/qqml.cpp index 2f037548a8..aee0020a67 100644 --- a/src/qml/qml/qqml.cpp +++ b/src/qml/qml/qqml.cpp @@ -19,6 +19,7 @@ #include <private/qv4qobjectwrapper_p.h> #include <private/qv4identifiertable_p.h> #include <private/qv4errorobject_p.h> +#include <private/qqmlbuiltinfunctions_p.h> #include <private/qqmlfinalizer_p.h> #include <QtCore/qmutex.h> @@ -763,18 +764,26 @@ void AOTCompiledContext::setReturnValueUndefined() const } } -static void captureFallbackProperty( - QObject *object, int coreIndex, int notifyIndex, bool isConstant, - QQmlContextData *qmlContext) +static QQmlPropertyCapture *propertyCapture(const QQmlContextData *qmlContext) { - if (!qmlContext || isConstant) - return; + if (!qmlContext) + return nullptr; QQmlEngine *engine = qmlContext->engine(); Q_ASSERT(engine); QQmlEnginePrivate *ep = QQmlEnginePrivate::get(engine); Q_ASSERT(ep); - if (QQmlPropertyCapture *capture = ep->propertyCapture) + return ep->propertyCapture; +} + +static void captureFallbackProperty( + QObject *object, int coreIndex, int notifyIndex, bool isConstant, + const QQmlContextData *qmlContext) +{ + if (isConstant) + return; + + if (QQmlPropertyCapture *capture = propertyCapture(qmlContext)) capture->captureProperty(object, coreIndex, notifyIndex); } @@ -782,14 +791,10 @@ static void captureObjectProperty( QObject *object, const QQmlPropertyCache *propertyCache, const QQmlPropertyData *property, QQmlContextData *qmlContext) { - if (!qmlContext || property->isConstant()) + if (property->isConstant()) return; - QQmlEngine *engine = qmlContext->engine(); - Q_ASSERT(engine); - QQmlEnginePrivate *ep = QQmlEnginePrivate::get(engine); - Q_ASSERT(ep); - if (QQmlPropertyCapture *capture = ep->propertyCapture) + if (QQmlPropertyCapture *capture = propertyCapture(qmlContext)) capture->captureProperty(object, propertyCache, property); } @@ -1059,6 +1064,21 @@ bool AOTCompiledContext::captureQmlContextPropertyLookup(uint index) const return false; } +void AOTCompiledContext::captureTranslation() const +{ + if (QQmlPropertyCapture *capture = propertyCapture(qmlContext)) + capture->captureTranslation(); +} + +QString AOTCompiledContext::translationContext() const +{ +#if QT_CONFIG(translation) + return QV4::GlobalExtensions::currentTranslationContext(engine->handle()); +#else + return QString(); +#endif +} + QMetaType AOTCompiledContext::lookupResultMetaType(uint index) const { QV4::Lookup *l = compilationUnit->runtimeLookups + index; diff --git a/src/qml/qml/qqmlbuiltinfunctions.cpp b/src/qml/qml/qqmlbuiltinfunctions.cpp index 4702797e74..80bd427970 100644 --- a/src/qml/qml/qqmlbuiltinfunctions.cpp +++ b/src/qml/qml/qqmlbuiltinfunctions.cpp @@ -1942,37 +1942,11 @@ ReturnedValue GlobalExtensions::method_qsTranslateNoOp(const FunctionObject *b, return argv[1].asReturnedValue(); } -/*! - \qmlmethod string Qt::qsTr(string sourceText, string disambiguation, int n) - - Returns a translated version of \a sourceText, optionally based on a - \a disambiguation string and value of \a n for strings containing plurals; - otherwise returns \a sourceText itself if no appropriate translated string - is available. - - If the same \a sourceText is used in different roles within the - same translation context, an additional identifying string may be passed in - for \a disambiguation. - - Example: - \snippet qml/qsTr.qml 0 - - \sa {Internationalization and Localization with Qt Quick} -*/ -ReturnedValue GlobalExtensions::method_qsTr(const FunctionObject *b, const Value *, const Value *argv, int argc) +QString GlobalExtensions::currentTranslationContext(ExecutionEngine *engine) { - QV4::Scope scope(b); - if (argc < 1) - THROW_GENERIC_ERROR("qsTr() requires at least one argument"); - if (!argv[0].isString()) - THROW_GENERIC_ERROR("qsTr(): first argument (sourceText) must be a string"); - if ((argc > 1) && !argv[1].isString()) - THROW_GENERIC_ERROR("qsTr(): second argument (disambiguation) must be a string"); - if ((argc > 2) && !argv[2].isNumber()) - THROW_GENERIC_ERROR("qsTr(): third argument (n) must be a number"); - QString context; - CppStackFrame *frame = scope.engine->currentStackFrame; + CppStackFrame *frame = engine->currentStackFrame; + // The first non-empty source URL in the call stack determines the translation context. while (frame && context.isEmpty()) { if (CompiledData::CompilationUnitBase *baseUnit = frame->v4Function->compilationUnit) { @@ -1992,7 +1966,7 @@ ReturnedValue GlobalExtensions::method_qsTr(const FunctionObject *b, const Value } if (context.isEmpty()) { - if (QQmlRefPointer<QQmlContextData> ctxt = scope.engine->callingQmlContext()) { + if (QQmlRefPointer<QQmlContextData> ctxt = engine->callingQmlContext()) { QString path = ctxt->urlString(); int lastSlash = path.lastIndexOf(QLatin1Char('/')); int lastDot = path.lastIndexOf(QLatin1Char('.')); @@ -2001,13 +1975,42 @@ ReturnedValue GlobalExtensions::method_qsTr(const FunctionObject *b, const Value } } - QString text = argv[0].toQStringNoThrow(); - QString comment; - if (argc > 1) - comment = argv[1].toQStringNoThrow(); - int n = -1; - if (argc > 2) - n = argv[2].toInt32(); + return context; +} + +/*! + \qmlmethod string Qt::qsTr(string sourceText, string disambiguation, int n) + + Returns a translated version of \a sourceText, optionally based on a + \a disambiguation string and value of \a n for strings containing plurals; + otherwise returns \a sourceText itself if no appropriate translated string + is available. + + If the same \a sourceText is used in different roles within the + same translation context, an additional identifying string may be passed in + for \a disambiguation. + + Example: + \snippet qml/qsTr.qml 0 + + \sa {Internationalization and Localization with Qt Quick} +*/ +ReturnedValue GlobalExtensions::method_qsTr(const FunctionObject *b, const Value *, const Value *argv, int argc) +{ + QV4::Scope scope(b); + if (argc < 1) + THROW_GENERIC_ERROR("qsTr() requires at least one argument"); + if (!argv[0].isString()) + THROW_GENERIC_ERROR("qsTr(): first argument (sourceText) must be a string"); + if ((argc > 1) && !argv[1].isString()) + THROW_GENERIC_ERROR("qsTr(): second argument (disambiguation) must be a string"); + if ((argc > 2) && !argv[2].isNumber()) + THROW_GENERIC_ERROR("qsTr(): third argument (n) must be a number"); + + const QString context = currentTranslationContext(scope.engine); + const QString text = argv[0].toQStringNoThrow(); + const QString comment = argc > 1 ? argv[1].toQStringNoThrow() : QString(); + const int n = argc > 2 ? argv[2].toInt32() : -1; if (QQmlEnginePrivate *ep = (scope.engine->qmlEngine() ? QQmlEnginePrivate::get(scope.engine->qmlEngine()) : nullptr)) if (ep->propertyCapture) diff --git a/src/qml/qml/qqmlbuiltinfunctions_p.h b/src/qml/qml/qqmlbuiltinfunctions_p.h index fad30e437f..026a79d1c7 100644 --- a/src/qml/qml/qqmlbuiltinfunctions_p.h +++ b/src/qml/qml/qqmlbuiltinfunctions_p.h @@ -209,6 +209,7 @@ struct Q_QML_PRIVATE_EXPORT GlobalExtensions { static void init(Object *globalObject, QJSEngine::Extensions extensions); #if QT_CONFIG(translation) + static QString currentTranslationContext(ExecutionEngine *engine); static ReturnedValue method_qsTranslate(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); static ReturnedValue method_qsTranslateNoOp(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); static ReturnedValue method_qsTr(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); diff --git a/src/qml/qml/qqmlprivate.h b/src/qml/qml/qqmlprivate.h index b7ec09c857..c3833e7848 100644 --- a/src/qml/qml/qqmlprivate.h +++ b/src/qml/qml/qqmlprivate.h @@ -600,6 +600,8 @@ namespace QQmlPrivate // Run QQmlPropertyCapture::captureProperty() without retrieving the value. bool captureLookup(uint index, QObject *object) const; bool captureQmlContextPropertyLookup(uint index) const; + void captureTranslation() const; + QString translationContext() const; QMetaType lookupResultMetaType(uint index) const; void storeNameSloppy(uint nameIndex, void *value, QMetaType type) const; QJSValue javaScriptGlobalProperty(uint nameIndex) const; |