JerryScript Documentation

repository·master·Indexed 27 days ago

https://github.com/jerryscript-project/jerryscript

An ultra-lightweight JavaScript engine designed for resource-constrained IoT devices and microcontrollers, optimized for low memory consumption (less than 64 KB RAM). The documentation covers building the engine via Python scripts or CMake, configuring features using profile files, and deploying to hardware targets including ESP32 (via esp-idf), ESP8266 (via ESP8266 RTOS SDK), and Particle Photon. It also details available debugging tools such as the console debugger client and Chrome webtool.

Tokens
105.2K
Snippets
358
Records
446
Agent score
90%

What's inside JerryScript

  1. Understand the JerryScript High-Level Design

    master

    JerryScript operates using two primary components: the Parser and the Virtual Machine (VM).

    1. Parser: Translates ECMAScript source code into a specific byte-code format. It uses a recursive descent approach and does not build an Abstract Syntax Tree (AST) to save memory.
    2. Virtual Machine (VM): Executes the prepared byte-code via interpretation.

    Key subcomponents of the Parser include the Lexer (tokenization), Scanner (pre-scanning for ambiguous tokens like /), Expression Parser, and Statement Parser.

  2. Use available JerryScript debugger tools

    master

    JerryScript provides several tools for debugging JavaScript execution in IoT environments:

    1. JerryScript console debugger client: A Python-based client (jerry_client.py) for console-based debugging.
    2. IoT.js Code: A specialized code repository for IoT-related JavaScript execution (https://github.com/jerryscript-project/iotjscode).
    3. JerryScript debugger Chrome webtool: A web-based debugger tool designed for use in the Chrome browser (https://github.com/jerryscript-project/jerryscript-debugger-ts).
  3. Understand the Virtual Machine (VM) implementation

    master

    The Virtual Machine is an interpreter that executes byte-code instructions sequentially.

    Key implementation details:

    • The entry point for interpretation is vm_run located in ./jerry-core/vm/vm.c.
    • The main loop, vm_loop, is non-recursive. This design choice prevents function calls from burdening the system stack, as the VM returns rather than calling itself recursively.
  4. Run JerryScript on ESP8266 and ESP32 boards

    master

    JerryScript can be deployed on Espressif hardware using the following SDKs:

    Both targets follow the ESP-IDF build system style and directory structure. For specific implementation details and setup instructions, refer to the target-specific documentation in the repository:

    • For ESP8266: esp8266-rtos-sdk/README.md
    • For ESP32: esp-idf/README.md
  5. Follow JerryScript function declaration standards

    master

    When writing code for JerryScript, use a verbose function declaration format to reduce maintenance costs and improve readability.

    Key requirements for a compliant function declaration:

    1. Doxygen-style header: Include a short overview, a detailed explanation (if necessary), and an @return description.
    2. Argument documentation: Use /**< description */ inline for each argument.
    3. Return type placement: Place the return type on a separate line above the function name.
    4. Closing comment: Include a comment at the end of the function body containing the function name (e.g., } /* function_name */).
    /**
     * Short overview about the purpose of this function.
     *
     * A more detailed explanation if needed.
     *
     * Note:
     *   Extra notes if needed.
     *
     * @return short description about the value
     *         returned by the function
     */
    return_value_type_t
    function_name (argument1, /**< description of argument1 */
                   argument2, /**< description of argument2 */
                   ...
                   argument_n, /**< description of argument n */
    {
    
      /* Function body. */
    
    } /* function_name */
  6. Handle references in property operations

    master

    When performing property operations like jerry_object_get or jerry_object_set, you must manage the references for both the retrieved/set value and the result of the operation itself.

    • jerry_object_get: Returns a new live reference to the property value. If the operation fails, it returns a live reference to an error object. Both must be freed.
    • jerry_object_set:
      1. The value being set (new_prop_value) must be released.
      2. The result of the operation (result) is a new live reference (representing a primitive or an error) and must also be released.
      jerry_value_t new_prop_value = jerry_number (2.718);
      jerry_value_t result = jerry_object_set (..., new_prop_value);
    
      /* The new_prop_value can be passed to other JerryScript API
       * functions before the jerry_value_free () call. */
      jerry_value_free (new_prop_value);
    
      /* The reference stored in the 'result' variable is live whether
       * the operation is successful or not, and must also be freed. */
      if (jerry_value_is_exception (result))
      {
        /* Errors can be handled here. */
      }
      jerry_value_free (result);
  7. Build and install JerryScript with default configuration

    master

    To use the JerryScript library, you must clone and build it for your target environment. The following steps demonstrate building with the default configuration and installing it to a local directory on a Linux system.

    After installation, the library headers and libraries will be located in the example_install/{include,lib} directories. To use pkg-config to manage includes and libraries, you must export the PKG_CONFIG_PATH pointing to the installation's lib/pkgconfig directory.

    $ mkdir jerry
    $ cd jerry
    $ git clone https://github.com/jerryscript-project/jerryscript.git
    $ jerryscript/tools/build.py --builddir=$(pwd)/example_build --cmake-param="-DCMAKE_INSTALL_PREFIX=$(pwd)/example_install/"
    $ make -C $(pwd)/example_build install
    
    # Set up pkg-config
    $ export PKG_CONFIG_PATH=$(pwd)/example_install/lib/pkgconfig/
    
    # Verify pkg-config works
    $ pkg-config --cflags --libs libjerry-core libjerry-port libjerry-ext libjerry-math