Skip to content

Commit

Permalink
ruleset: add aux function for async request processing
Browse files Browse the repository at this point in the history
Signed-off-by: He Xian <[email protected]>
  • Loading branch information
hexian000 committed Jan 25, 2025
1 parent cdd0091 commit 5e4fe2c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
13 changes: 12 additions & 1 deletion src/ruleset/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,23 @@ int aux_resume(lua_State *restrict L, lua_State *restrict from, const int narg)
int status, nres;
#if LUA_VERSION_NUM >= 504
status = lua_resume(L, from, narg, &nres);
#elif LUA_VERSION_NUM == 503
#else
status = lua_resume(L, from, narg);
#endif
return status;
}

int aux_async(
lua_State *restrict L, lua_State *restrict from, const int narg,
const int finishidx)
{
lua_pushvalue(from, finishidx);
lua_xmove(from, L, 1);
lua_pushnil(L);
lua_xmove(from, L, 1 + narg);
return aux_resume(L, from, 3 + narg);
}

static bool ruleset_pcallv(
lua_State *restrict L, const lua_CFunction func, const int nargs,
const int nresults, va_list args)
Expand Down
5 changes: 4 additions & 1 deletion src/ruleset/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ bool aux_todialreq(lua_State *L, int n);

int aux_traceback(lua_State *L);

/* co stack: finish ? func ...(narg) */
/* [-narg, +0, -] */
int aux_resume(lua_State *L, lua_State *from, int narg);

/* [-(narg+1), +(0|1), -] */
int aux_async(lua_State *L, lua_State *from, int narg, int finishidx);

/* main routine */
bool ruleset_pcall(
struct ruleset *r, lua_CFunction func, int nargs, int nresults, ...);
Expand Down
13 changes: 4 additions & 9 deletions src/ruleset/cfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,14 @@ int cfunc_request(lua_State *restrict L)
lua_pushcclosure(L, request_finish, 1);
(void)lua_getglobal(L, "ruleset");
(void)lua_getfield(L, -1, func);
lua_pushnil(L);
lua_replace(L, -3);
lua_pushstring(L, request);
lua_pushstring(L, username);
lua_pushstring(L, password);
lua_xmove(L, co, 6);
/* lua stack: state co; co stack: finish nil func request username password */
/* lua stack: state co finish ruleset func request username password */

state->request = *in_cb;
*pstate = state;
const int status = aux_resume(co, L, 6);
const int status = aux_async(co, L, 3, -6);
if (status != LUA_OK && status != LUA_YIELD) {
lua_xmove(co, L, 1);
return lua_error(L);
Expand Down Expand Up @@ -218,7 +215,6 @@ int cfunc_rpcall(lua_State *restrict L)
lua_State *restrict co = aux_getthread(L);
lua_pushvalue(L, 1);
lua_pushcclosure(L, rpcall_finish, 1);
lua_pushnil(L);
if (lua_load(L, aux_reader, stream, "=(rpc)", "t")) {
return lua_error(L);
}
Expand All @@ -231,12 +227,11 @@ int cfunc_rpcall(lua_State *restrict L)
const char *upvalue = lua_setupvalue(L, -2, 1);
ASSERT(upvalue != NULL && strcmp(upvalue, "_ENV") == 0);
UNUSED(upvalue);
lua_xmove(L, co, 3);
/* lua stack: state co; co stack: finish nil chunk */
/* lua stack: state co finish chunk */

state->rpcall = *in_cb;
*pstate = state;
const int status = aux_resume(co, L, 3);
const int status = aux_async(co, L, 0, -2);
if (status != LUA_OK && status != LUA_YIELD) {
lua_xmove(co, L, 1);
return lua_error(L);
Expand Down

0 comments on commit 5e4fe2c

Please sign in to comment.