Skip to content

Commit

Permalink
Hiding private nodes in paths, preventing resolution of private from …
Browse files Browse the repository at this point in the history
…scripts
  • Loading branch information
checkroom committed Nov 23, 2024
1 parent dc6ca0b commit e51b232
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 30 deletions.
2 changes: 0 additions & 2 deletions examples/demo-c.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@
<fl:button label="Remove" on.callback="on_dec" box="120,160,80,50" />
<fl:button label="Console" box="220,160,80,50">
<script lang="c" compact="true">
<![CDATA[
$log(LOG_LOG,"Hello world2!");
]]>
</script>
</fl:button>
</fl:window>
Expand Down
8 changes: 4 additions & 4 deletions include/ui-base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ class ui_base{
frame* resolve_frame() const;
//Resolve the namespace, basically the first parent which is not "transparent".
frame* resolve_namespace() const;
symbol_ret_t resolve_symbol(const char* str) const;
symbol_ret_t resolve_symbol_local(const char* str) const;
const ui_base* resolve_name(const char * str) const;
const ui_base* resolve_name_path(const char * str) const;
symbol_ret_t resolve_symbol(const char* str, bool safe) const;
symbol_ret_t resolve_symbol_local(const char* str, bool safe) const;
const ui_base* resolve_name(const char * str, bool safe) const;
const ui_base* resolve_name_path(const char * str, bool safe) const;

virtual int get_computed(const char* prop, const char ** value) = 0;
virtual int apply_prop(const char* prop, const char * value) = 0;
Expand Down
2 changes: 1 addition & 1 deletion include/ui-frame.private.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class frame{

frame(const char* name, frame_mode_t mode, ui_base* ui_node, frame* parent, frame_type_t type, frame_access_t access){
static int counter = 0;
if(name==nullptr)this->name=std::string("%__")+std::to_string(counter++); //TODO: replace with hash to make them not predictable. Filter out %__ prefixed names from any serialization
if(name==nullptr)this->name=std::string("#")+std::to_string(counter++); //TODO: replace with hash to make them not predictable. Filter out %__ prefixed names from any serialization
else this->name=name;
this->mode=mode;
this->ui_node=ui_node;
Expand Down
2 changes: 1 addition & 1 deletion src/pipelines/quickjs-js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static JSValue js_resolve_name_path(JSContext * ctx , JSValueConst this_val, int
const char* str;
JS_ToBigUint64(ctx, &node, argv[0]);
str = JS_ToCString(ctx, argv[1]);
auto t = JS_NewBigUint64(ctx,(uint64_t)((ui_base*)node)->resolve_name_path(str));
auto t = JS_NewBigUint64(ctx,(uint64_t)((ui_base*)node)->resolve_name_path(str,true));
JS_FreeCString(ctx, str);
return t;

Expand Down
16 changes: 8 additions & 8 deletions src/pipelines/tcc-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ std::shared_ptr<tcc> tcc_c_pipeline(bool is_runtime, vs::ui_base* obj, const cha
//- The location where it s headers are placed.
//- The path for bindings shall be computed as absolute based on VS_SHARE or whatever it is called.

script->add_lib_path("/usr/lib/x86_64-linux-gnu/"); //I dont' want to hardcode this one.
script->add_lib_path("./subprojects/libtcc");
//script->add_lib_path("/usr/lib/x86_64-linux-gnu/"); //I dont' want to hardcode this one.
//script->add_lib_path("./subprojects/libtcc");

script->set_out_type(tcc::memory);

Expand All @@ -70,13 +70,13 @@ std::shared_ptr<tcc> tcc_c_pipeline(bool is_runtime, vs::ui_base* obj, const cha
//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_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); });
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); });
script->add_sym("vs_resolve_symbol", (void *)+[](ui_base* w,const char* s){if(w==nullptr)return symbol_ret_t {symbol_t::VOID, symbol_t::VOID, nullptr};return w->resolve_symbol(s); });
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); });
script->add_sym("vs_resolve_symbol", (void *)+[](ui_base* w,const char* s){if(w==nullptr)return symbol_ret_t {symbol_t::VOID, symbol_t::VOID, nullptr};return w->resolve_symbol(s, true); });
script->add_sym("vs_apply_prop", (void *)+[](ui_base* w,const char* k, const char* v){if(w==nullptr)return -1;return w->apply_prop(k,v); });
script->add_sym("vs_get_computed", (void *)+[](ui_base* w,const char* k, const char** v){if(w==nullptr)return -1;return w->get_computed(k,v); });
script->add_sym("vs_set", (void *)+[](ui_base* w,const char* k, const value_t* v){if(w==nullptr)return -1;return w->use_setter(w->resolve_symbol(k), v);});
script->add_sym("vs_get", (void *)+[](ui_base* w,const char* k, value_t** v){if(w==nullptr)return -1;return w->use_getter(w->resolve_symbol(k), v);});
script->add_sym("vs_set", (void *)+[](ui_base* w,const char* k, const value_t* v){if(w==nullptr)return -1;return w->use_setter(w->resolve_symbol(k, true), v);});
script->add_sym("vs_get", (void *)+[](ui_base* w,const char* k, value_t** v){if(w==nullptr)return -1;return w->use_getter(w->resolve_symbol(k, true), v);});


// Fragments of stdlib
Expand Down Expand Up @@ -123,7 +123,7 @@ std::shared_ptr<tcc> tcc_c_pipeline(bool is_runtime, vs::ui_base* obj, const cha
script->compile_str_embedded(
"#include <vs.h>\n#include <stub.h>\n//#file embedded \nvoid callback(){\n#line 0\n", //TODO: Add custom header if linked with an external thing
src,
"\n}"
"\n}\n"
);
}
else{
Expand Down
21 changes: 14 additions & 7 deletions src/ui-base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ const std::string& ui_base::get_name() const{
void ui_base::path(std::stringstream& dst, bool scoped)const{
frame* current = this->local_frame;
while(current!=nullptr){
dst<<current->name<<"/";
if(current->name[0]=='#'){dst<<"???"<<"/";} //Symbols and namesstarting with # are private and not exposed
else dst<<current->name<<"/";
if(scoped==true && (current->type==frame_type_t::CONTAINER or current->type==frame_type_t::SLOT_CONTAINER))break;
current=current->parent;
}
Expand Down Expand Up @@ -87,22 +88,24 @@ frame* ui_base::resolve_namespace() const{
return nullptr;
}

const ui_base* ui_base::resolve_name_path(const char * str) const{
const ui_base* ui_base::resolve_name_path(const char * str, bool safe) const{
static char buffer[64];
const ui_base* current = this;
for(int i=0;str[i]!=0;){
int j=i;
for(;str[j]!='/' && str[j]!=0 && (j-i <64);j++)buffer[j-i]=str[j]; //TODO exception if going beyond the buffer limits?
buffer[j-i]=0;
current=current->resolve_name(buffer);
current=current->resolve_name(buffer, safe);
i=j;
if(str[i]!=0)i++;
if(current==nullptr)return nullptr;
}
return current;
}

const ui_base* ui_base::resolve_name(const char * str) const{
const ui_base* ui_base::resolve_name(const char * str, bool safe) const{
if(safe && str[0]=='#')return nullptr;

if(local_frame!=nullptr){
auto tmp = local_frame->resolve_name(str);
if(tmp==nullptr)return nullptr;
Expand All @@ -121,7 +124,9 @@ const ui_base* ui_base::resolve_name(const char * str) const{
}
}

symbol_ret_t ui_base::resolve_symbol(const char * str) const{
symbol_ret_t ui_base::resolve_symbol(const char * str, bool safe) const{
if(safe && str[0]=='#')return {symbol_t::VOID, symbol_t::VOID, nullptr};

if(local_frame!=nullptr){
return local_frame->resolve_symbol(str);
}
Expand All @@ -134,7 +139,9 @@ const ui_base* ui_base::resolve_name(const char * str) const{
}
}

symbol_ret_t ui_base::resolve_symbol_local(const char * str) const{
symbol_ret_t ui_base::resolve_symbol_local(const char * str, bool safe) const{
if(safe && str[0]=='#')return {symbol_t::VOID, symbol_t::VOID, nullptr};

if(local_frame!=nullptr){
return local_frame->resolve_symbol_local(str);
}
Expand Down Expand Up @@ -270,7 +277,7 @@ void ui_base::refresh_style(const char* local_mixins){
}

int ui_base::run_test(){
return use_test(this->resolve_symbol_local("#test"));
return use_test(this->resolve_symbol_local("#test",false));
}

int ui_base::use_getter(const symbol_ret_t& sym, value_t ** value){
Expand Down
8 changes: 1 addition & 7 deletions src/ui-tree.xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ void ui_xml_tree::_build_base_widget_extended_attr(const pugi::xml_node &root, u
bool is_module=false;

auto script_type=root.attribute("type").as_string("");
auto compact=root.attribute("mode").as_bool(false);
auto compact=root.attribute("compact").as_bool(false);

//Check if it is a module or single user; if module check for cache and use it.
if(strcmp(script_type,"module")==0){
Expand All @@ -388,8 +388,6 @@ void ui_xml_tree::_build_base_widget_extended_attr(const pugi::xml_node &root, u
current->set_mode(((cache::script_t*)found->ref.get())->mode);
current->attach_script(((cache::script_t*)found->ref.get())->script,is_module);
current->set_symbols(((cache::script_t*)found->ref.get())->symbols);

if(compact)current->apply_prop("on.callback", "callback");
//All done, precomputed and rightfully applied!
continue;
}
Expand Down Expand Up @@ -430,8 +428,6 @@ void ui_xml_tree::_build_base_widget_extended_attr(const pugi::xml_node &root, u
globals::mem_storage.fetch_from_shared({this->fullname.as_string().c_str(),local_unique_counter+1,cache::resource_t::SCRIPT,false,false}, tmp);
local_unique_counter++;
}

if(compact)current->apply_prop("on.callback", "callback");
}
continue;
}
Expand All @@ -452,8 +448,6 @@ void ui_xml_tree::_build_base_widget_extended_attr(const pugi::xml_node &root, u
globals::mem_storage.fetch_from_shared({this->fullname.as_string().c_str(),local_unique_counter+1,cache::resource_t::SCRIPT,false,false}, tmp);
local_unique_counter++;
}

if(compact)current->apply_prop("on.callback", "callback");
}
continue;
}
Expand Down

0 comments on commit e51b232

Please sign in to comment.