LuaJit vs number size & doubts about my game

Integers are 32 or 64 bits depending on build target. HTML5 is the spoiler, as there’s no 64 bit target for it. Please be aware that HTML5 uses vanilla Lua 5.1 while the other platforms uses LuaJIT (i.e. different Lua implementations).

Numbers in Lua (as configured by Defold) will always be 64 bit doubles. The characteristics of double is determined by the platform’s implementation (e.g. are they IEEE 754, and are they rounded up or down). In C++ extensions, you will get and set these as lua_Number. It’s defined like this:

#define LUA_NUMBER double
typedef LUA_NUMBER lua_Number;

Unfortunately, this means that in Lua code you don’t have access to the full 64 bit integer range, even on 64 bit platforms, since double cannot represent the full range. Additionally, if you plan to release on web, you may want to avoid integer values greater than 32 bit in C++ extensions when possible, since Lua’s own integer type will be 32 bit.

If you want to do integer operations in C++, you can call lua_tointeger to convert a lua_Number to a lua_Integer, which is defined like this:

#define LUA_INTEGER	ptrdiff_t // will be 32 or 64 bit depending on platform
typedef LUA_INTEGER lua_Integer;

To assign a Lua variable a lua_Integer value, you can call lua_pushinteger which will convert the integer to a lua_Number before assigning it. You can convert lua_Number to int and lua_Integer with lua_number2int and lua_number2integer respectively.

If you want to do bit operations to hash strings, you may be able to use the builtin bit module and make sure you work with integer values no greater than 32 bit. The bit module converts between number and integer internally. Alternatively, you can write a C++ extension, allowing you to more freely operate on integer values before converting back to number/double so that they can be passed to Lua code.

5 Likes