From 51acce51a60d9e32dee4b27ecddc907ab4d2d8c4 Mon Sep 17 00:00:00 2001 From: ame Date: Wed, 31 Jan 2024 11:55:58 -0600 Subject: docs stuff --- src/lua.h | 36 +++++++++++++++++++++++++++++++++--- src/net.c | 61 +++++++++++++++++++++++++------------------------------------ 2 files changed, 58 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/lua.h b/src/lua.h index f853408..c31e31f 100644 --- a/src/lua.h +++ b/src/lua.h @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include #if LUA_VERSION_NUM == 504 @@ -9,6 +9,10 @@ luaL_register(L, NULL, FN);\ lua_settable(L, -3); + + #define requiref( L, modname, f, glob ) \ + { luaL_requiref( L, modname, f, glob ); lua_pop( L, 1 ); } + #define lua_objlen(L,i) lua_rawlen(L,(i)) #define luaL_register(L, M, F) luaL_newlib(L, F); #else @@ -16,6 +20,32 @@ lua_newtable(L);\ luaL_register(L, NULL, FN);\ lua_setfield(L, 2, N); + + //taken straight from luaproc + #define requiref(L, modname, f, glob){\ + lua_pushcfunction(L, f);\ + lua_pushstring(L, modname); \ + lua_call(L, 1, 1);\ + lua_getfield(L, LUA_GLOBALSINDEX, LUA_LOADLIBNAME);\ + if(lua_type(L, -1) == LUA_TTABLE){\ + lua_getfield(L, -1, "loaded");\ + if(lua_type(L, -1) == LUA_TTABLE){\ + lua_getfield(L, -1, modname);\ + if(lua_type(L, -1) == LUA_TNIL) {\ + lua_pushvalue(L, 1);\ + lua_setfield(L, -3, modname);\ + }\ + lua_pop(L, 1);\ + }\ + lua_pop(L, 1);\ + }\ + lua_pop(L, 1);\ + if(glob){\ + lua_setglobal(L, modname);\ + }else{\ + lua_pop(L, 1);\ + }\ + } #endif diff --git a/src/net.c b/src/net.c index 786ce4f..8bb0cd3 100644 --- a/src/net.c +++ b/src/net.c @@ -35,6 +35,7 @@ struct lchar { }; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +pthread_mutex_t lua_mutex = PTHREAD_MUTEX_INITIALIZER; size_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof){ char* buffer = malloc(BUFFER_SIZE * sizeof * buffer); @@ -233,15 +234,14 @@ int l_send(lua_State* L){ str* header_vs = str_init(""); lua_pushnil(L); for(;lua_next(L, header) != 0;){ - char* key = (char)luaL_tolstring(L, -2, NULL); + char* key = (char*)luaL_checklstring(L, -2, NULL); if(strcmp(key, "Code") != 0){ str_push(header_vs, key); str_push(header_vs, ": "); - str_push(header_vs, (char)luaL_tolstring(L, -2, NULL)); + str_push(header_vs, (char*)luaL_checklstring(L, -2, NULL)); str_push(header_vs, "\r\n"); - lua_pop(L, 1); } - lua_pop(L, 2); + lua_pop(L, 1); } lua_pushvalue(L, header); @@ -414,7 +414,7 @@ int start_serv(lua_State* L, int port){ printf("failed to accept\n"); abort(); } - printf("%i\n",threads); + //printf("%i\n",threads); //open a state to call shit, should be somewhat thread safe @@ -435,9 +435,6 @@ int start_serv(lua_State* L, int port){ } -#define requiref( L, modname, f, glob ) \ - { luaL_requiref( L, modname, f, glob ); lua_pop( L, 1 ); } - int l_GET(lua_State* L){ lua_pushstring(L, "port"); lua_gettable(L, 1); @@ -454,7 +451,7 @@ int l_GET(lua_State* L){ lua_call(L, 1, 1); size_t len; - char* a = (char*)luaL_tolstring(L, -1, &len); + char* a = (char*)luaL_checklstring(L, -1, &len); struct lchar* awa = malloc(len + 1); awa->c = a; awa->len = len; @@ -462,34 +459,18 @@ int l_GET(lua_State* L){ if(paths == NULL) paths = parray_init(); parray_set(paths, portc, (void*)awa); - /* - int tab_idx = ports[port]; - int ot; - if(tab_idx == 0){ - lua_newtable(L); - ports[port] = tab_idx = luaL_ref(L, LUA_REGISTRYINDEX); - lua_rawgeti(L,LUA_REGISTRYINDEX,tab_idx); - - } else { - lua_rawgeti(L,LUA_REGISTRYINDEX,tab_idx); - } - int o = lua_gettop(L); - - lua_newtable(L); - lua_pushstring(L, "fn"); - lua_pushvalue(L, 3); - lua_settable(L, -3); - - lua_pushvalue(L, o); - lua_pushvalue(L, 2); - lua_pushvalue(L, -3); - lua_settable(L, -3); - */ - //printf("%i%s",port, lua_tostring(L, 2)); - return 1; } +int l_lock(lua_State* L){ + pthread_mutex_lock(&lua_mutex); + return 0; +} + +int l_unlock(lua_State* L){ + pthread_mutex_unlock(&lua_mutex); + return 0; +} int l_listen(lua_State* L){ if(lua_gettop(L) != 2) { @@ -507,6 +488,14 @@ int l_listen(lua_State* L){ lua_pushcfunction(L, l_GET); lua_settable(L, -3); + lua_pushstring(L, "lock"); + lua_pushcfunction(L, l_lock); + lua_settable(L, -3); + + lua_pushstring(L, "unlock"); + lua_pushcfunction(L, l_unlock); + lua_settable(L, -3); + lua_pushstring(L, "port"); lua_pushvalue(L, 2); lua_settable(L, -3); @@ -536,7 +525,7 @@ int l_spawn(lua_State* L){ lua_call(L, 1, 1); size_t len; - char* a = (char*)luaL_tolstring(L, -1, &len); + char* a = (char*)luaL_checklstring(L, -1, &len); //luaL_loadbuffer(L, a, len, a); //lua_call(L,0,0); @@ -546,7 +535,7 @@ int l_spawn(lua_State* L){ requiref(sL, "package", luaopen_package, 1); lua_pushlstring(sL, a, len); - char* b = (char*)luaL_tolstring(sL, -1, &len); + char* b = (char*)luaL_checklstring(sL, -1, &len); luaL_loadbuffer(sL, b, len, b); //l_pprint(L); -- cgit v1.2.3