diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-17 15:22:11 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-17 15:22:11 -0300 |
commit | 9c28ed05c95cb6854d917ac3e3ed7be9ae109480 (patch) | |
tree | 8f88e8fecf5be55fd9a7413683d033e07d5756d3 | |
parent | 8082906c059f2b1473de4363ca57fe19b52f281f (diff) | |
download | lua-9c28ed05c95cb6854d917ac3e3ed7be9ae109480.tar.gz lua-9c28ed05c95cb6854d917ac3e3ed7be9ae109480.tar.bz2 lua-9c28ed05c95cb6854d917ac3e3ed7be9ae109480.zip |
Calls 'luaF_close' in 'lua_settop' only when needed
In 'lua_settop', avoid calling 'luaF_close' when increasing the stack
or when the function has no to-be-closed variables.
-rw-r--r-- | lapi.c | 12 |
1 files changed, 7 insertions, 5 deletions
@@ -170,12 +170,13 @@ LUA_API int lua_gettop (lua_State *L) { | |||
170 | 170 | ||
171 | 171 | ||
172 | LUA_API void lua_settop (lua_State *L, int idx) { | 172 | LUA_API void lua_settop (lua_State *L, int idx) { |
173 | StkId func = L->ci->func; | 173 | CallInfo *ci = L->ci; |
174 | int diff; /* difference for new top */ | 174 | StkId func = ci->func; |
175 | ptrdiff_t diff; /* difference for new top */ | ||
175 | lua_lock(L); | 176 | lua_lock(L); |
176 | if (idx >= 0) { | 177 | if (idx >= 0) { |
177 | api_check(L, idx <= L->ci->top - (func + 1), "new top too large"); | 178 | api_check(L, idx <= ci->top - (func + 1), "new top too large"); |
178 | diff = (func + 1) + idx - L->top; | 179 | diff = ((func + 1) + idx) - L->top; |
179 | for (; diff > 0; diff--) | 180 | for (; diff > 0; diff--) |
180 | setnilvalue(s2v(L->top++)); /* clear new slots */ | 181 | setnilvalue(s2v(L->top++)); /* clear new slots */ |
181 | } | 182 | } |
@@ -183,7 +184,8 @@ LUA_API void lua_settop (lua_State *L, int idx) { | |||
183 | api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); | 184 | api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); |
184 | diff = idx + 1; /* will "subtract" index (as it is negative) */ | 185 | diff = idx + 1; /* will "subtract" index (as it is negative) */ |
185 | } | 186 | } |
186 | luaF_close(L, L->top + diff, LUA_OK); | 187 | if (diff < 0 && hastocloseCfunc(ci->nresults)) |
188 | luaF_close(L, L->top + diff, LUA_OK); | ||
187 | L->top += diff; /* correct top only after closing any upvalue */ | 189 | L->top += diff; /* correct top only after closing any upvalue */ |
188 | lua_unlock(L); | 190 | lua_unlock(L); |
189 | } | 191 | } |