diff options
Diffstat (limited to 'plop.c')
-rw-r--r-- | plop.c | 32 |
1 files changed, 31 insertions, 1 deletions
@@ -83,6 +83,36 @@ int make_server(const char * node, const char * service) return server; } +// TODO: Write documentation for load_handler +int load_handler(lua_State * L, const char * path) +{ + lua_getglobal(L, "package"); + lua_pushstring(L, "loaded"); + int result = lua_rawget(L, -2); + + if (LUA_TTABLE != result) + { + lua_pop(L, 2); + return LUA_ERRRUN; + } + + result = luaL_loadfile(L, path) || lua_pcall(L, 0, 1, 0); + + if (LUA_OK != result) + { + lua_pop(L, 3); + return result; + } + + lua_pushstring(L, "handler"); + lua_pushvalue(L, -2); + lua_rawset(L, -4); + lua_setglobal(L, "handler"); + lua_pop(L, 1); + + return LUA_OK; +} + /// Progresses the connection for a client and shifts it back in the array. /// `pfd` must be at least `shift_by + 1`-th element of the array. /// \param L Server's Lua state @@ -110,7 +140,7 @@ int handle_client(lua_State * L, struct pollfd * pfd, struct request ** request, } // TODO: Push the handler to stack earlier to avoid shifting it. - lua_getglobal((*request)->lua, "Handler"); + lua_getglobal((*request)->lua, "handler"); lua_insert((*request)->lua, 1); lua_pushlstring((*request)->lua, (*request)->data, (*request)->length); lua_call((*request)->lua, 5, 1); |