### Custom Profile Content Example Source: https://github.com/jerryscript-project/jerryscript/blob/master/jerry-core/profiles/README.md Define custom build configurations by listing compile definitions. Each definition should be on a single line. Lines starting with '#' are ignored, and semicolons are not allowed. ```bash # Disable not needed features JERRY_BUILTIN_CONTAINER=0 JERRY_BUILTIN_DATAVIEW=0 JERRY_BUILTIN_TYPEDARRAY=0 ``` -------------------------------- ### JerryScript Output Example Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/os/mbedos/README.md After connecting and resetting the board, observe the JerryScript application output. This example shows the execution of a simple 'Hello, World!' script. ```text This test run the following script code: [print ('Hello, World!');] Hello, World! ``` -------------------------------- ### Build and Install JerryScript Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/03.API-EXAMPLE.md Clone and build JerryScript with default configuration. Install it to a user directory for use in your projects. This setup is necessary before using the embedding API. ```sh 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 ``` -------------------------------- ### Install Jerry Core Targets Source: https://github.com/jerryscript-project/jerryscript/blob/master/jerry-core/CMakeLists.txt Installs the Jerry Core library, its pkg-config file, the configuration header, and public include directories. ```cmake install(TARGETS ${JERRY_CORE_NAME} DESTINATION lib) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libjerry-core.pc DESTINATION lib/pkgconfig) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/jerryscript-config.h DESTINATION include) install(DIRECTORY ${INCLUDE_CORE_PUBLIC}/ DESTINATION include) ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/baremetal-sdk/espressif/esp8266-rtos-sdk/README.md Install dependencies for both JerryScript and the ESP8266 RTOS SDK. Extract the downloaded toolchain to prepare for building. ```sh # Assuming you are in jerry-esp folder. jerryscript/tools/apt-get-install-deps.sh # Install dependencies of ESP8266-RTOS-SDK. python -m pip install --user -r ESP8266_RTOS_SDK/requirements.txt # Extract the downloaded toolchain. tar -xvf xtensa-lx106-elf-gcc8_4_0-esp-2020r3-linux-amd64.tar.gz ``` -------------------------------- ### Execute JerryScript Example Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/03.API-EXAMPLE.md This command executes the compiled JerryScript example program. ```sh ./api-example-10 ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/os/riot/README.md Install the required dependencies for both JerryScript and the STM32F4-Discovery board. This includes the ARM GCC toolchain and OpenOCD. ```sh # Assuming you are in jerry-riot folder. jerryscript/tools/apt-get-install-deps.sh sudo apt install gcc-arm-none-eabi openocd minicom ``` -------------------------------- ### Build kconfig-frontend for NuttX Configuration Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/os/nuttx/README.md Build and install kconfig-frontend from source if it's not available via package manager. This tool is used for configuring NuttX. Ensure the installation directory is added to the PATH. ```bash # Assuming you are in jerry-nuttx folder. cd tools/kconfig-frontends ./configure \ --disable-nconf \ --disable-gconf \ --disable-qconf \ --disable-shared \ --enable-static \ --prefix=${PWD}/install make make install # Add install folder to PATH PATH=${PWD}/install/bin:$PATH ``` -------------------------------- ### Install Node.js Source: https://github.com/jerryscript-project/jerryscript/blob/master/tools/babel/README.md Commands to update apt packages and install Node.js on a Debian-based system. ```bash $ sudo apt update $ sudo apt install nodejs ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/baremetal-sdk/espressif/esp-idf/README.md Install project dependencies using provided scripts and pip. This step ensures all necessary libraries and tools are available for building. ```sh # Assuming you are in jerry-esp folder. jerryscript/tools/apt-get-install-deps.sh # Install dependencies of esp-idf. python -m pip install --user -r esp-idf/requirements.txt # Extract the downloaded toolchain. tar -xvf xtensa-esp32-elf-gcc8_4_0-esp-2021r2-linux-amd64.tar.gz ``` -------------------------------- ### Example: Custom Context Data Management Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/02.API-REFERENCE.md Demonstrates how to define, initialize, and deinitialize custom context data using managers. This example shows how to retrieve and use custom data within the JerryScript context. ```c #include "jerryscript.h" typedef struct { int my_data1; double my_data2; char *my_data3; } my_context_data_t; /* Define how context items will be initialized. */ static void my_context_data_new (void *user_data_p) { my_context_data_t *my_data_p = (my_context_data_t *) user_data_p; /* * Initialize my_data_p. JerryScript will store it on the current context and return it whenever * jerry_context_data () is called with a pointer to my_manager as defined below. */ } /* Define how context items will be deinitialized */ static void my_context_data_free (void *user_data_p) { my_context_data_t *my_data_p = ((my_context_data_t *) user_data_p); /* Perform any necessary cleanup on my_data. JerryScript will free the pointer after this function completes. */ } /* Wrap the creation and destruction functions into a manager */ static const jerry_context_data_manager_t my_manager = { .init_cb = my_context_data_new, .deinit_cb = my_context_data_free, .bytes_needed = sizeof (my_context_data_t) }; /* * Then, in some function in your code, you can retrieve an item of type my_context_data_t from the currently active * context such that JerryScript will create and store such an item if one was not previously created */ static void someplace_in_the_code (void) { my_context_data_t *my_data = (my_context_data_t *) jerry_context_data (&my_manager); /* Perform useful things using the data found in my_data */ } ``` -------------------------------- ### Verify Node.js Installation Source: https://github.com/jerryscript-project/jerryscript/blob/master/tools/babel/README.md Command to check the installed version of Node.js. ```bash $ nodejs --version Output: v8.10.0 ``` -------------------------------- ### Compile JerryScript Example Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/03.API-EXAMPLE.md This command compiles the C source file for the JerryScript example, linking against the necessary JerryScript libraries. Ensure that `libjerry-ext` is listed before `libjerry-port` in the `pkg-config` call. ```sh gcc api-example-10.c -o api-example-10 $(pkg-config --cflags --libs libjerry-core libjerry-ext libjerry-port libjerry-math) ``` -------------------------------- ### Execute JerryScript Test Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/os/riot/README.md Once connected to the RIOT prompt, type 'test' to run the JerryScript 'Hello, World!' example. ```sh > test This test run the following script code: [print ('Hello, World!');] Hello, World! ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/os/mbedos/README.md Install the required dependencies for both JerryScript and Mbed OS. This includes system packages, flashing tools, and Python requirements. ```sh # Assuming you are in jerry-mbedos folder. jerryscript/tools/apt-get-install-deps.sh sudo apt install stlink-tools pip install --user mbed-cli # Install Python dependencies of Mbed OS. pip install --user -r mbed-os/requirements.txt ``` -------------------------------- ### Install Build Dependencies Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/00.GETTING-STARTED.md Installs the minimum required dependencies for building JerryScript on Ubuntu. Ensure your system meets the version requirements for each package. ```bash sudo apt-get install gcc gcc-arm-none-eabi cmake cppcheck clang-format-15 python ``` -------------------------------- ### Install OpenWrt Build Requirements Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/os/openwrt/readme.md Installs necessary packages for building OpenWrt on Ubuntu. Ensure you have these dependencies before proceeding. ```sh $ sudo apt-get install git-core build-essential libssl-dev libncurses5-dev unzip gawk zlib1g-dev subversion mercurial ``` -------------------------------- ### Install JerryScript Build Dependencies Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/os/nuttx/README.md Install the necessary dependencies for building JerryScript using the provided script. This ensures all required tools and libraries are available. ```bash # Assuming you are in jerry-nuttx folder. jerryscript/tools/apt-get-install-deps.sh ``` -------------------------------- ### Install udev Rules for Flashing Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/os/zephyr/README.md Install udev rules to allow flashing the STM32F4-Discovery board as a regular user. This simplifies the flashing process. ```sh # Assuming you are in jerry-zephyr folder. sudo cp zephyr-toolchain-0.13.2/sysroots/x86_64-pokysdk-linux/usr/share/openocd/contrib/60-openocd.rules /etc/udev/rules.d sudo udevadm control --reload ``` -------------------------------- ### Compile API Example 2 Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/03.API-EXAMPLE.md Command to compile the C code using gcc and pkg-config to link the necessary JerryScript libraries. ```sh $ gcc api-example-2.c -o api-example-2 $(pkg-config --cflags --libs libjerry-core libjerry-port libjerry-math) ``` -------------------------------- ### Example JerryScript Output on ESP8266 Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/baremetal-sdk/espressif/esp8266-rtos-sdk/README.md This output demonstrates the successful execution of a simple JavaScript 'Hello, World!' script on the ESP8266 device after flashing and connecting. ```text I (43) boot: ESP-IDF v3.4-43-ge9516e4c 2nd stage bootloader I (43) boot: compile time 11:14:06 I (43) qio_mode: Enabling default flash chip QIO I (51) boot: SPI Speed : 40MHz I (57) boot: SPI Mode : QIO I (63) boot: SPI Flash Size : 2MB I (69) boot: Partition Table: I (75) boot: ## Label Usage Type ST Offset Length I (86) boot: 0 nvs WiFi data 01 02 00009000 00006000 I (97) boot: 1 phy_init RF data 01 01 0000f000 00001000 I (108) boot: 2 factory factory app 00 00 00010000 000f0000 I (120) boot: End of partition table I (126) esp_image: segment 0: paddr=0x00010010 vaddr=0x40210010 size=0x5c540 (378176) map 0x40210010: _stext at ??:? I (140) esp_image: segment 1: paddr=0x0006c558 vaddr=0x4026c550 size=0x0baa8 ( 47784) map I (153) esp_image: segment 2: paddr=0x00078008 vaddr=0x3ffe8000 size=0x0042c ( 1068) load I (167) esp_image: segment 3: paddr=0x0007843c vaddr=0x40100000 size=0x00080 ( 128) load I (181) esp_image: segment 4: paddr=0x000784c4 vaddr=0x40100080 size=0x0484c ( 18508) load I (194) boot: Loaded app from partition at offset 0x10000 I (215) JS: This test run the following script code: I (216) JS: print ('Hello, World!'); Hello, World! ``` -------------------------------- ### Execute Compiled JerryScript Example Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/03.API-EXAMPLE.md Run the compiled C executable to see the JerryScript environment in action, demonstrating the dynamically added property. ```sh ./api-example-7 ``` -------------------------------- ### Execute JerryScript Example (Shell) Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/03.API-EXAMPLE.md Run the compiled C application to execute the embedded JavaScript code and observe the output. ```shell ./api-example-9 ``` -------------------------------- ### Compile JerryScript Extension Example Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/03.API-EXAMPLE.md Command to compile the C code that uses JerryScript extensions. Ensure the 'libjerry-ext' library is linked correctly. ```sh $ gcc api-example-6.c -o api-example-6 $(pkg-config --cflags --libs libjerry-core libjerry-ext libjerry-port libjerry-math) ``` -------------------------------- ### Run JavaScript Example in JerryScript Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/os/zephyr/README.md Execute a JavaScript code snippet within the JerryScript environment on the STM32F4-Discovery board. This demonstrates basic JerryScript functionality. ```javascript js> var test = 0; for (t = 100; t < 1000; t++) test += t; print ('Hello World! ' + test); Hello World! 494550 undefined ``` -------------------------------- ### Build JerryScript Source: https://github.com/jerryscript-project/jerryscript/blob/master/README.md Execute this script to build the JerryScript engine. Ensure you have Python installed. ```bash python tools/build.py ``` -------------------------------- ### Cross-Compile for Raspberry Pi 2 Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/00.GETTING-STARTED.md Example of cross-compiling JerryScript for a Raspberry Pi 2 using a specific toolchain file. ```bash python tools/build.py --toolchain=cmake/toolchain_linux_armv7l.cmake ``` -------------------------------- ### Example Usage When Library Constructors Are Unavailable Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/12.EXT-REFERENCE-MODULE.md When library constructors are unavailable, explicitly call the module registration and unregistration functions. Forward-declare these functions before use. ```c #include "jerryscript.h" #include "jerryscript-ext/module.h" /** * Forward-declare the module registration and unregistration function. */ extern void my_module_register (void); extern void my_module_unregister (void); int main (int argc, char **argv) { jerryx_module_resolver_t resolvers[] = { jerryx_native_module_resolver }; /* This plays the role of the library constructor. */ my_module_register (); jerry_init (JERRY_INIT_EMPTY); ... jerry_value_t my_module = jerryx_module_resolve ("my_module", resolvers, 1); ... jerry_cleanup (); /* This plays the role of the library destructor */ my_module_unregister(); return 0; } ``` -------------------------------- ### Verify Compiler Installation Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/os/openwrt/readme.md Checks if the OpenWrt cross-compiler is installed and accessible by running its version command. This confirms the toolchain setup is complete. ```sh $ mips-openwrt-linux-gcc --version # running this should print out the version information ``` -------------------------------- ### Clone Projects and Download Toolchain Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/baremetal-sdk/espressif/esp-idf/README.md Initial setup involves cloning the JerryScript and ESP-IDF repositories and downloading the Xtensa toolchain. Ensure you use the specified ESP-IDF branch for compatibility. ```sh mkdir jerry-esp && cd jerry-esp git clone https://github.com/jerryscript-project/jerryscript.git git clone https://github.com/espressif/esp-idf.git -b release/v4.4 # SDK requires Xtensa toolchain. wget https://dl.espressif.com/dl/xtensa-esp32-elf-gcc8_4_0-esp-2021r2-linux-amd64.tar.gz ``` -------------------------------- ### Setup Build Environment for ESP8266 RTOS SDK Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/baremetal-sdk/espressif/esp8266-rtos-sdk/README.md Clone the necessary projects and download the Xtensa toolchain. Ensure you use the specified SDK version for compatibility. ```sh # Create a base folder for all the projects. mkdir jerry-esp && cd jerry-esp git clone https://github.com/jerryscript-project/jerryscript.git git clone https://github.com/espressif/ESP8266_RTOS_SDK -b release/v3.4 # SDK requires Xtensa toolchain. wget https://dl.espressif.com/dl/xtensa-lx106-elf-gcc8_4_0-esp-2020r3-linux-amd64.tar.gz ``` -------------------------------- ### Verify npm Installation Source: https://github.com/jerryscript-project/jerryscript/blob/master/tools/babel/README.md Command to check the installed version of npm. ```bash $ npm --version Output: 6.10.2 ``` -------------------------------- ### List Available Build Options Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/00.GETTING-STARTED.md Displays a list of all available build options for Linux. Use this to explore customization possibilities. ```bash python tools/build.py --help ``` -------------------------------- ### Using convenience macros with jerryx_set_properties Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/10.EXT-REFERENCE-HANDLER.md This example demonstrates using convenience macros like JERRYX_PROPERTY_FUNCTION and JERRYX_PROPERTY_NUMBER for defining properties. It also includes error handling and cleanup for registered properties. ```c #include #include "jerryscript.h" #include "jerryscript-ext/handlers.h" #include "jerryscript-ext/properties.h" static jerry_value_t handler (const jerry_call_info_t *call_info_p, const jerry_value_t args_p[], const jerry_length_t args_cnt) { printf ("native handler called!\n"); return jerry_boolean (true); } int main (int argc, char **argv) { jerry_init (JERRY_INIT_EMPTY); /** * Create a array of properties to be registered. * This must be done after initializing the engine as creating `jerry_value_t` * elements are invalid before `jerry_init`. */ jerryx_property_entry methods[] = { JERRYX_PROPERTY_FUNCTION ("demo", handler), JERRYX_PROPERTY_NUMBER ("test_num", 2.3), JERRYX_PROPERTY_UNDEFINED ("this_is_undefined"), JERRYX_PROPERTY_LIST_END(), }; jerry_value_t global = jerry_current_realm (); jerryx_register_result reg = jerryx_set_properties (global, methods); /* if `reg.result` is undefined all methods are registered */ if (jerry_value_is_exception (reg.result)) { printf ("Only registered %d properties\r\n", reg.registered); /* clean up not registered property values */ jerryx_release_property_entry (methods, reg); /* clean up the error */ jerry_value_free (reg.result); } jerry_value_free (global); jerry_cleanup(); return 0; } ``` -------------------------------- ### Install npm Source: https://github.com/jerryscript-project/jerryscript/blob/master/tools/babel/README.md Command to install npm (Node Package Manager) on a Debian-based system. ```bash $ sudo apt install npm ``` -------------------------------- ### Install Babel Dependencies Source: https://github.com/jerryscript-project/jerryscript/blob/master/tools/babel/README.md Command to install project dependencies, including Babel, using npm. Assumes you are in the tools/babel directory. ```bash $ sudo npm install ``` -------------------------------- ### Install ST-Link and Serial Communication Dependencies Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/os/nuttx/README.md Install libraries required for ST-Link programming and serial communication, such as libusb and minicom. ```bash # ST-Link and serial communication dependencies. sudo apt install libusb-1.0-0-dev minicom ``` -------------------------------- ### Create Native Module with Exports Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/02.API-REFERENCE.md Demonstrates how to create a native module with a list of string identifiers as exports. Ensure to free the returned module and export values when no longer needed. ```c #include int main (void) { jerry_init (JERRY_INIT_EMPTY); jerry_value_t exports[2] = { jerry_string_sz ("first_export"), jerry_string_sz ("second_export") }; jerry_value_t native_module = jerry_native_module (NULL, exports, 2); jerry_value_free (exports[0]); jerry_value_free (exports[1]); jerry_value_free (native_module); jerry_cleanup (); return 0; } ``` -------------------------------- ### Setup Output Directories Source: https://github.com/jerryscript-project/jerryscript/blob/master/CMakeLists.txt Configures the output directories for runtime executables, archives, and libraries within the build directory. This mimics a conventional file system layout for build artifacts. ```cmake set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/") set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/") set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/") ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/os/zephyr/README.md Install dependencies for both JerryScript and Zephyr projects. This includes system packages, Python requirements, and the Zephyr toolchain. ```sh # Assuming you are in jerry-zephyr folder. jerryscript/tools/apt-get-install-deps.sh # Tool dependencies of Zephyr. sudo apt install --no-install-recommends \ git cmake ninja-build gperf ccache dfu-util device-tree-compiler \ python3-dev python3-pip python3-setuptools python3-tk python3-wheel \ xz-utils file make gcc gcc-multilib g++-multilib libsdl2-dev # Install Python dependencies of Zephyr. pip3 install --user -r zephyr/scripts/requirements.txt # Install Zephyr toolchain. chmod +x zephyr-toolchain-arm-0.13.2-linux-x86_64-setup.run ./zephyr-toolchain-arm-0.13.2-linux-x86_64-setup.run -- -y -d ${PWD}/zephyr-toolchain-0.13.2 ``` -------------------------------- ### Configure OpenWrt with Menuconfig Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/os/openwrt/readme.md Launches the OpenWrt configuration menu. Set the 'Target System' and 'Target Profile' according to your device specifications. ```sh $ make menuconfig ``` -------------------------------- ### Flash JerryScript Binary using Make Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/baremetal-sdk/particle/README.md Use the Makefile to simplify the process of flashing the compiled JerryScript binary to your Photon board. Ensure the Photon is in DFU mode. ```bash make -f targets/baremetal-sdk/particle/Makefile.particle flash ``` -------------------------------- ### Get Module Request Count Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/02.API-REFERENCE.md Demonstrates how to use jerry_module_request_count to get the number of import/export requests for a module. Ensure JERRY_MODULE_SYSTEM is enabled and JERRY_FEATURE_MODULE is available. ```c #include #include int main (void) { jerry_init (JERRY_INIT_EMPTY); const jerry_char_t script[] = "export * from 'b.mjs'" "import a from 'c.mjs'"; jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_SOURCE_NAME; parse_options.source_name = jerry_string_sz ("a.mjs"); jerry_value_t module_value = jerry_parse (script, sizeof (script) - 1, &parse_options); jerry_value_free (parse_options.source_name); /* Prints 2. */ printf ("Number of requests: %d\n", (int) jerry_module_request_count (module_value)); jerry_value_free (module_value); jerry_cleanup (); return 0; } ``` -------------------------------- ### Iterate and Find Live Objects with Native Info Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/02.API-REFERENCE.md This example demonstrates how to add objects with native pointers and then iterate through them to find a specific object based on its native data. The iteration stops once a match is found. ```c #include #include #include "jerryscript.h" typedef struct { int foo; bool bar; } native_obj_t; typedef struct { jerry_value_t found_object; native_obj_t *found_native_data_p; int match_foo_value; } find_object_data_t; static void native_freecb (void *native_p, jerry_object_native_info_t *info_p) { (void) info_p; /* `native_p` was allocated via malloc. */ free (native_p); } /* native_freecb */ /* * NOTE: The address (!) of type_info acts as a way to uniquely "identify" the * C type `native_obj_t *`. */ static const jerry_object_native_info_t native_obj_type_info = { .free_cb = native_freecb }; /* * Function creating JS object that is "backed" by a `native_obj_t`. */ static void add_object_with_nativeptr (int foo_value) { // construct object and native_set value: jerry_value_t test_object = jerry_object (); native_obj_t *native_obj_p = malloc (sizeof (*native_obj_p)); native_obj_p->foo = foo_value; native_obj_p->bar = true; jerry_object_set_native_ptr (test_object, &native_obj_type_info, native_obj_p); /* Register the test object into the global object. */ jerry_value_t global_object = jerry_current_realm (); jerry_value_t demo_property = jerry_string_sz ("DemoObject"); jerry_value_t set_result = jerry_object_set (global_object, demo_property, test_object); /* The `set_result` should be checked if it is an exception or not. */ jerry_value_free (set_result); jerry_value_free (demo_property); jerry_value_free (global_object); jerry_value_free (test_object); } /* create_object_with_nativeptr */ /* * Example native method that searches for a JavaScript object * with a `native_obj_type_info` has the correct value. */ static bool find_object (const jerry_value_t candidate, void *data_p, void *user_data_p) { find_object_data_t *find_data_p = (find_object_data_t *) user_data_p; native_obj_t *native_obj_p = (native_obj_t *) data_p; if (find_data_p->match_foo_value == native_obj_p->foo) { /* If the object was found, copy it and store it in the user data. */ find_data_p->found_object = jerry_value_copy (candidate); find_data_p->found_native_data_p = native_obj_p; /* Stop traversing over the objects. */ return false; } /* Indicate that the object was not found, so traversal must continue. */ return true; } /* find_object */ int main (void) { jerry_init (JERRY_INIT_EMPTY); add_object_with_nativeptr (4); add_object_with_nativeptr (3); add_object_with_nativeptr (2); find_object_data_t find_data = { .match_foo_value = 3, }; if (jerry_foreach_live_object_with_info (&native_obj_type_info, find_object, &find_data)) { /* The object was found and is now stored in `find_data.found_object`. After using it, it must be released. */ printf ("Object found, native foo value: %d\n", find_data.found_native_data_p->foo); jerry_value_free (find_data.found_object); } else { printf ("Object not found\n"); } jerry_cleanup (); return 0; } ``` -------------------------------- ### Getting Property Value and Handling Exceptions Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/06.REFERENCE-COUNTING.md Demonstrates getting a property value using jerry_object_get and handling potential exceptions. The returned value must always be freed. ```c jerry_value_t prop_value = jerry_object_get (...); /* The prop_value must be released later because both the base * object and the prop_value have an independent reference to * the same JavaScript value. When the operation fails, the * prop_value contains a live reference to an error object. * This reference must be released as well. */ if (jerry_value_is_exception (prop_value)) { /* Errors can be handled here. */ } else { /* The application has a live reference to the property * value even if the base object is freed by the garbage * collector. */ } /* The prop_value must be released. */ jerry_value_free (prop_value); ``` -------------------------------- ### Install NuttX Toolchain and Tool Dependencies Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/os/nuttx/README.md Install the ARM GCC toolchain and other essential build tools for NuttX. This includes compilers, assemblers, and utilities required for embedded development. ```bash # Toolchain dependencies of NuttX. sudo apt install gcc-arm-none-eabi binutils-arm-none-eabi # Tool dependencies of NuttX. sudo apt install bison flex gettext texinfo libncurses5-dev libncursesw5-dev gperf automake libtool pkg-config build-essential gperf genromfs libgmp-dev libmpc-dev libmpfr-dev libisl-dev binutils-dev libelf-dev libexpat-dev gcc-multilib g++-multilib picocom u-boot-tools util-linux ``` -------------------------------- ### Capture and Print Backtrace - C Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/02.API-REFERENCE.md This C example demonstrates how to use jerry_backtrace to capture the call stack and print it. It includes checks for feature availability and proper resource management using jerry_value_free. ```c #include #include #include "jerryscript.h" static jerry_value_t backtrace_handler (const jerry_call_info_t *call_info_p, const jerry_value_t args_p[], const jerry_length_t args_count) { if (!jerry_feature_enabled (JERRY_FEATURE_LINE_INFO)) { printf ("Line info disabled, no backtrace will be printed\n"); return jerry_undefined (); } /* If the line info feature is disabled an empty array will be returned. */ jerry_value_t backtrace_array = jerry_backtrace (5); uint32_t array_length = jerry_array_length (backtrace_array); for (uint32_t idx = 0; idx < array_length; idx++) { jerry_value_t property = jerry_object_get_index (backtrace_array, idx); jerry_char_t string_buffer[64]; jerry_size_t copied_bytes = jerry_string_to_buffer (property, JERRY_ENCODING_UTF8, string_buffer, sizeof (string_buffer) - 1); string_buffer[copied_bytes] = '\0'; printf(" %d: %s\n", idx, string_buffer); jerry_value_free (property); } jerry_value_free (backtrace_array); return jerry_undefined (); } /* backtrace_handler */ int main (void) { jerry_init (JERRY_INIT_EMPTY); jerry_value_t global = jerry_current_realm (); /* Register the "capture_backtrace" method. */ { jerry_value_t func = jerry_function_external (backtrace_handler); jerry_value_t name = jerry_string_sz ("backtrace"); jerry_value_t result = jerry_object_set (global, name, func); jerry_value_free (result); jerry_value_free (name); jerry_value_free (func); } jerry_value_free (global); const char *source = ("function f() { g (); }\n" "function g() { h (); }\n" "function h() { backtrace (); }\n" "f ();\n"); jerry_parse_options_t parse_options; parse_options.options = JERRY_PARSE_HAS_SOURCE_NAME; parse_options.source_name = jerry_string_sz ("demo_memory.js"); jerry_value_t program = jerry_parse ((const jerry_char_t *) source, strlen (source), &parse_options); jerry_value_free (parse_options.source_name); if (!jerry_value_is_exception (program)) { jerry_value_t run_result = jerry_run (program); jerry_value_free (run_result); } jerry_value_free (program); jerry_cleanup (); return 0; } ``` -------------------------------- ### jerry_proxy_handler Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/02.API-REFERENCE.md Gets the handler object of a Proxy object. This API depends on the `JERRY_BUILTIN_PROXY` build option. ```APIDOC ## jerry_proxy_handler ### Description Gets the handler object of a Proxy object. *Note*: - This API depends on a build option (`JERRY_BUILTIN_PROXY`) and can be checked in runtime with the `JERRY_FEATURE_PROXY` feature enum value. ### Prototype ```c jerry_value_t jerry_proxy_handler (jerry_value_t proxy_value); ``` - `proxy_value` - Proxy object value - return value - type error exception - if proxy_value is not a Proxy object - handler object - otherwise *Renamed in version 3.0, it was previously known as `jerry_get_proxy_handler` in earlier versions.* ### Example ```c { jerry_value_t target = jerry_object (); jerry_value_t handler = jerry_object (); jerry_value_t proxy = jerry_proxy (target, handler); jerry_value_free (target); jerry_value_free (handler); handler = jerry_proxy_handler (proxy); // ... usage of the handler jerry_value_free (handler); jerry_value_free (proxy); } ``` ### See also - [jerry_proxy](#jerry_proxy) - [jerry_proxy_custom](#jerry_proxy_custom) - [jerry_proxy_target](#jerry_proxy_target) ``` -------------------------------- ### jerry_proxy_target Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/02.API-REFERENCE.md Gets the target object of a Proxy object. This API depends on the `JERRY_BUILTIN_PROXY` build option. ```APIDOC ## jerry_proxy_target ### Description Gets the target object of a Proxy object. *Note*: - This API depends on a build option (`JERRY_BUILTIN_PROXY`) and can be checked in runtime with the `JERRY_FEATURE_PROXY` feature enum value. ### Prototype ```c jerry_value_t jerry_proxy_target (jerry_value_t proxy_value); ``` - `proxy_value` - Proxy object value - return value - type error exception - if proxy_value is not a Proxy object - target object - otherwise *Introduced in version 2.4*. *Renamed in version 3.0, it was previously known as `jerry_get_proxy_target` in earlier versions.* ### Example ```c { jerry_value_t target = jerry_object (); jerry_value_t handler = jerry_object (); jerry_value_t proxy = jerry_proxy (target, handler); jerry_value_free (target); jerry_value_free (handler); target = jerry_proxy_target (proxy); // ... usage of the target jerry_value_free (target); jerry_value_free (proxy); } ``` ### See also - [jerry_proxy](#jerry_proxy) - [jerry_proxy_custom](#jerry_proxy_custom) - [jerry_proxy_handler](#jerry_proxy_handler) ``` -------------------------------- ### Compile JerryScript Example (Shell) Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/03.API-EXAMPLE.md Compile the C code using gcc, linking against the necessary JerryScript libraries (core, ext, port, math). Ensure the order of libraries is correct for pkg-config. ```shell gcc api-example-9.c -o api-example-9 $(pkg-config --cflags --libs libjerry-core libjerry-ext libjerry-port libjerry-math) ``` -------------------------------- ### Generate and Merge Snapshots Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/02.API-REFERENCE.md This example demonstrates how to generate two separate JavaScript snapshots and then merge them into a single buffer using jerry_merge_snapshots. It includes initialization, parsing, snapshot generation, and cleanup for each snapshot before merging. ```c static uint32_t snapshot_buffer_0[SNAPSHOT_BUFFER_SIZE]; static uint32_t snapshot_buffer_1[SNAPSHOT_BUFFER_SIZE]; size_t snapshot_sizes[2]; static uint32_t merged_snapshot_buffer[SNAPSHOT_BUFFER_SIZE]; const jerry_char_t code_to_snapshot1[] = "var a = 'hello'; 123"; jerry_init (JERRY_INIT_EMPTY); jerry_value_t parse_result = jerry_parse (code_to_snapshot1, sizeof (code_to_snapshot1) - 1, NULL); jerry_value_t generate_result = jerry_generate_snapshot (parse_result, 0, snapshot_buffer_0, SNAPSHOT_BUFFER_SIZE); jerry_value_free (parse_result); snapshot_sizes[0] = (size_t) jerry_value_as_number (generate_result); jerry_value_free (generate_result); jerry_cleanup (); const jerry_char_t code_to_snapshot2[] = "var b = 'hello'; 456"; jerry_init (JERRY_INIT_EMPTY); parse_result = jerry_parse (code_to_snapshot2, sizeof (code_to_snapshot2) - 1, NULL); generate_result = jerry_generate_snapshot (parse_result, 0, snapshot_buffer_1, SNAPSHOT_BUFFER_SIZE); jerry_value_free (parse_result); snapshot_sizes[1] = (size_t) jerry_value_as_number (generate_result); jerry_value_free (generate_result); jerry_cleanup (); jerry_init (JERRY_INIT_EMPTY); const char *error_p; const uint32_t *snapshot_buffers[2]; snapshot_buffers[0] = snapshot_buffer_0; snapshot_buffers[1] = snapshot_buffer_1; static uint32_t snapshot_buffer_0_bck[SNAPSHOT_BUFFER_SIZE]; static uint32_t snapshot_buffer_1_bck[SNAPSHOT_BUFFER_SIZE]; memcpy (snapshot_buffer_0_bck, snapshot_buffer_0, SNAPSHOT_BUFFER_SIZE); memcpy (snapshot_buffer_1_bck, snapshot_buffer_1, SNAPSHOT_BUFFER_SIZE); size_t merged_size = jerry_merge_snapshots (snapshot_buffers, snapshot_sizes, 2, merged_snapshot_buffer, SNAPSHOT_BUFFER_SIZE, &error_p); jerry_cleanup (); ``` -------------------------------- ### jerry_debugger_transport_start Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/13.DEBUGGER-TRANSPORT.md Starts the communication with the debugger client. This function must be called after a successful connection establishment. ```APIDOC ## jerry_debugger_transport_start ### Description Starts the communication to the debugger client. Must be called after the connection is successfully established. ### Prototype ```c void jerry_debugger_transport_start (void); ``` ``` -------------------------------- ### Build OpenWrt Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/os/openwrt/readme.md Initiates the build process for OpenWrt. This step compiles the entire OpenWrt system based on the saved configuration. ```sh $ make ``` -------------------------------- ### Transpile ES6 to ES5.1 with Babel Source: https://github.com/jerryscript-project/jerryscript/blob/master/tools/babel/README.md Shows an example of an ES6 arrow function before and after Babel transpilation to ES5.1. ```javascript //Before const fn = () => 1; //After conversion var fn = function fn() { return 1; }; ``` -------------------------------- ### Get ArrayBuffer Size Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/02.API-REFERENCE.md Retrieves the byte length of an ArrayBuffer or SharedArrayBuffer. Returns 0 if the value is not an ArrayBuffer. ```c jerry_length_t jerry_arraybuffer_size (const jerry_value_t value); ``` ```c { jerry_value_t buffer = jerry_arraybuffer (15); jerry_length_t length = jerry_arraybuffer_size (buffer); // length should be 15 jerry_value_free (buffer); } ``` -------------------------------- ### jerry_realm_this Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/02.API-REFERENCE.md Gets the 'this' binding of a realm. The 'this' binding is always an object and can be changed using jerry_realm_set_this. ```APIDOC ## jerry_realm_this ### Description Gets the 'this' binding of a realm. The 'this' binding is always an object. By default the 'this' binding is the same as the realm object and can be changed by [jerry_realm_set_this](#jerry_realm_set_this). *Note*: - This feature depends on build option (`JERRY_BUILTIN_REALMS`) and can be checked in runtime with the `JERRY_FEATURE_REALM` feature enum value, see: [jerry_feature_enabled](#jerry_feature_enabled). ### Prototype ```c jerry_value_t jerry_realm_this (jerry_value_t realm_value) ``` - `realm_value` - realm value - return - type error exception- if realm_value is not a realm - 'this' binding object - otherwise *Introduced in version 2.4*. *Renamed in version 3.0, it was previously known as `jerry_realm_get_this` in earlier versions.* ### Example ```c { jerry_value_t realm_value = jerry_realm (); jerry_value_t this_value = jerry_realm_this (realm_value); ... // usage of the this_value jerry_value_free (this_value); jerry_value_free (realm_value); } ``` ### See also - [jerry_realm](#jerry_realm) - [jerry_realm_set_this](#jerry_realm_set_this) ``` -------------------------------- ### jerryx_arg_int32 Example Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/09.EXT-REFERENCE-ARG.md Creates a validation/transformation step for a 32-bit integer argument. It specifies the destination pointer, rounding policy, clamping policy, coercion flag, and optionality flag. ```c static inline jerryx_arg_t jerryx_arg_int32 (int32_t *dest, jerryx_arg_round_t round_flag, jerryx_arg_clamp_t clamp_flag, jerryx_arg_coerce_t coerce_flag, jerryx_arg_optional_t opt_flag); ``` -------------------------------- ### Configure Jerry Core Pkgconfig File Source: https://github.com/jerryscript-project/jerryscript/blob/master/jerry-core/CMakeLists.txt Generates the libjerry-core.pc file for pkg-config using a template. ```cmake configure_file(libjerry-core.pc.in libjerry-core.pc @ONLY) ``` -------------------------------- ### Start debugger communication Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/13.DEBUGGER-TRANSPORT.md Initiates communication with the debugger client. This function must be called after a successful connection establishment. ```c void jerry_debugger_transport_start (void); ``` -------------------------------- ### Test JerryScript pkg-config Setup Source: https://github.com/jerryscript-project/jerryscript/blob/master/docs/03.API-EXAMPLE.md Verify that pkg-config can correctly locate and provide flags for the JerryScript core, port, extension, and math libraries. This confirms the environment is set up for using JerryScript. ```sh pkg-config --cflags --libs libjerry-core libjerry-port libjerry-ext libjerry-math ``` -------------------------------- ### Configure NuttX for USB Console Source: https://github.com/jerryscript-project/jerryscript/blob/master/targets/os/nuttx/README.md Initializes NuttX configuration, setting up the system to use a USB console for shell access. ```sh # Assuming you are in jerry-nuttx folder. cd nuttx/tools # Configure NuttX to use USB console shell. ./configure.sh stm32f4discovery:usbnsh ```