aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorame <[email protected]>2024-02-08 13:03:13 -0600
committerame <[email protected]>2024-02-08 13:03:13 -0600
commit6df5a49d612737784e824e91cac645447a3d3080 (patch)
treece915237c42ddd79014b668f11713d2ae2b1a677 /src
parent6c6146a89f5d4f2e615c8e97f26c896d50ff6d08 (diff)
globals mostly working
Diffstat (limited to 'src')
-rw-r--r--src/i_str.c11
-rw-r--r--src/i_str.h1
-rw-r--r--src/lua.c35
-rw-r--r--src/net.c11
4 files changed, 45 insertions, 13 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; i<sz; i++){
+ //printf("%c\n",((char*)p)[i]);
+ o[0] = ((char*)p)[i];
+ str_pushl((str*)ud, o, 1);
+ //printf("%s\n",((str*)ud)->c);
+ }
+
+ 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);