Skip to content

Commit 64aa300

Browse files
att
1 parent 20be0ab commit 64aa300

File tree

10 files changed

+87
-47
lines changed

10 files changed

+87
-47
lines changed

LuaCEmbed.h

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23066,7 +23066,6 @@ typedef struct LuaCEmbed{
2306623066
lua_State *state;
2306723067
const char *current_function;
2306823068
bool is_lib;
23069-
bool public_functions;
2307023069
int total_args;
2307123070
char *error_msg;
2307223071
void (*delete_function)(struct LuaCEmbed *self);
@@ -23099,7 +23098,7 @@ static void *private_LuaCembed_custom_allocator(void *ud, void *ptr, size_t osiz
2309923098

2310023099
int private_LuaCemb_internal_free(lua_State *L);
2310123100

23102-
LuaCEmbed * newLuaCEmbedLib(lua_State *state,bool public_functions);
23101+
LuaCEmbed * newLuaCEmbedLib(lua_State *state);
2310323102

2310423103
int LuaCembed_perform(LuaCEmbed *self);
2310523104

@@ -23461,13 +23460,14 @@ void LuaCEmbed_set_global_table(LuaCEmbed *self, const char *name, LuaCEmbedTabl
2346123460

2346223461
int privateLuaCEmbed_main_callback_handler(lua_State *L);
2346323462

23464-
void private_LuaCEmbed_add_lib_callback(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args) );
23463+
void private_LuaCEmbed_add_lib_callback(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args),bool global_functions );
2346523464

2346623465
void private_LuaCEmbed_add_evaluation_callback(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args) );
2346723466

2346823467
void LuaCEmbed_add_callback(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args) );
2346923468

2347023469

23470+
void LuaCEmbed_add_global_callback(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args));
2347123471

2347223472

2347323473

@@ -23747,7 +23747,7 @@ typedef struct{
2374723747
LuaCEmbedGlobalModule globals;
2374823748
LuaCembedTableModule tables;
2374923749
void (*clear_errors)(LuaCEmbed *self);
23750-
LuaCEmbed * (*newLuaLib)(lua_State *state, bool public_functions);
23750+
LuaCEmbed * (*newLuaLib)(lua_State *state);
2375123751
void (*set_delete_function)(LuaCEmbed *self,void (*delelte_function)(struct LuaCEmbed *self));
2375223752
LuaCEmbed * (*newLuaEvaluation)();
2375323753
void (*load_lib_from_c)(LuaCEmbed *self,int (*callback)(lua_State *l),const char *name);
@@ -23781,6 +23781,8 @@ typedef struct{
2378123781

2378223782
int (*evaluete_file)(LuaCEmbed *self, const char *file);
2378323783
void (*add_callback)(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args) );
23784+
void (*add_global_callback)(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args) );
23785+
2378423786
void (*free)(LuaCEmbed *self);
2378523787

2378623788
} LuaCEmbedNamespace;
@@ -23951,7 +23953,7 @@ int private_LuaCemb_internal_free(lua_State *L){
2395123953
LuaCEmbed_free(self);
2395223954
return 0;
2395323955
}
23954-
LuaCEmbed * newLuaCEmbedLib(lua_State *state,bool public_functions){
23956+
LuaCEmbed * newLuaCEmbedLib(lua_State *state){
2395523957
LuaCEmbed *self = (LuaCEmbed*) malloc(sizeof (LuaCEmbed));
2395623958
*self = (LuaCEmbed){0};
2395723959

@@ -23973,7 +23975,6 @@ LuaCEmbed * newLuaCEmbedLib(lua_State *state,bool public_functions){
2397323975

2397423976

2397523977
self->is_lib = true;
23976-
self->public_functions = public_functions;
2397723978
self->global_tables = (void*)newprivateLuaCEmbedTableArray();
2397823979

2397923980

@@ -26204,7 +26205,7 @@ int privateLuaCEmbed_main_callback_handler(lua_State *L){
2620426205
}
2620526206

2620626207

26207-
void private_LuaCEmbed_add_lib_callback(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args) ){
26208+
void private_LuaCEmbed_add_lib_callback(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args),bool global_functions ){
2620826209

2620926210
char *main_lib_table = private_LuaCembed_format(PRIVATE_LUA_CEMBED_MAIN_LIB_TABLE_NAME__,self->lib_identifier);
2621026211

@@ -26226,7 +26227,7 @@ void private_LuaCEmbed_add_lib_callback(LuaCEmbed *self, const char *callback_na
2622626227

2622726228

2622826229
lua_settable(self->state,-3);
26229-
if(self->public_functions){
26230+
if(global_functions){
2623026231
//it points the function to a global function
2623126232
//like: callback = private_lua_c_embed_main_lib_table.callback
2623226233
lua_getglobal(self->state, main_lib_table);
@@ -26259,13 +26260,22 @@ void LuaCEmbed_add_callback(LuaCEmbed *self, const char *callback_name, LuaCEmbe
2625926260
PRIVATE_LUA_CEMBED_PROTECT_VOID
2626026261

2626126262
if(self->is_lib){
26262-
private_LuaCEmbed_add_lib_callback(self,callback_name,callback);
26263+
private_LuaCEmbed_add_lib_callback(self,callback_name,callback,false);
2626326264
return;
2626426265
}
2626526266
private_LuaCEmbed_add_evaluation_callback(self,callback_name,callback);
2626626267

2626726268
}
2626826269

26270+
void LuaCEmbed_add_global_callback(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args)){
26271+
PRIVATE_LUA_CEMBED_PROTECT_VOID
26272+
26273+
if(self->is_lib){
26274+
private_LuaCEmbed_add_lib_callback(self,callback_name,callback,true);
26275+
return;
26276+
}
26277+
private_LuaCEmbed_add_evaluation_callback(self,callback_name,callback);
26278+
}
2626926279

2627026280

2627126281

@@ -26924,7 +26934,7 @@ LuaCEmbedNamespace newLuaCEmbedNamespace(){
2692426934
self.get_evaluation_bool = LuaCEmbed_get_evaluation_bool;
2692526935
self.evaluete_file = LuaCEmbed_evaluete_file;
2692626936
self.add_callback = LuaCEmbed_add_callback;
26927-
26937+
self.add_global_callback = LuaCEmbed_add_global_callback;
2692826938
self.set_bool_lib_prop = LuaCEmbed_set_bool_lib_prop;
2692926939
self.set_long_lib_prop = LuaCEmbed_set_long_lib_prop;
2693026940
self.set_double_lib_prop = LuaCEmbed_set_double_lib_prop;

src/LuaCEmbed/LuaCEmbed.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ typedef struct LuaCEmbed{
33
lua_State *state;
44
const char *current_function;
55
bool is_lib;
6-
bool public_functions;
76
int total_args;
87
char *error_msg;
98
void (*delete_function)(struct LuaCEmbed *self);

src/LuaCEmbed/callback_handle/callback_handle.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ int privateLuaCEmbed_main_callback_handler(lua_State *L){
158158
}
159159

160160

161-
void private_LuaCEmbed_add_lib_callback(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args) ){
161+
void private_LuaCEmbed_add_lib_callback(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args),bool global_functions ){
162162

163163
char *main_lib_table = private_LuaCembed_format(PRIVATE_LUA_CEMBED_MAIN_LIB_TABLE_NAME__,self->lib_identifier);
164164

@@ -180,7 +180,7 @@ void private_LuaCEmbed_add_lib_callback(LuaCEmbed *self, const char *callback_na
180180

181181

182182
lua_settable(self->state,-3);
183-
if(self->public_functions){
183+
if(global_functions){
184184
//it points the function to a global function
185185
//like: callback = private_lua_c_embed_main_lib_table.callback
186186
lua_getglobal(self->state, main_lib_table);
@@ -213,9 +213,19 @@ void LuaCEmbed_add_callback(LuaCEmbed *self, const char *callback_name, LuaCEmbe
213213
PRIVATE_LUA_CEMBED_PROTECT_VOID
214214

215215
if(self->is_lib){
216-
private_LuaCEmbed_add_lib_callback(self,callback_name,callback);
216+
private_LuaCEmbed_add_lib_callback(self,callback_name,callback,false);
217217
return;
218218
}
219219
private_LuaCEmbed_add_evaluation_callback(self,callback_name,callback);
220220

221221
}
222+
223+
void LuaCEmbed_add_global_callback(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args)){
224+
PRIVATE_LUA_CEMBED_PROTECT_VOID
225+
226+
if(self->is_lib){
227+
private_LuaCEmbed_add_lib_callback(self,callback_name,callback,true);
228+
return;
229+
}
230+
private_LuaCEmbed_add_evaluation_callback(self,callback_name,callback);
231+
}

src/LuaCEmbed/callback_handle/callback_handle.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
int privateLuaCEmbed_main_callback_handler(lua_State *L);
44

5-
void private_LuaCEmbed_add_lib_callback(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args) );
5+
void private_LuaCEmbed_add_lib_callback(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args),bool global_functions );
66

77
void private_LuaCEmbed_add_evaluation_callback(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args) );
88

99
void LuaCEmbed_add_callback(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args) );
1010

1111

12+
void LuaCEmbed_add_global_callback(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args));
1213

src/LuaCEmbed/lib_start/lib_start.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ int private_LuaCemb_internal_free(lua_State *L){
77
LuaCEmbed_free(self);
88
return 0;
99
}
10-
LuaCEmbed * newLuaCEmbedLib(lua_State *state,bool public_functions){
10+
LuaCEmbed * newLuaCEmbedLib(lua_State *state){
1111
LuaCEmbed *self = (LuaCEmbed*) malloc(sizeof (LuaCEmbed));
1212
*self = (LuaCEmbed){0};
1313

@@ -29,7 +29,6 @@ LuaCEmbed * newLuaCEmbedLib(lua_State *state,bool public_functions){
2929

3030

3131
self->is_lib = true;
32-
self->public_functions = public_functions;
3332
self->global_tables = (void*)newprivateLuaCEmbedTableArray();
3433

3534

src/LuaCEmbed/lib_start/lib_start.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
int private_LuaCemb_internal_free(lua_State *L);
33

4-
LuaCEmbed * newLuaCEmbedLib(lua_State *state,bool public_functions);
4+
LuaCEmbed * newLuaCEmbedLib(lua_State *state);
55

66
int LuaCembed_perform(LuaCEmbed *self);

src/namespace/LuaCEmbedNamespace/LuaCEmbedNamespace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ LuaCEmbedNamespace newLuaCEmbedNamespace(){
2929
self.get_evaluation_bool = LuaCEmbed_get_evaluation_bool;
3030
self.evaluete_file = LuaCEmbed_evaluete_file;
3131
self.add_callback = LuaCEmbed_add_callback;
32-
32+
self.add_global_callback = LuaCEmbed_add_global_callback;
3333
self.set_bool_lib_prop = LuaCEmbed_set_bool_lib_prop;
3434
self.set_long_lib_prop = LuaCEmbed_set_long_lib_prop;
3535
self.set_double_lib_prop = LuaCEmbed_set_double_lib_prop;

src/namespace/LuaCEmbedNamespace/LuaCEmbedNamespace.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ typedef struct{
77
LuaCEmbedGlobalModule globals;
88
LuaCembedTableModule tables;
99
void (*clear_errors)(LuaCEmbed *self);
10-
LuaCEmbed * (*newLuaLib)(lua_State *state, bool public_functions);
10+
LuaCEmbed * (*newLuaLib)(lua_State *state);
1111
void (*set_delete_function)(LuaCEmbed *self,void (*delelte_function)(struct LuaCEmbed *self));
1212
LuaCEmbed * (*newLuaEvaluation)();
1313
void (*load_lib_from_c)(LuaCEmbed *self,int (*callback)(lua_State *l),const char *name);
@@ -41,6 +41,8 @@ typedef struct{
4141

4242
int (*evaluete_file)(LuaCEmbed *self, const char *file);
4343
void (*add_callback)(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args) );
44+
void (*add_global_callback)(LuaCEmbed *self, const char *callback_name, LuaCEmbedResponse* (*callback)(LuaCEmbed *args) );
45+
4446
void (*free)(LuaCEmbed *self);
4547

4648
} LuaCEmbedNamespace;

teste.c

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,35 @@
44
LuaCEmbedNamespace lua_n;
55

66

7-
LuaCEmbedResponse * hello(LuaCEmbed *args){
8-
printf("my first callback\n");
9-
return NULL;
10-
}
11-
12-
int main(int argc, char *argv[]){
7+
LuaCEmbedResponse *add_cfunc(LuaCEmbed *args){
8+
double first_num = lua_n.args.get_double(args,0);
9+
double second_num = lua_n.args.get_double(args,1);
1310

14-
lua_n = newLuaCEmbedNamespace();
15-
LuaCEmbed * l = lua_n.newLuaEvaluation();
16-
int one_mega = 1;
17-
lua_n.set_memory_limit(l,one_mega);
18-
lua_n.add_callback(l,"teste",hello);
19-
lua_n.evaluate(l,"teste()");
20-
lua_n.evaluate(l,"t = 'a';while true do t = t .. t end");
11+
if(lua_n.has_errors(args)){
12+
char *message = lua_n.get_error_message(args);
13+
return lua_n.response.send_error(message);
14+
}
15+
double result = first_num + second_num;
16+
return lua_n.response.send_double(result);
17+
}
18+
LuaCEmbedResponse *sub_cfunc(LuaCEmbed *args){
19+
double first_num = lua_n.args.get_double(args,0);
20+
double second_num = lua_n.args.get_double(args,1);
2121

22-
if(lua_n.has_errors(l)){
23-
printf("error: %s\n",lua_n.get_error_message(l));
22+
if(lua_n.has_errors(args)){
23+
char *message = lua_n.get_error_message(args);
24+
return lua_n.response.send_error(message);
2425
}
26+
double result = first_num - second_num;
27+
return lua_n.response.send_double(result);
28+
}
29+
int luaopen_my_lib(lua_State *state){
30+
lua_n = newLuaCEmbedNamespace();
31+
//functions will be only assescible by the required reciver
2532

26-
lua_n.free(l);
33+
LuaCEmbed * l = lua_n.newLuaLib(state);
34+
lua_n.add_callback(l,"add",add_cfunc);
35+
lua_n.add_global_callback(l,"sub",sub_cfunc);
36+
return lua_n.perform(l);
2737

28-
return 0;
2938
}

0 commit comments

Comments
 (0)