wrap app_conf for fscript

This commit is contained in:
lixianjing 2021-01-07 15:14:14 +08:00
parent 51d28cfda2
commit 51560cf43f
29 changed files with 136 additions and 1 deletions

View File

@ -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 <lixianjing@zlg.cn> 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;
}

View File

@ -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 <lixianjing@zlg.cn> 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*/

View File

@ -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;
}

View File

@ -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

View File

@ -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;
}

View File

@ -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;

View File

@ -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())