From 6df5a49d612737784e824e91cac645447a3d3080 Mon Sep 17 00:00:00 2001 From: ame Date: Thu, 8 Feb 2024 13:03:13 -0600 Subject: globals mostly working --- src/i_str.c | 11 +++++++++++ src/i_str.h | 1 + src/lua.c | 35 ++++++++++++++++++++++++----------- src/net.c | 11 +++++++++-- tests/net.lua | 17 +++++++++++------ 5 files changed, 56 insertions(+), 19 deletions(-) diff --git a/src/i_str.c b/src/i_str.c index 804a33d..5b12a3f 100644 --- a/src/i_str.c +++ b/src/i_str.c @@ -26,6 +26,17 @@ void str_push(str* s, char* insert){ strcat(s->c, insert); } +void str_pushl(str* s, char* insert, size_t l){ + + s->c = realloc(s->c, s->len + l + 5); + //strcat(s->c, insert); + for(int i = 0; i != l; i++){ + s->c[i + s->len] = insert[i]; + } + s->len += l; + s->c[s->len] = '\0'; +} + void str_clear(str* s){ memset(s->c, 0, s->len); diff --git a/src/i_str.h b/src/i_str.h index 18409bf..60b0cb9 100644 --- a/src/i_str.h +++ b/src/i_str.h @@ -11,4 +11,5 @@ typedef struct { str* str_init(char*); void str_free(str*); void str_push(str*, char*); +void str_pushl(str*, char*, size_t); void str_clear(str*); diff --git a/src/lua.c b/src/lua.c index 30cec89..eb9637b 100644 --- a/src/lua.c +++ b/src/lua.c @@ -7,6 +7,18 @@ #include "parray.h" static int ii = 0; + +int writer(lua_State *L, const void* p, size_t sz, void* ud){ + char o[2] = {0}; + for (int i =0; ic); + } + + return 0; +} void i_dcopy(lua_State* src, lua_State* dest, void* _seen){ parray_t* seen = (parray_t*)_seen; if(seen == NULL) seen = parray_init(); @@ -55,25 +67,26 @@ void i_dcopy(lua_State* src, lua_State* dest, void* _seen){ 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); + str* awa = str_init(""); + lua_dump(src, writer, (void*)awa, 0); + //l_pprint(src); + //lua_pcall(src, 1, 1, 0); + //l_pprint(src); + //lua_settop(src, oo); + //lua_pop(src, 6); - s = (char*)luaL_checklstring(src, -1, &len); - lua_pushlstring(dest, s, len); - //for(int i = 0; i != len; i++) printf("%c",s[i]); - luaL_loadbuffer(dest, s, len, "test"); + //s = (char*)luaL_checklstring(src, -1, &len); + lua_pushlstring(dest, awa->c, awa->len); + //for(int i = 0; i != awa->len; i++) printf("%i : %c\n",i, awa->c[i]); + luaL_loadbuffer(dest, awa->c, awa->len, awa->c); lua_remove(dest, -2); + str_free(awa); //lua_pushvalue(dest, -1); break; case LUA_TUSERDATA: diff --git a/src/net.c b/src/net.c index 5dde288..ccfa28c 100644 --- a/src/net.c +++ b/src/net.c @@ -338,8 +338,11 @@ int l_close(lua_State* L){ lua_pushstring(L, "client_fd"); lua_gettable(L, res_idx); int client_fd = luaL_checkinteger(L, -1); - if(client_fd <= 0) abort(); // add error message - + if(client_fd <= 0){ + printf("already closed\n"); + abort(); + }// add error message + return 0; lua_pushstring(L, "client_fd"); lua_pushinteger(L, -1); lua_settable(L, res_idx); @@ -361,11 +364,15 @@ void* handle_client(void *_arg){ //create state for this thread lua_State* L = luaL_newstate(); luaL_openlibs(L); + pthread_mutex_lock(&mutex); + int old_top = lua_gettop(args->L); lua_getglobal(args->L, "_G"); i_dcopy(args->L, L, NULL); + lua_settop(args->L, old_top); lua_setglobal(L, "_G"); pthread_mutex_unlock(&mutex); + //read full request size_t bytes_received = recv_full_buffer(client_fd, &buffer, &header_eof); diff --git a/tests/net.lua b/tests/net.lua index 34943db..9a7b49c 100644 --- a/tests/net.lua +++ b/tests/net.lua @@ -7,10 +7,13 @@ sleep = function(a) end end aea = 5 -_G.ww = llib +--_G.wo = llib +--_G.ww = llib --llib.io.pprint(_G) llib.io.pprint(llib.net.listen( function(server) + --llib = nil + llib.io.pprint(_G) print("wowa") server:all("/*", function(res, req) --llib.io.pprint(res) @@ -20,17 +23,19 @@ llib.io.pprint(llib.net.listen( --res.Code = 201 --wwo.sleep(1) --wwo.llib.io.pprint(wwo.sleep) - require "llib" - llib.io.pprint(_G) - llib.config.set({max_depth=4}) - llib.io.pprint(_G.ww) + --require "llib" + --llib.io.pprint(_G) + + --_G.llib.io.pprint(_G.ww) + --llib.io.pprint(_G.wo) --print("hi from first") --llib.io.pprint(llib.crypto.md5("hewwo")) + --_G.sleep(1) end) server:GET("/aa", function(res, req) res.header["Content-Type"] = "text/plain" - server:lock() + --_G.server:lock() res:write("hi\n") res:write("next") res:close() -- cgit v1.2.3