Skip to content

Commit

Permalink
Fixed duplication of basically all UI elements.
Browse files Browse the repository at this point in the history
Introduced static_test capabilities and some code for runtime_test.
Fixed several bugs
  • Loading branch information
checkroom committed Nov 23, 2024
1 parent 3960824 commit 1029764
Show file tree
Hide file tree
Showing 17 changed files with 128 additions and 44 deletions.
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ does not somehow. But just `./two-buttons.xml` does? Check why!
- [x] flatpak
- [x] Swift6 is added, but meson cannot find swiftc. Why?
- [x] Unable to append args to meson setup
- [ ] Escape vs_debug (tabs).

### Scripting

Expand Down
6 changes: 3 additions & 3 deletions bindings/native/src/stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ char* itoa(int value, char* result, int base) {
return result;
}

node_t vs_self = 0;

void* vs_set_env(void* ptr){
void* t = vs_self;
vs_self=ptr;
return t;
}

node_t vs_self = 0;
}
53 changes: 53 additions & 0 deletions examples/demo-c-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<app xmlns:fl="vs.fltk">
<fl:window box="0,0,320,240" label="Demo Counter">
<mixin name="+fl:button" bg.colour="#aa3300" label.colour="#99ee4f" />
<script lang="c">
<![CDATA[
unsigned int counter = 0;
void _update(){
static char str_buf[32];
itoa(counter,str_buf,10);
$$prop($("main-text"),"label",str_buf);
}
void on_add(){
counter++;
_update();
}
void on_dec(){
counter--;
_update();
}
int static_test(){
$$debug("test.n1","value-4");
return 0;
}
$cb(on_add);
$cb(on_dec);
]]>
</script>

<fl:label name="main-text" label="0" label.size="90" box="20,20,280,120" />
<fl:button label="Add" on.callback="on_add" box="20,160,80,50" />
<fl:button label="Remove" on.callback="on_dec" box="120,160,80,50" />
<fl:label name=">" label="Console" box="220,160,80,50">
<script lang="c">
<![CDATA[
void callback(){
$log(LOG_LOG,"Hello world2!");
}
int static_test(){
$$debug("test.n2","value-2");
return 0;
}
]]>
</script>
</fl:label>
<fl:button name="w"><script lang="c"></script></fl:button>
</fl:window>
</app>
2 changes: 2 additions & 0 deletions include/actions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#pragma once
//Part of the testing pipeline. These xml files are read and its operation run on the ui tree.
1 change: 1 addition & 0 deletions include/globals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ struct vs_test_debug_t{
extern vs_test_debug_t debug;

extern value_models_t value_models;
extern bool is_testing;
}
}
1 change: 1 addition & 0 deletions include/loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ struct app_loader{
app_loader(const char* profile, const char* path);
~app_loader();
int run();
int test();
};
}
5 changes: 2 additions & 3 deletions include/ui-base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ class ui_base{

void set_dispatcher(symbol_t value);

int run_test();

int all_tests() const;
//This is to centralize implementations in one place instead of having them spread all across for no good reason.
//Return 0 if success, 1 if failure for all of them.
static int use_getter(const symbol_ret_t& sym, value_t ** value);
static int use_setter(const symbol_ret_t& sym, const value_t * value);
static int use_callback(const symbol_ret_t& sym, ui_base * node);
static int use_test(const symbol_ret_t& sym);
//use_draw, use_function. use_dispatched does not exist as its usage is extremely constrained.
//TODO: Add vs_event or something like that to let scripts access the global event queue information.

Expand Down
6 changes: 4 additions & 2 deletions include/ui-tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ struct ui_tree {
frame_mode_t mode = frame_mode_t::AUTO;

//Run test propagating from the root.
int run_test();

bool string2key256(const char* str, uint8_t array[256/32] );

Expand Down Expand Up @@ -72,7 +71,10 @@ struct ui_tree {
static bool h_flag(bool *dst, const char *expr,
const ui_base *env);

~ui_tree();
virtual ~ui_tree();
virtual void cleanup();
virtual int runtime_testsuite();

};


Expand Down
4 changes: 3 additions & 1 deletion include/ui-tree.xml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ struct ui_xml_tree : ui_tree {

int load(const char* file, type_t type, const pugi::xml_node* caller_node=nullptr, ui_base* caller_ui_node=nullptr, const scoped_rpath_t* caller_path=nullptr, const policies_t& base_policies=globals::policy);

~ui_xml_tree();
virtual ~ui_xml_tree();
virtual void cleanup();
virtual int runtime_testsuite(){if(this->root!=nullptr)return this->root->all_tests();return 0;}

// Logging

Expand Down
12 changes: 10 additions & 2 deletions src/app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,17 @@ int run(const char* path, const char *entry, const char* profile, const char* te
if(t==nullptr)std::filesystem::copy_file(globals::path_env.app_path.location+"commons/db.sqlite",db_path);
}
}

//TODO implement test
if(tests!=nullptr){
globals::is_testing=true;
}

app_loader loader(profile,entry);

//TODO implement test
if(tests!=nullptr){
}
auto t= loader.run();
std::cout<<"\n";
return t;
Expand All @@ -75,7 +83,7 @@ int main(int argc, char **argv) {
}
else{
if(strcmp(argv[1],"run")==0){
if(argc<2){
if(argc<3){
std::cerr<<"This application requires the path to a valid xml file or native component passed as first argument\n";
return 1;
}
Expand All @@ -84,7 +92,7 @@ int main(int argc, char **argv) {
return t;
}
else if(strcmp(argv[1],"test")==0){
if(argc<2){
if(argc<4){
std::cerr<<"This application in test mode requires the path to a valid xml file or native component as first argument, and an xml file of actions as its second\n";
return 1;
}
Expand Down
2 changes: 2 additions & 0 deletions src/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ cache::mem_storage_t mem_storage;

vs_test_debug_t debug;

bool is_testing = false;

vs_test_debug_t::vs_test_debug_t(){auto file=getenv("VS_DEBUG_FILE");if(file!=nullptr)fd=fopen(file,"w+");}
vs_test_debug_t::~vs_test_debug_t(){if(fd!=nullptr)fclose(fd);}

Expand Down
6 changes: 6 additions & 0 deletions src/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ app_loader::app_loader(const char *profile, const char* path){
}
}

int app_loader::test(){
return root->runtime_testsuite();
//TODO
}

int app_loader::run(){
globals::mem_storage.cleanup();
root->cleanup();
if(!globals::policy.headless){
auto t= Fl::run();
delete root;
Expand Down
1 change: 1 addition & 0 deletions src/pipelines/quickjs-js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace pipelines{

void qjs_error_func_xml(const pugi::xml_node& env, const char * msg) {
printf("\n\033[41;37;1m[QJS]\033[0m : %s @ [\033[93;3m%s\033[0m]", msg,env.path().c_str());
fflush(stdout);
}

void qjs_log_symbol_func_xml(const pugi::xml_node& ctx, const char * msg, const char * name) {
Expand Down
17 changes: 10 additions & 7 deletions src/pipelines/tcc-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace pipelines{

void tcc_error_func_xml(const pugi::xml_node& ctx, const char * msg) {
printf("\n\033[41;37;1m[TCC]\033[0m : %s @ [\033[93;3m%s\033[0m]", msg,ctx.path().c_str());
fflush(stdout);

}

void tcc_log_symbol_func_xml(const pugi::xml_node& ctx, const char * msg, const char * name) {
Expand All @@ -22,7 +24,7 @@ void tcc_log_symbol_func_xml(const pugi::xml_node& ctx, const char * msg, const

//'/home/checkroom/Documents/projects/vs-fltk/subprojects/libtcc/tcc' test.c -I../../subprojects/libtcc/include/ -L. -L../../subprojects/libtcc -lapp

static void vs_test_debug(const char* k, const char* v){globals::debug(k,v);}
static void vs_debug(const char* k, const char* v){globals::debug(k,v);}

#define LIB(x) script->add_sym(#x, (void*) x)
#define LIBT(x,t) script->add_sym(#x, (void*) t x)
Expand Down Expand Up @@ -68,7 +70,7 @@ std::shared_ptr<tcc> tcc_c_pipeline(bool is_runtime, vs::ui_base* obj, const cha

// Custom symbol
//script->add_sym("vs_self", (void *)obj==0?(void*)-1:obj); //Needed as obj nullptr would remove the symbol for some stupid reason.
script->add_sym("vs_test_debug", (void *)vs_test_debug);
script->add_sym("vs_debug", (void *)vs_debug);
script->add_sym("vs_log", (void *)vs_log);
script->add_sym("vs_resolve_name", (void *)+[](ui_base* w,const char* s){if(w==nullptr)return (const ui_base*)nullptr;return w->resolve_name(s, true); });
script->add_sym("vs_resolve_name_path", (void *)+[](ui_base* w,const char* s){if(w==nullptr)return (const ui_base*)nullptr;return w->resolve_name_path(s, true); });
Expand Down Expand Up @@ -134,17 +136,21 @@ std::shared_ptr<tcc> tcc_c_pipeline(bool is_runtime, vs::ui_base* obj, const cha
);
}


//TODO Tcc error handling if compile fails to show error but fail to generate the tcc smart pointer.
//TODO Tcc error handling if compile fails to show error but fail to generate the tcc smart pointer. I need to flush cout :/
script->relocate();

auto on_compiled = (uint64_t(*)())script->get_sym("on_compiled");
if(on_compiled!=nullptr)on_compiled();

auto on_static_test = (int(*)())script->get_sym("static_test");
if(on_static_test!=nullptr && globals::is_testing)on_static_test();

if(obj!=nullptr){
//Apply the environment for single use scripts.
auto vs_set_env = (void(*)(void*))script->get_sym("vs_set_env");
vs_set_env(obj);
}

return script;
}

Expand All @@ -163,9 +169,6 @@ std::shared_ptr<smap<symbol_t>> tcc_c_pipeline_apply(const std::shared_ptr<tcc>
if(strcmp("callback", name)==0){
ctx->log(ctx->ref,"Registering default callback symbol `%s`",name);
ctx->symbols.emplace(name, symbol_t{symbol_mode_t::NATIVE,symbol_type_t::CALLBACK,value});
}else if(strcmp("on_test", name)==0){
ctx->log(ctx->ref,"Registering default test symbol `%s`",name);
ctx->symbols.emplace("#test", symbol_t{symbol_mode_t::NATIVE,symbol_type_t::TEST,value});
}else if(strcmp("draw", name)==0){
ctx->log(ctx->ref,"Registering default drawing symbol `%s`",name);
ctx->symbols.emplace(name, symbol_t{symbol_mode_t::NATIVE,symbol_type_t::DRAW,value});
Expand Down
41 changes: 16 additions & 25 deletions src/ui-base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,22 @@ void ui_base::refresh_style(const char* local_mixins){
}
}

int ui_base::run_test(){
return use_test(this->resolve_symbol_local("#test",false));
int ui_base::all_tests() const{
/*
if(local_frame!=nullptr){
auto sym =this->resolve_symbol_local("#test",false);
int tmp = 0;
if(sym.symbol.type!=symbol_type_t::VOID){
tmp = use_test(sym);
}
for(auto& it: local_frame->children){
auto element = it.second->widget();
tmp += element->all_tests();
}
return tmp;
}
*/
return 0;
}

int ui_base::use_getter(const symbol_ret_t& sym, value_t ** value){
Expand Down Expand Up @@ -349,29 +363,6 @@ int ui_base::use_callback(const symbol_ret_t& sym, ui_base * node){
return 1;
}

int ui_base::use_test(const symbol_ret_t& sym){
symbol_ret_t::test_fn fn = (symbol_ret_t::test_fn)sym.symbol.symbol;

if(sym.found_at->get_mode()==frame_mode_t::NATIVE){
return fn();
}
else if(sym.found_at->get_mode()==frame_mode_t::QUICKJS){
pipelines::quickjs_t* script = (pipelines::quickjs_t*)sym.found_at->script.get();
auto globalThis = JS_GetGlobalObject(script->ctx);
auto ret= JS_Call(script->ctx,std::get<2>(script->handles[(size_t)sym.symbol.symbol-1]),globalThis,0,nullptr);
int retval;
JS_ToInt32(script->ctx, &retval, ret);
JS_FreeValue(script->ctx, ret);
JS_FreeValue(script->ctx, globalThis);
return retval;
}
else{
//Callback type not supported yet.
}
return 1;
}



void ui_callback_handler(Fl_Widget* _, void* _data){
ui_callback_t* data = (ui_callback_t*)_data;
Expand Down
5 changes: 5 additions & 0 deletions src/ui-tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace vs{

bool ui_tree::string2key256(const char* str, uint8_t array[256/32]){
memset(array,0,256/8);
return false;
}

//TODO: I need to check if number conversions failed or not too.
Expand Down Expand Up @@ -39,6 +40,10 @@ namespace vs{
return false;
}

void ui_tree::cleanup(){}
int ui_tree::runtime_testsuite(){return 0;}


ui_tree::~ui_tree(){
for(auto i : nodes){
delete i;
Expand Down
9 changes: 8 additions & 1 deletion src/ui-tree.xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
for(auto& i : root.children()){_build(i,root_ui);}\
t->widget().end();\
t->widget().show();\
return;\
}
//TODO: Avoid parsing its head
#define mkLeafWidget($ns,$name,$class_name) else if(strcmp(root.name(),#$name)==0){build_base_widget<$class_name>(root,root_ui); }
Expand All @@ -48,6 +49,7 @@
for(auto& i : root.children()){_build(i,root_ui);}\
t->widget().end();\
t->widget().show();\
return;\
}

#define mkNSLeafWidget($ns,$name,$class_name) else if(strcmp(root.name(),(ns.fltk + #$name).c_str())==0 ){\
Expand Down Expand Up @@ -79,7 +81,7 @@ void ui_xml_tree::log(int severety, const void* _ctx, const char* str, ...){
vfprintf(log_device,rstr.c_str(), args);
va_end(args);

fflush(stdout);
fflush(log_device);
}


Expand Down Expand Up @@ -171,6 +173,11 @@ int ui_xml_tree::load(const char* file, type_t type, const pugi::xml_node* calle

ui_xml_tree::~ui_xml_tree(){if(root!=nullptr)delete root;}

void ui_xml_tree::cleanup(){
this->doc.reset();
}


int ui_xml_tree::build(){

const auto& xml_root = doc.child((type==type_t::APP)?strings.APP_TAG:strings.COMPONENT_TAG);
Expand Down

0 comments on commit 1029764

Please sign in to comment.