Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use lua_tointeger() for off_t?

Tags:

c

lua

Should I use lua_tointeger(), or lua_tonumber(), when converting Lua numbers to off_t values?

I checked the source code of Lua itself and I see that their file:seek function uses lua_Number, not lua_Integer.

I also see that the luaposix package uses lua_tonumber() (or luaL_checknumber() etc) extensively, even to read file decriptors(!).

And what about size_t?

Should I go to the "extreme" and use lua_tonumber() (and lua_pushnumber()) for all integral C types (mode_t, size_t, etc.)? Or should I normally use lua_tointeger() and resort to lua_tonumber() only when I "feel" it's a potentially big number?

like image 754
Niccolo M. Avatar asked Feb 14 '26 05:02

Niccolo M.


1 Answers

Since off_t and size_t are both integral types, you should use lua_tointeger.

From the source, lua_tointeger actually gets the number as lua_Number, then converts it to lua_Integer. The only concern is that lua_Integer may be too small, but as it's implemented as ptrdiff_t which in practice is usually 32-bit on 32-bit machine and 64-bit on 64-bit machine.

like image 101
Yu Hao Avatar answered Feb 15 '26 20:02

Yu Hao