Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtqml-qml-script-compiler.qdoc
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qtqml-qml-script-compiler.html
6\title QML script compiler
7\brief A tool to compile functions and expressions in QML.
8\keyword qmlsc
9\ingroup qtqml-tooling
10
11The QML script compiler compiles functions and expressions in QML and JavaScript
12files to a byte code that can be interpreted or Just-in-time compiled by the
13QML engine.
14
15In addition, it compiles some functions and expressions in QML files into C++
16code, within limitations set by the nature of JavaScript. It generates C++ code
17for functions that can be exhaustively analyzed. The following flow chart
18explains the compilation workflow.
19
20\image qmlsc-compilation-scheme.png
21
22QML script compiler is available in two versions. One is \e qmlcachegen, which
23is a part of the \l{Qt Quick Compiler}. Another one is \e qmlsc, which is a part
24of the commercial-only add-on \e{Qt Quick Compiler Extensions}.
25
26\section1 qmlcachegen
27\e qmlcachegen uses the meta-object system and generates lookups and stores them in a
28central place, a compilation unit. The compilation unit contains a representation of
29document structure, compact byte code representation for each function and expression,
30and native code for functions and bindings that compiler fully understands.
31The byte code in a compilation unit can be used by the QML engine to avoid re-compilation
32and to speed up execution.
33
34\section1 qmlsc
35\e qmlsc, on the flip side, extends the base functionality of qmlcachegen by providing
36two extra modes.
37
38\list
39\li \l {static mode}
40\li \l {direct mode}
41\endlist
42
43\section2 static mode
44In static mode, qmlsc assumes that no properties of any types exposed to C++ can be
45shadowed by derived types. It eliminates the shadow checking mechanism and allows more
46JavaScript code to be compiled to C++ and eventually generates faster code.
47
48To enable static mode in qmlsc, you should pass \c{--static} via \c{QT_QMLCACHEGEN_ARGUMENTS} to \l{qt_add_qml_module}.
49\badcode
50 qt_add_qml_module(someTarget
51 ...
52 )
53
54 set_target_properties(someTarget PROPERTIES
55 QT_QMLCACHEGEN_ARGUMENTS "--static"
56 )
57\endcode
58
59\warning qmlsc static-mode generates invalid code if any properties are shadowed in
60the QML document.
61
62\section2 direct mode
63In direct mode, qmlsc assumes that all C++ types used in your QML code are available
64and can be included as C++ headers to the generated code. Then the generated code
65accesses or modifies properties by directly calling getters, setters and invokable
66functions in those headers which makes the execution even faster. This means you have to
67link to private Qt APIs in CMake.
68
69\warning Private Qt APIs change often. You will need to recompile Qt for each new version.
70
71\warning If a type is only defined in a plugin or has no header, you can’t use it in direct mode.
72
73To enable direct mode, you should consider the followings:
74
75\list
76 \li you should pass \c{--direct-calls} via \c{QT_QMLCACHEGEN_ARGUMENTS} to \l{qt_add_qml_module}.
77
78\badcode
79 qt_add_qml_module(someTarget
80 ...
81 )
82
83 set_target_properties(someTarget PROPERTIES
84 QT_QMLCACHEGEN_ARGUMENTS "--direct-calls"
85 )
86\endcode
87
88 \li Link all the relavant private Qt modules instead of their public counterparts.
89\badcode
90 qt_add_qml_module(someTarget
91 ...
92 )
93
94 target_link_libraries(someTarget PRIVATE
95 Qt::QmlPrivate
96 Qt::QuickPrivate
97 ...
98 )
99\endcode
100
101 \li Do not set the \c{PLUGIN_TARGET} to be the same as the backing library target.
102\badcode
103 # direct mode will not function in this setup.
104 qt_add_qml_module(someTarget
105 PLUGIN_TARGET someTarget
106 ...
107 )
108\endcode
109\endlist
110
111\section1 Limitations when compiling JavaScript to C++
112
113Many JavaScript constructs cannot be efficiently represented in C++. The QML
114script compiler skips the C++ code generation for functions that contain such
115constructs and only generates byte code to be interpreted or run through the
116Just-in-time compiler. Most common QML expressions are rather simple: value
117lookups on QObjects, arithmetics, simple if/else or loop constructs. Those can
118easily be expressed in C++, and doing so makes your application run faster.
119
120*/