1// Copyright (C) 2022 The Qt Company Ltd.
 
    2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
 
    5\page qtqml-qml-script-compiler.html
 
    6\title QML script compiler
 
    7\brief A tool to compile functions and expressions in QML.
 
   10The QML script compiler compiles functions and expressions in QML and JavaScript
 
   11files to a byte code that can be interpreted or Just-in-time compiled by the
 
   14In addition, it compiles some functions and expressions in QML files into C++
 
   15code, within limitations set by the nature of JavaScript. It generates C++ code
 
   16for functions that can be exhaustively analyzed. The following flow chart
 
   17explains the compilation workflow.
 
   19\image qmlsc-compilation-scheme.png
 
   21\section1 qmlsc and qmlcachegen
 
   23QML script compiler is available in two versions. One is \e qmlcachegen, which
 
   24is a part of the \l{Qt Quick Compiler}. Another one is \e qmlsc, which is a part
 
   25of the commercial-only add-on \e{Qt Quick Compiler Extensions}.
 
   27The base functionality of the QML script compiler is available in both the
 
   28\e qmlsc tool and the \e qmlcachegen tool. \e qmlsc can generate more efficient
 
   29C++ code when run in \e direct mode. It is enabled by passing \c{--direct-calls}
 
   30via \c QT_QMLCACHEGEN_ARGUMENTS to \l{qt_add_qml_module}. In direct mode
 
   31\e qmlsc expects that all C++ types used in your QML code can be made available
 
   32to the generated code by simple C++ include statements. This means you have to
 
   33link to private Qt APIs by passing Qt::QuickPrivate, Qt::QmlPrivate, etc to
 
   34link_libraries() in CMake. \e qmlcachegen does not implement direct mode and
 
   35ignores the \e{--direct-calls} option. Furthermore, \e qmlsc accepts
 
   36a \e {--static} option that you can also pass via \e QT_QMLCACHEGEN_ARGUMENTS
 
   37in \l qt_add_qml_module. In \e static mode, \e qmlsc ignores shadowing of
 
   38properties. This is dangerous if your code actually shadows properties, but
 
   39allows for more JavaScript code to be compiled to C++. \e qmlcachegen ignores
 
   40the \e {--static} option.
 
   42\section1 Limitations when compiling JavaScript to C++
 
   44Many JavaScript constructs cannot be efficiently represented in C++. The QML
 
   45script compiler skips the C++ code generation for functions that contain such
 
   46constructs and only generates byte code to be interpreted or run through the
 
   47Just-in-time compiler. Most common QML expressions are rather simple: value
 
   48lookups on QObjects, arithmetics, simple if/else or loop constructs. Those can
 
   49easily be expressed in C++, and doing so makes your application run faster.