lua-users home
lua-l archive

Re: Should we explicitly cast from luaL_checkinteger() et al?

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Am 23.01.2015 um 04:37 schröbte William Ahern:
On Fri, Jan 23, 2015 at 01:05:48AM +0200, Niccolo Medici wrote:
<snip>
Lua 5.3 deprecates luaL_checkint, luaL_checklong, luaL_optint,
luaL_optlong, which were just convenience macros calling
luaL_{check|opt}integer.
While we can still use them (because of -DLUA_COMPAT_5_2), they aren't
mentioned in the user manual and we're advised to use
luaL_{check|opt}integer "with a type cast".
[...]
(2) If a cast isn't needed here, where *is* it needed?
Assuming an architecture where sizeof (int) < sizeof (lua_Integer), then the
following would print two different results:
 printf("%zu\n", sizeof luaL_checkinteger(0, 0));
 printf("%zu\n", sizeof ((int)luaL_checkinteger(0, 0)));
I'll throw in two more examples:
 printf("%d\n", (int)luaL_checkinteger(L, 1));
(undefined behavior without the cast)
or:
 lua_Integer x = 0U + (int)luaL_checkinteger(L, 1);
(with cast: 0 <= x <= UINT_MAX, without cast: LUA_MININTEGER <= x <= LUA_MAXINTEGER)
<snip>
(4) Aren't we losing "documentation" by not having the words
"int"/"long" embedded in the function name?
The "int"/"long" is embedded in the cast instead, which is a normal place for C programmers to look. Actually, we *gain* documentation, because we have the target type *and* the internal Lua type mentioned explicitly in the code.
Philipp

AltStyle によって変換されたページ (->オリジナル) /