diff --git a/.gitmodules b/.gitmodules index 71fdddbb3..9404ff594 100644 --- a/.gitmodules +++ b/.gitmodules @@ -32,6 +32,3 @@ [submodule "APP_Framework/lib/lorawan/lorawan_gateway_single_channel"] path = APP_Framework/lib/lorawan/lorawan_gateway_single_channel url = https://gitlink.org.cn/xuos/lorawan_gateway_single_channel.git -[submodule "APP_Framework/lib/JerryScript/jerryscript"] - path = APP_Framework/lib/JerryScript/jerryscript - url = https://gitlink.org.cn/wgzAIIT/jerryscript.git diff --git a/APP_Framework/lib/JerryScript/Makefile b/APP_Framework/lib/JerryScript/Makefile index 4c2fc92f8..6a03d0354 100644 --- a/APP_Framework/lib/JerryScript/Makefile +++ b/APP_Framework/lib/JerryScript/Makefile @@ -15,6 +15,6 @@ ifeq ($(CONFIG_BOARD_EDU_ARM32_EVB),y) SRC_FILES += mcu/hc32f4a0/setjmp.S endif -SRC_FILES += jerry_port.c jerry_main.c jerry_test.c +SRC_FILES += jerry_port.c jerry_main.c include $(KERNEL_ROOT)/compiler.mk diff --git a/APP_Framework/lib/JerryScript/README.md b/APP_Framework/lib/JerryScript/README.md index 20e6c5599..8896f5d48 100644 --- a/APP_Framework/lib/JerryScript/README.md +++ b/APP_Framework/lib/JerryScript/README.md @@ -1,15 +1,5 @@ -# jerryscript编译步骤,本文档依赖 Ubuntu 20.04 操作系统. -## 1、jerryscript源码下载 - -JerryScript 源码,已经以子模块的形式保存在xiuos/APP_Framework/lib/JerryScript/jerryscript下,第一次进行编译前需要将该源码下载下来,在xiuos根目录下执行: - -```bash -git submodule -git submodule init -git submodule update APP_Framework/lib/JerryScript/jerryscript -``` - -## 2、jerryscript编译依赖工具链安装 +# jerryscript编译步骤,注意本文档依赖 Ubuntu 20.04 操作系统. +## 1、jerryscript编译依赖工具链安装 第一次编译需要安装依赖,在APP_Framework/lib/JerryScript路径下,依次执行: @@ -23,7 +13,7 @@ sudo apt install \ libexpat-dev gcc-multilib g++-multilib picocom u-boot-tools util-linux ``` -## 3、jerryscript源码编译 +## 2、jerryscript源码编译 当前在XiZi操作系统下,支持HC32F4A0单片机和STM32F4单片机两个系列的开发板,在APP_Framework/lib/JerryScript路径下,以edu-arm32开发板(HC32F4A0单片机)为例,进入到APP_Framework/lib/JerryScript下执行: @@ -41,7 +31,7 @@ jerryscript/tools/build.py \ 如果是STM32F4的单片机,将.cmake指定为toolchain_mcu_stm32f4.cmake即可,这一步完成后在APP_Framework/lib/JerryScript/jerryscript/build目录下会编译出几个.a文件,这些文件是接下来的bin包构建过程中需要的。 -## 4、开发板bin包构建 +## 3、开发板bin包构建 以edu-arm32开发板为例进行构建,进入到xiuos/Ubiquitous/XiZi_IIoT目录下,执行: @@ -64,13 +54,7 @@ make BOARD=edu-arm32 APP_Framework/lib/JerryScript/testfile目录下存放了几个.js文件,可以将这些文件拷贝到内存卡中,也可以自行创建一些.js文件,完成烧录后,插入存有.js文件的内存卡,正确完成挂载后执行: ```shell -jerrytest hello.js +jerrytest demo.js ``` -也可以同时执行多个.js文件: - -```shell -jerrytest hello.js test.js mathfunction.js -``` - -即可看到执行.js文件执行结果,如果jerrytest命令没有带文件参数,则执行一条默认的js语句。 +会执行一个数组的快速排序demo,打印排序后的数组。 diff --git a/APP_Framework/lib/JerryScript/jerry_test.c b/APP_Framework/lib/JerryScript/jerry_test.c deleted file mode 100644 index 077436f55..000000000 --- a/APP_Framework/lib/JerryScript/jerry_test.c +++ /dev/null @@ -1,271 +0,0 @@ -/* - * Copyright (c) 2022 AIIT XUOS Lab - * XiUOS is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, - * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, - * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -/** - * @file jerry_test.c - * @brief support jerryscript - * @version 1.0 - * @author AIIT XUOS Lab - * @date 2023.08.07 - */ -#include -#include -#include -#include "jerryscript.h" -#include "jerryscript-ext/handler.h" - -bool js_parse_test1(void* parameter) -{ - bool run_ok = false; - - const jerry_char_t script[] = "var str = 'Hello, World!';"; - - /* Initialize engine */ - jerry_init (JERRY_INIT_EMPTY); - - /* Setup Global scope code */ - jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS); - - /* Check if there is any JS code parse error */ - if (!jerry_value_is_error (parsed_code)) - { - /* Execute the parsed source code in the Global scope */ - jerry_value_t ret_value = jerry_run (parsed_code); - - /* Check the execution return value if there is any error */ - run_ok = !jerry_value_is_error (ret_value); - - /* Returned value must be freed */ - jerry_release_value (ret_value); - printf("jerry_run ret=%ld\n", ret_value); - } - - /* Parsed source code must be freed */ - jerry_release_value (parsed_code); - - /* Cleanup engine */ - jerry_cleanup (); - - printf("run_ok is %d.\n", run_ok); - - return (run_ok ? 0 : 1); -} - -int js_parse_test2(void* parameter) -{ - int return_value = 1; - - /* Initialize engine */ - jerry_init (JERRY_INIT_EMPTY); - - /* Parse the 'function (a,b) { return a + b; }' function */ - const char function_args[] = "a, b"; - const char function_source[] = "return a * b"; - - jerry_value_t parsed_function = jerry_parse_function (NULL, - 0, - (const jerry_char_t *) function_args, - strlen (function_args), - (const jerry_char_t *) function_source, - strlen (function_source), - JERRY_PARSE_NO_OPTS); - - if (!jerry_value_is_error (parsed_function)) - { - /* Run the parsed function */ - jerry_value_t args[] = { - jerry_create_number (3), - jerry_create_number (55), - }; - jerry_size_t argc = sizeof (args) / sizeof (args[0]); - jerry_value_t ret_value = jerry_call_function (parsed_function, - jerry_create_undefined(), - args, - argc); - - /* Process result value */ - if (jerry_value_is_number (ret_value)) { - double value = jerry_get_number_value (ret_value); - printf ("Function result: %lf\n", value); - - return_value = !(value == (3 * 55)); - } - - /* Release the function arguments */ - for (jerry_size_t idx = 0; idx < argc; idx++) { - jerry_release_value (args[idx]); - } - - /* Returned value must be freed */ - jerry_release_value (ret_value); - } - - /* Parsed function must be freed */ - jerry_release_value (parsed_function); - - /* Cleanup engine */ - jerry_cleanup (); - - return return_value; -} - - -int js_parse_test3(void* parameter) -{ - jerry_init (JERRY_INIT_EMPTY); - - jerry_value_t value; - // create or acquire value - value = jerry_create_string ((const jerry_char_t *) "Demo string"); - - // Read the string into a byte buffer. - jerry_size_t string_size = jerry_get_string_size (value); - jerry_char_t *string_buffer_p = (jerry_char_t *) malloc (sizeof (jerry_char_t) * (string_size + 1)); - - jerry_size_t copied_bytes = jerry_string_to_char_buffer (value, string_buffer_p, string_size); - string_buffer_p[copied_bytes] = '\0'; - - jerry_release_value (value); - - jerry_cleanup (); - - printf ("Test string: %s\n", string_buffer_p); - free (string_buffer_p); - - return 0; -} - - -static int counter = 0; - -static jerry_value_t -method_getter (const jerry_value_t this_obj, - const jerry_value_t func_obj, - const jerry_value_t args[], - const jerry_length_t argc) -{ - counter++; - printf("Getter called, returning: %d\n", counter); - - return jerry_create_number (counter); -} - -static jerry_value_t -method_setter (const jerry_value_t this_obj, - const jerry_value_t func_obj, - const jerry_value_t args[], - const jerry_length_t argc) -{ - // Note: the arguments count and type should be checked - // in this example it is ommitted! - - double new_value = jerry_get_number_value (args[0]); - counter = (int) new_value; - - printf("Setter called, setting: %d\n", counter); - - return jerry_create_undefined (); -} - -int js_parse_test4(void* parameter) -{ - jerry_init (JERRY_INIT_EMPTY); - - jerry_value_t global_obj_val = jerry_get_global_object (); - - // configure the property - jerry_property_descriptor_t prop_desc; - jerry_init_property_descriptor_fields (&prop_desc); - - // set the property descriptor fields: - - prop_desc.is_get_defined = true; - prop_desc.getter = jerry_create_external_function (method_getter); - prop_desc.is_set_defined = true; - prop_desc.setter = jerry_create_external_function (method_setter); - - // add the property as "my_prop" for the global object - jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "my_prop"); - jerry_value_t return_value = jerry_define_own_property (global_obj_val, prop_name, &prop_desc); - if (jerry_value_is_error (return_value)) - { - // there was an error - } - - // if there was no error at this point the global object should have a "my_prop" property - - jerry_release_value (return_value); - jerry_release_value (prop_name); - - jerry_free_property_descriptor_fields (&prop_desc); - jerry_release_value (global_obj_val); - - // run an example js code to use the getter/setters - - const char *src_p = "this.my_prop; this.my_prop; this.my_prop = 4; this.my_prop"; - jerry_value_t eval_result = jerry_eval ((const jerry_char_t *) src_p, strlen (src_p), JERRY_PARSE_NO_OPTS); - - // "eval_result" is the last result of "this.my_prop" that is "5" currently. - double result_number = jerry_get_number_value (eval_result); - printf("output: %lf\n", result_number); - - jerry_cleanup (); - - return result_number != 5.0; -} - -int js_parse_test5(void* parameter) -{ - const jerry_char_t script[] = "print ('Hello, World!');"; - - /* Initialize engine */ - jerry_init (JERRY_INIT_EMPTY); - - /* Register 'print' function from the extensions */ - jerryx_handler_register_global ((const jerry_char_t *) "print",jerryx_handler_print); - - /* Setup Global scope code */ - jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS); - - if (!jerry_value_is_error (parsed_code)) - { - /* Execute the parsed source code in the Global scope */ - jerry_value_t ret_value = jerry_run (parsed_code); - - /* Returned value must be freed */ - jerry_release_value (ret_value); - } - - /* Parsed source code must be freed */ - jerry_release_value (parsed_code); - - /* Cleanup engine */ - jerry_cleanup (); - return 0; -} - -void jstest(void) -{ - int ret; - pthread_t thread; - pthread_attr_t attr; - attr.schedparam.sched_priority = 22; - attr.stacksize = 8192; - int32 task = 0; - - ret = PrivTaskCreate(&thread, &attr, (void*)js_parse_test5, NULL); - if (ret < 0) { - printf("taskcreate failed, status=%d\n", ret); - return; - } -} -PRIV_SHELL_CMD_FUNCTION(jstest, jerryscript test cmd, PRIV_SHELL_CMD_FUNC_ATTR); diff --git a/APP_Framework/lib/JerryScript/jerryscript b/APP_Framework/lib/JerryScript/jerryscript deleted file mode 160000 index e367747bd..000000000 --- a/APP_Framework/lib/JerryScript/jerryscript +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e367747bd9b6c2e762761a8cadb05e9d86206e44 diff --git a/APP_Framework/lib/JerryScript/testflie/demo.js b/APP_Framework/lib/JerryScript/testflie/demo.js new file mode 100644 index 000000000..c16b4ef44 --- /dev/null +++ b/APP_Framework/lib/JerryScript/testflie/demo.js @@ -0,0 +1,28 @@ +//Quickly sort an array +function quickSort(array, start, end) { + if (end - start < 1) { + return; + } + const target = array[start]; + let l = start; + let r = end; + while (l < r) { + while (l < r && array[r] >= target) { + r--; + } + array[l] = array[r]; + while (l < r && array[l] < target) { + l++; + } + array[r] = array[l]; + } + array[l] = target; + quickSort(array, start, l - 1); + quickSort(array, l + 1, end); + return array; + } + +const array = [5, 3, 8, 4, 2, 9, 1, 7, 6]; +console.log("The array before sorting is:",array); +const sortedArray = quickSort(array, 0, array.length - 1); +console.log("The sorted array is:",sortedArray); \ No newline at end of file diff --git a/APP_Framework/lib/JerryScript/testflie/mathfunction.js b/APP_Framework/lib/JerryScript/testflie/mathfunction.js index 2caa436f7..3f4a0119b 100644 --- a/APP_Framework/lib/JerryScript/testflie/mathfunction.js +++ b/APP_Framework/lib/JerryScript/testflie/mathfunction.js @@ -1,9 +1,3 @@ -/** - * 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;