aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-06-10 14:41:38 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-06-10 14:41:38 -0300
commit6f6fd96e3bd2cc1f61291717aee9d89ea0180cd4 (patch)
tree77247f6bb46460afa33749cedfc841f22089f785
parent35a6aad0d745d3acb9d5feb895f84a05ae8bf5ba (diff)
downloadlua-6f6fd96e3bd2cc1f61291717aee9d89ea0180cd4.tar.gz
lua-6f6fd96e3bd2cc1f61291717aee9d89ea0180cd4.tar.bz2
lua-6f6fd96e3bd2cc1f61291717aee9d89ea0180cd4.zip
new type lua_KFunction + no more 'lua_getctx'
-rw-r--r--lapi.c15
-rw-r--r--lbaselib.c40
-rw-r--r--ldo.c8
-rw-r--r--lstate.h4
-rw-r--r--ltests.c20
-rw-r--r--lua.h18
6 files changed, 44 insertions, 61 deletions
diff --git a/lapi.c b/lapi.c
index 7010ec70..2b874530 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 2.213 2014/05/15 15:22:45 roberto Exp roberto $ 2** $Id: lapi.c,v 2.214 2014/05/15 20:28:39 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -900,17 +900,8 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) {
900 "results from function overflow current stack size") 900 "results from function overflow current stack size")
901 901
902 902
903LUA_API int lua_getctx (lua_State *L, int *ctx) {
904 if (L->ci->callstatus & CIST_YIELDED) {
905 if (ctx) *ctx = L->ci->u.c.ctx;
906 return L->ci->u.c.status;
907 }
908 else return LUA_OK;
909}
910
911
912LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx, 903LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
913 lua_CFunction k) { 904 lua_KFunction k) {
914 StkId func; 905 StkId func;
915 lua_lock(L); 906 lua_lock(L);
916 api_check(L, k == NULL || !isLua(L->ci), 907 api_check(L, k == NULL || !isLua(L->ci),
@@ -949,7 +940,7 @@ static void f_call (lua_State *L, void *ud) {
949 940
950 941
951LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, 942LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
952 int ctx, lua_CFunction k) { 943 int ctx, lua_KFunction k) {
953 struct CallS c; 944 struct CallS c;
954 int status; 945 int status;
955 ptrdiff_t func; 946 ptrdiff_t func;
diff --git a/lbaselib.c b/lbaselib.c
index 40e56b82..8d635aa3 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.287 2014/05/16 18:54:01 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.288 2014/06/02 03:06:26 roberto Exp roberto $
3** Basic library 3** Basic library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -341,7 +341,8 @@ static int luaB_load (lua_State *L) {
341/* }====================================================== */ 341/* }====================================================== */
342 342
343 343
344static int dofilecont (lua_State *L) { 344static int dofilecont (lua_State *L, int d1, int d2) {
345 (void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */
345 return lua_gettop(L) - 1; 346 return lua_gettop(L) - 1;
346} 347}
347 348
@@ -352,7 +353,7 @@ static int luaB_dofile (lua_State *L) {
352 if (luaL_loadfile(L, fname) != LUA_OK) 353 if (luaL_loadfile(L, fname) != LUA_OK)
353 return lua_error(L); 354 return lua_error(L);
354 lua_callk(L, 0, LUA_MULTRET, 0, dofilecont); 355 lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
355 return dofilecont(L); 356 return dofilecont(L, 0, 0);
356} 357}
357 358
358 359
@@ -385,13 +386,14 @@ static int luaB_select (lua_State *L) {
385 386
386 387
387/* 388/*
388** Finishes a 'pcall' or 'xpcall'. Both functions already pushed a 389** Continuation function for 'pcall' and 'xpcall'. Both functions
389** 'true' before doing the call, so in case of sucess 'finishpcall' 390** already pushed a 'true' before doing the call, so in case of sucess
390** only has to return everything in the stack minus 'extra' values 391** 'finishpcall' only has to return everything in the stack minus
391** (where 'extra' is exactly the number of items to be ignored). 392** 'extra' values (where 'extra' is exactly the number of items to be
393** ignored).
392*/ 394*/
393static int finishpcall (lua_State *L, int ok, int extra) { 395static int finishpcall (lua_State *L, int status, int extra) {
394 if (!ok) { /* error? */ 396 if (status != LUA_OK && status != LUA_YIELD) { /* error? */
395 lua_pushboolean(L, 0); /* first result (false) */ 397 lua_pushboolean(L, 0); /* first result (false) */
396 lua_pushvalue(L, -2); /* error message */ 398 lua_pushvalue(L, -2); /* error message */
397 return 2; /* return false, msg */ 399 return 2; /* return false, msg */
@@ -401,25 +403,13 @@ static int finishpcall (lua_State *L, int ok, int extra) {
401} 403}
402 404
403 405
404/*
405** Continuation function for 'pcall' and 'xpcall': get appropriate
406** state through 'lua_getctx' and call 'finishpcall' to finish the
407** original function.
408*/
409static int pcallcont (lua_State *L) {
410 int extra;
411 int status = lua_getctx(L, &extra);
412 return finishpcall(L, (status == LUA_YIELD), extra);
413}
414
415
416static int luaB_pcall (lua_State *L) { 406static int luaB_pcall (lua_State *L) {
417 int status; 407 int status;
418 luaL_checkany(L, 1); 408 luaL_checkany(L, 1);
419 lua_pushboolean(L, 1); /* first result if no errors */ 409 lua_pushboolean(L, 1); /* first result if no errors */
420 lua_insert(L, 1); /* put it in place */ 410 lua_insert(L, 1); /* put it in place */
421 status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, pcallcont); 411 status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, finishpcall);
422 return finishpcall(L, (status == LUA_OK), 0); 412 return finishpcall(L, status, 0);
423} 413}
424 414
425 415
@@ -435,8 +425,8 @@ static int luaB_xpcall (lua_State *L) {
435 lua_pushboolean(L, 1); /* first result */ 425 lua_pushboolean(L, 1); /* first result */
436 lua_pushvalue(L, 1); /* function */ 426 lua_pushvalue(L, 1); /* function */
437 lua_rotate(L, 3, 2); /* move them below function's arguments */ 427 lua_rotate(L, 3, 2); /* move them below function's arguments */
438 status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, pcallcont); 428 status = lua_pcallk(L, n - 2, LUA_MULTRET, 2, 2, finishpcall);
439 return finishpcall(L, (status == LUA_OK), 2); 429 return finishpcall(L, status, 2);
440} 430}
441 431
442 432
diff --git a/ldo.c b/ldo.c
index c6cf8c8b..b41855c9 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 2.116 2014/05/08 13:52:20 roberto Exp roberto $ 2** $Id: ldo.c,v 2.117 2014/06/09 16:32:18 roberto Exp roberto $
3** Stack and Call structure of Lua 3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -433,7 +433,7 @@ static void finishCcall (lua_State *L) {
433 lua_assert(ci->u.c.status != LUA_OK); 433 lua_assert(ci->u.c.status != LUA_OK);
434 ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED; 434 ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED;
435 lua_unlock(L); 435 lua_unlock(L);
436 n = (*ci->u.c.k)(L); 436 n = (*ci->u.c.k)(L, ci->u.c.status, ci->u.c.ctx);
437 lua_lock(L); 437 lua_lock(L);
438 api_checknelems(L, n); 438 api_checknelems(L, n);
439 /* finish 'luaD_precall' */ 439 /* finish 'luaD_precall' */
@@ -539,7 +539,7 @@ static void resume (lua_State *L, void *ud) {
539 ci->u.c.status = LUA_YIELD; /* 'default' status */ 539 ci->u.c.status = LUA_YIELD; /* 'default' status */
540 ci->callstatus |= CIST_YIELDED; 540 ci->callstatus |= CIST_YIELDED;
541 lua_unlock(L); 541 lua_unlock(L);
542 n = (*ci->u.c.k)(L); /* call continuation */ 542 n = (*ci->u.c.k)(L, ci->u.c.status, ci->u.c.ctx); /* call continuation */
543 lua_lock(L); 543 lua_lock(L);
544 api_checknelems(L, n); 544 api_checknelems(L, n);
545 firstArg = L->top - n; /* yield results come from continuation */ 545 firstArg = L->top - n; /* yield results come from continuation */
@@ -589,7 +589,7 @@ LUA_API int lua_isyieldable (lua_State *L) {
589} 589}
590 590
591 591
592LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) { 592LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_KFunction k) {
593 CallInfo *ci = L->ci; 593 CallInfo *ci = L->ci;
594 luai_userstateyield(L, nresults); 594 luai_userstateyield(L, nresults);
595 lua_lock(L); 595 lua_lock(L);
diff --git a/lstate.h b/lstate.h
index fd21fb9f..67b43c29 100644
--- a/lstate.h
+++ b/lstate.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.h,v 2.102 2014/02/18 13:46:26 roberto Exp roberto $ 2** $Id: lstate.h,v 2.103 2014/05/15 20:41:27 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -69,7 +69,7 @@ typedef struct CallInfo {
69 const Instruction *savedpc; 69 const Instruction *savedpc;
70 } l; 70 } l;
71 struct { /* only for C functions */ 71 struct { /* only for C functions */
72 lua_CFunction k; /* continuation in case of yields */ 72 lua_KFunction k; /* continuation in case of yields */
73 ptrdiff_t old_errfunc; 73 ptrdiff_t old_errfunc;
74 int ctx; /* context info. in case of yields */ 74 int ctx; /* context info. in case of yields */
75 lu_byte old_allowhook; 75 lu_byte old_allowhook;
diff --git a/ltests.c b/ltests.c
index cc1f72e5..1ff5c5f0 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltests.c,v 2.169 2014/05/08 19:08:46 roberto Exp roberto $ 2** $Id: ltests.c,v 2.170 2014/05/13 19:40:28 roberto Exp roberto $
3** Internal Module for Debugging of the Lua Implementation 3** Internal Module for Debugging of the Lua Implementation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -980,7 +980,7 @@ static void pushcode (lua_State *L, int code) {
980 980
981 981
982static int testC (lua_State *L); 982static int testC (lua_State *L);
983static int Cfunck (lua_State *L); 983static int Cfunck (lua_State *L, int status, int ctx);
984 984
985/* 985/*
986** arithmetic operation encoding for 'arith' instruction 986** arithmetic operation encoding for 'arith' instruction
@@ -1044,12 +1044,6 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
1044 lua_CFunction func = lua_tocfunction(L1, getindex); 1044 lua_CFunction func = lua_tocfunction(L1, getindex);
1045 lua_pushnumber(L1, cast(size_t, func)); 1045 lua_pushnumber(L1, cast(size_t, func));
1046 } 1046 }
1047 else if EQ("getctx") {
1048 int i = 0;
1049 int s = lua_getctx(L1, &i);
1050 pushcode(L1, s);
1051 lua_pushinteger(L1, i);
1052 }
1053 else if EQ("getfield") { 1047 else if EQ("getfield") {
1054 int t = getindex; 1048 int t = getindex;
1055 lua_getfield(L1, t, getstring); 1049 lua_getfield(L1, t, getstring);
@@ -1326,10 +1320,12 @@ static int Cfunc (lua_State *L) {
1326} 1320}
1327 1321
1328 1322
1329static int Cfunck (lua_State *L) { 1323static int Cfunck (lua_State *L, int status, int ctx) {
1330 int i = 0; 1324 pushcode(L, status);
1331 lua_getctx(L, &i); 1325 lua_setglobal(L, "status");
1332 return runC(L, L, lua_tostring(L, i)); 1326 lua_pushinteger(L, ctx);
1327 lua_setglobal(L, "ctx");
1328 return runC(L, L, lua_tostring(L, ctx));
1333} 1329}
1334 1330
1335 1331
diff --git a/lua.h b/lua.h
index aa187338..5beb5411 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.305 2014/05/08 13:52:20 roberto Exp roberto $ 2** $Id: lua.h,v 1.306 2014/05/13 19:40:28 roberto Exp roberto $
3** Lua - A Scripting Language 3** Lua - A Scripting Language
4** Lua.org, PUC-Rio, Brazil (http://www.lua.org) 4** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
5** See Copyright Notice at the end of this file 5** See Copyright Notice at the end of this file
@@ -53,8 +53,16 @@
53 53
54typedef struct lua_State lua_State; 54typedef struct lua_State lua_State;
55 55
56/*
57** Type for C functions registered with Lua
58*/
56typedef int (*lua_CFunction) (lua_State *L); 59typedef int (*lua_CFunction) (lua_State *L);
57 60
61/*
62** Type for continuation functions
63*/
64typedef int (*lua_KFunction) (lua_State *L, int status, int ctx);
65
58 66
59/* 67/*
60** functions that read/write blocks when loading/dumping Lua chunks 68** functions that read/write blocks when loading/dumping Lua chunks
@@ -257,13 +265,11 @@ LUA_API void (lua_setuservalue) (lua_State *L, int idx);
257** 'load' and 'call' functions (load and run Lua code) 265** 'load' and 'call' functions (load and run Lua code)
258*/ 266*/
259LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, int ctx, 267LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
260 lua_CFunction k); 268 lua_KFunction k);
261#define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL) 269#define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL)
262 270
263LUA_API int (lua_getctx) (lua_State *L, int *ctx);
264
265LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, 271LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
266 int ctx, lua_CFunction k); 272 int ctx, lua_KFunction k);
267#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL) 273#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL)
268 274
269LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, 275LUA_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);
277** coroutine functions 283** coroutine functions
278*/ 284*/
279LUA_API int (lua_yieldk) (lua_State *L, int nresults, int ctx, 285LUA_API int (lua_yieldk) (lua_State *L, int nresults, int ctx,
280 lua_CFunction k); 286 lua_KFunction k);
281#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) 287#define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL)
282LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg); 288LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg);
283LUA_API int (lua_status) (lua_State *L); 289LUA_API int (lua_status) (lua_State *L);