From 741d7034818aa3f9328b9c558489a3541871a346 Mon Sep 17 00:00:00 2001 From: ame Date: Thu, 1 Feb 2024 15:57:48 -0600 Subject: multiple function and globals soon maybe --- src/io.c | 6 ++++- src/lua.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lua.h | 7 ++--- src/net.c | 53 +++++++++++++++++++++++++++--------- src/parray.h | 1 + tests/net.lua | 33 ++++++++++++++--------- 6 files changed, 158 insertions(+), 29 deletions(-) create mode 100644 src/lua.c diff --git a/src/io.c b/src/io.c index 4a0fc93..14117ec 100644 --- a/src/io.c +++ b/src/io.c @@ -125,7 +125,11 @@ void i_pprint(lua_State* L, int indent, int skip_indent){ break; case LUA_TSTRING: if(!skip_indent) print_indentation(indent); - printf("\"%s\"", lua_tostring(L,-1)); + size_t len; + char* wowa = (char*)luaL_tolstring(L, -1, &len); + printf("\""); + for(int i = 0; i != len; i++) printf("%c",wowa[i]); + printf("\""); break; case LUA_TFUNCTION: if(!skip_indent) print_indentation(indent); diff --git a/src/lua.c b/src/lua.c new file mode 100644 index 0000000..13cbd9b --- /dev/null +++ b/src/lua.c @@ -0,0 +1,87 @@ +#include "lua.h" +#include +#include "io.h" +#include +#include +#include "i_str.h" +#include "parray.h" + +static int ii = 0; +void i_dcopy(lua_State* src, lua_State* dest, void* _seen){ + parray_t* seen = (parray_t*)_seen; + if(seen == NULL) seen = parray_init(); + size_t len; + int at, at2; + int *sp = malloc(1); + char* s; + void* whar; + int old_top = lua_gettop(src); + //printf("%i\n",ii++); + switch(lua_type(src, -1)){ + case LUA_TNUMBER: + lua_pushnumber(dest, luaL_checknumber(src, -1)); + break; + case LUA_TSTRING: + s = (char*)luaL_checklstring(src, -1, &len); + lua_pushlstring(dest, s, len); + break; + case LUA_TTABLE: + lua_newtable(dest); + at = lua_gettop(dest); + at2 = lua_gettop(src); + + *sp = at2; + whar = parray_get(seen, (void*)lua_topointer(src, at2)); + if( whar != NULL){ + //printf("%s\n",lua_tostring(src, at2)); + //printf("WHAR\n"); + + lua_pushvalue(dest, *(int*)whar); + return; + } else parray_set(seen, (void*)lua_topointer(src, at2), sp); + + lua_pushnil(src); + for(;lua_next(src, at2) != 0;){ + lua_pushvalue(src, -2); + + i_dcopy(src, dest, seen); + + lua_pushvalue(src, -2); + i_dcopy(src, dest, seen); + + lua_settable(dest, at); + lua_pop(src, 3); + } + lua_settop(dest, at); + break; + case LUA_TFUNCTION: + //lua_pushnil(dest); + //break; + if(lua_iscfunction(src, old_top)){ + //kinda silly + lua_pushcfunction(dest, lua_tocfunction(src, -1)); + break; + } + + lua_getglobal(src, "string"); + lua_pushstring(src, "dump"); + lua_gettable(src, -2); + lua_pushvalue(src, old_top); + lua_call(src, 1, 1); + + s = (char*)luaL_checklstring(src, -1, &len); + lua_pushlstring(dest, s, len); + //for(int i = 0; i != len; i++) printf("%c",s[i]); + printf("%i\n",luaL_loadbuffer(dest, s, len, "test")); + //lua_pushvalue(dest, -1); + break; + case LUA_TUSERDATA: + lua_pushlightuserdata(dest, lua_touserdata(src, -1)); + break; + default: + printf("%i\n",lua_type(src, -1)); + lua_pushnil(dest); + break; + } + //lua_settop(src, old_top); +} \ No newline at end of file diff --git a/src/lua.h b/src/lua.h index c31e31f..a4933ff 100644 --- a/src/lua.h +++ b/src/lua.h @@ -1,7 +1,8 @@ -#include -#include -#include +#include +#include +#include +void i_dcopy(lua_State* src, lua_State* dest, void*); #if LUA_VERSION_NUM == 504 #define lreg(N, FN)\ diff --git a/src/net.c b/src/net.c index 8bb0cd3..c831803 100644 --- a/src/net.c +++ b/src/net.c @@ -34,6 +34,11 @@ struct lchar { int len; }; +struct sarray_t { + struct lchar** cs; + int len; +}; + pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t lua_mutex = PTHREAD_MUTEX_INITIALIZER; @@ -297,11 +302,6 @@ void* handle_client(void *_arg){ send(client_fd, resp->c, resp->len, 0); str_free(resp); } else { - struct lchar* awa = (struct lchar*)v; - luaL_loadbuffer(L, awa->c, awa->len, awa->c); - - int func = lua_gettop(L); - lua_newtable(L); lua_newtable(L); for(int i = 0; i != len * 2; i+=2){ @@ -338,12 +338,21 @@ void* handle_client(void *_arg){ lua_pushvalue(L, -2); lua_settable(L, res_idx); - lua_pushvalue(L, func); // push function call - lua_pushvalue(L, res_idx); //push methods related to dealing with the request - lua_pushvalue(L, req_idx); //push info about the request + //the function(s) + struct sarray_t* awa = (struct sarray_t*)v; + for(int i = 0; i != awa->len; i++){ + struct lchar* wowa = awa->cs[i]; + luaL_loadbuffer(L, wowa->c, wowa->len, wowa->c); + + int func = lua_gettop(L); + + lua_pushvalue(L, func); // push function call + lua_pushvalue(L, res_idx); //push methods related to dealing with the request + lua_pushvalue(L, req_idx); //push info about the request - //call the function - lua_call(L, 2, 0); + //call the function + lua_call(L, 2, 0); + } } } @@ -416,11 +425,11 @@ int start_serv(lua_State* L, int port){ } //printf("%i\n",threads); //open a state to call shit, should be somewhat thread safe - - + thread_arg_struct* args = malloc(sizeof * args); args->fd = *client_fd; args->port = port; + //args->L = oL; pthread_mutex_lock(&mutex); threads++; @@ -458,7 +467,21 @@ int l_GET(lua_State* L){ if(paths == NULL) paths = parray_init(); - parray_set(paths, portc, (void*)awa); + + //please free this + void* v_old_paths = parray_get(paths, portc); + struct sarray_t* old_paths; + if(v_old_paths == NULL){ + old_paths = malloc(sizeof * old_paths); + old_paths->len = 0; + old_paths->cs = malloc(sizeof * old_paths->cs); + } else old_paths = (struct sarray_t*)v_old_paths; + + old_paths->len++; + old_paths->cs = realloc(old_paths->cs, sizeof * old_paths->cs * old_paths->len); + old_paths->cs[old_paths->len - 1] = awa; + + parray_set(paths, portc, (void*)old_paths); return 1; } @@ -473,6 +496,10 @@ int l_unlock(lua_State* L){ } int l_listen(lua_State* L){ + lua_State* src = luaL_newstate(); + lua_State* dest = luaL_newstate(); + + if(lua_gettop(L) != 2) { printf("not enough args"); abort(); diff --git a/src/parray.h b/src/parray.h index 2926bb2..5821922 100644 --- a/src/parray.h +++ b/src/parray.h @@ -1,3 +1,4 @@ + typedef struct { void* value; str* key; diff --git a/tests/net.lua b/tests/net.lua index e7a03a1..fd75cee 100644 --- a/tests/net.lua +++ b/tests/net.lua @@ -1,34 +1,43 @@ require "llib" llib.config.set({max_depth=5}) --local print = llib.io.pprint -function sleep (a) +sleep = function(a) local sec = tonumber(os.clock() + a); while (os.clock() < sec) do end end +aea = 5 + +--llib.io.pprint(_G.sleep) llib.io.pprint(llib.net.listen( function(server) - print("wowa") + --print("wowa") + server:GET("/", function(res, req) + --llib.io.pprint(res) + --llib.io.pprint(res) + --print(res.send) + --res:send("hi"); + --res.Code = 201 + --wwo.sleep(1) + --wwo.llib.io.pprint(wwo.sleep) + print("hi from first") + end) - llib.io.pprint(server:GET("/", function(res, req) + server:GET("/", function(res, req) --llib.io.pprint(res) --llib.io.pprint(res) --print(res.send) --res:send("hi"); --res.Code = 201 - --sleep(1) - require "llib" - llib.io.pprint(_G) - local sec = tonumber(os.clock() + 5); - while (os.clock() < sec) do - end + --wwo.sleep(1) + --wwo.llib.io.pprint(wwo.sleep) res:send("

hello world

") - end)) + end) - llib.io.pprint(server:GET("/test", function(res, req) + server:GET("/test", function(res, req) res.Code = 403 res:send("

you would never

") - end)) + end) end, -- cgit v1.2.3