Skip to content

Commit

Permalink
Improving errors in tcc with #line. Fixed test function execution in …
Browse files Browse the repository at this point in the history
…scripts. Added support for compact scripts in C.
  • Loading branch information
checkroom committed Nov 22, 2024
1 parent b0fdb81 commit ed28d6b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 25 deletions.
6 changes: 6 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ does not somehow. But just `./two-buttons.xml` does? Check why!
### Scripting

- [ ] Expose path and the fetcher to embedded script at some degree.
- [ ] Make errors in C scripts less useless
- [x] Add `#line` to C library to properly count lines for error location
- [ ] Add `#file` based on the path in the path where it happens
- [ ] Support for compact scripts
- [x] C script
- [ ] JS script

### Components

Expand Down
2 changes: 1 addition & 1 deletion bindings/native/include/vs.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ extern void vs_debug(const char* key, const char* value);
#define $plotter(x) void* __EXPORT_DRW_##x = &x;
#define $getter(x) void* __EXPORT_GET_##x = &x;
#define $setter(x) void* __EXPORT_SET_##x = &x;
#define $fn(x) void* __EXPORT_UKN_##x = &x;
#define $fn(x) void* __EXPORT_UKN_##x = &x;
6 changes: 3 additions & 3 deletions include/pipelines/tcc-c.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ extern void tcc_log_symbol_func_xml(const pugi::xml_node& env, const char * msg,
* @param link_with path of a library to be linked against (exposing the standard vs interface)
* @return std::shared_ptr<tcc>
*/
extern std::shared_ptr<tcc> tcc_c_pipeline(bool is_runtime, ui_base* obj, const char* src, void* ctx, void(*error_fn)(void*,const char*), const char *link_with);
extern std::shared_ptr<tcc> tcc_c_pipeline(bool is_runtime, ui_base* obj, const char* src, void* ctx, void(*error_fn)(void*,const char*), bool compact, const char *link_with);
extern std::shared_ptr<smap<symbol_t>> tcc_c_pipeline_apply(const std::shared_ptr<tcc>& script,vs::ui_base* obj,void* ctx,void(*register_fn)(void*,const char*, const char*));

inline std::shared_ptr<tcc> tcc_c_pipeline_xml(bool is_runtime, vs::ui_base* obj, pugi::xml_node& ctx, const char *link_with){
return tcc_c_pipeline(is_runtime,obj,ctx.text().as_string(),&ctx,(void(*)(void*,const char*))tcc_error_func_xml,link_with);
inline std::shared_ptr<tcc> tcc_c_pipeline_xml(bool is_runtime, vs::ui_base* obj, pugi::xml_node& ctx, bool compact, const char *link_with){
return tcc_c_pipeline(is_runtime,obj,ctx.text().as_string(),&ctx,(void(*)(void*,const char*))tcc_error_func_xml, compact, link_with);
}

}
Expand Down
22 changes: 16 additions & 6 deletions src/pipelines/tcc-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static void vs_test_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)

std::shared_ptr<tcc> tcc_c_pipeline(bool is_runtime, vs::ui_base* obj, const char* src, void* ctx, void(*error_fn)(void*,const char*), const char *link_with){
std::shared_ptr<tcc> tcc_c_pipeline(bool is_runtime, vs::ui_base* obj, const char* src, void* ctx, void(*error_fn)(void*,const char*), bool compact, const char *link_with){
auto script = std::make_shared<tcc>();

//This part is a bit of a mess.
Expand Down Expand Up @@ -116,11 +116,21 @@ std::shared_ptr<tcc> tcc_c_pipeline(bool is_runtime, vs::ui_base* obj, const cha
script->add_file("./bindings/native/src/commons.c");
script->add_file("./bindings/native/src/stub.c");

script->compile_str_embedded(
"#include <vs.h>\n#include <stub.h>\n", //TODO: Add custom header if linked with an external thing
src,
""
);
if(compact){
script->compile_str_embedded(
"#include <vs.h>\n#include <stub.h>\n#file embedded \nint callback(){\n#line 0\n", //TODO: Add custom header if linked with an external thing
src,
"\n}"
);
}
else{
script->compile_str_embedded(
"#include <vs.h>\n#include <stub.h>\n#file embedded \n#line 0\n", //TODO: Add custom header if linked with an external thing
src,
""
);
}


//TODO Tcc error handling if compile fails to show error but fail to generate the tcc smart pointer.
script->relocate();
Expand Down
19 changes: 5 additions & 14 deletions src/ui-base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <ui-tree.hpp>

#include "pipelines/quickjs-js.hpp"
#include "quickjs.h"
#include "ui-frame.hpp"
#include "utils/strings.hpp"

Expand Down Expand Up @@ -345,27 +346,17 @@ 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){
if(sym.ctx_apply.symbol!=nullptr){
const ui_base* (*ctx_apply)(const ui_base*) = ( const ui_base* (*)(const ui_base*) ) sym.ctx_apply.symbol;
const ui_base* tmp =ctx_apply(sym.found_at->widget());
fn();
ctx_apply(tmp);
return 0;
}
else{
fn();
return 0;
}
return fn();
}
//TODO: To implement
else if(sym.found_at->get_mode()==frame_mode_t::QUICKJS){
//TODO: Add support for quickjs script modules
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 0;
return retval;
}
else{
//Callback type not supported yet.
Expand Down
5 changes: 4 additions & 1 deletion src/ui-tree.xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@ 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);
if(compact)current->apply_prop("on.callback", "callback");

//Check if it is a module or single user; if module check for cache and use it.
if(strcmp(script_type,"module")==0){
is_module=true;
Expand Down Expand Up @@ -414,7 +417,7 @@ void ui_xml_tree::_build_base_widget_extended_attr(const pugi::xml_node &root, u
if (mode == frame_mode_t::NATIVE || mode == frame_mode_t::AUTO) {
const auto &lang = root.attribute("lang").as_string(mode==frame_mode_t::NATIVE?"c":"");
if (strcmp(lang, "c") == 0) {
auto compiler = pipelines::tcc_c_pipeline_xml(true, is_module?nullptr:current, root, (link_with==nullptr)?nullptr:tmp_link.c_str());
auto compiler = pipelines::tcc_c_pipeline_xml(true, is_module?nullptr:current, root, compact, (link_with==nullptr)?nullptr:tmp_link.c_str());
if(compiler!=nullptr){
current->set_mode(frame_mode_t::NATIVE);
current->attach_script(compiler,is_module);
Expand Down

0 comments on commit ed28d6b

Please sign in to comment.