Skip to content

Commit

Permalink
Improved field registration, now supporting arbitrary names
Browse files Browse the repository at this point in the history
  • Loading branch information
karurochari committed Nov 30, 2024
1 parent d5b7106 commit 4320638
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
23 changes: 19 additions & 4 deletions bindings/native/include/vs.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ struct symbol_ret_t{

typedef struct symbol_ret_t symbol_ret_t;

struct vs_field_t{
const char* name;
int(*setter)(const char*);
int(*getter)(char**);
};

typedef struct vs_field_t vs_field_t;


//extern int printf(const char*restrict, ...);

Expand Down Expand Up @@ -85,12 +93,19 @@ extern int vs_get(node_t, const char* k, char** v);
extern void vs_debug(const char* key, const char* value);
#define $$debug(k,v) vs_debug(k,v)


#define STRINGIZE(x) STRINGIZE2(x)
#define STRINGIZE2(x) #x
#define LINE_STRING STRINGIZE(__LINE__)

#define CONCAT_(prefix, suffix) prefix##suffix
/// Concatenate `prefix, suffix` into `prefixsuffix`
#define CONCAT(prefix, suffix) CONCAT_(prefix, suffix)
#define MAKE_UNIQUE_VARIABLE_NAME(prefix) CONCAT(prefix##_, __LINE__)

//Utility functions to export symbols
#define $callback(x) void* __EXPORT_CB__##x = x;
#define $cb(x) void* __EXPORT_CB__##x = x;
#define $plotter(x) void* __EXPORT_DRW_##x = x;
#define $getter(f,x) void* __EXPORT_GET_##f = x;
#define $setter(f,x) void* __EXPORT_SET_##f = x;
#define $field(f,x,y) void* __EXPORT_SET_##f = x;\
void* __EXPORT_GET_##f = y;
#define $field vs_field_t MAKE_UNIQUE_VARIABLE_NAME(__EXPORT_FIELD_)=
#define $fn(x) void* __EXPORT_UKN_##x = x;
8 changes: 5 additions & 3 deletions examples/custom-fields.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<script lang="c">
<![CDATA[
int setter(const void* value){
int setter(const char* value){
$log(LOG_LOG,"Setter");
return 0;
}
int getter(void** value){
int getter(char** value){
$log(LOG_LOG,"Getter");
return 0;
}
Expand All @@ -24,7 +24,9 @@
if($get("foo",ptr)!=0)$log(LOG_PANIC,"Getter failed");
}
$field(foo,setter,getter);
$field{"foo",setter,getter};
$field{"foo2",setter,getter};
$cb(on_set)
$cb(on_get)
]]>
Expand Down
18 changes: 17 additions & 1 deletion src/pipelines/tcc-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ std::shared_ptr<smap<symbol_t>> tcc_c_pipeline_apply(const std::shared_ptr<tcc>
}else if(strncmp("__EXPORT_CB__", name, 13)==0){
ctx->log(ctx->ref,"Registering public callback symbol `%s`",name+13);
ctx->symbols.emplace(name+13, symbol_t{symbol_mode_t::NATIVE,symbol_type_t::CALLBACK,(const void*)(((size_t*)value)[0])});
}else if(strncmp("__EXPORT_GET_", name, 13)==0){
}/*else if(strncmp("__EXPORT_GET_", name, 13)==0){
if(((size_t*)value)[0]!=0){
ctx->log(ctx->ref,"Registering public getter symbol `%s`",name+13);
std::string tmp = std::string("#g_") + (name+13); //TODO: Avoid temp string
Expand All @@ -192,6 +192,22 @@ std::shared_ptr<smap<symbol_t>> tcc_c_pipeline_apply(const std::shared_ptr<tcc>
ctx->log(ctx->ref,"Registering public setter symbol `%s`",name+13);
ctx->symbols.emplace(tmp, symbol_t{symbol_mode_t::NATIVE,symbol_type_t::SETTER,(const void*)(((size_t*)value)[0])});
}
}*/
else if(strncmp("__EXPORT_FIELD_", name, 15)==0){
if(((size_t*)value)[0]!=0){
struct vs_field_t{
const char* name;
int(*setter)(const char*);
int(*getter)(char**);
};
vs_field_t* fvalue = (vs_field_t* )value;

std::string stmp = std::string("#s_") + (fvalue->name); //TODO: Avoid temp string
std::string gtmp = std::string("#g_") + (fvalue->name); //TODO: Avoid temp string
ctx->log(ctx->ref,"Registering public field `%s`",fvalue->name);
ctx->symbols.emplace(stmp, symbol_t{symbol_mode_t::NATIVE,symbol_type_t::SETTER,(const void*)fvalue->setter});
ctx->symbols.emplace(gtmp, symbol_t{symbol_mode_t::NATIVE,symbol_type_t::GETTER,(const void*)fvalue->getter});
}
}else if(strncmp("__EXPORT_UKN_", name, 13)==0){
ctx->log(ctx->ref,"Registering public unknown symbol `%s`",name+13);
ctx->symbols.emplace(name+13, symbol_t{symbol_mode_t::NATIVE,symbol_type_t::UNKNOWN,(const void*)(((size_t*)value)[0])});
Expand Down

0 comments on commit 4320638

Please sign in to comment.