From cc1692515e2a6aabc6d07159e7926656e38eda53 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 11 Jan 2021 15:03:01 -0300 Subject: New API function 'lua_closeslot' Closing a to-be-closed variable with 'lua_settop' is too restrictive, as it erases all slots above the variable. Moreover, it adds side effects to 'lua_settop', which should be a fairly basic function. --- lauxlib.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'lauxlib.c') diff --git a/lauxlib.c b/lauxlib.c index 074ff08c..e8fc486e 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -545,10 +545,8 @@ static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { if (buffonstack(B)) /* buffer already has a box? */ newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */ else { /* no box yet */ - lua_pushnil(L); /* reserve slot for final result */ newbox(L); /* create a new box */ - /* move box (and slot) to its intended position */ - lua_rotate(L, boxidx - 1, 2); + lua_insert(L, boxidx); /* move box to its intended position */ lua_toclose(L, boxidx); newbuff = (char *)resizebox(L, boxidx, newsize); memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */ @@ -585,8 +583,8 @@ LUALIB_API void luaL_pushresult (luaL_Buffer *B) { lua_State *L = B->L; lua_pushlstring(L, B->b, B->n); if (buffonstack(B)) { - lua_copy(L, -1, -3); /* move string to reserved slot */ - lua_pop(L, 2); /* pop string and box (closing the box) */ + lua_closeslot(L, -2); /* close the box */ + lua_remove(L, -2); /* remove box from the stack */ } } -- cgit v1.2.3-55-g6feb