I need to manipulate addresses. These addresses would be happy with specific operators, namely bitwise operators. Currently the address I manipulate are 32 bits, but I may imagine they become 64bits in future.
Previously two approaches have been taken to support bitwise operators:
- defining a pure lua library
- patching lua with adding bitwise operators to lua_Number
First one may not be very efficient for those needing speed and doesn’t introduce overloadable operators that may be of some use,
Second one is bound to lua_Integer which can be represented with lua_Number. The sign issue arises then with converting the lua_Number to a lua_Integer, doing the bitwise operation and pushing back the result, when the address is > 0x8000000. This problem discouraged me from using the integer patch.
So I’ve tried another heavier approach: introducing a hex type besides numbers, which supports bitwise operators.
This type is represented by an unsigned long long (can be changed in luaconf.h).
I’ve introduced a tohex() that allows converting a “number” or a “string” to a “hex”, and tonumber accepts an hex, there is no implicit conversion.
I’ve also extended APi to support lua*_*hex function equivalent to these for number. (lua_ishex, lua_tohex, …).
It hasn’t been extensively tested, but shall be already quite reliable. It can be totally turned off in luaconf.h (just have to undefine LUA_BITWISE_OPERATORS). It also comes with not so related changes (support for int division noted \ and tolerance of != for ~= ).
Comments welcome on the interest of supporting such a new type.