aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-09-11 14:24:14 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-09-11 14:24:14 -0300
commitb114c7d4871051cbdd7af185a61f35fe4028da79 (patch)
tree6ee42a4b6a4af27789329c8a65c75bba508e8258
parent9cbf17b0f1bb4001b237c4027b271f0db9bde62c (diff)
downloadlua-b114c7d4871051cbdd7af185a61f35fe4028da79.tar.gz
lua-b114c7d4871051cbdd7af185a61f35fe4028da79.tar.bz2
lua-b114c7d4871051cbdd7af185a61f35fe4028da79.zip
Added "cost" for the use of C stack by a coroutine invocation.
Resuming a coroutine uses more C stack than other operations (such as function calls or recursive syntax). So, to avoid stack overflow in recursive coroutine invocations, either LUAI_MAXCCALLS must be too small or a coroutine invocation must "pay" a higher price. New constant LUAL_COROCSTK ("COROutine C STaK") defines how much is this price.
-rw-r--r--ldo.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/ldo.c b/ldo.c
index ebb35c97..0d68d36c 100644
--- a/ldo.c
+++ b/ldo.c
@@ -611,6 +611,13 @@ static int resume_error (lua_State *L, const char *msg, int narg) {
611 611
612 612
613/* 613/*
614** "Cost" in the C stack for a coroutine invocation.
615*/
616#if !defined(LUAL_COROCSTK)
617#define LUAL_COROCSTK 3
618#endif
619
620/*
614** Do the work for 'lua_resume' in protected mode. Most of the work 621** Do the work for 'lua_resume' in protected mode. Most of the work
615** depends on the status of the coroutine: initial state, suspended 622** depends on the status of the coroutine: initial state, suspended
616** inside a hook, or regularly suspended (optionally with a continuation 623** inside a hook, or regularly suspended (optionally with a continuation
@@ -642,7 +649,6 @@ static void resume (lua_State *L, void *ud) {
642 } 649 }
643} 650}
644 651
645
646LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, 652LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
647 int *nresults) { 653 int *nresults) {
648 int status; 654 int status;
@@ -657,7 +663,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
657 if (from == NULL) 663 if (from == NULL)
658 L->nCcalls = 1; 664 L->nCcalls = 1;
659 else /* correct 'nCcalls' for this thread */ 665 else /* correct 'nCcalls' for this thread */
660 L->nCcalls = from->nCcalls - from->nci + L->nci + 1; 666 L->nCcalls = from->nCcalls - from->nci + L->nci + LUAL_COROCSTK;
661 if (L->nCcalls >= LUAI_MAXCCALLS) 667 if (L->nCcalls >= LUAI_MAXCCALLS)
662 return resume_error(L, "C stack overflow", nargs); 668 return resume_error(L, "C stack overflow", nargs);
663 luai_userstateresume(L, nargs); 669 luai_userstateresume(L, nargs);