diff --git a/src/fscript_ext/fscript_app_conf.c b/src/fscript_ext/fscript_app_conf.c new file mode 100644 index 000000000..fe7724704 --- /dev/null +++ b/src/fscript_ext/fscript_app_conf.c @@ -0,0 +1,38 @@ +/** + * File: fscript_fs.c + * Author: AWTK Develop Team + * Brief: fs functions for fscript + * + * Copyright (c) 2020 - 2021 Guangzhou ZHIYUAN Electronics Co.,Ltd. + * + */ + +/** + * History: + * ================================================================ + * 2021-01-07 Li XianJing created + * + */ + +#include "tkc/fs.h" +#include "tkc/path.h" +#include "tkc/utils.h" +#include "tkc/fscript.h" +#include "conf_io/app_conf.h" + +static ret_t func_app_conf(fscript_t* fscript, fscript_args_t* args, value_t* result) { + value_set_object(result, app_conf_get_instance()); + return RET_OK; +} + +static ret_t func_app_conf_save(fscript_t* fscript, fscript_args_t* args, value_t* result) { + value_set_bool(result, app_conf_save() == RET_OK); + return RET_OK; +} + +ret_t fscript_app_conf_register(void) { + ENSURE(fscript_register_func("app_conf", func_app_conf) == RET_OK); + ENSURE(fscript_register_func("app_conf_save", func_app_conf_save) == RET_OK); + + return RET_OK; +} diff --git a/src/fscript_ext/fscript_app_conf.h b/src/fscript_ext/fscript_app_conf.h new file mode 100644 index 000000000..76660c3f5 --- /dev/null +++ b/src/fscript_ext/fscript_app_conf.h @@ -0,0 +1,35 @@ +/** + * File: fscript_fs.h + * Author: AWTK Develop Team + * Brief: fs functions for fscript + * + * Copyright (c) 2020 - 2021 Guangzhou ZHIYUAN Electronics Co.,Ltd. + * + */ + +/** + * History: + * ================================================================ + * 2021-01-07 Li XianJing created + * + */ + +#ifndef TK_FSCRIPT_APP_CONF_H +#define TK_FSCRIPT_APP_CONF_H + +#include "tkc/str.h" +#include "tkc/object.h" + +BEGIN_C_DECLS + +/** + * @method fscript_app_conf_register + * 注册app_conf函数。 + * + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + */ +ret_t fscript_app_conf_register(void); + +END_C_DECLS + +#endif /*TK_FSCRIPT_APP_CONF_H*/ diff --git a/src/fscript_ext/fscript_ext.c b/src/fscript_ext/fscript_ext.c index 7362d9b87..06158e818 100644 --- a/src/fscript_ext/fscript_ext.c +++ b/src/fscript_ext/fscript_ext.c @@ -25,6 +25,7 @@ #include "fscript_ext/fscript_object.h" #include "fscript_ext/fscript_rbuffer.h" #include "fscript_ext/fscript_wbuffer.h" +#include "fscript_ext/fscript_app_conf.h" #include "fscript_ext/fscript_typed_array.h" #include "fscript_ext/fscript_istream.h" @@ -117,5 +118,9 @@ ret_t fscript_ext_init(void) { fscript_typed_array_register(); #endif /*FSCRIPT_WITH_TYPED_ARRAY*/ +#ifdef FSCRIPT_WITH_APP_CONF + fscript_app_conf_register(); +#endif /*FSCRIPT_WITH_TYPED_ARRAY*/ + return RET_OK; } diff --git a/src/fscript_ext/fscript_ext.h b/src/fscript_ext/fscript_ext.h index e8333ed4e..25c2fab16 100644 --- a/src/fscript_ext/fscript_ext.h +++ b/src/fscript_ext/fscript_ext.h @@ -41,6 +41,7 @@ ret_t fscript_ext_init(void); #define FSCRIPT_WITH_STREAM 1 #define FSCRIPT_WITH_BITS 1 #define FSCRIPT_WITH_FS 1 +#define FSCRIPT_WITH_APP_CONF 1 #define FSCRIPT_WITH_STREAM_FILE 1 #define FSCRIPT_WITH_STREAM_INET 1 #define FSCRIPT_WITH_STREAM_SERIAL 1 diff --git a/src/fscript_ext/fscript_object.c b/src/fscript_ext/fscript_object.c index de5a021f2..e6201d829 100644 --- a/src/fscript_ext/fscript_object.c +++ b/src/fscript_ext/fscript_object.c @@ -42,12 +42,29 @@ static ret_t func_object_unref(fscript_t* fscript, fscript_args_t* args, value_t } static ret_t func_object_get_prop(fscript_t* fscript, fscript_args_t* args, value_t* result) { + object_t* obj = NULL; + FSCRIPT_FUNC_CHECK(args->size >= 2, RET_BAD_PARAMS); + obj = value_object(args->args); + return_value_if_fail(obj != NULL, RET_BAD_PARAMS); + + if (object_get_prop(obj, value_str(args->args + 1), result) != RET_OK) { + if (args->size > 2) { + value_deep_copy(result, args->args + 2); + } else { + value_set_uint32(result, 0); + } + } + + return RET_OK; +} + +static ret_t func_object_remove_prop(fscript_t* fscript, fscript_args_t* args, value_t* result) { object_t* obj = NULL; FSCRIPT_FUNC_CHECK(args->size == 2, RET_BAD_PARAMS); obj = value_object(args->args); return_value_if_fail(obj != NULL, RET_BAD_PARAMS); - object_get_prop(obj, value_str(args->args + 1), result); + value_set_bool(result, object_remove_prop(obj, value_str(args->args + 1)) == RET_OK); return RET_OK; } @@ -78,6 +95,7 @@ ret_t fscript_object_register(void) { ENSURE(fscript_register_func("object_unref", func_object_unref) == RET_OK); ENSURE(fscript_register_func("object_set", func_object_set_prop) == RET_OK); ENSURE(fscript_register_func("object_get", func_object_get_prop) == RET_OK); + ENSURE(fscript_register_func("object_remove", func_object_remove_prop) == RET_OK); return RET_OK; } diff --git a/tests/fscript_run.cpp b/tests/fscript_run.cpp index 209b3b9d8..ee6927328 100644 --- a/tests/fscript_run.cpp +++ b/tests/fscript_run.cpp @@ -6,6 +6,14 @@ #include "tkc/fscript.h" #include "tkc/object_default.h" #include "fscript_ext/fscript_ext.h" +#include "conf_io/app_conf_init_json.h" +#include "tkc/data_reader_factory.h" +#include "tkc/data_writer_factory.h" +#include "tkc/data_writer_file.h" +#include "tkc/data_writer_wbuffer.h" +#include "tkc/data_reader_file.h" +#include "tkc/data_reader_mem.h" +#include "base/data_reader_asset.h" static ret_t run_fscript(const char* code, uint32_t times) { value_t v; @@ -48,7 +56,15 @@ int main(int argc, char* argv[]) { fscript_global_init(); fscript_ext_init(); + data_writer_factory_set(data_writer_factory_create()); + data_reader_factory_set(data_reader_factory_create()); + data_writer_factory_register(data_writer_factory(), "file", data_writer_file_create); + data_reader_factory_register(data_reader_factory(), "file", data_reader_file_create); + data_reader_factory_register(data_reader_factory(), "asset", data_reader_asset_create); + data_reader_factory_register(data_reader_factory(), "mem", data_reader_mem_create); + data_writer_factory_register(data_writer_factory(), "wbuffer", data_writer_wbuffer_create); + app_conf_init_json("runFScript"); tk_mem_dump(); if (argc < 2) { printf("Usage: %s script\n", argv[0]); @@ -63,6 +79,10 @@ int main(int argc, char* argv[]) { } } tk_mem_dump(); + data_writer_factory_destroy(data_writer_factory()); + data_reader_factory_destroy(data_reader_factory()); + data_writer_factory_set(NULL); + data_reader_factory_set(NULL); fscript_global_deinit(); return 0; diff --git a/tests/fscripts/demo_app_conf.fs b/tests/fscripts/demo_app_conf.fs new file mode 100644 index 000000000..f0beba4ce --- /dev/null +++ b/tests/fscripts/demo_app_conf.fs @@ -0,0 +1,18 @@ + +assert(object_set(app_conf(), "timeout", 100)) +assert(object_set(app_conf(), "serial.device", "/dev/tty01")) +assert(object_set(app_conf(), "serial.baudrate", 115200)) +assert(app_conf_save()) + +assert(object_get(app_conf(), "timeout") == 100) +assert(object_get(app_conf(), "serial.device") == "/dev/tty01") +assert(object_get(app_conf(), "serial.baudrate") == 115200) + +assert(object_remove(app_conf(), "timeout")) +assert(object_remove(app_conf(), "serial")) + +assert(object_get(app_conf(), "timeout", 10) == 10) +assert(object_get(app_conf(), "serial.device", "a") == "a") +assert(object_get(app_conf(), "serial.baudrate", 1000) == 1000) + +assert(app_conf_save()) diff --git a/tests/testdata/demo_bits.fs b/tests/fscripts/demo_bits.fs similarity index 100% rename from tests/testdata/demo_bits.fs rename to tests/fscripts/demo_bits.fs diff --git a/tests/testdata/demo_comments.fs b/tests/fscripts/demo_comments.fs similarity index 100% rename from tests/testdata/demo_comments.fs rename to tests/fscripts/demo_comments.fs diff --git a/tests/testdata/demo_expr.fs b/tests/fscripts/demo_expr.fs similarity index 100% rename from tests/testdata/demo_expr.fs rename to tests/fscripts/demo_expr.fs diff --git a/tests/testdata/demo_fs.fs b/tests/fscripts/demo_fs.fs similarity index 100% rename from tests/testdata/demo_fs.fs rename to tests/fscripts/demo_fs.fs diff --git a/tests/testdata/demo_if1.fs b/tests/fscripts/demo_if1.fs similarity index 100% rename from tests/testdata/demo_if1.fs rename to tests/fscripts/demo_if1.fs diff --git a/tests/testdata/demo_object.fs b/tests/fscripts/demo_object.fs similarity index 100% rename from tests/testdata/demo_object.fs rename to tests/fscripts/demo_object.fs diff --git a/tests/testdata/demo_serial.fs b/tests/fscripts/demo_serial.fs similarity index 100% rename from tests/testdata/demo_serial.fs rename to tests/fscripts/demo_serial.fs diff --git a/tests/testdata/demo_stream_file.fs b/tests/fscripts/demo_stream_file.fs similarity index 100% rename from tests/testdata/demo_stream_file.fs rename to tests/fscripts/demo_stream_file.fs diff --git a/tests/testdata/demo_stream_file1.fs b/tests/fscripts/demo_stream_file1.fs similarity index 100% rename from tests/testdata/demo_stream_file1.fs rename to tests/fscripts/demo_stream_file1.fs diff --git a/tests/testdata/demo_tcp.fs b/tests/fscripts/demo_tcp.fs similarity index 100% rename from tests/testdata/demo_tcp.fs rename to tests/fscripts/demo_tcp.fs diff --git a/tests/testdata/demo_typed_array.fs b/tests/fscripts/demo_typed_array.fs similarity index 100% rename from tests/testdata/demo_typed_array.fs rename to tests/fscripts/demo_typed_array.fs diff --git a/tests/testdata/demo_typed_array1.fs b/tests/fscripts/demo_typed_array1.fs similarity index 100% rename from tests/testdata/demo_typed_array1.fs rename to tests/fscripts/demo_typed_array1.fs diff --git a/tests/testdata/demo_typed_array2.fs b/tests/fscripts/demo_typed_array2.fs similarity index 100% rename from tests/testdata/demo_typed_array2.fs rename to tests/fscripts/demo_typed_array2.fs diff --git a/tests/testdata/demo_udp.fs b/tests/fscripts/demo_udp.fs similarity index 100% rename from tests/testdata/demo_udp.fs rename to tests/fscripts/demo_udp.fs diff --git a/tests/testdata/demo_wbuffer.fs b/tests/fscripts/demo_wbuffer.fs similarity index 100% rename from tests/testdata/demo_wbuffer.fs rename to tests/fscripts/demo_wbuffer.fs diff --git a/tests/testdata/demo_wbuffer1.fs b/tests/fscripts/demo_wbuffer1.fs similarity index 100% rename from tests/testdata/demo_wbuffer1.fs rename to tests/fscripts/demo_wbuffer1.fs diff --git a/tests/testdata/demo_wbuffer2.fs b/tests/fscripts/demo_wbuffer2.fs similarity index 100% rename from tests/testdata/demo_wbuffer2.fs rename to tests/fscripts/demo_wbuffer2.fs diff --git a/tests/testdata/demo_wbuffer3.fs b/tests/fscripts/demo_wbuffer3.fs similarity index 100% rename from tests/testdata/demo_wbuffer3.fs rename to tests/fscripts/demo_wbuffer3.fs diff --git a/tests/testdata/demo_while1.fs b/tests/fscripts/demo_while1.fs similarity index 100% rename from tests/testdata/demo_while1.fs rename to tests/fscripts/demo_while1.fs diff --git a/tests/testdata/demo_while2.fs b/tests/fscripts/demo_while2.fs similarity index 100% rename from tests/testdata/demo_while2.fs rename to tests/fscripts/demo_while2.fs diff --git a/tests/testdata/demo_while3.fs b/tests/fscripts/demo_while3.fs similarity index 100% rename from tests/testdata/demo_while3.fs rename to tests/fscripts/demo_while3.fs diff --git a/tests/testdata/demo_while4.fs b/tests/fscripts/demo_while4.fs similarity index 100% rename from tests/testdata/demo_while4.fs rename to tests/fscripts/demo_while4.fs