Reputation: 14603
If you check some lua
docs, you can see:
There are eight basic types in Lua: nil, boolean, number, string, userdata, function, thread, and table.
What purpose then do the C API functions, such as lua_pushunsigned()
and lua_pushinteger()
serve, since a lua
number type is usually defined as some floating point type (e.g. double
)? Why not just lua_pushnumber()
?
Upvotes: 4
Views: 2057
Reputation: 54589
The implementation for lua_pushinteger and lua_pushnumber are identical except for a check for signalling NaN values in pushnumber. The integer passed to pushinteger is cast to a lua_Number
(which is a double
by default) before pushing it to the Lua stack.
The main advantage of the additional abstraction here is that the exact implementation of the type conversion is handled by Lua and not by the user. For example, you will notice that the casting done for pushunsigned is a little more complex than expected for performance reasons. However, if you prefer to do the casting yourself (and you know the types of lua_Number
and lua_Integer
beforehand) there is no harm in just calling pushnumber everywhere.
Note that the upcoming Lua 5.3 is planned to introduce an integer number type to the language, so the difference between the API functions is probably going to become more relevant there. However, it is still too early to predict the final impact of this change on the API.
Upvotes: 5