Making LuaJIT built-in bit library work with 64 bit numbers

  • From: Konstantin Osipov <kostja@xxxxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 3 Oct 2012 00:13:26 +0400

Hi,

To make 64-bit integers available in a LuaJIT instance,
while keeping defaults Lua-compatible (we need a floating point
number as well), we use the following binding: 

int
luaL_pushnumber64(struct lua_State *L, uint64_t val)
{
    GCcdata *cd = luaL_pushcdata(L, CTID_UINT64, 8);
    *(uint64_t*)cdataptr(cd) = val;
    return 1;
}

It works nicely with all arithmetic operations, but not 
with the built-in LuaJIT bit.* library. For example (tonumber64 is our
extension above):

lua> i = tonumber64(1)
lua> type(i)
cdata
lua> i
1
lua> i = i + 1
lua> type(i)
cdata
lua> i
2
lua> i = bit.band(i, 1)
error: [string "i = bit.band(i, 1)"]:1: bad argument #1 to 'band' (number 
expected, got cdata)

Is there a way to fix this?

-- 
http://tarantool.org - an efficient, extensible in-memory data store

Other related posts: