diff --git a/APP_Framework/lib/JerryScript/README.md b/APP_Framework/lib/JerryScript/README.md index 67f953775..7f5048bc0 100644 --- a/APP_Framework/lib/JerryScript/README.md +++ b/APP_Framework/lib/JerryScript/README.md @@ -1,7 +1,7 @@ # jerryscript编译步骤,本文档依赖 Ubuntu 20.04 操作系统. ## 1、jerryscript源码下载 -JerryScript 源码以子模块的形式保存在xiuos/APP_Framework/lib/JerryScript/jerryscript下,进行编译前需要下载,在xiuos根目录下执行: +JerryScript 源码,已经以子模块的形式保存在xiuos/APP_Framework/lib/JerryScript/jerryscript下,第一次进行编译前需要将该源码下载下来,在xiuos根目录下执行: ```bash git submodule @@ -9,7 +9,7 @@ git submodule init git submodule update APP_Framework/lib/JerryScript/jerryscript ``` -## 2、jerryscript编译依赖安装 +## 2、jerryscript编译依赖工具链安装 第一次编译需要安装依赖,在APP_Framework/lib/JerryScript路径下,依次执行: @@ -25,7 +25,7 @@ sudo apt install \ ## 3、jerryscript源码编译 -当前在XIZI操作系统下,支持HC32F4A0单片机和STM32F4单片机两个系列的开发板,在APP_Framework/lib/JerryScript路径下,以edu-arm32开发板(HC32F4A0单片机)为例,执行: +当前在XiZi操作系统下,支持HC32F4A0单片机和STM32F4单片机两个系列的开发板,在APP_Framework/lib/JerryScript路径下,以edu-arm32开发板(HC32F4A0单片机)为例,进入到APP_Framework/lib/JerryScript下执行: ```bash jerryscript/tools/build.py \ @@ -39,11 +39,11 @@ jerryscript/tools/build.py \ --toolchain=${PWD}/jerryscript/cmake/toolchain_mcu_hc32f4a0.cmake ``` -如果是STM32F4的单片机,将.cmake指定为toolchain_mcu_stm32f4.cmake即可。 +如果是STM32F4的单片机,将.cmake指定为toolchain_mcu_stm32f4.cmake即可,这一步完成后在APP_Framework/lib/JerryScript/jerryscript/build目录下会编译出几个.a文件,这些文件是接下来的bin包构建过程中需要的。 -## 4、edu-arm32开发板bin包构建 +## 4、开发板bin包构建 -在xiuos/Ubiquitous/XiZi_IIoT目录下,执行 +以edu-arm32开发板为例进行构建,进入到xiuos/Ubiquitous/XiZi_IIoT目录下,执行: ```makefile make BOARD=edu-arm32 menuconfig @@ -51,22 +51,26 @@ make BOARD=edu-arm32 menuconfig 然后在menuconfig界面进入APP_Framework → app lib → lib using JerryScript ,完成勾选; -为了支持文件解析,需要存储js文件,勾选edu-arm32 feature → Using SD CARD device,使用sd卡进行.js文件的保存。 +为了支持javascript文件解析,需要存储.js文件,勾选edu-arm32 feature → Using SD CARD device,使用sd卡进行.js文件的保存; -保存并,退出menuconfig - -执行 +保存并退出menuconfig,然后执行编译: ```makefile make BOARD=BOARD=edu-arm32 ``` -完成编译,edu-arm32开发板的烧录方式参考xiuos/Ubiquitous/XiZi_IIoT/board/edu-arm32/目录下的README.md。 +完成编译后,进行bin包的烧录,edu-arm32开发板的烧录方式参考xiuos/Ubiquitous/XiZi_IIoT/board/edu-arm32/目录下的README.md。 -完成烧录后,插入存有js文件的内存卡,内存卡正确完成挂载后,执行: +APP_Framework/lib/JerryScript/testfile目录下存放了几个.js文件,可以将这些文件拷贝到内存卡中,也可以自行创建一些.js文件,完成烧录后,插入存有.js文件的内存卡,正确完成挂载后执行: ```shell -jerrytest xxx.js +jerrytest hello.js ``` -即可看到执行js文件解析,如果jerrytest没有带文件参数,则执行一条js的语句退出。 +也可以同时执行多个.js文件: + +```shell +jerrytest hello.js test.js mathfunction.js +``` + +即可看到执行.js文件执行结果,如果jerrytest命令没有带文件参数,则执行一条默认的js语句。 diff --git a/APP_Framework/lib/JerryScript/testflie/hello.js b/APP_Framework/lib/JerryScript/testflie/hello.js index d05bb729c..1213d0b9c 100644 --- a/APP_Framework/lib/JerryScript/testflie/hello.js +++ b/APP_Framework/lib/JerryScript/testflie/hello.js @@ -7,4 +7,3 @@ console.log("helo js engine!!"); console.log("helo world!!" + str2); console.log("num=" + num); console.log("[hello world] end"); - diff --git a/APP_Framework/lib/JerryScript/testflie/mathfunction.js b/APP_Framework/lib/JerryScript/testflie/mathfunction.js new file mode 100644 index 000000000..2caa436f7 --- /dev/null +++ b/APP_Framework/lib/JerryScript/testflie/mathfunction.js @@ -0,0 +1,36 @@ +/** + * File: mathUtils.js + * Function: Math utilities + * Description: Contains various math utility functions. + */ + +// Calculates the square of a number +function square(number) { + var square = number * number; + console.log("The square of", number, "is", square); + return square; +} + +// Calculates the cube of a number +function cube(number) { + var cube = number * number * number; + console.log("The cube of", number, "is", cube); + return cube; +} + +// Calculates the average of an array of numbers +function average(numbers) { + var sum = numbers.reduce(function (acc, curr) { + return acc + curr; + }, 0); + var avg = sum / numbers.length; + console.log("The average of the following numbers:",numbers,"is", avg); + return avg; +} + +var num = 7; +var arr = [17, 12, 33, 24, 45, 36, 18, 85, 97, 48]; + +var res1 = square(num); +var res2 = cube(num); +var res3 = average(arr); diff --git a/APP_Framework/lib/JerryScript/testflie/test.js b/APP_Framework/lib/JerryScript/testflie/test.js new file mode 100644 index 000000000..81e26400a --- /dev/null +++ b/APP_Framework/lib/JerryScript/testflie/test.js @@ -0,0 +1,11 @@ +var numbers = [1, 2, 3, 4, 5]; + +function sumArray(arr) { + var sum = arr.reduce(function (a, b) { + return a + b; + }, 0); + console.log("Sum of array", arr, "is", sum); + return sum; +} + +var arraySum = sumArray(numbers);