From 34840301b529686ce8168828b140a478a5d44b53 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 25 Oct 2018 15:30:15 -0300 Subject: To-be-closed variables in the C API --- ldo.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'ldo.c') diff --git a/ldo.c b/ldo.c index 78e4c5c3..b7a76ef6 100644 --- a/ldo.c +++ b/ldo.c @@ -366,32 +366,38 @@ void luaD_tryfuncTM (lua_State *L, StkId func) { ** separated. */ static void moveresults (lua_State *L, StkId res, int nres, int wanted) { + StkId firstresult; + int i; switch (wanted) { /* handle typical cases separately */ case 0: /* no values needed */ L->top = res; - break; + return; case 1: /* one value needed */ if (nres == 0) /* no results? */ setnilvalue(s2v(res)); /* adjust with nil */ else setobjs2s(L, res, L->top - nres); /* move it to proper place */ L->top = res + 1; - break; + return; case LUA_MULTRET: wanted = nres; /* we want all results */ - /* FALLTHROUGH */ - default: { /* multiple results */ - StkId firstresult = L->top - nres; /* index of first result */ - int i; - /* move all results to correct place */ - for (i = 0; i < nres && i < wanted; i++) - setobjs2s(L, res + i, firstresult + i); - for (; i < wanted; i++) /* complete wanted number of results */ - setnilvalue(s2v(res + i)); - L->top = res + wanted; /* top points after the last result */ break; - } + default: /* multiple results (or to-be-closed variables) */ + if (hastocloseCfunc(wanted)) { + luaF_close(L, res, LUA_OK); + wanted = codeNresults(wanted); /* correct value */ + if (wanted == LUA_MULTRET) + wanted = nres; + } + break; } + firstresult = L->top - nres; /* index of first result */ + /* move all results to correct place */ + for (i = 0; i < nres && i < wanted; i++) + setobjs2s(L, res + i, firstresult + i); + for (; i < wanted; i++) /* complete wanted number of results */ + setnilvalue(s2v(res + i)); + L->top = res + wanted; /* top points after the last result */ } -- cgit v1.2.3-55-g6feb