aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorame <[email protected]>2024-02-01 15:57:48 -0600
committerame <[email protected]>2024-02-01 15:57:48 -0600
commit741d7034818aa3f9328b9c558489a3541871a346 (patch)
treec2ed0df011a1a07e226679f1121646bba0c09989 /src
parent1b459e523badaa51cf249390f4b407130e1037fb (diff)
multiple function and globals soon maybe
Diffstat (limited to 'src')
-rw-r--r--src/io.c6
-rw-r--r--src/lua.c87
-rw-r--r--src/lua.h7
-rw-r--r--src/net.c53
-rw-r--r--src/parray.h1
5 files changed, 137 insertions, 17 deletions
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 <stdio.h>
+#include "io.h"
+#include <stdlib.h>
+#include <string.h>
+#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 <lua5.1/lua.h>
-#include <lua5.1/lualib.h>
-#include <lua5.1/lauxlib.h>
+#include <lua.h>
+#include <lualib.h>
+#include <lauxlib.h>
+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;