Skip to content

Commit fea5869

Browse files
fix on clojures
1 parent beb09ed commit fea5869

File tree

7 files changed

+64
-13
lines changed

7 files changed

+64
-13
lines changed

LuaCEmbed.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22653,6 +22653,8 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
2265322653

2265422654
#define PRIVATE_LUA_CEMBE_SUB_ARG_TABLE "private_lua_c_embed_table_arg_%s_%d"
2265522655
#define PRIVATE_LUA_CEMBED_ANONYMOUS_TABLE "private_lua_c_embed_anononymous_table_%ld"
22656+
#define PRIVATE_LUA_CEMBED_ANONYMOUS_FUNC_TABLE "private_lua_c_embed_anononymous_func_table_%ld"
22657+
2265622658
#define PRIVATE_LUA_CEMBED_SELFNAME "private_lua_c_embed_self"
2265722659
#define PRIVATE_LUA_CEMBED_MULTIRETURN "private_lua_c_embed_multi_return%d"
2265822660
#define PRIVATE_LUA_CEMBED_STAGE_AREA_TABLE "private_lua_c_embed_stage_area_table"
@@ -25264,6 +25266,7 @@ void privateLuaCEmbedTableArray_free(privateLuaCEmbedTableArray *self){
2526425266
for(int i = 0; i < self->size;i++){
2526525267
LuaCEmbedTable *current_table = self->tables[i];
2526625268
privateLuaCEmbedTable_free(current_table);
25269+
2526725270
}
2526825271

2526925272
free(self->tables);
@@ -25357,10 +25360,14 @@ LuaCEmbedTable * LuaCembed_new_anonymous_table(LuaCEmbed *self){
2535725360
PRIVATE_LUA_CEMBED_PROTECT_NULL
2535825361
private_lua_cembed_memory_limit = self->memory_limit;
2535925362

25363+
const char *format_raw = PRIVATE_LUA_CEMBED_ANONYMOUS_TABLE;
25364+
if(self->current_function){
25365+
format_raw =PRIVATE_LUA_CEMBED_ANONYMOUS_FUNC_TABLE;
25366+
}
2536025367
privateLuaCEmbedTableArray *target = (privateLuaCEmbedTableArray*)privateLuaCEmbed_get_current_table_array(self);
25361-
25362-
char *buffer= private_LuaCembed_format(PRIVATE_LUA_CEMBED_ANONYMOUS_TABLE, target->size);
25368+
char *buffer= private_LuaCembed_format(format_raw, target->size);
2536325369
LuaCEmbedTable *created_table =LuaCembed_new_global_table(self,buffer);
25370+
2536425371
free(buffer);
2536525372
return created_table;
2536625373
}
@@ -25512,7 +25519,13 @@ int privateLuaCEmbed_main_callback_handler(lua_State *L){
2551225519

2551325520
const char *func_name = lua_tostring(L,lua_upvalueindex(3));
2551425521
self->current_function = func_name;
25515-
self->func_tables = (void*)newprivateLuaCEmbedTableArray();
25522+
bool func_tables_created_in_these_scope = false;
25523+
25524+
if(self->func_tables == NULL) {
25525+
self->func_tables = (void*)newprivateLuaCEmbedTableArray();
25526+
func_tables_created_in_these_scope = true;
25527+
}
25528+
2551625529

2551725530
if(is_a_method){
2551825531
LuaCEmbedResponse *(*method_callback)(LuaCEmbedTable *tb, LuaCEmbed *self);
@@ -25532,8 +25545,12 @@ int privateLuaCEmbed_main_callback_handler(lua_State *L){
2553225545
function_callback = (LuaCEmbedResponse *(*)(LuaCEmbed *self))lua_touserdata(L, lua_upvalueindex(4));
2553325546
possible_return = function_callback(self);
2553425547
}
25548+
if(func_tables_created_in_these_scope) {
25549+
privateLuaCEmbedTableArray_free((privateLuaCEmbedTableArray*)self->func_tables);
25550+
self->func_tables = NULL;
25551+
25552+
}
2553525553

25536-
privateLuaCEmbedTableArray_free((privateLuaCEmbedTableArray*)self->func_tables);
2553725554
lua_settop(self->state, 0);
2553825555

2553925556
self->current_function = NULL;

main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ int main(int argc, char *argv[]){
1111
LuaCEmbed * l = lua_n.newLuaEvaluation();
1212

1313
lua_n.evaluate(l,"function test_func(a,b) return a + b end ");
14-
1514
LuaCEmbedTable * args = lua_n.tables.new_anonymous_table(l);
1615
lua_n.tables.append_long(args,20);
1716
lua_n.tables.append_long(args,30);
17+
LuaCembed_new_anonymous_table()
1818
LuaCEmbedTable *result = lua_n.globals.run_global_lambda(l,"test_func",args,1);
1919

2020
double result_0 = lua_n.tables.get_double_by_index(result,0);

src/LuaCEmbed/callback_handle/callback_handle.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ int privateLuaCEmbed_main_callback_handler(lua_State *L){
1616

1717
const char *func_name = lua_tostring(L,lua_upvalueindex(3));
1818
self->current_function = func_name;
19-
self->func_tables = (void*)newprivateLuaCEmbedTableArray();
19+
bool func_tables_created_in_these_scope = false;
20+
//these its nescessary for function clojures like map, filter, where the stack will be increased
21+
if(self->func_tables == NULL) {
22+
self->func_tables = (void*)newprivateLuaCEmbedTableArray();
23+
func_tables_created_in_these_scope = true;
24+
}
25+
2026

2127
if(is_a_method){
2228
LuaCEmbedResponse *(*method_callback)(LuaCEmbedTable *tb, LuaCEmbed *self);
@@ -36,8 +42,12 @@ int privateLuaCEmbed_main_callback_handler(lua_State *L){
3642
function_callback = (LuaCEmbedResponse *(*)(LuaCEmbed *self))lua_touserdata(L, lua_upvalueindex(4));
3743
possible_return = function_callback(self);
3844
}
45+
if(func_tables_created_in_these_scope) {
46+
privateLuaCEmbedTableArray_free((privateLuaCEmbedTableArray*)self->func_tables);
47+
self->func_tables = NULL;
48+
49+
}
3950

40-
privateLuaCEmbedTableArray_free((privateLuaCEmbedTableArray*)self->func_tables);
4151
lua_settop(self->state, 0);
4252

4353
self->current_function = NULL;

src/LuaCEmbed/globals/getters/getters.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,14 @@ LuaCEmbedTable * LuaCembed_new_anonymous_table(LuaCEmbed *self){
5757
PRIVATE_LUA_CEMBED_PROTECT_NULL
5858
private_lua_cembed_memory_limit = self->memory_limit;
5959

60+
const char *format_raw = PRIVATE_LUA_CEMBED_ANONYMOUS_TABLE;
61+
if(self->current_function){
62+
format_raw =PRIVATE_LUA_CEMBED_ANONYMOUS_FUNC_TABLE;
63+
}
6064
privateLuaCEmbedTableArray *target = (privateLuaCEmbedTableArray*)privateLuaCEmbed_get_current_table_array(self);
61-
62-
char *buffer= private_LuaCembed_format(PRIVATE_LUA_CEMBED_ANONYMOUS_TABLE, target->size);
65+
char *buffer= private_LuaCembed_format(format_raw, target->size);
6366
LuaCEmbedTable *created_table =LuaCembed_new_global_table(self,buffer);
67+
6468
free(buffer);
6569
return created_table;
6670
}

src/LuaCEmbed/table/table_array/table_array.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ void privateLuaCEmbedTableArray_free(privateLuaCEmbedTableArray *self){
5555
for(int i = 0; i < self->size;i++){
5656
LuaCEmbedTable *current_table = self->tables[i];
5757
privateLuaCEmbedTable_free(current_table);
58+
5859
}
5960

6061
free(self->tables);

src/constants/lua_code.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#define PRIVATE_LUA_CEMBE_SUB_ARG_TABLE "private_lua_c_embed_table_arg_%s_%d"
1010
#define PRIVATE_LUA_CEMBED_ANONYMOUS_TABLE "private_lua_c_embed_anononymous_table_%ld"
11+
#define PRIVATE_LUA_CEMBED_ANONYMOUS_FUNC_TABLE "private_lua_c_embed_anononymous_func_table_%ld"
12+
1113
#define PRIVATE_LUA_CEMBED_SELFNAME "private_lua_c_embed_self"
1214
#define PRIVATE_LUA_CEMBED_MULTIRETURN "private_lua_c_embed_multi_return%d"
1315
#define PRIVATE_LUA_CEMBED_STAGE_AREA_TABLE "private_lua_c_embed_stage_area_table"

tests/LuaCEmbed.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22653,6 +22653,8 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
2265322653

2265422654
#define PRIVATE_LUA_CEMBE_SUB_ARG_TABLE "private_lua_c_embed_table_arg_%s_%d"
2265522655
#define PRIVATE_LUA_CEMBED_ANONYMOUS_TABLE "private_lua_c_embed_anononymous_table_%ld"
22656+
#define PRIVATE_LUA_CEMBED_ANONYMOUS_FUNC_TABLE "private_lua_c_embed_anononymous_func_table_%ld"
22657+
2265622658
#define PRIVATE_LUA_CEMBED_SELFNAME "private_lua_c_embed_self"
2265722659
#define PRIVATE_LUA_CEMBED_MULTIRETURN "private_lua_c_embed_multi_return%d"
2265822660
#define PRIVATE_LUA_CEMBED_STAGE_AREA_TABLE "private_lua_c_embed_stage_area_table"
@@ -25264,6 +25266,7 @@ void privateLuaCEmbedTableArray_free(privateLuaCEmbedTableArray *self){
2526425266
for(int i = 0; i < self->size;i++){
2526525267
LuaCEmbedTable *current_table = self->tables[i];
2526625268
privateLuaCEmbedTable_free(current_table);
25269+
2526725270
}
2526825271

2526925272
free(self->tables);
@@ -25357,10 +25360,14 @@ LuaCEmbedTable * LuaCembed_new_anonymous_table(LuaCEmbed *self){
2535725360
PRIVATE_LUA_CEMBED_PROTECT_NULL
2535825361
private_lua_cembed_memory_limit = self->memory_limit;
2535925362

25363+
const char *format_raw = PRIVATE_LUA_CEMBED_ANONYMOUS_TABLE;
25364+
if(self->current_function){
25365+
format_raw =PRIVATE_LUA_CEMBED_ANONYMOUS_FUNC_TABLE;
25366+
}
2536025367
privateLuaCEmbedTableArray *target = (privateLuaCEmbedTableArray*)privateLuaCEmbed_get_current_table_array(self);
25361-
25362-
char *buffer= private_LuaCembed_format(PRIVATE_LUA_CEMBED_ANONYMOUS_TABLE, target->size);
25368+
char *buffer= private_LuaCembed_format(format_raw, target->size);
2536325369
LuaCEmbedTable *created_table =LuaCembed_new_global_table(self,buffer);
25370+
2536425371
free(buffer);
2536525372
return created_table;
2536625373
}
@@ -25512,7 +25519,13 @@ int privateLuaCEmbed_main_callback_handler(lua_State *L){
2551225519

2551325520
const char *func_name = lua_tostring(L,lua_upvalueindex(3));
2551425521
self->current_function = func_name;
25515-
self->func_tables = (void*)newprivateLuaCEmbedTableArray();
25522+
bool func_tables_created_in_these_scope = false;
25523+
25524+
if(self->func_tables == NULL) {
25525+
self->func_tables = (void*)newprivateLuaCEmbedTableArray();
25526+
func_tables_created_in_these_scope = true;
25527+
}
25528+
2551625529

2551725530
if(is_a_method){
2551825531
LuaCEmbedResponse *(*method_callback)(LuaCEmbedTable *tb, LuaCEmbed *self);
@@ -25532,8 +25545,12 @@ int privateLuaCEmbed_main_callback_handler(lua_State *L){
2553225545
function_callback = (LuaCEmbedResponse *(*)(LuaCEmbed *self))lua_touserdata(L, lua_upvalueindex(4));
2553325546
possible_return = function_callback(self);
2553425547
}
25548+
if(func_tables_created_in_these_scope) {
25549+
privateLuaCEmbedTableArray_free((privateLuaCEmbedTableArray*)self->func_tables);
25550+
self->func_tables = NULL;
25551+
25552+
}
2553525553

25536-
privateLuaCEmbedTableArray_free((privateLuaCEmbedTableArray*)self->func_tables);
2553725554
lua_settop(self->state, 0);
2553825555

2553925556
self->current_function = NULL;

0 commit comments

Comments
 (0)