aboutsummaryrefslogtreecommitdiff
path: root/lstrlib.c
diff options
context:
space:
mode:
authorRoberto I <roberto@inf.puc-rio.br>2025-10-18 10:34:42 -0300
committerRoberto I <roberto@inf.puc-rio.br>2025-10-18 10:34:42 -0300
commitfca974486d12aa29bb6d731fdb5b25055157ece8 (patch)
tree9e2fd562d0da3480ab1a24d12884c6b621b07255 /lstrlib.c
parent26755cad99cd2a362a3f149114a2e7f05928db0a (diff)
downloadlua-fca974486d12aa29bb6d731fdb5b25055157ece8.tar.gz
lua-fca974486d12aa29bb6d731fdb5b25055157ece8.tar.bz2
lua-fca974486d12aa29bb6d731fdb5b25055157ece8.zip
Small change in 'trymt'
Operation name can be diferent from metamethod name.
Diffstat (limited to '')
-rw-r--r--lstrlib.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/lstrlib.c b/lstrlib.c
index d9735903..23df839e 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -269,11 +269,18 @@ static int tonum (lua_State *L, int arg) {
269} 269}
270 270
271 271
272static void trymt (lua_State *L, const char *mtname) { 272/*
273** To be here, either the first operand was a string or the first
274** operand didn't have a corresponding metamethod. (Otherwise, that
275** other metamethod would have been called.) So, if this metamethod
276** doesn't work, the only other option would be for the second
277** operand to have a different metamethod.
278*/
279static void trymt (lua_State *L, const char *mtkey, const char *opname) {
273 lua_settop(L, 2); /* back to the original arguments */ 280 lua_settop(L, 2); /* back to the original arguments */
274 if (l_unlikely(lua_type(L, 2) == LUA_TSTRING || 281 if (l_unlikely(lua_type(L, 2) == LUA_TSTRING ||
275 !luaL_getmetafield(L, 2, mtname))) 282 !luaL_getmetafield(L, 2, mtkey)))
276 luaL_error(L, "attempt to %s a '%s' with a '%s'", mtname + 2, 283 luaL_error(L, "attempt to %s a '%s' with a '%s'", opname,
277 luaL_typename(L, -2), luaL_typename(L, -1)); 284 luaL_typename(L, -2), luaL_typename(L, -1));
278 lua_insert(L, -3); /* put metamethod before arguments */ 285 lua_insert(L, -3); /* put metamethod before arguments */
279 lua_call(L, 2, 1); /* call metamethod */ 286 lua_call(L, 2, 1); /* call metamethod */
@@ -284,7 +291,7 @@ static int arith (lua_State *L, int op, const char *mtname) {
284 if (tonum(L, 1) && tonum(L, 2)) 291 if (tonum(L, 1) && tonum(L, 2))
285 lua_arith(L, op); /* result will be on the top */ 292 lua_arith(L, op); /* result will be on the top */
286 else 293 else
287 trymt(L, mtname); 294 trymt(L, mtname, mtname + 2);
288 return 1; 295 return 1;
289} 296}
290 297