Browse Source

new type lua_KFunction + no more 'lua_getctx'

pull/9/head
Roberto Ierusalimschy 11 years ago
parent
commit
6f6fd96e3b
  1. 15
      lapi.c
  2. 40
      lbaselib.c
  3. 8
      ldo.c
  4. 4
      lstate.h
  5. 20
      ltests.c
  6. 18
      lua.h

15
lapi.c

@ -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
** 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")
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_CFunction k) {
lua_KFunction k) {
StkId func;
lua_lock(L);
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,
int ctx, lua_CFunction k) {
int ctx, lua_KFunction k) {
struct CallS c;
int status;
ptrdiff_t func;

40
lbaselib.c

@ -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
** 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;
}
@ -352,7 +353,7 @@ static int luaB_dofile (lua_State *L) {
if (luaL_loadfile(L, fname) != LUA_OK)
return lua_error(L);
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
** 'true' before doing the call, so in case of sucess 'finishpcall'
** only has to return everything in the stack minus 'extra' values
** (where 'extra' is exactly the number of items to be ignored).
** Continuation function for 'pcall' and 'xpcall'. Both functions
** already pushed a 'true' before doing the call, so in case of sucess
** 'finishpcall' only has to return everything in the stack minus
** 'extra' values (where 'extra' is exactly the number of items to be
** ignored).
*/
static int finishpcall (lua_State *L, int ok, int extra) {
if (!ok) { /* error? */
static int finishpcall (lua_State *L, int status, int extra) {
if (status != LUA_OK && status != LUA_YIELD) { /* error? */
lua_pushboolean(L, 0); /* first result (false) */
lua_pushvalue(L, -2); /* error message */
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) {
int status;
luaL_checkany(L, 1);
lua_pushboolean(L, 1); /* first result if no errors */
lua_insert(L, 1); /* put it in place */
status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, pcallcont);
return finishpcall(L, (status == LUA_OK), 0);
status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, finishpcall);
return finishpcall(L, status, 0);
}
@ -435,8 +425,8 @@ static int luaB_xpcall (lua_State *L) {
lua_pushboolean(L, 1); /* first result */
lua_pushvalue(L, 1); /* function */
lua_rotate(L, 3, 2); /* move them below function's arguments */
status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, pcallcont);
return finishpcall(L, (status == LUA_OK), 2);
status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, finishpcall);
return finishpcall(L, status, 2);
}

8
ldo.c

@ -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
** See Copyright Notice in lua.h
*/
@ -433,7 +433,7 @@ static void finishCcall (lua_State *L) {
lua_assert(ci->u.c.status != LUA_OK);
ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED;
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);
api_checknelems(L, n);
/* finish 'luaD_precall' */
@ -539,7 +539,7 @@ static void resume (lua_State *L, void *ud) {
ci->u.c.status = LUA_YIELD; /* 'default' status */
ci->callstatus |= CIST_YIELDED;
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);
api_checknelems(L, n);
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;
luai_userstateyield(L, nresults);
lua_lock(L);

4
lstate.h

@ -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
** See Copyright Notice in lua.h
*/
@ -69,7 +69,7 @@ typedef struct CallInfo {
const Instruction *savedpc;
} l;
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;
int ctx; /* context info. in case of yields */
lu_byte old_allowhook;

20
ltests.c

@ -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
** 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 Cfunck (lua_State *L);
static int Cfunck (lua_State *L, int status, int ctx);
/*
** 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_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") {
int t = getindex;
lua_getfield(L1, t, getstring);
@ -1326,10 +1320,12 @@ static int Cfunc (lua_State *L) {
}
static int Cfunck (lua_State *L) {
int i = 0;
lua_getctx(L, &i);
return runC(L, L, lua_tostring(L, i));
static int Cfunck (lua_State *L, int status, int ctx) {
pushcode(L, status);
lua_setglobal(L, "status");
lua_pushinteger(L, ctx);
lua_setglobal(L, "ctx");
return runC(L, L, lua_tostring(L, ctx));
}

18
lua.h

@ -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.org, PUC-Rio, Brazil (http://www.lua.org)
** See Copyright Notice at the end of this file
@ -53,8 +53,16 @@
typedef struct lua_State lua_State;
/*
** Type for C functions registered with Lua
*/
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
@ -257,13 +265,11 @@ LUA_API void (lua_setuservalue) (lua_State *L, int idx);
** 'load' and 'call' functions (load and run Lua code)
*/
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)
LUA_API int (lua_getctx) (lua_State *L, int *ctx);
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)
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
*/
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)
LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg);
LUA_API int (lua_status) (lua_State *L);

Loading…
Cancel
Save