From 59e565d9555c07e82808d8c1db8f4f4d159b5e5c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 29 Dec 2020 10:23:02 -0300 Subject: No need to recheck close method before calling it A to-be-closed variable is constant and it must have a close metamethod when it is created. A program has to go out of its way (e.g., by changing the variable's metamethod) to invalidate that check. So, it is not worth to test that again. If the program tampers with the metamethod, Lua will raise a regular error when attempting to call it. --- lfunc.c | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) (limited to 'lfunc.c') diff --git a/lfunc.c b/lfunc.c index ae68487c..a8030afa 100644 --- a/lfunc.c +++ b/lfunc.c @@ -101,31 +101,32 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { /* -** Prepare closing method plus its arguments for object 'obj' with -** error message 'err'. (This function assumes EXTRA_STACK.) +** Call closing method for object 'obj' with error message 'err'. +** (This function assumes EXTRA_STACK.) */ -static int prepclosingmethod (lua_State *L, TValue *obj, TValue *err) { +static void callclosemethod (lua_State *L, TValue *obj, TValue *err) { StkId top = L->top; const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); - if (ttisnil(tm)) /* no metamethod? */ - return 0; /* nothing to call */ setobj2s(L, top, tm); /* will call metamethod... */ setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */ setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */ L->top = top + 3; /* add function and arguments */ - return 1; + luaD_callnoyield(L, top, 0); /* call method */ } /* -** Raise an error with message 'msg', inserting the name of the -** local variable at position 'level' in the stack. +** Check whether 'obj' has a close metamethod and raise an error +** if not. */ -static void varerror (lua_State *L, StkId level, const char *msg) { - int idx = cast_int(level - L->ci->func); - const char *vname = luaG_findlocal(L, L->ci, idx, NULL); - if (vname == NULL) vname = "?"; - luaG_runerror(L, msg, vname); +static void checkclosemth (lua_State *L, StkId level, const TValue *obj) { + const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); + if (ttisnil(tm)) { /* no metamethod? */ + int idx = cast_int(level - L->ci->func); /* variable index */ + const char *vname = luaG_findlocal(L, L->ci, idx, NULL); + if (vname == NULL) vname = "?"; + luaG_runerror(L, "variable '%s' got a non-closable value", vname); + } } @@ -136,7 +137,7 @@ static void varerror (lua_State *L, StkId level, const char *msg) { ** the 'level' of the upvalue being closed, as everything after that ** won't be used again. */ -static void callclosemth (lua_State *L, StkId level, int status) { +static void prepcallclosemth (lua_State *L, StkId level, int status) { TValue *uv = s2v(level); /* value being closed */ TValue *errobj; if (status == CLOSEKTOP) @@ -145,10 +146,7 @@ static void callclosemth (lua_State *L, StkId level, int status) { errobj = s2v(level + 1); /* error object goes after 'uv' */ luaD_seterrorobj(L, status, level + 1); /* set error object */ } - if (prepclosingmethod(L, uv, errobj)) /* something to call? */ - luaD_callnoyield(L, L->top - 3, 0); /* call method */ - else if (!l_isfalse(uv)) /* non-closable non-false value? */ - varerror(L, level, "attempt to close non-closable variable '%s'"); + callclosemethod(L, uv, errobj); } @@ -171,16 +169,12 @@ void luaF_newtbcupval (lua_State *L, StkId level) { lua_assert(L->openupval == NULL || uplevel(L->openupval) < level); if (!l_isfalse(obj)) { /* false doesn't need to be closed */ int status; - const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); - if (ttisnil(tm)) /* no metamethod? */ - varerror(L, level, "variable '%s' got a non-closable value"); + checkclosemth(L, level, obj); status = luaD_rawrunprotected(L, trynewtbcupval, level); if (unlikely(status != LUA_OK)) { /* memory error creating upvalue? */ lua_assert(status == LUA_ERRMEM); luaD_seterrorobj(L, LUA_ERRMEM, level + 1); /* save error message */ - /* next call must succeed, as object is closable */ - prepclosingmethod(L, s2v(level), s2v(level + 1)); - luaD_callnoyield(L, L->top - 3, 0); /* call method */ + callclosemethod(L, s2v(level), s2v(level + 1)); luaD_throw(L, LUA_ERRMEM); /* throw memory error */ } } @@ -215,7 +209,7 @@ void luaF_close (lua_State *L, StkId level, int status) { } if (uv->tbc && status != NOCLOSINGMETH) { ptrdiff_t levelrel = savestack(L, level); - callclosemth(L, upl, status); /* may change the stack */ + prepcallclosemth(L, upl, status); /* may change the stack */ level = restorestack(L, levelrel); } } -- cgit v1.2.3-55-g6feb