diff options
author | Roland Takacs <1487864+rtakacs@users.noreply.github.com> | 2021-12-02 15:33:29 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-02 15:33:29 +0100 |
commit | a1c1d4271061eff9f4bb4a0c9c31bb68c7f1514d (patch) | |
tree | b7b208dc57f8054746aa80f88667e682a5ae7d3b /targets | |
parent | d69ac0e070a97babb4b8e4c116db765f47c3eb02 (diff) |
Update Zephyr target to 2.7.0 (#4838)
JerryScript-DCO-1.0-Signed-off-by: Roland Takacs roland.takacs@h-lab.eu
Diffstat (limited to 'targets')
-rw-r--r-- | targets/zephyr/CMakeLists.txt | 51 | ||||
-rw-r--r-- | targets/zephyr/Makefile.travis | 25 | ||||
-rw-r--r-- | targets/zephyr/Makefile.zephyr | 170 | ||||
-rw-r--r-- | targets/zephyr/README.md | 153 | ||||
-rw-r--r-- | targets/zephyr/docs/arduino_101.jpg | bin | 22138 -> 0 bytes | |||
-rw-r--r-- | targets/zephyr/src/Makefile | 22 | ||||
-rw-r--r-- | targets/zephyr/src/jerry-module.c | 302 | ||||
-rw-r--r-- | targets/zephyr/src/jerry-port.c | 153 |
8 files changed, 550 insertions, 326 deletions
diff --git a/targets/zephyr/CMakeLists.txt b/targets/zephyr/CMakeLists.txt index 30b7c7ba..ce6be95e 100644 --- a/targets/zephyr/CMakeLists.txt +++ b/targets/zephyr/CMakeLists.txt @@ -11,38 +11,35 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -cmake_minimum_required(VERSION 3.8) -include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(NONE) -target_sources(app PRIVATE src/main-zephyr.c src/jerry-port.c src/getline-zephyr.c) +set(JERRY_BASE ${CMAKE_SOURCE_DIR}/../..) + +# Build JerryScript +execute_process(COMMAND ${JERRY_BASE}/tools/build.py + --clean + --lto=OFF + --jerry-cmdline=OFF + --jerry-math=ON + --amalgam=ON + --mem-heap=70 + --profile=es.next + --compile-flag=--sysroot=${ZEPHYR_BASE} + --toolchain=${JERRY_BASE}/cmake/toolchain_mcu_stm32f4.cmake) +# Define library targets add_library(jerry-core STATIC IMPORTED) add_library(jerry-ext STATIC IMPORTED) -set_target_properties(jerry-core PROPERTIES IMPORTED_LOCATION - ${CMAKE_CURRENT_BINARY_DIR}/../obj/lib/libjerry-core.a) -set_target_properties(jerry-core PROPERTIES INTERFACE_INCLUDE_DIRECTORIES - ${CMAKE_CURRENT_SOURCE_DIR}/../../jerry-core/include) -set_target_properties(jerry-ext PROPERTIES IMPORTED_LOCATION - ${CMAKE_CURRENT_BINARY_DIR}/../obj/lib/libjerry-ext.a) -set_target_properties(jerry-ext PROPERTIES INTERFACE_INCLUDE_DIRECTORIES - ${CMAKE_CURRENT_SOURCE_DIR}/../../jerry-ext/include) -target_link_libraries(app PUBLIC jerry-core jerry-ext) -zephyr_get_include_directories_for_lang_as_string(C includes) -zephyr_get_system_include_directories_for_lang_as_string(C system_includes) -zephyr_get_compile_definitions_for_lang_as_string(C definitions) -zephyr_get_compile_options_for_lang_as_string(C options) - -add_custom_target( - outputexports - COMMAND echo CC="${CMAKE_C_COMPILER}" - COMMAND echo Z_CFLAGS=${system_includes} ${includes} ${definitions} ${options} - COMMAND echo NOSTDINC_FLAGS=${system_includes} - COMMAND echo ZEPHYRINCLUDE=${includes} - COMMAND echo KBUILD_CFLAGS=${definitions} ${options} - VERBATIM - USES_TERMINAL -) +# Define include directories and archives +set_target_properties(jerry-core PROPERTIES IMPORTED_LOCATION ${JERRY_BASE}/build/lib/libjerry-core.a) +set_target_properties(jerry-core PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${JERRY_BASE}/jerry-core/include) +set_target_properties(jerry-ext PROPERTIES IMPORTED_LOCATION ${JERRY_BASE}/build/lib/libjerry-ext.a) +set_target_properties(jerry-ext PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${JERRY_BASE}/jerry-ext/include) +target_sources(app PRIVATE src/main-zephyr.c src/getline-zephyr.c src/jerry-port.c src/jerry-module.c) +target_link_libraries(app PUBLIC jerry-core jerry-ext) diff --git a/targets/zephyr/Makefile.travis b/targets/zephyr/Makefile.travis index 0b22bbef..93f09966 100644 --- a/targets/zephyr/Makefile.travis +++ b/targets/zephyr/Makefile.travis @@ -23,16 +23,16 @@ all: # Install tools via apt. install-apt-get-deps: - sudo apt-get install -q -y gperf dfu-util device-tree-compiler + sudo apt-get install -q -y gperf dfu-util device-tree-compiler gcc-arm-none-eabi # Install Zephyr SDK. -install-zephyr-sdk: - wget https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.13.0/zephyr-sdk-0.13.0-linux-x86_64-setup.run -O ../zephyr-sdk-0.13.0-linux-x86_64-setup.run - sh ../zephyr-sdk-0.13.0-linux-x86_64-setup.run -- -y -d $(CURDIR)/../zephyr-sdk-0.13.0 +install-zephyr-toolchain: + wget https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.13.2/zephyr-toolchain-arm-0.13.2-linux-x86_64-setup.run -O ../zephyr-toolchain-arm-0.13.2-linux-x86_64-setup.run + sh ../zephyr-toolchain-arm-0.13.2-linux-x86_64-setup.run -- -y -d $(CURDIR)/../zephyr-toolchain-0.13.2 # Fetch Zephyr Project repository and install python dependencies. install-zephyr: - git clone https://github.com/zephyrproject-rtos/zephyr.git ../zephyr -b v2.5-branch + git clone https://github.com/zephyrproject-rtos/zephyr.git ../zephyr -b v2.7.0 pip3 install -U pip pip3 install -U setuptools pip3 install -r ../zephyr/scripts/requirements.txt @@ -43,16 +43,21 @@ install-cmake: cmake --version # Perform all the necessary (JerryScript-independent) installation steps. -install-noapt: install-zephyr-sdk install-zephyr install-cmake +install-noapt: install-zephyr-toolchain install-zephyr install-cmake install: install-apt-get-deps install-noapt ## Targets for building Zephyr with JerryScript. +# Initialize west meta-tool +init-west: + west init -l $(CURDIR)/../zephyr + west update hal_stm32 cmsis + west zephyr-export + # Build the firmware (Zephyr with JerryScript). SHELL=bash -script: +script: init-west export ZEPHYR_TOOLCHAIN_VARIANT=zephyr && \ - export ZEPHYR_SDK_INSTALL_DIR=$(CURDIR)/../zephyr-sdk-0.13.0 && \ - source ../zephyr/zephyr-env.sh && \ - $(MAKE) -f ./targets/zephyr/Makefile.zephyr BOARD=qemu_x86 + export ZEPHYR_SDK_INSTALL_DIR=$(CURDIR)/../zephyr-toolchain-0.13.2 && \ + west build -d $(CURDIR)/../build -p auto -b stm32f4_disco targets/zephyr/ -- -G'Unix Makefiles' diff --git a/targets/zephyr/Makefile.zephyr b/targets/zephyr/Makefile.zephyr deleted file mode 100644 index d182df34..00000000 --- a/targets/zephyr/Makefile.zephyr +++ /dev/null @@ -1,170 +0,0 @@ -# Copyright JS Foundation and other contributors, http://js.foundation -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -.DEFAULT_GOAL := all - -ifeq ($(.DEFAULT_GOAL),) - $(warning no default goal is set) -endif - -BOARD ?= qemu_x86 -CONF_FILE = prj.conf -export BOARD - -TARGET_ZEPHYR ?= ./targets/zephyr -TARGET_ZEPHYR_SRC_DIR = $(TARGET_ZEPHYR)/src - -COMPONENTS ?= jerry-core jerry-ext -JERRYHEAP ?= 16 -JERRYPROFILE ?= minimal - -# Include functionality like regular expressions -# check Jerry script documentation -# -# -cp -# -cp_minimal -# -cp_minimal-mem_stats -# -mem_stats -# -mem_stress_test - -ifndef ZEPHYR_BASE -$(error Missing Zephyr base, did you source zephyr-env.sh? ) -endif - -INTERM = build/$(BOARD)/obj -OUTPUT = build/$(BOARD)/zephyr - -include $(OUTPUT)/Makefile.export - -EXT_CFLAGS := -fno-asynchronous-unwind-tables -fno-omit-frame-pointer -EXT_CFLAGS += -fno-stack-protector -fno-strict-overflow -ffreestanding -EXT_CFLAGS += -fno-reorder-functions -fno-defer-pop -fdata-sections -EXT_CFLAGS += -ffunction-sections -fno-inline-functions - -EXT_CFLAGS += $(KBUILD_CFLAGS) $(NOSTDINC_FLAGS) $(subst -I,-isystem,$(ZEPHYRINCLUDE)) - -EXT_CFLAGS += -Wall -Wno-format-zero-length -Wno-pointer-sign -EXT_CFLAGS += -Werror=format -Werror=implicit-int -Wno-unused-but-set-variable -EXT_CFLAGS += -Wno-main -Wno-strict-aliasing -Wno-old-style-declaration -EXT_CFLAGS += -Wno-error=format= -EXT_CFLAGS += -D_XOPEN_SOURCE=700 - -EXT_CFLAGS += -Wno-error=conversion - -EXTERNAL_LIB = $(INTERM)/lib/libjerry-core.a $(INTERM)/lib/libjerry-ext.a - -LIBS = jerry-core jerry-ext - -BUILD_CONFIG = O="$(OUTPUT)" V=$(V) USER_LIBS="$(LIBS)" USER_LIB_INCLUDE_DIR="-L $(CURDIR)/$(INTERM)/lib" TARGET_ZEPHYR=$(TARGET_ZEPHYR) - -.PHONY: all -all: zephyr - -$(EXTERNAL_LIB): -ifdef V - @echo "- JERRY SCRIPT -------------------------------------------------" -endif - mkdir -p $(INTERM) - mkdir -p $(OUTPUT) - cmake -B$(INTERM) -H./ \ - -DCMAKE_SYSTEM_NAME=Zephyr \ - -DCMAKE_C_COMPILER="$(subst ccache ,,$(CC))" \ - -DCMAKE_C_COMPILER_WORKS=TRUE \ - -DENABLE_LTO=OFF \ - -DJERRY_CMDLINE=OFF \ - -DJERRY_PROFILE=$(JERRYPROFILE) \ - -DJERRY_ERROR_MESSAGES=ON \ - -DCMAKE_BUILD_TYPE=MinSizeRel \ - -DCMAKE_VERBOSE_MAKEFILE=$(V) \ - -DJERRY_GLOBAL_HEAP_SIZE=$(JERRYHEAP) \ - -DEXTERNAL_COMPILE_FLAGS="$(EXT_CFLAGS)" \ - $(EXT_JERRY_FLAGS) - - make -C $(INTERM) $(COMPONENTS) V=1 - -$(OUTPUT)/Makefile.export: $(OUTPUT)/Makefile - make --no-print-directory -C $(OUTPUT) outputexports CMAKE_COMMAND=: >$@ - -$(OUTPUT)/Makefile: - mkdir -p $(OUTPUT) && cmake -DBOARD=$(BOARD) -DCONF_FILE=$(CONF_FILE) -B$(OUTPUT) -Htargets/zephyr/ - -zephyr: jerry -ifdef V - @echo "- ZEPHYR -------------------------------------------------------" -endif - +make -C $(OUTPUT) - @echo "Finished" - @size $(OUTPUT)/zephyr/zephyr.elf - -jerry: $(EXTERNAL_LIB) - @touch $(EXTERNAL_LIB) - -GENERIC_TARGETS = run qemugdb flash debug debugserver -KCONFIG_TARGETS = \ - initconfig config nconfig menuconfig xconfig gconfig \ - oldconfig silentoldconfig defconfig savedefconfig \ - allnoconfig allyesconfig alldefconfig randconfig \ - listnewconfig olddefconfig -CLEAN_TARGETS = mrproper - -$(GENERIC_TARGETS): jerry -$(CLEAN_TARGETS): clean - -$(GENERIC_TARGETS) $(KCONFIG_TARGETS) $(CLEAN_TARGETS): -ifdef V - @echo "- ZEPHYR -------------------------------------------------------" -endif - make -C $(OUTPUT) $@ - -dfu-x86: all - @- dfu-util -a x86_app -D build/$(BOARD)/zephyr/zephyr.bin; \ - if [ $$? -eq 0 ] ; then echo "\nYour program will launch in 5 seconds." ; \ - else echo "\nProgram didn't flash, try pressing the reset buttons \nand wait a second for the bootloader to load, \nor flash again the factory bootloader."; fi - -usage: -help: - @echo Usage: - @echo showconfig Show parameters and configuration - @echo flash Flash into board - @echo all Compile jerryscript and zephyr - -showconfig: - @echo "- CONFIGURATION ------------------------------------------------" - @echo "INTERM = $(INTERM)" - @echo "OUTPUT = $(OUTPUT)" - @echo "CC = $(CC) " - @echo "BOARD = $(ZEPHYR_BASE)/boards/$(BOARD)/Makefile.board " - @echo "TOOLCHAIN = $(ZEPHYR_BASE)/scripts/Makefile.toolchain.$(ZEPHYR_GCC_VARIANT) " - @echo "TOOLCHAIN_CFLAGS = $(TOOLCHAIN_CFLAGS) " - @echo "CROSS_COMPILE = $(CROSS_COMPILE) " - @echo "TOOLCHAIN_LIBS = $(TOOLCHAIN_LIBS) " - @echo "LIBS = $(LIBS) " - @echo "BUILD_CONFIG = $(BUILD_CONFIG) " - make -f $(TARGET_ZEPHYR)/Makefile $(BUILD_CONFIG) showconfig - -clean: -ifdef V - @echo "- CLEANING JERRY SCRIPT ----------------------------------------" -endif - rm -rf $(INTERM) $(TARGET_ZEPHYR_SRC_DIR)/*.o -ifdef V - @echo "- CLEANING ZEPHYR ----------------------------------------------" -endif - make -f $(TARGET_ZEPHYR)/Makefile $(BUILD_CONFIG) clean - -pristine: clean -ifdef V - @echo "- CLEANING BUILD DIRECTORY -------------------------------------" -endif - rm -rf build/ diff --git a/targets/zephyr/README.md b/targets/zephyr/README.md index 8e52f016..2cdd4f17 100644 --- a/targets/zephyr/README.md +++ b/targets/zephyr/README.md @@ -1,128 +1,129 @@ ### About -This folder contains files to integrate JerryScript with Zephyr RTOS -(https://zephyrproject.org/). +This folder contains files to run JerryScript on +[STM32F4-Discovery board](https://www.st.com/en/evaluation-tools/stm32f4discovery.html) with +[Zephyr](https://zephyrproject.org/). +The document had been validated on Ubuntu 20.04 operating system. -### How to build +#### 1. Setup the build environment -#### 1. Preface +Clone the necessary projects into a `jerry-zephyr` directory. +The latest tested working version of Zephyr is `v2.7.0`. -1. Directory structure +```sh +mkdir jerry-zephyr && cd jerry-zephyr -Assume `harmony` as the path to the projects to build. -The folder tree related would look like this. +git clone https://github.com/jerryscript-project/jerryscript.git +git clone https://github.com/zephyrproject-rtos/zephyr -b v2.7.0 +# Zephyr requires its toolchain. +wget https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.13.2/zephyr-toolchain-arm-0.13.2-linux-x86_64-setup.run ``` -harmony + +The following directory structure has been created: + +``` +jerry-zephyr + jerryscript | + targets - | + zephyr - + zephyr-project + | + zephyr + + zephyr + + zephyr-toolchain-arm-0.13.2-linux-x86_64-setup.run ``` +#### 2. Install dependencies of the projects -2. Target boards/emulations - -Following Zephyr boards are known to work: qemu_x86, qemu_cortex_m3, -frdm_k64f. But generally, any board supported by Zephyr should work, -as long as it has enough Flash and RAM (boards which don't have -enough of those, will simply have link-time errors of ROM/RAM -overflow). +```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 -#### 2. Prepare Zephyr +# Install Python dependencies of Zephyr. +pip3 install --user -r zephyr/scripts/requirements.txt -Follow [this](https://www.zephyrproject.org/doc/getting_started/getting_started.html) page to get -the Zephyr source and configure the environment. +# 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 +``` -If you just start with Zephyr, you may want to follow "Building a Sample -Application" section in the doc above and check that you can flash your -target board. +Note: CMake 3.20 is required. If the installed CMake is older, upgrade it for example [this way](https://apt.kitware.com/). -Remember to source the Zephyr environment as explained in the zephyr documentation: +#### 3. Initialize west meta-tool for Zephyr ``` -cd zephyr-project -source zephyr-env.sh +# Assuming you are in jerry-zephyr folder. +west init -l zephyr +west update hal_stm32 cmsis +west zephyr-export +``` -export ZEPHYR_GCC_VARIANT=zephyr -export ZEPHYR_SDK_INSTALL_DIR=<sdk installation directory> +#### 4. Build Zephyr (with JerryScript) + +``` +# Assuming you are in jerry-zephyr folder. +west build -p auto -b stm32f4_disco jerryscript/targets/zephyr/ ``` -#### 3. Build JerryScript for Zephyr QEMU +The created binary is a `zephyr.elf` named file located in `jerry-zephyr/build/zephyr/bin/` folder. -The easiest way is to build and run on a QEMU emulator: +#### 5. Flash -For x86 architecture: +Install `udev` rules which allows to flash STM32F4-Discovery as a regular user: ``` -make -f ./targets/zephyr/Makefile.zephyr BOARD=qemu_x86 run +# 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 ``` -For ARM (Cortex-M) architecture: +Connect Mini-USB for charging and flasing the device. ``` -make -f ./targets/zephyr/Makefile.zephyr BOARD=qemu_cortex_m3 run +# Assuming you are in jerry-zephyr folder. +west flash ``` -#### 4. Build for a real board +#### 6. Connect to the device -Below, we build for NXP FRDM-K64F board (`frdm_k64f` Zephyr board -identifier). Building for other boards is similar. You are expected -to read [Supported Boards](https://docs.zephyrproject.org/latest/boards/index.html) -section in the Zephyr documentation for more information about -Zephyr's support for a particular board, means to flash binaries, -etc. +Use `USB To TTL Serial Converter` for serial communication. STM32F4-Discovery pins are mapped by Zephyr as follows: ``` -# assuming you are in top-level folder -cd jerryscript -make -f ./targets/zephyr/Makefile.zephyr BOARD=frdm_k64f + STM32f4-Discovery PA2 pin is configured for TX. + STM32f4-Discovery PA3 pin is configured for RX. ``` -At the end of the build, you will be given a path to and stats about -the ELF binary: +* Connect `STM32f4-Discovery` **PA2** pin to **RX** pin of `USB To TTL Serial Converter` +* Connect `STM32f4-Discovery` **PA3** pin to **TX** pin of `USB To TTL Serial Converter` +* Connect `STM32f4-Discovery` **GND** pin to **GND** pin of `USB To TTL Serial Converter` -``` -... -Finished - text data bss dec hex filename - 117942 868 24006 142816 22de0 build/frdm_k64f/zephyr/zephyr/zephyr.elf -``` +The device should be visible as `/dev/ttyUSB0`. Use `minicom` communication program with `115200`. -Flashing the binary depends on a particular board used (see links above). -For the FRDM-K64F used as the example, you should copy the *raw* binary -file corresponding to the ELF file above (at -`build/frdm_k64f/zephyr/zephyr/zephyr.bin`) to the USB drive appearing -after connecting the board to a computer: +* In `minicom`, set `Add Carriage Ret` to `off` in by `CTRL-A -> Z -> U` key combinations. +* In `minicom`, set `Hardware Flow Control` to `no` by `CTRL-A -> Z -> O -> Serial port setup -> F` key combinations. +```sh +sudo minicom --device=/dev/ttyUSB0 --baud=115200 ``` -cp build/frdm_k64f/zephyr/zephyr/zephyr.bin /media/pfalcon/DAPLINK/ -``` - -To interact with JerryScript, connect to a board via serial connection -and press Reset button (you first should wait while LEDs on the board -stop blinking). For `frdm_k64f`: -``` -picocom -b115200 /dev/ttyACM0 -``` +Press `RESET` on the board to get the initial message with JerryScript command prompt: -You should see something similar to this: ``` -JerryScript build: Aug 11 2021 16:03:07 +*** Booting Zephyr OS build v2.7.99-1786-ga08b65ef42db *** +JerryScript build: Nov 25 2021 14:17:17 JerryScript API 3.0.0 -Zephyr version 2.6.99 +Zephyr version 2.7.99 js> ``` -Run the example javascript command test function -``` -js> var test=0; for (t=100; t<1000; t++) test+=t; print ('Hi JS World! '+test); -Hi JS World! 494550 -``` +Run the following JavaScript example: -Try a more complex function: ``` -js> function hello(t) {t=t*10;return t}; print("result"+hello(10.5)); +js> var test = 0; for (t = 100; t < 1000; t++) test += t; print ('Hello World! ' + test); +Hello World! 494550 +undefined ``` diff --git a/targets/zephyr/docs/arduino_101.jpg b/targets/zephyr/docs/arduino_101.jpg Binary files differdeleted file mode 100644 index b05683c0..00000000 --- a/targets/zephyr/docs/arduino_101.jpg +++ /dev/null diff --git a/targets/zephyr/src/Makefile b/targets/zephyr/src/Makefile deleted file mode 100644 index 7801a1cf..00000000 --- a/targets/zephyr/src/Makefile +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright JS Foundation and other contributors, http://js.foundation -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -ifdef V -$(info Compiling application) -endif - -# Adding path for jerry script APIs -ZEPHYRINCLUDE += $(JERRY_INCLUDE) - -obj-y += main-zephyr.o getline-zephyr.o jerry-port.o diff --git a/targets/zephyr/src/jerry-module.c b/targets/zephyr/src/jerry-module.c new file mode 100644 index 00000000..97f92dcf --- /dev/null +++ b/targets/zephyr/src/jerry-module.c @@ -0,0 +1,302 @@ +/* Copyright JS Foundation and other contributors, http://js.foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> + +#include "jerryscript.h" +#include "jerryscript-port.h" + +/** + * Computes the end of the directory part of a path. + * + * @return end of the directory part of a path. + */ +static size_t +jerry_port_get_directory_end (const jerry_char_t *path_p) /**< path */ +{ + const jerry_char_t *end_p = path_p + strlen ((const char *) path_p); + + while (end_p > path_p) + { + if (end_p[-1] == '/') + { + return (size_t) (end_p - path_p); + } + + end_p--; + } + + return 0; +} /* jerry_port_get_directory_end */ + +/** + * Normalize a file path. + * + * @return a newly allocated buffer with the normalized path if the operation is successful, + * NULL otherwise + */ +static jerry_char_t * +jerry_port_normalize_path (const jerry_char_t *in_path_p, /**< path to the referenced module */ + size_t in_path_length, /**< length of the path */ + const jerry_char_t *base_path_p, /**< base path */ + size_t base_path_length) /**< length of the base path */ +{ + char *path_p; + + if (base_path_length > 0) + { + path_p = (char *) malloc (base_path_length + in_path_length + 1); + + if (path_p == NULL) + { + return NULL; + } + + memcpy (path_p, base_path_p, base_path_length); + memcpy (path_p + base_path_length, in_path_p, in_path_length); + path_p[base_path_length + in_path_length] = '\0'; + } + else + { + path_p = (char *) malloc (in_path_length + 1); + + if (path_p == NULL) + { + return NULL; + } + + memcpy (path_p, in_path_p, in_path_length); + path_p[in_path_length] = '\0'; + } + + return (jerry_char_t *) path_p; +} /* jerry_port_normalize_path */ + +/** + * A module descriptor. + */ +typedef struct jerry_port_module_t +{ + struct jerry_port_module_t *next_p; /**< next_module */ + jerry_char_t *path_p; /**< path to the module */ + size_t base_path_length; /**< base path length for relative difference */ + jerry_value_t realm; /**< the realm of the module */ + jerry_value_t module; /**< the module itself */ +} jerry_port_module_t; + +/** + * Native info descriptor for modules. + */ +static const jerry_object_native_info_t jerry_port_module_native_info = +{ + .free_cb = NULL, +}; + +/** + * Default module manager. + */ +typedef struct +{ + jerry_port_module_t *module_head_p; /**< first module */ +} jerry_port_module_manager_t; + +/** + * Release known modules. + */ +static void +jerry_port_module_free (jerry_port_module_manager_t *manager_p, /**< module manager */ + const jerry_value_t realm) /**< if this argument is object, release only those modules, + * which realm value is equal to this argument. */ +{ + jerry_port_module_t *module_p = manager_p->module_head_p; + + bool release_all = !jerry_value_is_object (realm); + + jerry_port_module_t *prev_p = NULL; + + while (module_p != NULL) + { + jerry_port_module_t *next_p = module_p->next_p; + + if (release_all || module_p->realm == realm) + { + free (module_p->path_p); + jerry_release_value (module_p->realm); + jerry_release_value (module_p->module); + + free (module_p); + + if (prev_p == NULL) + { + manager_p->module_head_p = next_p; + } + else + { + prev_p->next_p = next_p; + } + } + else + { + prev_p = module_p; + } + + module_p = next_p; + } +} /* jerry_port_module_free */ + +/** + * Initialize the default module manager. + */ +static void +jerry_port_module_manager_init (void *user_data_p) +{ + ((jerry_port_module_manager_t *) user_data_p)->module_head_p = NULL; +} /* jerry_port_module_manager_init */ + +/** + * Deinitialize the default module manager. + */ +static void +jerry_port_module_manager_deinit (void *user_data_p) /**< context pointer to deinitialize */ +{ + jerry_value_t undef = jerry_create_undefined (); + jerry_port_module_free ((jerry_port_module_manager_t *) user_data_p, undef); + jerry_release_value (undef); +} /* jerry_port_module_manager_deinit */ + +/** + * Declare the context data manager for modules. + */ +static const jerry_context_data_manager_t jerry_port_module_manager = +{ + .init_cb = jerry_port_module_manager_init, + .deinit_cb = jerry_port_module_manager_deinit, + .bytes_needed = sizeof (jerry_port_module_manager_t) +}; + +/** + * Default module resolver. + * + * @return a module object if resolving is successful, an error otherwise + */ +jerry_value_t +jerry_port_module_resolve (const jerry_value_t specifier, /**< module specifier string */ + const jerry_value_t referrer, /**< parent module */ + void *user_p) /**< user data */ +{ + (void) user_p; + + jerry_port_module_t *module_p; + const jerry_char_t *base_path_p = NULL; + size_t base_path_length = 0; + + if (jerry_get_object_native_pointer (referrer, (void **) &module_p, &jerry_port_module_native_info)) + { + base_path_p = module_p->path_p; + base_path_length = module_p->base_path_length; + } + + jerry_size_t in_path_length = jerry_get_utf8_string_size (specifier); + jerry_char_t *in_path_p = (jerry_char_t *) malloc (in_path_length + 1); + jerry_string_to_utf8_char_buffer (specifier, in_path_p, in_path_length); + in_path_p[in_path_length] = '\0'; + + jerry_char_t *path_p = jerry_port_normalize_path (in_path_p, in_path_length, base_path_p, base_path_length); + + if (path_p == NULL) + { + return jerry_create_error (JERRY_ERROR_COMMON, (const jerry_char_t *) "Out of memory"); + } + + jerry_value_t realm = jerry_get_global_object (); + + jerry_port_module_manager_t *manager_p; + manager_p = (jerry_port_module_manager_t *) jerry_get_context_data (&jerry_port_module_manager); + + module_p = manager_p->module_head_p; + + while (module_p != NULL) + { + if (module_p->realm == realm + && strcmp ((const char *) module_p->path_p, (const char *) path_p) == 0) + { + free (path_p); + free (in_path_p); + jerry_release_value (realm); + return jerry_acquire_value (module_p->module); + } + + module_p = module_p->next_p; + } + + size_t source_size; + uint8_t *source_p = jerry_port_read_source ((const char *) path_p, &source_size); + + if (source_p == NULL) + { + free (path_p); + free (in_path_p); + jerry_release_value (realm); + /* TODO: This is incorrect, but makes test262 module tests pass + * (they should throw SyntaxError, but not because the module cannot be found). */ + return jerry_create_error (JERRY_ERROR_SYNTAX, (const jerry_char_t *) "Module file not found"); + } + + jerry_parse_options_t parse_options; + parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_RESOURCE; + parse_options.resource_name = jerry_create_string_sz ((const jerry_char_t *) in_path_p, in_path_length); + + jerry_value_t ret_value = jerry_parse (source_p, + source_size, + &parse_options); + + jerry_release_value (parse_options.resource_name); + jerry_port_release_source (source_p); + free (in_path_p); + + if (jerry_value_is_error (ret_value)) + { + free (path_p); + jerry_release_value (realm); + return ret_value; + } + + module_p = (jerry_port_module_t *) malloc (sizeof (jerry_port_module_t)); + + module_p->next_p = manager_p->module_head_p; + module_p->path_p = path_p; + module_p->base_path_length = jerry_port_get_directory_end (module_p->path_p); + module_p->realm = realm; + module_p->module = jerry_acquire_value (ret_value); + + jerry_set_object_native_pointer (ret_value, module_p, &jerry_port_module_native_info); + manager_p->module_head_p = module_p; + + return ret_value; +} /* jerry_port_module_resolve */ + +/** + * Release known modules. + */ +void +jerry_port_module_release (const jerry_value_t realm) /**< if this argument is object, release only those modules, + * which realm value is equal to this argument. */ +{ + jerry_port_module_free ((jerry_port_module_manager_t *) jerry_get_context_data (&jerry_port_module_manager), + realm); +} /* jerry_port_module_release */ diff --git a/targets/zephyr/src/jerry-port.c b/targets/zephyr/src/jerry-port.c index 01651069..56778218 100644 --- a/targets/zephyr/src/jerry-port.c +++ b/targets/zephyr/src/jerry-port.c @@ -13,12 +13,32 @@ * limitations under the License. */ -#include <stdarg.h> - #include <zephyr.h> +#include <stdarg.h> +#include <stdlib.h> #include "jerryscript-port.h" +/** + * JerryScript log level + */ +static jerry_log_level_t jerry_log_level = JERRY_LOG_LEVEL_ERROR; + +/** + * Sets log level. + */ +void set_log_level (jerry_log_level_t level) +{ + jerry_log_level = level; +} /* set_log_level */ + +/** + * Aborts the program. + */ +void jerry_port_fatal (jerry_fatal_code_t code) +{ + exit (1); +} /* jerry_port_fatal */ /** * Provide log message implementation for the engine. @@ -28,35 +48,81 @@ jerry_port_log (jerry_log_level_t level, /**< log level */ const char *format, /**< format string */ ...) /**< parameters */ { - (void) level; /* ignore log level */ - - va_list args; - va_start (args, format); - vfprintf (stderr, format, args); - va_end (args); + if (level <= jerry_log_level) + { + va_list args; + va_start (args, format); + vfprintf (stderr, format, args); + va_end (args); + } } /* jerry_port_log */ +/** + * Determines the size of the given file. + * @return size of the file + */ +static size_t +jerry_port_get_file_size (FILE *file_p) /**< opened file */ +{ + fseek (file_p, 0, SEEK_END); + long size = ftell (file_p); + fseek (file_p, 0, SEEK_SET); + + return (size_t) size; +} /* jerry_port_get_file_size */ /** - * Provide fatal message implementation for the engine. + * Opens file with the given path and reads its source. + * @return the source of the file */ -void jerry_port_fatal (jerry_fatal_code_t code) +uint8_t * +jerry_port_read_source (const char *file_name_p, /**< file name */ + size_t *out_size_p) /**< [out] read bytes */ { - jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Jerry Fatal Error!\n"); - while (true); -} /* jerry_port_fatal */ + FILE *file_p = fopen (file_name_p, "rb"); + + if (file_p == NULL) + { + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to open file: %s\n", file_name_p); + return NULL; + } + + size_t file_size = jerry_port_get_file_size (file_p); + uint8_t *buffer_p = (uint8_t *) malloc (file_size); + + if (buffer_p == NULL) + { + fclose (file_p); + + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to allocate memory for module"); + return NULL; + } + + size_t bytes_read = fread (buffer_p, 1u, file_size, file_p); + + if (!bytes_read) + { + fclose (file_p); + free (buffer_p); + + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to read file: %s\n", file_name_p); + return NULL; + } + + fclose (file_p); + *out_size_p = bytes_read; + + return buffer_p; +} /* jerry_port_read_source */ /** - * Implementation of jerry_port_get_current_time. - * - * @return current timer's counter value in milliseconds + * Release the previously opened file's content. */ -double -jerry_port_get_current_time (void) +void +jerry_port_release_source (uint8_t *buffer_p) /**< buffer to free */ { - int64_t ms = k_uptime_get(); - return (double) ms; -} /* jerry_port_get_current_time */ + free (buffer_p); +} /* jerry_port_release_source */ /** * Dummy function to get the time zone adjustment. @@ -71,6 +137,18 @@ jerry_port_get_local_time_zone_adjustment (double unix_ms, bool is_utc) } /* jerry_port_get_local_time_zone_adjustment */ /** + * Dummy function to get the current time. + * + * @return 0 + */ +double +jerry_port_get_current_time (void) +{ + int64_t ms = k_uptime_get(); + return (double) ms; +} /* jerry_port_get_current_time */ + +/** * Provide the implementation of jerry_port_print_char. * Uses 'printf' to print a single character to standard output. */ @@ -79,3 +157,36 @@ jerry_port_print_char (char c) /**< the character to print */ { printf ("%c", c); } /* jerry_port_print_char */ + +/** + * Provide implementation of jerry_port_sleep. + */ +void jerry_port_sleep (uint32_t sleep_time) /**< milliseconds to sleep */ +{ + k_usleep ((useconds_t) sleep_time * 1000); +} /* jerry_port_sleep */ + +/** + * Pointer to the current context. + */ +static jerry_context_t *current_context_p = NULL; + +/** + * Set the current_context_p as the passed pointer. + */ +void +jerry_port_default_set_current_context (jerry_context_t *context_p) /**< points to the created context */ +{ + current_context_p = context_p; +} /* jerry_port_default_set_current_context */ + +/** + * Get the current context. + * + * @return the pointer to the current context + */ +jerry_context_t * +jerry_port_get_current_context (void) +{ + return current_context_p; +} /* jerry_port_get_current_context */ |