From 76ce905ef3e93f067b6d06458186bcf9eb9b5c01 Mon Sep 17 00:00:00 2001 From: ame Date: Sat, 3 Aug 2024 02:30:02 -0500 Subject: fix tests, module based require, thread improvments --- docs/readme.md | 18 +++++++++++++++++ makefile | 2 +- readme.md | 8 +++++--- src/reg.c | 49 ++++++++++++++++++++++++++++++++++----------- src/thread.c | 47 ++++++++++++++++++++++++++++++++----------- tests/h.lua | 34 +++++++------------------------ tests/hash.lua | 2 +- tests/net.lua | 63 +++++++++++++++------------------------------------------- tests/net2.lua | 23 +++++++++++++++++++++ 9 files changed, 143 insertions(+), 103 deletions(-) create mode 100644 tests/net2.lua diff --git a/docs/readme.md b/docs/readme.md index c9aa7d7..26fc5cb 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -15,3 +15,21 @@ llib.io.pprint(llib) --pprint is a part of the io module, pprint meaning pretty all subtables and functions have a corresponding file in this directory on usage +you can also select just a specific module + +```lua +crypto = require "lullaby.crypto" +crypto.sha224() +``` + +--- + +## big changes + +### __clone metamethod (todo) + +takes a single argument (the object to be cloned) returns a 'copy' of the object + +this is for cloning a object to be the same, but not share any internals + +created for luaI_deepcopy (see src/lua.c) too create a seperate object for the other state diff --git a/makefile b/makefile index 3668e86..6b2dd48 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,7 @@ CC := clang CFLAGS := -fPIC LFLAGS := -lm -shared -LINKER := clang +LINKER := clang TARGET := lullaby.so diff --git a/readme.md b/readme.md index 80bdd8c..4720337 100644 --- a/readme.md +++ b/readme.md @@ -7,16 +7,18 @@ heres an example of a webserver to return a [sha0](https://en.wikipedia.org/wiki
```lua -llib = require "lullaby" +--(this is in tests/net2.lua) +net = require "lullaby.net" +crypto = require "lullaby.crypto" local port = 8080 MAX_LENGTH = 2048 -llib.net.listen(function(server) +net.listen(function(server) --listen to post requests at localhost:8080 (root directory) server:POST("/", function(res, req) --creates a sha0 hash object - local hash = llib.crypto.sha0() + local hash = crypto.sha0() --loads an extra 2048 characters from the request body (the body is not guaranteed to be >= 2048 characters, reasoning in docs) req:roll(MAX_LENGTH) diff --git a/src/reg.c b/src/reg.c index 19b9df5..d8db25a 100644 --- a/src/reg.c +++ b/src/reg.c @@ -23,27 +23,52 @@ static int lua_exit(lua_State* L){ return 0; } +#define open_common(name)\ + int luaopen_lullaby_##name (lua_State* L){\ + luaL_register(L, NULL, name##_function_list);\ + return 1;\ + } + +open_common(array); +open_common(crypto); +open_common(io); +open_common(math); +open_common(config); +open_common(net); +open_common(thread); + +#define push(T, name)\ + lua_pushstring(L, #name);\ + luaopen_lullaby_##name(L);\ + lua_settable(L, T); + int luaopen_lullaby(lua_State* L) { - lua_newuserdata(L, 1); + /*lua_newuserdata(L, 1); int ud = lua_gettop(L); lua_newtable(L); int meta = lua_gettop(L); luaI_tsetcf(L, meta, "__gc", lua_exit); lua_pushvalue(L, meta); - lua_setmetatable(L, ud); - + lua_setmetatable(L, ud);*/ //create .array functions lua_newtable(L); - + int top = lua_gettop(L); //lua_newtable(L); - lreg("array", array_function_list); - lreg("crypto", crypto_function_list); - lreg("io", io_function_list); - lreg("math", math_function_list); - lreg("config", config_function_list); - lreg("net", net_function_list); - lreg("thread", thread_function_list); - + push(top, array); + push(top, crypto); + push(top, io); + push(top, math); + push(top, config); + push(top, net); + push(top, thread); + //lreg("array", array_function_list); + //lreg("crypto", crypto_function_list); + //lreg("io", io_function_list); + //lreg("math", math_function_list); + //lreg("config", config_function_list); + //lreg("net", net_function_list); + //lreg("thread", thread_function_list); + lua_settop(L, top); return 1; } diff --git a/src/thread.c b/src/thread.c index e6858f5..4757d30 100644 --- a/src/thread.c +++ b/src/thread.c @@ -280,25 +280,48 @@ int hi(lua_State* L){ return 0; } +//not thread safe yet int meta_proxy(lua_State* L){ - printf("proxy"); + int argc = lua_gettop(L); + struct thread_buffer *buffer = lua_touserdata(L, 1); + + lua_getmetatable(buffer->L, 1); + lua_pushstring(buffer->L, lua_tostring(L, 2)); + lua_gettable(buffer->L, 2); + + lua_pushvalue(buffer->L, 1); + + int count = 0; + for(int i = 4; i <= argc; i++){ + count++; + lua_pushvalue(L, i); + luaI_deepcopy(L, buffer->L, SKIP_GC); + } + + //printf("%i\n",count); + lua_call(buffer->L, count + 1, 1); + luaI_deepcopy(buffer->L, L, 0); + //printf("%p\n", lua_topointer(buffer->L, -1)); return 1; } -void meta_proxy_gen(lua_State* L, int meta_idx, int new_meta_idx){ +void meta_proxy_gen(lua_State* L, struct thread_buffer *buffer, int meta_idx, int new_meta_idx){ + + lua_pushcfunction(L, meta_proxy); + lua_setglobal(L, "__proxy_call"); + + lua_pushlightuserdata(L, buffer); + lua_setglobal(L, "__this_obj"); lua_pushnil(L); for(; lua_next(L, meta_idx) != 0;){ int k, v = lua_gettop(L); k = lua_gettop(L) - 1; - lua_pushcfunction(L, meta_proxy); - lua_setglobal(L, "__proxy_call"); - char* fn = calloc(128, sizeof * fn); //todo: find optimal value for these const char* key = lua_tostring(L, k); sprintf(fn, "return function(_a,_b,_c)\ -return __proxy_call('%s',table.unpack({_a,_b,_c}));end", key); +return __proxy_call(__this_obj,'%s',table.unpack({_a,_b,_c}));end", key); luaL_dostring(L, fn); //printf("%s\n",fn); free(fn); @@ -322,12 +345,6 @@ int l_buffer(lua_State* L){ int use = lua_getmetatable(L, 1); int old_meta_idx = lua_gettop(L); - lua_newtable(L); - int meta_idx = lua_gettop(L); - if(use!=0) meta_proxy_gen(L, old_meta_idx, meta_idx); - luaI_tsetcf(L, meta_idx, "__index", l_buffer_index); - luaI_tsetcf(L, meta_idx, "__gc", l_buffer_gc); - struct thread_buffer *buffer = lua_newuserdata(L, sizeof * buffer); int buffer_idx = lua_gettop(L); @@ -336,6 +353,12 @@ int l_buffer(lua_State* L){ lua_pushvalue(L, 1); luaI_deepcopy(L, buffer->L, SKIP_GC); + lua_newtable(L); + int meta_idx = lua_gettop(L); + if(use!=0) meta_proxy_gen(L, buffer, old_meta_idx, meta_idx); + luaI_tsetcf(L, meta_idx, "__index", l_buffer_index); + luaI_tsetcf(L, meta_idx, "__gc", l_buffer_gc); + lua_pushvalue(L, meta_idx); lua_setmetatable(L, buffer_idx); lua_pushvalue(L, buffer_idx); diff --git a/tests/h.lua b/tests/h.lua index 310d6a8..9717650 100644 --- a/tests/h.lua +++ b/tests/h.lua @@ -1,31 +1,11 @@ -llib = require "llib" +llib = require "lullaby" +llib.config.set({print_meta = 1}) -llib.config.set({print_meta=1,max_depth=22}) ---llib.thread.lock(1) ---llib.thread.lock(2) ---llib.thread.unlock(2) -a = llib.thread.buffer(llib.crypto.md5()) +local a = llib.crypto.sha1() +local b = llib.thread.buffer(a) -print(a:get():final()) -for i=1,2009 do - llib.thread.async(function (res) - --llib.io.pprint(a); - --l = a + a - --a:mod(function(e) return e:update("meow") end) - end) -end +llib.io.pprint(a) +llib.io.pprint(b) +llib.io.pprint((b + "meow"):final()) ---os.execute("sleep 1") -print(a:get():final()) ---print("unlock") ---llib.thread.unlock(1) - - ---awa = thread_a:await() - ---print(awa:await()) ---print((awa + "hi"):final()) ---thread_a:clean() - -print("clean exit") diff --git a/tests/hash.lua b/tests/hash.lua index b35515a..60d33a8 100644 --- a/tests/hash.lua +++ b/tests/hash.lua @@ -1,4 +1,4 @@ -llib = require "llib" +llib = require "lullaby" local hashes_working = 0 local hashes_failed = 0 diff --git a/tests/net.lua b/tests/net.lua index 1afd02f..25ca696 100644 --- a/tests/net.lua +++ b/tests/net.lua @@ -1,66 +1,35 @@ -llib = require "llib" -llib.config.set({max_depth=5}) ---local print = llib.io.pprint +--yes, this is kinda a mess of requires, just proof it works:3 +net = require "lullaby.net" +io = require "lullaby.io" +crypto = require "lullaby.crypto" +config = require "lullaby.config" + +config.set({max_depth=5}) + sleep = function(a) local sec = tonumber(os.clock() + a); while (os.clock() < sec) do end end -aea = 5 -local wowa = 5 ---_G.wo = llib -_G._llib = _G.llib ---_G.ww = llib ---llib.io.pprint(_G) -llib.net.listen( + +net.listen( function(server) - --llib = nil - --llib.io.pprint(_G) - llib.io.pprint("online") + + io.pprint("online") _G.server = server server:all("/", function(res, req) - b = llib.crypto.md5("hello") + b = crypto.md5("hello") - --llib.io.pprint(a + '5') res:send(b) - --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) - --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) - --_G.llib.io.pprint(_G._G._G._llib.crypto.md5("hi")) - --_G.llib.io.pprint(_G._Go) - --_G.llib.io.pprint(_G.wo.crypto.md5("55")) - --_G.llib.io.pprint(req) - --print(req.partial) - --_G.llib.io.pprint(_G.llib.io.readfile(".gitignore")) - --print(req._bytes) - --res:send(_G.llib.io.readfile("llib.dll")) - --_G.llib.io.pprint(_G.llib.crypto.md5(_G.llib.io.readfile(".gitignore"))) - --_G.llib.io.pprint(req) - --_G.llib.io.pprint(req) - --print("start") a = req:roll() - --print(a) + while a > -1 do a = req:roll() print(req._bytes .. "/" .. req["Content-Length"]) - --print(a) end - llib.io.pprint(req) - --_G.llib.io.pprint(req) - --_G.llib.io.pprint("hi") - --res:send("done") + io.pprint(req) + end) server:GET("/aa", function(res, req) diff --git a/tests/net2.lua b/tests/net2.lua new file mode 100644 index 0000000..0ef3644 --- /dev/null +++ b/tests/net2.lua @@ -0,0 +1,23 @@ +--(this is in tests/net2.lua) +net = require "lullaby.net" +crypto = require "lullaby.crypto" +local port = 8080 +MAX_LENGTH = 2048 + +net.listen(function(server) + + --listen to post requests at localhost:8080 (root directory) + server:POST("/", function(res, req) + --creates a sha0 hash object + local hash = crypto.sha0() + --loads an extra 2048 characters from the request body (the body is not guaranteed to be >= 2048 characters, reasoning in docs) + req:roll(MAX_LENGTH) + + --incremental hashes allow updating via addition, in this case adding the body and getting a string from it + hash = (hash + req.Body):final() + --send the hash to the client, closes connection, but thread is live until it ends + res:send(hash) + end) + +end, port) + -- cgit v1.2.3