[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: LUA_COMPAT_VARARG incompatibility
- From: David Manura <dm.lua@...>
- Date: Wed, 30 Jul 2008 03:08:14 +0000 (UTC)
There is a small class of functions that are valid according to the
Lua 5.1 Reference Manual but will not run successfully when
LUA_COMPAT_VARARG is enabled. This includes the stock version of Lua
5.1.3 and current Lua Binaries, which enable LUA_COMPAT_VARARG by
default.
-- valid Lua 5.1 but fails with LUA_COMPAT_VARARG
do local arg=1; (function(...) assert(arg == 1, ...) end)() end
The behavior of LUA_COMPAT_VARARG is implementation defined, but what
happens in lparser.c is that for a vararg function, the local variable
"arg" is always created (i.e. VARARG_HASARG set), and a table is
initially scheduled to be created in that variable
(i.e. VARARG_NEEDSARG set). Only if "..." is referenced inside the
function is VARARG_NEEDSARG then cleared.
So, it seems odd that the default behavior of 5.1 is to provide
compatibility with 5.0 while breaking compatibility with itself
(albeit in a small way).
Perhaps a better behavior is to make VARARG_HASARG <=>
VARARG_NEEDSARG. Create neither the arg variable nor the arg value
unless there is no reference to "..." in the body. After all, it is
odd and "probably" a typo to place "..." in a parameter list but not
in a function body...unless e.g. if you're running 5.0 code.
function(...) print(1) end -- valid in 5.0 and 5.1
-- but probably an error
function(...) print(arg[1]) end -- valid in 5.0 and 5.1
-- but likely 5.0 intended
function(...) print(...) end -- valid only in 5.1
function(...) print(arg[1], ...) end -- valid only in 5.1
Still, there is a remote chance that the 5.1 behavior is intended in
all cases above (e.g. when doing code generation).