summaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-11-12 14:15:50 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-11-12 14:15:50 -0200
commit5fda30b4f9f82b901113a6e666c797f835c708eb (patch)
treea8011434f8c2ccee920d955db05ba4917a00e5f8 /lapi.c
parent9eafe9c053ef17a0980ab32082bf229bd58e963b (diff)
downloadlua-github-5fda30b4f9f82b901113a6e666c797f835c708eb.tar.gz
'lua_toclose' gets the index to be closed as an argument
Sometimes it is useful to mark to-be-closed an index that is not at the top of the stack (e.g., if the value to be closed came from a function call returning multiple values).
Diffstat (limited to 'lapi.c')
-rw-r--r--lapi.c13
1 files changed, 10 insertions, 3 deletions
diff --git a/lapi.c b/lapi.c
index 91a6e389..da866a66 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1207,12 +1207,19 @@ LUA_API int lua_next (lua_State *L, int idx) {
}
-LUA_API void lua_toclose (lua_State *L) {
- int nresults = L->ci->nresults;
- luaF_newtbcupval(L, L->top - 1); /* create new to-be-closed upvalue */
+LUA_API void lua_toclose (lua_State *L, int idx) {
+ int nresults;
+ StkId o;
+ lua_lock(L);
+ o = index2stack(L, idx);
+ nresults = L->ci->nresults;
+ api_check(L, L->openupval == NULL || uplevel(L->openupval) < o,
+ "there is an already marked index below");
+ luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */
if (!hastocloseCfunc(nresults)) /* function not marked yet? */
L->ci->nresults = codeNresults(nresults); /* mark it */
lua_assert(hastocloseCfunc(L->ci->nresults));
+ lua_unlock(L);
}