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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With