mirror of
https://github.com/lua/lua.git
synced 2026-01-27 01:44:31 +00:00
Small change in 'trymt'
Operation name can be diferent from metamethod name.
This commit is contained in:
parent
26755cad99
commit
fca974486d
15
lstrlib.c
15
lstrlib.c
@ -269,11 +269,18 @@ static int tonum (lua_State *L, int arg) {
|
||||
}
|
||||
|
||||
|
||||
static void trymt (lua_State *L, const char *mtname) {
|
||||
/*
|
||||
** To be here, either the first operand was a string or the first
|
||||
** operand didn't have a corresponding metamethod. (Otherwise, that
|
||||
** other metamethod would have been called.) So, if this metamethod
|
||||
** doesn't work, the only other option would be for the second
|
||||
** operand to have a different metamethod.
|
||||
*/
|
||||
static void trymt (lua_State *L, const char *mtkey, const char *opname) {
|
||||
lua_settop(L, 2); /* back to the original arguments */
|
||||
if (l_unlikely(lua_type(L, 2) == LUA_TSTRING ||
|
||||
!luaL_getmetafield(L, 2, mtname)))
|
||||
luaL_error(L, "attempt to %s a '%s' with a '%s'", mtname + 2,
|
||||
!luaL_getmetafield(L, 2, mtkey)))
|
||||
luaL_error(L, "attempt to %s a '%s' with a '%s'", opname,
|
||||
luaL_typename(L, -2), luaL_typename(L, -1));
|
||||
lua_insert(L, -3); /* put metamethod before arguments */
|
||||
lua_call(L, 2, 1); /* call metamethod */
|
||||
@ -284,7 +291,7 @@ static int arith (lua_State *L, int op, const char *mtname) {
|
||||
if (tonum(L, 1) && tonum(L, 2))
|
||||
lua_arith(L, op); /* result will be on the top */
|
||||
else
|
||||
trymt(L, mtname);
|
||||
trymt(L, mtname, mtname + 2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user