Lines Matching refs:L
8 static mcp_funcgen_t *mcp_funcgen_route(lua_State *L, mcp_funcgen_t *fgen, mcp_parser_t *pr);
9 static int mcp_funcgen_router_cleanup(lua_State *L, mcp_funcgen_t *fgen);
11 static void mcp_funcgen_cleanup(lua_State *L, mcp_funcgen_t *fgen);
32 int mcplib_funcgen_gc(lua_State *L) { in mcplib_funcgen_gc() argument
33 mcp_funcgen_t *fgen = luaL_checkudata(L, -1, "mcp.funcgen"); in mcplib_funcgen_gc()
39 mcp_funcgen_cleanup(L, fgen); in mcplib_funcgen_gc()
78 static void mcp_rcontext_cleanup(lua_State *L, mcp_funcgen_t *fgen, mcp_rcontext_t *rctx, int fgen_… in mcp_rcontext_cleanup() argument
79 luaL_unref(L, LUA_REGISTRYINDEX, rctx->coroutine_ref); in mcp_rcontext_cleanup()
80 luaL_unref(L, LUA_REGISTRYINDEX, rctx->function_ref); in mcp_rcontext_cleanup()
82 luaL_unref(L, LUA_REGISTRYINDEX, rctx->request_ref); in mcp_rcontext_cleanup()
94 lua_rawgeti(L, LUA_REGISTRYINDEX, subrctx->fgen->self_ref); in mcp_rcontext_cleanup()
95 mcp_rcontext_cleanup(L, subrctx->fgen, subrctx, lua_absindex(L, -1)); in mcp_rcontext_cleanup()
96 lua_pop(L, 1); // drop subrctx fgen in mcp_rcontext_cleanup()
102 luaL_unref(L, LUA_REGISTRYINDEX, rqu->res_ref); in mcp_rcontext_cleanup()
107 luaL_unref(L, LUA_REGISTRYINDEX, rqu->cb_ref); in mcp_rcontext_cleanup()
118 lua_getiuservalue(L, fgen_idx, 1); in mcp_rcontext_cleanup()
119 luaL_unref(L, -1, rctx->self_ref); in mcp_rcontext_cleanup()
121 lua_pop(L, 1); // drop freelist table in mcp_rcontext_cleanup()
124 LIBEVENT_THREAD *t = PROXY_GET_THR(L); in mcp_rcontext_cleanup()
192 lua_State *L = fgen->thread->L; in _mcplib_funcgen_cache() local
193 lua_rawgeti(L, LUA_REGISTRYINDEX, fgen->self_ref); in _mcplib_funcgen_cache()
194 mcp_rcontext_cleanup(L, fgen, rctx, lua_absindex(L, -1)); in _mcplib_funcgen_cache()
195 lua_pop(L, 1); // drop fgen in _mcplib_funcgen_cache()
201 mcp_funcgen_cleanup(fgen->thread->L, fgen); in _mcplib_funcgen_cache()
206 static int _mcplib_funcgen_gencall(lua_State *L) { in _mcplib_funcgen_gencall() argument
207 mcp_funcgen_t *fgen = luaL_checkudata(L, -2, "mcp.funcgen"); in _mcplib_funcgen_gencall()
208 int fgen_idx = lua_absindex(L, -2); in _mcplib_funcgen_gencall()
211 mcp_rcontext_t *rc = lua_newuserdatauv(L, rctx_len, 0); in _mcplib_funcgen_gencall()
214 luaL_getmetatable(L, "mcp.rcontext"); in _mcplib_funcgen_gencall()
215 lua_setmetatable(L, -2); in _mcplib_funcgen_gencall()
228 mcp_resp_t *r = mcp_prep_bare_resobj(L, fgen->thread); in _mcplib_funcgen_gencall()
229 rqu->res_ref = luaL_ref(L, LUA_REGISTRYINDEX); in _mcplib_funcgen_gencall()
235 mcp_rcontext_t *subrctx = mcp_funcgen_get_rctx(L, fg->self_ref, fg); in _mcplib_funcgen_gencall()
237 proxy_lua_error(L, "failed to generate request slot during queue_assign()"); in _mcplib_funcgen_gencall()
244 luaL_unref(L, LUA_REGISTRYINDEX, subrctx->request_ref); in _mcplib_funcgen_gencall()
258 lua_pushvalue(L, -1); in _mcplib_funcgen_gencall()
263 lua_rotate(L, -3, 1); in _mcplib_funcgen_gencall()
269 lua_rawgeti(L, LUA_REGISTRYINDEX, fgen->argument_ref); in _mcplib_funcgen_gencall()
274 lua_call(L, call_argnum, 1); in _mcplib_funcgen_gencall()
277 if (!lua_isfunction(L, -1)) { in _mcplib_funcgen_gencall()
278 proxy_lua_error(L, "function generator didn't return a function"); in _mcplib_funcgen_gencall()
284 rc->function_ref = luaL_ref(L, LUA_REGISTRYINDEX); in _mcplib_funcgen_gencall()
289 lua_getiuservalue(L, fgen_idx, 1); // get the reference table. in _mcplib_funcgen_gencall()
291 lua_rotate(L, -2, 1); in _mcplib_funcgen_gencall()
292 rc->self_ref = luaL_ref(L, -2); // pop rcontext in _mcplib_funcgen_gencall()
293 lua_pop(L, 1); // pop ref table. in _mcplib_funcgen_gencall()
298 rc->Lc = lua_newthread(L); in _mcplib_funcgen_gencall()
300 rc->coroutine_ref = luaL_ref(L, LUA_REGISTRYINDEX); in _mcplib_funcgen_gencall()
303 LIBEVENT_THREAD *t = PROXY_GET_THR(L); in _mcplib_funcgen_gencall()
326 lua_State *L = fgen->thread->L; in _mcp_funcgen_return_rctx() local
327 luaL_unref(L, LUA_REGISTRYINDEX, rctx->coroutine_ref); in _mcp_funcgen_return_rctx()
328 rctx->Lc = lua_newthread(L); in _mcp_funcgen_return_rctx()
330 rctx->coroutine_ref = luaL_ref(L, LUA_REGISTRYINDEX); in _mcp_funcgen_return_rctx()
397 mcp_rcontext_t *mcp_funcgen_get_rctx(lua_State *L, int fgen_ref, mcp_funcgen_t *fgen) { in mcp_funcgen_get_rctx() argument
405 lua_pushcclosure(L, _mcplib_funcgen_gencall, 0); in mcp_funcgen_get_rctx()
407 lua_rawgeti(L, LUA_REGISTRYINDEX, fgen_ref); in mcp_funcgen_get_rctx()
409 lua_rawgeti(L, LUA_REGISTRYINDEX, fgen->generator_ref); in mcp_funcgen_get_rctx()
411 int res = lua_pcall(L, 2, 1, 0); in mcp_funcgen_get_rctx()
413 LOGGER_LOG(NULL, LOG_PROXYEVENTS, LOGGER_PROXY_ERROR, NULL, lua_tostring(L, -1)); in mcp_funcgen_get_rctx()
414 lua_settop(L, 0); in mcp_funcgen_get_rctx()
417 lua_pop(L, 1); // drop the extra funcgen in mcp_funcgen_get_rctx()
430 mcp_rcontext_t *mcp_funcgen_start(lua_State *L, mcp_funcgen_t *fgen, mcp_parser_t *pr) { in mcp_funcgen_start() argument
432 fgen = mcp_funcgen_route(L, fgen, pr); in mcp_funcgen_start()
439 mcp_rcontext_t *rctx = mcp_funcgen_get_rctx(L, fgen->self_ref, fgen); in mcp_funcgen_start()
450 …mcp_request_t *rq = lua_newuserdatauv(L, sizeof(mcp_request_t) + MCP_REQUEST_MAXLEN + KEY_MAX_LENG… in mcp_funcgen_start()
452 luaL_getmetatable(L, "mcp.request"); in mcp_funcgen_start()
453 lua_setmetatable(L, -2); in mcp_funcgen_start()
455 rctx->request_ref = luaL_ref(L, LUA_REGISTRYINDEX); // pop the request in mcp_funcgen_start()
467 static void mcp_funcgen_cleanup(lua_State *L, mcp_funcgen_t *fgen) { in mcp_funcgen_cleanup() argument
469 lua_checkstack(L, 5); // paranoia. this can recurse from a router. in mcp_funcgen_cleanup()
473 lua_rawgeti(L, LUA_REGISTRYINDEX, fgen->self_ref); in mcp_funcgen_cleanup()
474 fgen_idx = lua_absindex(L, -1); // remember fgen offset in mcp_funcgen_cleanup()
476 luaL_unref(L, LUA_REGISTRYINDEX, fgen->self_ref); in mcp_funcgen_cleanup()
483 fgen_idx = lua_absindex(L, -1); in mcp_funcgen_cleanup()
488 mcp_funcgen_router_cleanup(L, fgen); in mcp_funcgen_cleanup()
492 LIBEVENT_THREAD *t = PROXY_GET_THR(L); in mcp_funcgen_cleanup()
501 mcp_rcontext_cleanup(L, fgen, rctx, fgen_idx); in mcp_funcgen_cleanup()
505 luaL_unref(L, LUA_REGISTRYINDEX, fgen->argument_ref); in mcp_funcgen_cleanup()
510 luaL_unref(L, LUA_REGISTRYINDEX, fgen->generator_ref); in mcp_funcgen_cleanup()
519 luaL_unref(L, LUA_REGISTRYINDEX, rqu->obj_ref); in mcp_funcgen_cleanup()
523 mcp_funcgen_dereference(L, subfgen); in mcp_funcgen_cleanup()
533 lua_pop(L, 1); // drop funcgen reference in mcp_funcgen_cleanup()
538 void mcp_funcgen_reference(lua_State *L) { in mcp_funcgen_reference() argument
539 mcp_funcgen_t *fgen = luaL_checkudata(L, -1, "mcp.funcgen"); in mcp_funcgen_reference()
542 lua_pop(L, 1); // ensure we drop the extra value. in mcp_funcgen_reference()
544 fgen->self_ref = luaL_ref(L, LUA_REGISTRYINDEX); in mcp_funcgen_reference()
550 void mcp_funcgen_dereference(lua_State *L, mcp_funcgen_t *fgen) { in mcp_funcgen_dereference() argument
559 mcp_funcgen_cleanup(L, fgen); in mcp_funcgen_dereference()
566 static int _mcplib_funcgenbare_generator(lua_State *L) { in _mcplib_funcgenbare_generator() argument
567 lua_pushvalue(L, lua_upvalueindex(1)); in _mcplib_funcgenbare_generator()
575 int mcplib_funcgenbare_new(lua_State *L) { in mcplib_funcgenbare_new() argument
576 if (!lua_isfunction(L, -1)) { in mcplib_funcgenbare_new()
577 proxy_lua_error(L, "Must pass a function to mcp.funcgenbare_new"); in mcplib_funcgenbare_new()
582 lua_pushcclosure(L, _mcplib_funcgenbare_generator, 1); in mcplib_funcgenbare_new()
587 lua_pushvalue(L, -1); in mcplib_funcgenbare_new()
588 int gen_ref = luaL_ref(L, LUA_REGISTRYINDEX); in mcplib_funcgenbare_new()
591 mcplib_funcgen_new(L); in mcplib_funcgenbare_new()
593 mcp_funcgen_t *fgen = lua_touserdata(L, -1); in mcplib_funcgenbare_new()
603 int mcplib_funcgen_new(lua_State *L) { in mcplib_funcgen_new() argument
604 LIBEVENT_THREAD *t = PROXY_GET_THR(L); in mcplib_funcgen_new()
606 mcp_funcgen_t *fgen = lua_newuserdatauv(L, sizeof(mcp_funcgen_t), 2); in mcplib_funcgen_new()
612 luaL_getmetatable(L, "mcp.funcgen"); in mcplib_funcgen_new()
613 lua_setmetatable(L, -2); in mcplib_funcgen_new()
616 lua_createtable(L, 8, 0); in mcplib_funcgen_new()
619 lua_setiuservalue(L, -2, 1); in mcplib_funcgen_new()
624 int mcplib_funcgen_new_handle(lua_State *L) { in mcplib_funcgen_new_handle() argument
625 mcp_funcgen_t *fgen = lua_touserdata(L, 1); in mcplib_funcgen_new_handle()
630 proxy_lua_error(L, "cannot modify function generator after calling ready"); in mcplib_funcgen_new_handle()
634 if ((pp = luaL_testudata(L, 2, "mcp.pool_proxy")) != NULL) { in mcplib_funcgen_new_handle()
636 } else if ((fg = luaL_testudata(L, 2, "mcp.funcgen")) != NULL) { in mcplib_funcgen_new_handle()
638 proxy_lua_error(L, "cannot assign a router to a handle in new_handle"); in mcplib_funcgen_new_handle()
642 proxy_lua_error(L, "cannot use a replaced function in new_handle"); in mcplib_funcgen_new_handle()
646 proxy_lua_error(L, "invalid argument to new_handle"); in mcplib_funcgen_new_handle()
657 proxy_lua_error(L, "failed to realloc queue list during new_handle()"); in mcplib_funcgen_new_handle()
666 rqu->obj_ref = luaL_ref(L, LUA_REGISTRYINDEX); in mcplib_funcgen_new_handle()
671 mcp_funcgen_reference(L); in mcplib_funcgen_new_handle()
676 lua_pushinteger(L, fgen->max_queues-1); in mcplib_funcgen_new_handle()
680 int mcplib_funcgen_ready(lua_State *L) { in mcplib_funcgen_ready() argument
681 mcp_funcgen_t *fgen = lua_touserdata(L, 1); in mcplib_funcgen_ready()
682 luaL_checktype(L, 2, LUA_TTABLE); in mcplib_funcgen_ready()
685 proxy_lua_error(L, "cannot modify function generator after calling ready"); in mcplib_funcgen_ready()
689 if (lua_getfield(L, 2, "f") != LUA_TFUNCTION) { in mcplib_funcgen_ready()
690 proxy_lua_error(L, "Must specify generator function ('f') to fgen:ready"); in mcplib_funcgen_ready()
693 fgen->generator_ref = luaL_ref(L, LUA_REGISTRYINDEX); in mcplib_funcgen_ready()
695 if (lua_getfield(L, 2, "a") != LUA_TNIL) { in mcplib_funcgen_ready()
696 fgen->argument_ref = luaL_ref(L, LUA_REGISTRYINDEX); in mcplib_funcgen_ready()
698 lua_pop(L, 1); in mcplib_funcgen_ready()
701 if (lua_getfield(L, 2, "n") == LUA_TSTRING) { in mcplib_funcgen_ready()
703 const char *name = lua_tolstring(L, -1, &len); in mcplib_funcgen_ready()
707 lua_pop(L, 1); in mcplib_funcgen_ready()
711 lua_pushvalue(L, 1); // copy the funcgen to pass into gencall in mcplib_funcgen_ready()
712 lua_rawgeti(L, LUA_REGISTRYINDEX, fgen->generator_ref); // for gencall in mcplib_funcgen_ready()
713 _mcplib_funcgen_gencall(L); in mcplib_funcgen_ready()
714 lua_pop(L, 1); // drop extra funcgen ref. in mcplib_funcgen_ready()
725 int mcplib_rcontext_handle_set_cb(lua_State *L) { in mcplib_rcontext_handle_set_cb() argument
726 mcp_rcontext_t *rctx = lua_touserdata(L, 1); in mcplib_rcontext_handle_set_cb()
727 luaL_checktype(L, 2, LUA_TNUMBER); in mcplib_rcontext_handle_set_cb()
728 luaL_checktype(L, 3, LUA_TFUNCTION); in mcplib_rcontext_handle_set_cb()
730 int handle = lua_tointeger(L, 2); in mcplib_rcontext_handle_set_cb()
732 proxy_lua_error(L, "invalid handle passed to queue_set_cb"); in mcplib_rcontext_handle_set_cb()
738 luaL_unref(L, LUA_REGISTRYINDEX, rqu->cb_ref); in mcplib_rcontext_handle_set_cb()
740 rqu->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX); in mcplib_rcontext_handle_set_cb()
751 static void _mcplib_rcontext_queue(lua_State *L, mcp_rcontext_t *rctx, mcp_request_t *rq, int handl… in _mcplib_rcontext_queue() argument
753 proxy_lua_error(L, "attempted to enqueue an invalid handle"); in _mcplib_rcontext_queue()
759 lua_pop(L, 1); in _mcplib_rcontext_queue()
768 lua_pushvalue(L, 2); // duplicate the request obj in _mcplib_rcontext_queue()
770 lua_xmove(L, subrctx->Lc, 1); // move the requet object. in _mcplib_rcontext_queue()
775 rqu->req_ref = luaL_ref(L, LUA_REGISTRYINDEX); in _mcplib_rcontext_queue()
784 int mcplib_rcontext_enqueue(lua_State *L) { in mcplib_rcontext_enqueue() argument
785 mcp_rcontext_t *rctx = lua_touserdata(L, 1); in mcplib_rcontext_enqueue()
786 mcp_request_t *rq = luaL_checkudata(L, 2, "mcp.request"); in mcplib_rcontext_enqueue()
789 proxy_lua_error(L, "enqueue: cannot enqueue new requests while in a wait"); in mcplib_rcontext_enqueue()
794 proxy_lua_error(L, "cannot queue requests without a key"); in mcplib_rcontext_enqueue()
798 int type = lua_type(L, 3); in mcplib_rcontext_enqueue()
800 int handle = lua_tointeger(L, 3); in mcplib_rcontext_enqueue()
802 lua_pushvalue(L, 2); in mcplib_rcontext_enqueue()
803 _mcplib_rcontext_queue(L, rctx, rq, handle); in mcplib_rcontext_enqueue()
805 unsigned int len = lua_rawlen(L, 3); in mcplib_rcontext_enqueue()
807 type = lua_rawgeti(L, 3, x+1); in mcplib_rcontext_enqueue()
809 proxy_lua_error(L, "invalid handle passed to queue via array table"); in mcplib_rcontext_enqueue()
813 int handle = lua_tointeger(L, 4); in mcplib_rcontext_enqueue()
814 lua_pop(L, 1); in mcplib_rcontext_enqueue()
816 lua_pushvalue(L, 2); in mcplib_rcontext_enqueue()
817 _mcplib_rcontext_queue(L, rctx, rq, handle); in mcplib_rcontext_enqueue()
820 proxy_lua_error(L, "must pass a handle or a table to queue"); in mcplib_rcontext_enqueue()
1162 static inline void _mcplib_set_rctx_alarm(lua_State *L, mcp_rcontext_t *rctx, int arg) { in _mcplib_set_rctx_alarm() argument
1164 lua_Number secondsf = lua_tonumberx(L, arg, &isnum); in _mcplib_set_rctx_alarm()
1166 proxy_lua_error(L, "timeout argument to wait or sleep must be a number"); in _mcplib_set_rctx_alarm()
1184 static inline int _mcplib_rcontext_wait_prep(lua_State *L, mcp_rcontext_t *rctx, int argc) { in _mcplib_rcontext_wait_prep() argument
1189 proxy_lua_error(L, "wait_cond: cannot call while already in wait mode"); in _mcplib_rcontext_wait_prep()
1194 proxy_lua_error(L, "must pass at least count to wait_cond"); in _mcplib_rcontext_wait_prep()
1199 wait = lua_tointegerx(L, 2, &isnum); in _mcplib_rcontext_wait_prep()
1201 proxy_lua_error(L, "wait count for wait_cond must be a positive integer"); in _mcplib_rcontext_wait_prep()
1206 mode = lua_tointeger(L, 3); in _mcplib_rcontext_wait_prep()
1216 proxy_lua_error(L, "invalid mode sent to wait_cond"); in _mcplib_rcontext_wait_prep()
1228 int mcplib_rcontext_wait_cond(lua_State *L) { in mcplib_rcontext_wait_cond() argument
1229 int argc = lua_gettop(L); in mcplib_rcontext_wait_cond()
1230 mcp_rcontext_t *rctx = lua_touserdata(L, 1); in mcplib_rcontext_wait_cond()
1232 _mcplib_rcontext_wait_prep(L, rctx, argc); in mcplib_rcontext_wait_cond()
1243 _mcplib_set_rctx_alarm(L, rctx, 4); in mcplib_rcontext_wait_cond()
1246 lua_pushinteger(L, MCP_YIELD_WAITCOND); in mcplib_rcontext_wait_cond()
1247 return lua_yield(L, 1); in mcplib_rcontext_wait_cond()
1250 int mcplib_rcontext_enqueue_and_wait(lua_State *L) { in mcplib_rcontext_enqueue_and_wait() argument
1251 mcp_rcontext_t *rctx = lua_touserdata(L, 1); in mcplib_rcontext_enqueue_and_wait()
1252 mcp_request_t *rq = luaL_checkudata(L, 2, "mcp.request"); in mcplib_rcontext_enqueue_and_wait()
1254 int handle = lua_tointegerx(L, 3, &isnum); in mcplib_rcontext_enqueue_and_wait()
1257 proxy_lua_error(L, "wait_cond: cannot call while already in wait mode"); in mcplib_rcontext_enqueue_and_wait()
1262 proxy_lua_error(L, "cannot queue requests without a key"); in mcplib_rcontext_enqueue_and_wait()
1267 proxy_lua_error(L, "invalid handle passed to enqueue_and_wait"); in mcplib_rcontext_enqueue_and_wait()
1272 lua_pushvalue(L, 2); in mcplib_rcontext_enqueue_and_wait()
1273 _mcplib_rcontext_queue(L, rctx, rq, handle); in mcplib_rcontext_enqueue_and_wait()
1275 if (lua_gettop(L) > 3) { in mcplib_rcontext_enqueue_and_wait()
1276 _mcplib_set_rctx_alarm(L, rctx, 4); in mcplib_rcontext_enqueue_and_wait()
1284 lua_pushinteger(L, MCP_YIELD_WAITHANDLE); in mcplib_rcontext_enqueue_and_wait()
1285 return lua_yield(L, 1); in mcplib_rcontext_enqueue_and_wait()
1288 int mcplib_rcontext_wait_handle(lua_State *L) { in mcplib_rcontext_wait_handle() argument
1289 mcp_rcontext_t *rctx = lua_touserdata(L, 1); in mcplib_rcontext_wait_handle()
1291 int handle = lua_tointegerx(L, 2, &isnum); in mcplib_rcontext_wait_handle()
1294 proxy_lua_error(L, "wait: cannot call while already in wait mode"); in mcplib_rcontext_wait_handle()
1299 proxy_lua_error(L, "invalid handle passed to wait_handle"); in mcplib_rcontext_wait_handle()
1305 proxy_lua_error(L, "wait_handle called on unqueued handle"); in mcplib_rcontext_wait_handle()
1309 if (lua_gettop(L) > 2) { in mcplib_rcontext_wait_handle()
1310 _mcplib_set_rctx_alarm(L, rctx, 3); in mcplib_rcontext_wait_handle()
1318 lua_pushinteger(L, MCP_YIELD_WAITHANDLE); in mcplib_rcontext_wait_handle()
1319 return lua_yield(L, 1); in mcplib_rcontext_wait_handle()
1348 int mcplib_rcontext_sleep(lua_State *L) { in mcplib_rcontext_sleep() argument
1349 mcp_rcontext_t *rctx = lua_touserdata(L, 1); in mcplib_rcontext_sleep()
1351 proxy_lua_error(L, "sleep: cannot call while already in wait mode"); in mcplib_rcontext_sleep()
1355 _mcplib_set_rctx_alarm(L, rctx, 2); in mcplib_rcontext_sleep()
1358 lua_pushinteger(L, MCP_YIELD_SLEEP); in mcplib_rcontext_sleep()
1359 return lua_yield(L, 1); in mcplib_rcontext_sleep()
1362 static inline struct mcp_rqueue_s *_mcplib_rcontext_checkhandle(lua_State *L) { in _mcplib_rcontext_checkhandle() argument
1363 mcp_rcontext_t *rctx = lua_touserdata(L, 1); in _mcplib_rcontext_checkhandle()
1365 int handle = lua_tointegerx(L, 2, &isnum); in _mcplib_rcontext_checkhandle()
1367 proxy_lua_error(L, "invalid queue handle passed to :good/:ok:/:any"); in _mcplib_rcontext_checkhandle()
1375 int mcplib_rcontext_res_good(lua_State *L) { in mcplib_rcontext_res_good() argument
1376 struct mcp_rqueue_s *rqu = _mcplib_rcontext_checkhandle(L); in mcplib_rcontext_res_good()
1378 lua_rawgeti(L, LUA_REGISTRYINDEX, rqu->res_ref); in mcplib_rcontext_res_good()
1380 lua_pushnil(L); in mcplib_rcontext_res_good()
1385 int mcplib_rcontext_res_ok(lua_State *L) { in mcplib_rcontext_res_ok() argument
1386 struct mcp_rqueue_s *rqu = _mcplib_rcontext_checkhandle(L); in mcplib_rcontext_res_ok()
1388 lua_rawgeti(L, LUA_REGISTRYINDEX, rqu->res_ref); in mcplib_rcontext_res_ok()
1390 lua_pushnil(L); in mcplib_rcontext_res_ok()
1395 int mcplib_rcontext_res_any(lua_State *L) { in mcplib_rcontext_res_any() argument
1396 struct mcp_rqueue_s *rqu = _mcplib_rcontext_checkhandle(L); in mcplib_rcontext_res_any()
1398 lua_rawgeti(L, LUA_REGISTRYINDEX, rqu->res_ref); in mcplib_rcontext_res_any()
1402 lua_pushnil(L); in mcplib_rcontext_res_any()
1408 int mcplib_rcontext_result(lua_State *L) { in mcplib_rcontext_result() argument
1409 struct mcp_rqueue_s *rqu = _mcplib_rcontext_checkhandle(L); in mcplib_rcontext_result()
1411 lua_rawgeti(L, LUA_REGISTRYINDEX, rqu->res_ref); in mcplib_rcontext_result()
1413 lua_pushinteger(L, rqu->flags & (RQUEUE_R_ANY|RQUEUE_R_OK|RQUEUE_R_GOOD)); in mcplib_rcontext_result()
1415 lua_pushnil(L); in mcplib_rcontext_result()
1416 lua_pushnil(L); in mcplib_rcontext_result()
1422 int mcplib_rcontext_cfd(lua_State *L) { in mcplib_rcontext_cfd() argument
1423 mcp_rcontext_t *rctx = lua_touserdata(L, 1); in mcplib_rcontext_cfd()
1424 lua_pushinteger(L, rctx->conn_fd); in mcplib_rcontext_cfd()
1429 int mcplib_rcontext_tls_peer_cn(lua_State *L) { in mcplib_rcontext_tls_peer_cn() argument
1430 mcp_rcontext_t *rctx = lua_touserdata(L, 1); in mcplib_rcontext_tls_peer_cn()
1432 lua_pushnil(L); in mcplib_rcontext_tls_peer_cn()
1440 lua_pushlstring(L, (const char *)cn, len); in mcplib_rcontext_tls_peer_cn()
1442 lua_pushnil(L); in mcplib_rcontext_tls_peer_cn()
1445 lua_pushnil(L); in mcplib_rcontext_tls_peer_cn()
1453 int mcplib_rcontext_request_new(lua_State *L) { in mcplib_rcontext_request_new() argument
1456 mcp_new_request(L, &pr, " ", 1); in mcplib_rcontext_request_new()
1461 int mcplib_rcontext_response_new(lua_State *L) { in mcplib_rcontext_response_new() argument
1462 mcp_resp_t *r = lua_newuserdatauv(L, sizeof(mcp_resp_t), 0); in mcplib_rcontext_response_new()
1464 luaL_getmetatable(L, "mcp.response"); in mcplib_rcontext_response_new()
1465 lua_setmetatable(L, -2); in mcplib_rcontext_response_new()
1470 void mcp_rcontext_push_rqu_res(lua_State *L, mcp_rcontext_t *rctx, int handle) { in mcp_rcontext_push_rqu_res() argument
1472 lua_rawgeti(L, LUA_REGISTRYINDEX, rqu->res_ref); in mcp_rcontext_push_rqu_res()
1566 static mcp_funcgen_t *mcp_funcgen_route(lua_State *L, mcp_funcgen_t *fgen, mcp_parser_t *pr) { in mcp_funcgen_route() argument
1602 lua_rawgeti(L, LUA_REGISTRYINDEX, fr->map_ref); in mcp_funcgen_route()
1603 lua_pushlstring(L, lookup, lookuplen); in mcp_funcgen_route()
1604 lua_rawget(L, -2); // pops key, returns value in mcp_funcgen_route()
1605 if (lua_isnil(L, -1)) { in mcp_funcgen_route()
1606 lua_pop(L, 2); // drop nil and map. in mcp_funcgen_route()
1609 int type = lua_type(L, -1); in mcp_funcgen_route()
1611 mcp_funcgen_t *nfgen = lua_touserdata(L, -1); in mcp_funcgen_route()
1612 lua_pop(L, 2); // drop fgen and map. in mcp_funcgen_route()
1615 lua_rawgeti(L, -1, pr->command); in mcp_funcgen_route()
1617 if (lua_isnil(L, -1)) { in mcp_funcgen_route()
1618 lua_pop(L, 1); // drop nil. in mcp_funcgen_route()
1620 lua_rawgeti(L, -1, CMD_ANY_STORAGE); in mcp_funcgen_route()
1621 if (lua_isnil(L, -1)) { in mcp_funcgen_route()
1622 lua_pop(L, 3); // drop map, cmd map, nil in mcp_funcgen_route()
1625 mcp_funcgen_t *nfgen = lua_touserdata(L, -1); in mcp_funcgen_route()
1626 lua_pop(L, 3); // drop map, cmd map, fgen in mcp_funcgen_route()
1630 mcp_funcgen_t *nfgen = lua_touserdata(L, -1); in mcp_funcgen_route()
1631 lua_pop(L, 3); // drop fgen, cmd map, map in mcp_funcgen_route()
1640 static int mcp_funcgen_router_cleanup(lua_State *L, mcp_funcgen_t *fgen) { in mcp_funcgen_router_cleanup() argument
1643 lua_rawgeti(L, LUA_REGISTRYINDEX, fr->map_ref); in mcp_funcgen_router_cleanup()
1646 int tidx = lua_absindex(L, -1); in mcp_funcgen_router_cleanup()
1647 lua_pushnil(L); in mcp_funcgen_router_cleanup()
1648 while (lua_next(L, tidx) != 0) { in mcp_funcgen_router_cleanup()
1649 int type = lua_type(L, -1); in mcp_funcgen_router_cleanup()
1651 mcp_funcgen_t *mfgen = lua_touserdata(L, -1); in mcp_funcgen_router_cleanup()
1652 mcp_funcgen_dereference(L, mfgen); in mcp_funcgen_router_cleanup()
1653 lua_pop(L, 1); in mcp_funcgen_router_cleanup()
1655 int midx = lua_absindex(L, -1); in mcp_funcgen_router_cleanup()
1656 lua_pushnil(L); in mcp_funcgen_router_cleanup()
1657 while (lua_next(L, midx) != 0) { in mcp_funcgen_router_cleanup()
1658 mcp_funcgen_t *mfgen = lua_touserdata(L, -1); in mcp_funcgen_router_cleanup()
1659 mcp_funcgen_dereference(L, mfgen); in mcp_funcgen_router_cleanup()
1660 lua_pop(L, 1); // drop value in mcp_funcgen_router_cleanup()
1662 lua_pop(L, 1); // drop command map table in mcp_funcgen_router_cleanup()
1666 lua_pop(L, 1); // drop the table. in mcp_funcgen_router_cleanup()
1667 luaL_unref(L, LUA_REGISTRYINDEX, fr->map_ref); in mcp_funcgen_router_cleanup()
1674 mcp_funcgen_dereference(L, fr->cmap[x]); in mcp_funcgen_router_cleanup()
1680 mcp_funcgen_dereference(L, fr->def_fgen); in mcp_funcgen_router_cleanup()
1690 static const char *_mcplib_router_new_check(lua_State *L, const char *arg, size_t *len) { in _mcplib_router_new_check() argument
1691 int type = lua_getfield(L, 1, arg); in _mcplib_router_new_check()
1693 const char *sep = lua_tolstring(L, -1, len); in _mcplib_router_new_check()
1695 proxy_lua_ferror(L, "must pass a non-zero length string to %s in mcp.router_new", arg); in _mcplib_router_new_check()
1697 proxy_lua_ferror(L, "%s is too long in mcp.router_new", arg); in _mcplib_router_new_check()
1699 lua_pop(L, 1); // drop key in _mcplib_router_new_check()
1702 proxy_lua_ferror(L, "must pass a string to %s in mcp.router_new", arg); in _mcplib_router_new_check()
1707 static void _mcplib_router_new_cmapcheck(lua_State *L) { in _mcplib_router_new_cmapcheck() argument
1708 int tidx = lua_absindex(L, -1); in _mcplib_router_new_cmapcheck()
1709 lua_pushnil(L); // init next table key. in _mcplib_router_new_cmapcheck()
1710 while (lua_next(L, tidx) != 0) { in _mcplib_router_new_cmapcheck()
1711 if (!lua_isinteger(L, -2)) { in _mcplib_router_new_cmapcheck()
1712 proxy_lua_error(L, "Non integer key in router command map in router_new"); in _mcplib_router_new_cmapcheck()
1714 int cmd = lua_tointeger(L, -2); in _mcplib_router_new_cmapcheck()
1716 proxy_lua_error(L, "Bad command in router command map in router_new"); in _mcplib_router_new_cmapcheck()
1718 luaL_checkudata(L, -1, "mcp.funcgen"); in _mcplib_router_new_cmapcheck()
1719 lua_pop(L, 1); // drop val, keep key. in _mcplib_router_new_cmapcheck()
1723 static size_t _mcplib_router_new_mapcheck(lua_State *L) { in _mcplib_router_new_mapcheck() argument
1725 if (!lua_istable(L, -1)) { in _mcplib_router_new_mapcheck()
1726 proxy_lua_error(L, "Must pass a table to map argument of router_new"); in _mcplib_router_new_mapcheck()
1729 lua_pushnil(L); // init table key. in _mcplib_router_new_mapcheck()
1730 while (lua_next(L, 2) != 0) { in _mcplib_router_new_mapcheck()
1731 int type = lua_type(L, -1); in _mcplib_router_new_mapcheck()
1733 luaL_checkudata(L, -1, "mcp.funcgen"); in _mcplib_router_new_mapcheck()
1736 _mcplib_router_new_cmapcheck(L); in _mcplib_router_new_mapcheck()
1738 proxy_lua_error(L, "unhandled data in router_new map"); in _mcplib_router_new_mapcheck()
1741 lua_pop(L, 1); // drop val, keep key. in _mcplib_router_new_mapcheck()
1748 static void _mcplib_router_new_mode(lua_State *L, struct mcp_funcgen_router *fr) { in _mcplib_router_new_mode() argument
1749 const char *type = lua_tostring(L, -1); in _mcplib_router_new_mode()
1755 sep = _mcplib_router_new_check(L, "stop", &len); in _mcplib_router_new_mode()
1772 const char *usep = _mcplib_router_new_check(L, "stop", &elen); in _mcplib_router_new_mode()
1773 sep = _mcplib_router_new_check(L, "start", &len); in _mcplib_router_new_mode()
1781 proxy_lua_error(L, "must specify start and stop if mode is anchor in mcp.router_new"); in _mcplib_router_new_mode()
1794 proxy_lua_error(L, "unknown type passed to mcp.router_new"); in _mcplib_router_new_mode()
1799 int mcplib_router_new(lua_State *L) { in mcplib_router_new() argument
1804 if (!lua_istable(L, 1)) { in mcplib_router_new()
1805 proxy_lua_error(L, "Must pass a table of arguments to mcp.router_new"); in mcplib_router_new()
1808 if (lua_getfield(L, 1, "map") != LUA_TNIL) { in mcplib_router_new()
1809 route_count = _mcplib_router_new_mapcheck(L); in mcplib_router_new()
1812 lua_pop(L, 1); // drop map or nil in mcplib_router_new()
1814 if (lua_getfield(L, 1, "cmap") != LUA_TNIL) { in mcplib_router_new()
1815 if (!lua_istable(L, -1)) { in mcplib_router_new()
1816 proxy_lua_error(L, "Must pass a table to cmap argument of mcp.router_new"); in mcplib_router_new()
1818 _mcplib_router_new_cmapcheck(L); in mcplib_router_new()
1821 proxy_lua_error(L, "Must pass map and/or cmap to mcp.router_new"); in mcplib_router_new()
1824 lua_pop(L, 1); in mcplib_router_new()
1836 if (lua_getfield(L, 1, "mode") == LUA_TSTRING) { in mcplib_router_new()
1837 _mcplib_router_new_mode(L, &fr); in mcplib_router_new()
1839 lua_pop(L, 1); // drop mode or nil. in mcplib_router_new()
1845 struct mcp_funcgen_router *router = lua_newuserdatauv(L, sizeof(struct mcp_funcgen_router), 0); in mcplib_router_new()
1849 luaL_getmetatable(L, "mcp.funcgen"); in mcplib_router_new()
1850 lua_setmetatable(L, -2); in mcplib_router_new()
1852 int type = lua_getfield(L, 1, "default"); in mcplib_router_new()
1854 fr.def_fgen = luaL_checkudata(L, -1, "mcp.funcgen"); in mcplib_router_new()
1855 mcp_funcgen_reference(L); // pops the funcgen. in mcplib_router_new()
1857 lua_pop(L, 1); in mcplib_router_new()
1865 lua_createtable(L, 0, route_count); in mcplib_router_new()
1866 lua_pushvalue(L, -1); // dupe table ref for a moment. in mcplib_router_new()
1867 router->map_ref = luaL_ref(L, LUA_REGISTRYINDEX); // pops extra map in mcplib_router_new()
1869 int mymap = lua_absindex(L, -1); in mcplib_router_new()
1870 lua_getfield(L, 1, "map"); in mcplib_router_new()
1871 int argmap = lua_absindex(L, -1); in mcplib_router_new()
1872 lua_pushnil(L); // seed walk of the passed in map in mcplib_router_new()
1874 while (lua_next(L, argmap) != 0) { in mcplib_router_new()
1876 int type = lua_type(L, -1); in mcplib_router_new()
1879 lua_pushvalue(L, -1); // duplicate value. in mcplib_router_new()
1880 mcp_funcgen_reference(L); // pops the funcgen after referencing. in mcplib_router_new()
1883 lua_pushvalue(L, -2); in mcplib_router_new()
1885 lua_insert(L, -2); // take top (key) and move it down one. in mcplib_router_new()
1887 lua_rawset(L, mymap); // pops k, v into our internal table. in mcplib_router_new()
1889 int tidx = lua_absindex(L, -1); // idx of our command map table. in mcplib_router_new()
1890 lua_createtable(L, CMD_END_STORAGE, 0); in mcplib_router_new()
1891 int midx = lua_absindex(L, -1); // idx of our new command map. in mcplib_router_new()
1892 lua_pushnil(L); // seed the iterator in mcplib_router_new()
1893 while (lua_next(L, tidx) != 0) { in mcplib_router_new()
1894 lua_pushvalue(L, -1); // duplicate value. in mcplib_router_new()
1895 mcp_funcgen_reference(L); // pop funcgen. in mcplib_router_new()
1897 lua_pushvalue(L, -2); // dupe key. in mcplib_router_new()
1898 lua_insert(L, -2); // move key down one. in mcplib_router_new()
1899 lua_rawset(L, midx); // set to new map table. in mcplib_router_new()
1905 lua_pushvalue(L, -3); // dupe key in mcplib_router_new()
1906 lua_insert(L, -2); // move key down below new cmd map in mcplib_router_new()
1907 lua_rawset(L, mymap); // pop key, new map into main map. in mcplib_router_new()
1908 lua_pop(L, 1); // drop input table. in mcplib_router_new()
1912 lua_pop(L, 2); // drop argmap, mymap. in mcplib_router_new()
1916 if (lua_getfield(L, 1, "cmap") != LUA_TNIL) { in mcplib_router_new()
1917 int tidx = lua_absindex(L, -1); // idx of our command map table. in mcplib_router_new()
1918 lua_pushnil(L); // seed the iterator in mcplib_router_new()
1919 while (lua_next(L, tidx) != 0) { in mcplib_router_new()
1920 int cmd = lua_tointeger(L, -2); in mcplib_router_new()
1921 mcp_funcgen_t *cfgen = lua_touserdata(L, -1); in mcplib_router_new()
1922 mcp_funcgen_reference(L); // pop funcgen. in mcplib_router_new()
1926 lua_pop(L, 1); in mcplib_router_new()
1928 LIBEVENT_THREAD *t = PROXY_GET_THR(L); in mcplib_router_new()