new type lua_KFunction + no more 'lua_getctx'

This commit is contained in:
Roberto Ierusalimschy 2014-06-10 14:41:38 -03:00
parent 35a6aad0d7
commit 6f6fd96e3b
6 changed files with 44 additions and 61 deletions

15
lapi.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lapi.c,v 2.213 2014/05/15 15:22:45 roberto Exp roberto $ ** $Id: lapi.c,v 2.214 2014/05/15 20:28:39 roberto Exp roberto $
** Lua API ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -900,17 +900,8 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) {
"results from function overflow current stack size") "results from function overflow current stack size")
LUA_API int lua_getctx (lua_State *L, int *ctx) {
if (L->ci->callstatus & CIST_YIELDED) {
if (ctx) *ctx = L->ci->u.c.ctx;
return L->ci->u.c.status;
}
else return LUA_OK;
}
LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx, LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
lua_CFunction k) { lua_KFunction k) {
StkId func; StkId func;
lua_lock(L); lua_lock(L);
api_check(L, k == NULL || !isLua(L->ci), api_check(L, k == NULL || !isLua(L->ci),
@ -949,7 +940,7 @@ static void f_call (lua_State *L, void *ud) {
LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
int ctx, lua_CFunction k) { int ctx, lua_KFunction k) {
struct CallS c; struct CallS c;
int status; int status;
ptrdiff_t func; ptrdiff_t func;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lbaselib.c,v 1.287 2014/05/16 18:54:01 roberto Exp roberto $ ** $Id: lbaselib.c,v 1.288 2014/06/02 03:06:26 roberto Exp roberto $
** Basic library ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -341,7 +341,8 @@ static int luaB_load (lua_State *L) {
/* }====================================================== */ /* }====================================================== */
static int dofilecont (lua_State *L) { static int dofilecont (lua_State *L, int d1, int d2) {
(void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */
return lua_gettop(L) - 1; return lua_gettop(L) - 1;
} }
@ -352,7 +353,7 @@ static int luaB_dofile (lua_State *L) {
if (luaL_loadfile(L, fname) != LUA_OK) if (luaL_loadfile(L, fname) != LUA_OK)
return lua_error(L); return lua_error(L);
lua_callk(L, 0, LUA_MULTRET, 0, dofilecont); lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
return dofilecont(L); return dofilecont(L, 0, 0);
} }
@ -385,13 +386,14 @@ static int luaB_select (lua_State *L) {
/* /*
** Finishes a 'pcall' or 'xpcall'. Both functions already pushed a ** Continuation function for 'pcall' and 'xpcall'. Both functions
** 'true' before doing the call, so in case of sucess 'finishpcall' ** already pushed a 'true' before doing the call, so in case of sucess
** only has to return everything in the stack minus 'extra' values ** 'finishpcall' only has to return everything in the stack minus
** (where 'extra' is exactly the number of items to be ignored). ** 'extra' values (where 'extra' is exactly the number of items to be
** ignored).
*/ */
static int finishpcall (lua_State *L, int ok, int extra) { static int finishpcall (lua_State *L, int status, int extra) {
if (!ok) { /* error? */ if (status != LUA_OK && status != LUA_YIELD) { /* error? */
lua_pushboolean(L, 0); /* first result (false) */ lua_pushboolean(L, 0); /* first result (false) */
lua_pushvalue(L, -2); /* error message */ lua_pushvalue(L, -2); /* error message */
return 2; /* return false, msg */ return 2; /* return false, msg */
@ -401,25 +403,13 @@ static int finishpcall (lua_State *L, int ok, int extra) {
} }
/*
** Continuation function for 'pcall' and 'xpcall': get appropriate
** state through 'lua_getctx' and call 'finishpcall' to finish the
** original function.
*/
static int pcallcont (lua_State *L) {
int extra;
int status = lua_getctx(L, &extra);
return finishpcall(L, (status == LUA_YIELD), extra);
}
static int luaB_pcall (lua_State *L) { static int luaB_pcall (lua_State *L) {
int status; int status;
luaL_checkany(L, 1); luaL_checkany(L, 1);
lua_pushboolean(L, 1); /* first result if no errors */ lua_pushboolean(L, 1); /* first result if no errors */
lua_insert(L, 1); /* put it in place */ lua_insert(L, 1); /* put it in place */
status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, pcallcont); status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, finishpcall);
return finishpcall(L, (status == LUA_OK), 0); return finishpcall(L, status, 0);
} }
@ -435,8 +425,8 @@ static int luaB_xpcall (lua_State *L) {
lua_pushboolean(L, 1); /* first result */ lua_pushboolean(L, 1); /* first result */
lua_pushvalue(L, 1); /* function */ lua_pushvalue(L, 1); /* function */
lua_rotate(L, 3, 2); /* move them below function's arguments */ lua_rotate(L, 3, 2); /* move them below function's arguments */
status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, pcallcont); status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, finishpcall);
return finishpcall(L, (status == LUA_OK), 2); return finishpcall(L, status, 2);
} }

8
ldo.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.c,v 2.116 2014/05/08 13:52:20 roberto Exp roberto $ ** $Id: ldo.c,v 2.117 2014/06/09 16:32:18 roberto Exp roberto $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -433,7 +433,7 @@ static void finishCcall (lua_State *L) {
lua_assert(ci->u.c.status != LUA_OK); lua_assert(ci->u.c.status != LUA_OK);
ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED; ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED;
lua_unlock(L); lua_unlock(L);
n = (*ci->u.c.k)(L); n = (*ci->u.c.k)(L, ci->u.c.status, ci->u.c.ctx);
lua_lock(L); lua_lock(L);
api_checknelems(L, n); api_checknelems(L, n);
/* finish 'luaD_precall' */ /* finish 'luaD_precall' */
@ -539,7 +539,7 @@ static void resume (lua_State *L, void *ud) {
ci->u.c.status = LUA_YIELD; /* 'default' status */ ci->u.c.status = LUA_YIELD; /* 'default' status */
ci->callstatus |= CIST_YIELDED; ci->callstatus |= CIST_YIELDED;
lua_unlock(L); lua_unlock(L);
n = (*ci->u.c.k)(L); /* call continuation */ n = (*ci->u.c.k)(L, ci->u.c.status, ci->u.c.ctx); /* call continuation */
lua_lock(L); lua_lock(L);
api_checknelems(L, n); api_checknelems(L, n);
firstArg = L->top - n; /* yield results come from continuation */ firstArg = L->top - n; /* yield results come from continuation */
@ -589,7 +589,7 @@ LUA_API int lua_isyieldable (lua_State *L) {
} }
LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) { LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_KFunction k) {
CallInfo *ci = L->ci; CallInfo *ci = L->ci;
luai_userstateyield(L, nresults); luai_userstateyield(L, nresults);
lua_lock(L); lua_lock(L);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstate.h,v 2.102 2014/02/18 13:46:26 roberto Exp roberto $ ** $Id: lstate.h,v 2.103 2014/05/15 20:41:27 roberto Exp roberto $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -69,7 +69,7 @@ typedef struct CallInfo {
const Instruction *savedpc; const Instruction *savedpc;
} l; } l;
struct { /* only for C functions */ struct { /* only for C functions */
lua_CFunction k; /* continuation in case of yields */ lua_KFunction k; /* continuation in case of yields */
ptrdiff_t old_errfunc; ptrdiff_t old_errfunc;
int ctx; /* context info. in case of yields */ int ctx; /* context info. in case of yields */
lu_byte old_allowhook; lu_byte old_allowhook;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltests.c,v 2.169 2014/05/08 19:08:46 roberto Exp roberto $ ** $Id: ltests.c,v 2.170 2014/05/13 19:40:28 roberto Exp roberto $
** Internal Module for Debugging of the Lua Implementation ** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -980,7 +980,7 @@ static void pushcode (lua_State *L, int code) {
static int testC (lua_State *L); static int testC (lua_State *L);
static int Cfunck (lua_State *L); static int Cfunck (lua_State *L, int status, int ctx);
/* /*
** arithmetic operation encoding for 'arith' instruction ** arithmetic operation encoding for 'arith' instruction
@ -1044,12 +1044,6 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
lua_CFunction func = lua_tocfunction(L1, getindex); lua_CFunction func = lua_tocfunction(L1, getindex);
lua_pushnumber(L1, cast(size_t, func)); lua_pushnumber(L1, cast(size_t, func));
} }
else if EQ("getctx") {
int i = 0;
int s = lua_getctx(L1, &i);
pushcode(L1, s);
lua_pushinteger(L1, i);
}
else if EQ("getfield") { else if EQ("getfield") {
int t = getindex; int t = getindex;
lua_getfield(L1, t, getstring); lua_getfield(L1, t, getstring);
@ -1326,10 +1320,12 @@ static int Cfunc (lua_State *L) {
} }
static int Cfunck (lua_State *L) { static int Cfunck (lua_State *L, int status, int ctx) {
int i = 0; pushcode(L, status);
lua_getctx(L, &i); lua_setglobal(L, "status");
return runC(L, L, lua_tostring(L, i)); lua_pushinteger(L, ctx);
lua_setglobal(L, "ctx");
return runC(L, L, lua_tostring(L, ctx));
} }

18
lua.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lua.h,v 1.305 2014/05/08 13:52:20 roberto Exp roberto $ ** $Id: lua.h,v 1.306 2014/05/13 19:40:28 roberto Exp roberto $
** Lua - A Scripting Language ** Lua - A Scripting Language
** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
** See Copyright Notice at the end of this file ** See Copyright Notice at the end of this file
@ -53,8 +53,16 @@
typedef struct lua_State lua_State; typedef struct lua_State lua_State;
/*
** Type for C functions registered with Lua
*/
typedef int (*lua_CFunction) (lua_State *L); typedef int (*lua_CFunction) (lua_State *L);
/*
** Type for continuation functions
*/
typedef int (*lua_KFunction) (lua_State *L, int status, int ctx);
/* /*
** functions that read/write blocks when loading/dumping Lua chunks ** functions that read/write blocks when loading/dumping Lua chunks
@ -257,13 +265,11 @@ LUA_API void (lua_setuservalue) (lua_State *L, int idx);
** 'load' and 'call' functions (load and run Lua code) ** 'load' and 'call' functions (load and run Lua code)
*/ */
LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, int ctx, LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
lua_CFunction k); lua_KFunction k);
#define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL) #define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL)
LUA_API int (lua_getctx) (lua_State *L, int *ctx);
LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
int ctx, lua_CFunction k); int ctx, lua_KFunction k);
#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL) #define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL)
LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
@ -277,7 +283,7 @@ LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip);
** coroutine functions ** coroutine functions
*/ */
LUA_API int (lua_yieldk) (lua_State *L, int nresults, int ctx, LUA_API int (lua_yieldk) (lua_State *L, int nresults, int ctx,
lua_CFunction k); lua_KFunction k);
#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) #define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL)
LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg); LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg);
LUA_API int (lua_status) (lua_State *L); LUA_API int (lua_status) (lua_State *L);