From 76ce905ef3e93f067b6d06458186bcf9eb9b5c01 Mon Sep 17 00:00:00 2001 From: ame Date: Sat, 3 Aug 2024 02:30:02 -0500 Subject: fix tests, module based require, thread improvments --- src/reg.c | 49 +++++++++++++++++++++++++++++++++++++------------ src/thread.c | 47 +++++++++++++++++++++++++++++++++++------------ 2 files changed, 72 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/reg.c b/src/reg.c index 19b9df5..d8db25a 100644 --- a/src/reg.c +++ b/src/reg.c @@ -23,27 +23,52 @@ static int lua_exit(lua_State* L){ return 0; } +#define open_common(name)\ + int luaopen_lullaby_##name (lua_State* L){\ + luaL_register(L, NULL, name##_function_list);\ + return 1;\ + } + +open_common(array); +open_common(crypto); +open_common(io); +open_common(math); +open_common(config); +open_common(net); +open_common(thread); + +#define push(T, name)\ + lua_pushstring(L, #name);\ + luaopen_lullaby_##name(L);\ + lua_settable(L, T); + int luaopen_lullaby(lua_State* L) { - lua_newuserdata(L, 1); + /*lua_newuserdata(L, 1); int ud = lua_gettop(L); lua_newtable(L); int meta = lua_gettop(L); luaI_tsetcf(L, meta, "__gc", lua_exit); lua_pushvalue(L, meta); - lua_setmetatable(L, ud); - + lua_setmetatable(L, ud);*/ //create .array functions lua_newtable(L); - + int top = lua_gettop(L); //lua_newtable(L); - lreg("array", array_function_list); - lreg("crypto", crypto_function_list); - lreg("io", io_function_list); - lreg("math", math_function_list); - lreg("config", config_function_list); - lreg("net", net_function_list); - lreg("thread", thread_function_list); - + push(top, array); + push(top, crypto); + push(top, io); + push(top, math); + push(top, config); + push(top, net); + push(top, thread); + //lreg("array", array_function_list); + //lreg("crypto", crypto_function_list); + //lreg("io", io_function_list); + //lreg("math", math_function_list); + //lreg("config", config_function_list); + //lreg("net", net_function_list); + //lreg("thread", thread_function_list); + lua_settop(L, top); return 1; } diff --git a/src/thread.c b/src/thread.c index e6858f5..4757d30 100644 --- a/src/thread.c +++ b/src/thread.c @@ -280,25 +280,48 @@ int hi(lua_State* L){ return 0; } +//not thread safe yet int meta_proxy(lua_State* L){ - printf("proxy"); + int argc = lua_gettop(L); + struct thread_buffer *buffer = lua_touserdata(L, 1); + + lua_getmetatable(buffer->L, 1); + lua_pushstring(buffer->L, lua_tostring(L, 2)); + lua_gettable(buffer->L, 2); + + lua_pushvalue(buffer->L, 1); + + int count = 0; + for(int i = 4; i <= argc; i++){ + count++; + lua_pushvalue(L, i); + luaI_deepcopy(L, buffer->L, SKIP_GC); + } + + //printf("%i\n",count); + lua_call(buffer->L, count + 1, 1); + luaI_deepcopy(buffer->L, L, 0); + //printf("%p\n", lua_topointer(buffer->L, -1)); return 1; } -void meta_proxy_gen(lua_State* L, int meta_idx, int new_meta_idx){ +void meta_proxy_gen(lua_State* L, struct thread_buffer *buffer, int meta_idx, int new_meta_idx){ + + lua_pushcfunction(L, meta_proxy); + lua_setglobal(L, "__proxy_call"); + + lua_pushlightuserdata(L, buffer); + lua_setglobal(L, "__this_obj"); 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); +return __proxy_call(__this_obj,'%s',table.unpack({_a,_b,_c}));end", key); luaL_dostring(L, fn); //printf("%s\n",fn); free(fn); @@ -322,12 +345,6 @@ 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); - struct thread_buffer *buffer = lua_newuserdata(L, sizeof * buffer); int buffer_idx = lua_gettop(L); @@ -336,6 +353,12 @@ int l_buffer(lua_State* L){ lua_pushvalue(L, 1); luaI_deepcopy(L, buffer->L, SKIP_GC); + lua_newtable(L); + int meta_idx = lua_gettop(L); + if(use!=0) meta_proxy_gen(L, buffer, old_meta_idx, meta_idx); + luaI_tsetcf(L, meta_idx, "__index", l_buffer_index); + luaI_tsetcf(L, meta_idx, "__gc", l_buffer_gc); + lua_pushvalue(L, meta_idx); lua_setmetatable(L, buffer_idx); lua_pushvalue(L, buffer_idx); -- cgit v1.2.3