[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Behavior of lua_arith(L, LUA_OPUNM), arguments to __unm metamethod
- From: Jan Behrens <public@...>
- Date: Thu, 31 Mar 2011 17:19:46 +0200
Hi,
I think I have discovered a bug in Lua 5.2 alpha:
// the following function throws following error with Lua 5.2 alpha:
// "attempt to perform arithmetic on a nil value"
int luaopen_test(lua_State *L) {
lua_settop(L, 0);
lua_newtable(L);
lua_pushinteger(L, 1);
lua_pushnil(L); // when this is commented out: "attempt to perform arithmetic on a table value"
lua_arith(L, LUA_OPUNM);
lua_setfield(L, 1, "result");
return 1;
}
I can get it to work, when I push a second dummy number
instead of nil, but that's not what the specification
requests:
http://www.lua.org/work/doc/manual.html#lua_arith
In any case it might be nicier to change the interface so
that you don't need to push an extra nil (or dummy) value to
the stack when using LUA_OPUNM.
Another issue related to unary minus is the following:
In Lua 5.1:
> t = setmetatable({}, {__unm = print}) print(-t)
table: 0x800e12700 table: 0x800e12700
nil
In Lua 5.2 alpha:
> t = setmetatable({}, {__unm = print}) print(-t)
table: 0x800e0d680 nil
nil
The documentation of both Lua 5.1 and Lua 5.2 alpha imply
that there would only be one argument passed to the
metamethod. The implementation in Lua 5.2 alpha is closer to
the documentation but not yet exactly matching.
Regards
Jan Behrens