awtk/tests/fscript_run.cpp

130 lines
3.7 KiB
C++
Raw Normal View History

2021-01-02 12:36:30 +08:00
#include "tkc/fs.h"
2020-11-13 17:09:17 +08:00
#include "tkc/mem.h"
2022-01-23 12:02:44 +08:00
#include "tkc/async.h"
2020-11-13 08:57:05 +08:00
#include "tkc/utils.h"
2020-11-10 22:06:48 +08:00
#include "tkc/platform.h"
#include "tkc/time_now.h"
#include "tkc/fscript.h"
2022-01-21 11:06:05 +08:00
#include "tkc/socket_helper.h"
2020-11-10 22:06:48 +08:00
#include "tkc/object_default.h"
#include "fscript_ext/fscript_ext.h"
2021-01-07 15:14:14 +08:00
#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"
2022-01-21 11:06:05 +08:00
#include "debugger/debugger_global.h"
#include "debugger/debugger_server_tcp.h"
2020-11-10 22:06:48 +08:00
2022-01-21 11:06:05 +08:00
static ret_t run_fscript(const char* code, uint32_t times, bool_t debug) {
2021-01-02 12:36:30 +08:00
value_t v;
char buff[64];
uint64_t start = time_now_us();
tk_object_t* obj = object_default_create();
2021-01-02 12:36:30 +08:00
tk_mem_dump();
2021-08-26 07:40:37 +08:00
log_debug("======================\n");
2022-01-21 11:06:05 +08:00
if (times > 1 && !debug) {
2021-01-02 12:36:30 +08:00
/*stress test*/
uint32_t i = 0;
fscript_t* fscript = fscript_create(obj, code);
for (i = 0; i < times; i++) {
2021-08-26 07:40:37 +08:00
log_debug("%u times....\n", i);
tk_mem_dump();
2021-01-02 12:36:30 +08:00
fscript_exec(fscript, &v);
2021-08-26 07:40:37 +08:00
tk_mem_dump();
2021-01-02 12:36:30 +08:00
value_reset(&v);
}
fscript_destroy(fscript);
2022-03-10 09:25:10 +08:00
} else if (debug) {
str_t str;
2022-01-21 11:06:05 +08:00
fscript_t* fscript = NULL;
2022-03-10 09:25:10 +08:00
debugger_global_init();
debugger_server_tcp_init(DEBUGGER_TCP_PORT);
debugger_server_tcp_start();
debugger_server_set_single_mode(TRUE);
sleep_ms(1000);
str_init(&str, 1000);
str_set(&str, code);
str_append_format(&str, 100, "//code_id(\"%s\")\n", DEBUGGER_DEFAULT_CODE_ID);
code = str.str;
2022-01-21 11:06:05 +08:00
2022-01-22 18:38:08 +08:00
fscript = fscript_create(obj, code);
2022-01-21 11:06:05 +08:00
fscript_exec(fscript, &v);
fscript_destroy(fscript);
2022-03-10 09:25:10 +08:00
str_reset(&str);
debugger_server_tcp_deinit();
debugger_global_deinit();
2022-01-21 11:06:05 +08:00
2021-01-02 12:36:30 +08:00
log_debug("result:%s\n", value_str_ex(&v, buff, sizeof(buff) - 1));
value_reset(&v);
2022-03-10 09:25:10 +08:00
} else {
fscript_eval(obj, code, &v);
value_reset(&v);
2021-01-02 12:36:30 +08:00
}
TK_OBJECT_UNREF(obj);
2021-01-02 12:36:30 +08:00
log_debug("cost: %d us\n", (int)(time_now_us() - start));
return RET_OK;
}
2022-01-21 11:06:05 +08:00
static ret_t run_fscript_file(const char* filename, uint32_t times, bool_t debug) {
2021-01-02 12:36:30 +08:00
uint32_t size = 0;
char* code = (char*)file_read(filename, &size);
return_value_if_fail(code != NULL, RET_BAD_PARAMS);
2022-01-21 11:06:05 +08:00
run_fscript(code, times, debug);
2021-01-02 12:36:30 +08:00
TKMEM_FREE(code);
return RET_OK;
}
2020-11-10 22:06:48 +08:00
int main(int argc, char* argv[]) {
platform_prepare();
2022-01-22 18:38:08 +08:00
tk_socket_init();
2021-08-27 16:21:24 +08:00
tk_mem_dump();
fscript_global_init();
fscript_ext_init();
2021-08-27 16:21:24 +08:00
tk_mem_dump();
2022-01-23 12:02:44 +08:00
async_call_init();
2021-01-07 15:14:14 +08:00
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(), "mem", data_reader_mem_create);
data_writer_factory_register(data_writer_factory(), "wbuffer", data_writer_wbuffer_create);
2021-08-27 16:21:24 +08:00
tk_mem_dump();
2021-01-07 15:14:14 +08:00
app_conf_init_json("runFScript");
2020-11-13 17:09:17 +08:00
tk_mem_dump();
2020-11-13 08:57:05 +08:00
if (argc < 2) {
2022-01-21 11:06:05 +08:00
printf("Usage: %s script [debug|times]\n", argv[0]);
2022-01-21 11:34:58 +08:00
printf("Usage: %s @filename [debug|times\n", argv[0]);
2020-11-10 22:06:48 +08:00
return 0;
} else {
2020-11-13 08:57:05 +08:00
const char* code = argv[1];
2021-01-02 12:36:30 +08:00
uint32_t times = argc > 2 ? tk_atoi(argv[2]) : 1;
2022-01-21 11:06:05 +08:00
bool_t debug = argc > 2 ? tk_str_eq(argv[2], "debug") : FALSE;
2021-01-02 12:36:30 +08:00
if (*code == '@') {
2022-01-21 11:06:05 +08:00
run_fscript_file(code + 1, times, debug);
2020-11-13 08:57:05 +08:00
} else {
2022-01-21 11:06:05 +08:00
run_fscript(code, times, debug);
2020-11-13 08:57:05 +08:00
}
2020-11-10 22:06:48 +08:00
}
2020-11-13 17:09:17 +08:00
tk_mem_dump();
2021-01-10 09:58:41 +08:00
app_conf_set_int("foobar", 1);
app_conf_save();
2021-01-07 15:14:14 +08:00
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();
2022-01-22 18:38:08 +08:00
tk_socket_deinit();
2022-01-23 12:02:44 +08:00
async_call_deinit();
2020-11-10 22:06:48 +08:00
return 0;
}