From 2999d3412ccf5c7fc1a9e84695c7a2bce69d3e82 Mon Sep 17 00:00:00 2001 From: ame Date: Thu, 13 Jun 2024 00:01:51 -0500 Subject: working on deepcopy ub --- src/thread.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 76 insertions(+), 23 deletions(-) (limited to 'src/thread.c') diff --git a/src/thread.c b/src/thread.c index dec9ceb..5b3cb11 100644 --- a/src/thread.c +++ b/src/thread.c @@ -97,7 +97,7 @@ int l_res(lua_State* L){ int ot = lua_gettop(L); lua_pushvalue(L, 2 + i); - luaI_deepcopy(L, info->L, NULL, 0); + luaI_deepcopy(L, info->L, 0); lua_settop(L, ot); } @@ -149,7 +149,7 @@ int l_await(lua_State* L){ int ot = lua_gettop(info->L); lua_pushvalue(info->L, ot - info->return_count + i); - luaI_deepcopy(info->L, L, NULL, 0); + luaI_deepcopy(info->L, L, 0); lua_settop(info->L, ot); } @@ -176,17 +176,18 @@ int l_clean(lua_State* L){ } int l_async(lua_State* oL){ - lua_State* L = luaL_newstate(); - - //luaL_openlibs(L); + lua_State* L = luaL_newstate(); lua_getglobal(oL, "_G"); - luaI_deepcopy(oL, L, NULL, 0); - lua_set_global_table(L); + luaI_deepcopy(oL, L, SKIP_GC); + //lua_set_global_table(L); + + return 0; struct thread_info* args = calloc(1, sizeof * args); args->L = L; - args->lock = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER; + //args->lock = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER; + pthread_mutex_init(&args->lock, NULL); pthread_mutex_lock(&args->lock); args->return_count = 0; @@ -221,7 +222,7 @@ struct thread_buffer { int _buffer_get(lua_State* L){ struct thread_buffer *buffer = lua_touserdata(L, 1); pthread_mutex_lock(&buffer->lock); - luaI_deepcopy(buffer->L, L, NULL, 0); + luaI_deepcopy(buffer->L, L, 0); pthread_mutex_unlock(&buffer->lock); return 1; } @@ -230,7 +231,7 @@ int _buffer_set(lua_State* L){ struct thread_buffer *buffer = lua_touserdata(L, 1); pthread_mutex_lock(&buffer->lock); lua_settop(buffer->L, 0); - luaI_deepcopy(L, buffer->L, NULL, 0); + luaI_deepcopy(L, buffer->L, 0); pthread_mutex_unlock(&buffer->lock); return 1; } @@ -238,7 +239,7 @@ int _buffer_set(lua_State* L){ int _buffer_mod(lua_State* L){ struct thread_buffer *buffer = lua_touserdata(L, 1); pthread_mutex_lock(&buffer->lock); - luaI_deepcopy(buffer->L, L, NULL, 0); + luaI_deepcopy(buffer->L, L, 0); int item = lua_gettop(L); lua_pushvalue(L, 2); lua_pushvalue(L, item); @@ -246,7 +247,7 @@ int _buffer_mod(lua_State* L){ if(lua_type(L, -1) != LUA_TNIL){ lua_settop(buffer->L, 0); - luaI_deepcopy(L, buffer->L, NULL, 0); + luaI_deepcopy(L, buffer->L, 0); } pthread_mutex_unlock(&buffer->lock); return 1; @@ -276,27 +277,79 @@ int l_buffer_index(lua_State* L){ return 1; } -int l_buffer(lua_State* L){ - struct thread_buffer *buffer = malloc(sizeof * buffer); - buffer->L = luaL_newstate(); - pthread_mutex_init(&buffer->lock, NULL); - luaI_deepcopy(L, buffer->L, NULL, 0); +int hi(lua_State* L){ + printf("hi\n"); + return 0; +} + +int meta_proxy(lua_State* L){ + printf("proxy"); + return 1; +} + +void meta_proxy_gen(lua_State* L, int meta_idx, int new_meta_idx){ + + lua_pushnil(L); + for(; lua_next(L, meta_idx) != 0;){ + int k, v = lua_gettop(L); + k = lua_gettop(L) - 1; + + lua_pushcfunction(L, meta_proxy); + lua_setglobal(L, "__proxy_call"); + + char* fn = calloc(128, sizeof * fn); //todo: find optimal value for these + const char* key = lua_tostring(L, k); + sprintf(fn, "return function(_a,_b,_c)\ +return __proxy_call('%s',table.unpack({_a,_b,_c}));end", key); + luaL_dostring(L, fn); + //printf("%s\n",fn); + free(fn); + int nf = lua_gettop(L); + + luaI_tsetv(L, new_meta_idx, lua_tostring(L, k), nf); + + lua_pop(L, 3); + } +} + +int l_buffer_gc(lua_State* L){ + printf("gc\n"); + struct thread_buffer *buffer = lua_touserdata(L, 1); + pthread_mutex_lock(&buffer->lock); + lua_close(buffer->L); + return 0; +} + +int l_buffer(lua_State* L){ + int use = lua_getmetatable(L, 1); + int old_meta_idx = lua_gettop(L); lua_newtable(L); int meta_idx = lua_gettop(L); + if(use!=0) meta_proxy_gen(L, old_meta_idx, meta_idx); luaI_tsetcf(L, meta_idx, "__index", l_buffer_index); + luaI_tsetcf(L, meta_idx, "__gc", l_buffer_gc); - lua_pushlightuserdata(L, buffer); - lua_pushvalue(L, meta_idx); - lua_setmetatable(L, -2); + struct thread_buffer *buffer = lua_newuserdata(L, sizeof * buffer); + int buffer_idx = lua_gettop(L); + + buffer->L = luaL_newstate(); + pthread_mutex_init(&buffer->lock, NULL); + lua_pushvalue(L, 1); + luaI_deepcopy(L, buffer->L, SKIP_GC); + lua_pushvalue(L, meta_idx); + lua_setmetatable(L, buffer_idx); + lua_pushvalue(L, buffer_idx); return 1; } int l_testcopy(lua_State* L){ + lua_settop(L, 0); lua_State* temp = luaL_newstate(); - luaI_deepcopy(L, temp, NULL, 0); - luaI_deepcopy(temp, L, NULL, 0); - + lua_getglobal(L, "_G"); + luaI_deepcopy(L, temp, SKIP_GC); + //luaI_deepcopy(temp, L, NULL, SKIP_GC); + lua_close(temp); return 1; } -- cgit v1.2.3