// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
/*!

\page qtqml-javascript-finetuning.html
\title Configuring the JavaScript Engine
\brief Describes the environment variables available, to control how Javascript is run.

Running JavaScript code can be influenced by a few environment variables, particularly:

\table
    \header
        \li Environment Variable
        \li Description
    \row
        \li \c{QV4_JIT_CALL_THRESHOLD}
        \li The JavaScript engine contains a Just-In-Time compiler (JIT). The JIT will compile
            frequently run JavaScript functions into machine code to run faster. This
            environment variable determines how often a function needs to be run to be
            considered for JIT compilation. The default value is 3 times.
    \row
        \li \c{QV4_FORCE_INTERPRETER}
        \li Setting this environment variable runs all functions and expressions through the
            interpreter. The JIT is never used, no matter how often a function or expression is
            called. Functions and expressions may still be compiled ahead of time using
            \l{qmlcachegen} or \l{qmlsc}, but only the generated byte code is used at run time. Any
            generated C++ code and the machine code resulting from it is ignored.
    \row
        \li \c{QV4_JS_MAX_STACK_SIZE}
        \li The JavaScript engine reserves a special memory area as a stack to run JavaScript.
            This stack is separate from the C++ stack. Usually this area is 4MB in size. If this
            environment variable contains a number, the JavaScript engine interprets it as the
            size of the memory area, in bytes, to be allocated as the JavaScript stack.
    \row
        \li \c{QV4_GC_MAX_STACK_SIZE}
        \li In addition to the regular JavaScript stack, the JavaScript engine keeps another stack
            for the garbage collector, usually 2MB of memory. If the garbage collector needs to
            handle an excessive number of objects at the same time, this stack might overrun.
            If it contains a number, this environment variable is interpreted as the size in bytes
            of the memory area that will be allocated as the stack for the garbage collector.
    \row
        \li \c{QV4_STACK_SOFT_LIMIT}
        \li When this environment variable is set, the JavaScript engine will throw a "RangeError:
            Maximum call stack size exceeded" exception once the call stack usage reaches the
            defined soft limit. If the variable is not set, the engine defaults to runtime detected
            or Qt predefined limits depending on the OS.

    \row
        \li \c{QV4_CRASH_ON_STACKOVERFLOW}
        \li Usually the JavaScript engine tries to catch C++ stack overflows caused by
            excessively recursive JavaScript code, and generates a non-fatal error condition.
            There are separate recursion checks for compiling JavaScript and running JavaScript. A
            stack overflow when compiling JavaScript indicates that the code contains deeply nested
            objects and functions. A stack overflow at run-time indicates that the code results in
            a deeply recursive program. The check for this is only indirectly related to the
            JavaScript stack size mentioned above, as each JavaScript function call consumes stack
            space on both, the C++ and the JavaScript stack. The code that checks for excessive
            recursion is necessarily conservative, as the available stack size depends on many
            factors and can often be customized by the user. With this environment variable set, the
            JavaScript engine does not check for stack overflows when compiling or running
            JavaScript and will not generate exceptions for them. Instead, when the stack overflows
            the program attempts an invalid memory access. This most likely terminates the
            program. In turn, the program gets to use up all the stack space the operating system
            can provide.
            \warning malicious code may be able to evade the termination and access unexpected
                     memory locations this way.
    \row
        \li \c{QV4_MAX_CALL_DEPTH}
        \li Stack overflows when running (as opposed to compiling) JavaScript are prevented by
            controlling the call depth: the number of nested function invocations. By
            default, an exception is generated if the call depth exceeds a maximum number tuned
            to the platform's default stack size. If the \c{QV4_MAX_CALL_DEPTH} environment
            variable contains a number, this number is used as maximum call depth. Beware that
            the recursion limit when compiling JavaScript is not affected. The default maximum
            call depth is 1234 on most platforms. On QNX it is 640 because on QNX the default
            stack size is smaller than on most platforms.
    \row
        \li \target{QV4_GC_TIMELIMIT}
            \c{QV4_GC_TIMELIMIT}
        \li This value is used to tell the engine how much time it should spend in each
            incremental garbage collection step. It can either be a positive number,
            specifying the timelimit in milliseconds, or 0. If the value is 0,
            garbage collection becomes non-incremental.
    \row
        \li \c{QV4_MM_AGGRESSIVE_GC}
        \li Setting this environment variable runs the garbage collector before each memory
            allocation. This is very expensive at run-time, but it quickly uncovers many memory
            management errors, for example the manual deletion of an object belonging to the QML
            engine from C++.
    \row
        \li \c{QV4_PROFILE_WRITE_PERF_MAP}
        \li On Linux, the \c perf utility can be used to profile programs. To analyze JIT-compiled
            JavaScript functions, it needs to know about their names and locations in memory. To
            provide this information, there's a convention to create a special file called
            \c{perf-<pid>.map} in \e{/tmp} which perf then reads. This environment variable, if
            set, causes the JIT to generate this file.
    \row
        \li \c{QV4_SHOW_BYTECODE}
        \li Outputs the IR bytecode generated by Qt to the console.
            Has to be combined with \c{QML_DISABLE_DISK_CACHE} or already cached bytecode will not
            be shown.
    \row
        \li \c{QV4_DUMP_BASIC_BLOCKS}
        \li Outputs the basic blocks of each function compiled ahead of time. The details of the
            blocks are printed to the console. Additionally, control flow graphs with the byte code
            for each block are generated in the DOT format for each compiled function. The value of
            \c {QV4_DUMP_BASIC_BLOCKS} is used as the path to the folder where the DOT files should
            be generated. If the path is any of ["-", "1", "true"] or if files can't be opened,
            the graphs are dumped to stdout instead.
    \row
        \li \c{QV4_VALIDATE_BASIC_BLOCKS}
        \li Performs checks on the basic blocks of a function compiled ahead of time to validate
            its structure and coherence. If the validation fails, an error message is printed to
            the console.
\endtable

\l{The QML Disk Cache} accepts further environment variables that allow fine tuning its behavior.
In particular \c{QML_DISABLE_DISK_CACHE} may be useful for debugging.

*/

