Skip to content

Commit 20be0ab

Browse files
att
1 parent df376ac commit 20be0ab

File tree

15 files changed

+222
-255
lines changed

15 files changed

+222
-255
lines changed

LuaCEmbed.h

Lines changed: 81 additions & 117 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ int main(int argc, char *argv[]){
437437
LuaCEmbed * l = lua_n.newLuaEvaluation();
438438
lua_n.add_callback(l,"test",test_func);
439439
int seconds = 2;
440-
lua_n.set_timeout(l,seconds);
440+
lua_n.set_timeout(seconds);
441441

442442
lua_n.evaluate(l,"while true do end ;");
443443

@@ -482,14 +482,15 @@ AND IT WILL KILL THE APPLICATION
482482
<!--codeof:exemples/evaluation/memory_usage.c-->
483483
~~~c
484484
#include "LuaCEmbed.h"
485+
485486
LuaCEmbedNamespace lua_n;
486487

487488

488489

489490
int main(int argc, char *argv[]){
490491

491492
lua_n = newLuaCEmbedNamespace();
492-
LuaCEmbed * l = lua_n.newLuaEvaluation_with_custom_allocator();
493+
LuaCEmbed * l = lua_n.newLuaEvaluation();
493494
int one_mega = 1;
494495
lua_n.set_memory_limit(l,one_mega);
495496
lua_n.evaluate(l,"t = 'a';while true do t = t .. t end");

exemples/evaluation/memory_usage.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#include "LuaCEmbed.h"
2+
23
LuaCEmbedNamespace lua_n;
34

45

56

67
int main(int argc, char *argv[]){
78

89
lua_n = newLuaCEmbedNamespace();
9-
LuaCEmbed * l = lua_n.newLuaEvaluation_with_custom_allocator();
10+
LuaCEmbed * l = lua_n.newLuaEvaluation();
1011
int one_mega = 1;
1112
lua_n.set_memory_limit(l,one_mega);
1213
lua_n.evaluate(l,"t = 'a';while true do t = t .. t end");

exemples/evaluation/timeout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ int main(int argc, char *argv[]){
1313
LuaCEmbed * l = lua_n.newLuaEvaluation();
1414
lua_n.add_callback(l,"test",test_func);
1515
int seconds = 2;
16-
lua_n.set_timeout(l,seconds);
16+
lua_n.set_timeout(seconds);
1717

1818
lua_n.evaluate(l,"while true do end ;");
1919

src/LuaCEmbed/basic/basic.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ LuaCEmbed * newLuaCEmbedEvaluation(){
44
LuaCEmbed *self = (LuaCEmbed*) malloc(sizeof (LuaCEmbed));
55
*self = (LuaCEmbed){0};
66
self->state = luaL_newstate();
7-
if(lua_cembed_memory_limit > 0){
8-
lua_setallocf(self->state, private_LuaCembed_custom_allocator, &lua_cembed_used_memory);
9-
}
7+
108
self->global_tables = (void*)newprivateLuaCEmbedTableArray();
119

1210
return self;
1311
}
12+
void LuaCEmbed_load_lib_from_c(LuaCEmbed *self,int (*callback)(lua_State *l),const char *name){
13+
int result = callback(self->state);
14+
if(result > 0){
15+
lua_setglobal(self->state,name);
16+
}
17+
}
18+
1419

1520

16-
void LuaCEmbed_set_memory_limit( double limit){
17-
lua_cembed_memory_limit = limit;
18-
}
1921

2022

2123
void LuaCembed_set_delete_function(LuaCEmbed *self,void (*delelte_function)(struct LuaCEmbed *self)){

src/LuaCEmbed/basic/basic.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ LuaCEmbed * newLuaCEmbedEvaluation();
66

77
int private_LuaCemb_internal_free(lua_State *L);
88

9-
10-
11-
12-
9+
void LuaCEmbed_load_lib_from_c(LuaCEmbed *self,int (*callback)(lua_State *l),const char *name);
1310

1411
void LuaCembed_set_delete_function(LuaCEmbed *self,void (*delelte_function)(struct LuaCEmbed *self));
1512

@@ -29,7 +26,6 @@ void * privateLuaCEmbed_get_current_table_array(LuaCEmbed *self);
2926

3027
void LuaCEmbed_set_timeout(int seconds);
3128

32-
void LuaCEmbed_set_memory_limit( double limit);
3329

3430

3531
void privata_LuaCEmbed_increment_stack_(LuaCEmbed *self);

src/LuaCEmbed/declaration.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
typedef long private_lua_cembed_incremented_arg ;
2-
#include "memory/memory.h"
32
#include "LuaCEmbed.h"
3+
#include "memory/memory.h"
4+
45
#include "lib_start/lib_start.h"
56
#include "basic/basic.h"
67
#include "table/declaration.h"

src/LuaCEmbed/memory/memory.c

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

22

3-
3+
void LuaCEmbed_set_memory_limit(LuaCEmbed *self, double limit){
4+
lua_setallocf(self->state, private_LuaCembed_custom_allocator, &lua_cembed_used_memory);
5+
lua_cembed_memory_limit = limit;
6+
}
47
static void *private_LuaCembed_custom_allocator(void *ud, void *ptr, size_t osize, size_t nsize) {
58
int *used = (int *)ud;
69

src/LuaCEmbed/memory/memory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
long lua_cembed_used_memory = 0;
44
long lua_cembed_memory_limit = -1;
55

6+
void LuaCEmbed_set_memory_limit(LuaCEmbed *self, double limit);
67

78

89
static void *private_LuaCembed_custom_allocator(void *ud, void *ptr, size_t osize, size_t nsize) ;

src/namespace/LuaCEmbedNamespace/LuaCEmbedNamespace.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11

22

3+
#include "../../LuaCEmbed/basic/basic.h"
4+
35
LuaCEmbedNamespace newLuaCEmbedNamespace(){
46
LuaCEmbedNamespace self = {0};
57
self.newLuaLib = newLuaCEmbedLib;
8+
self.load_lib_from_c = LuaCEmbed_load_lib_from_c;
69
self.newLuaEvaluation = newLuaCEmbedEvaluation;
710
self.set_delete_function = LuaCembed_set_delete_function;
811
self.perform = LuaCembed_perform;
@@ -40,4 +43,4 @@ LuaCEmbedNamespace newLuaCEmbedNamespace(){
4043

4144
self.free = LuaCEmbed_free;
4245
return self;
43-
}
46+
}

src/namespace/LuaCEmbedNamespace/LuaCEmbedNamespace.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ typedef struct{
1010
LuaCEmbed * (*newLuaLib)(lua_State *state, bool public_functions);
1111
void (*set_delete_function)(LuaCEmbed *self,void (*delelte_function)(struct LuaCEmbed *self));
1212
LuaCEmbed * (*newLuaEvaluation)();
13+
void (*load_lib_from_c)(LuaCEmbed *self,int (*callback)(lua_State *l),const char *name);
1314
int (*perform)(LuaCEmbed *self);
1415
const char * (*convert_arg_code)(int arg_code);
15-
void (*set_memory_limit)( double limit);
16+
void (*set_memory_limit)(LuaCEmbed *self, double limit);
1617

1718
char * (*get_error_message)(LuaCEmbed *self);
1819
bool (*has_errors)(LuaCEmbed *self);

teste.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
#include "src/one.c"
3+
4+
LuaCEmbedNamespace lua_n;
5+
6+
7+
LuaCEmbedResponse * hello(LuaCEmbed *args){
8+
printf("my first callback\n");
9+
return NULL;
10+
}
11+
12+
int main(int argc, char *argv[]){
13+
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");
21+
22+
if(lua_n.has_errors(l)){
23+
printf("error: %s\n",lua_n.get_error_message(l));
24+
}
25+
26+
lua_n.free(l);
27+
28+
return 0;
29+
}

0 commit comments

Comments
 (0)