(44 lines)
1 -- This file defines wrapper functions around the Lua 5.2/LuaJIT bitwise operators2 -- The wrapper functions check for the appropriate metatable functions like3 -- "__bxor" for bit.bxor(), etc.4 bitlib = if jit then require('bit')5 elseif _VERSION == "Lua 5.2" bit326 else error("no bit library for Lua 5.3+")8 ret = {k,v for k,v in pairs(bitlib)}9 ret.bnot = (x)->10 if mt = getmetatable(x)11 if mt.__bnot then return mt.__bnot(x)12 return bitlib.bnot(x)13 ret.band = (x,y)->14 if mt_x = getmetatable(x)15 if mt_x.__band then return mt_x.__band(x, y)16 if mt_y = getmetatable(x)17 if mt_y.__band then return mt_y.__band(x, y)18 return bitlib.band(x, y)19 ret.bor = (x,y)->20 if mt_x = getmetatable(x)21 if mt_x.__bor then return mt_x.__bor(x, y)22 if mt_y = getmetatable(x)23 if mt_y.__bor then return mt_y.__bor(x, y)24 return bitlib.bor(x, y)25 ret.bxor = (x,y)->26 if mt_x = getmetatable(x)27 if mt_x.__bxor then return mt_x.__bxor(x, y)28 if mt_y = getmetatable(x)29 if mt_y.__bxor then return mt_y.__bxor(x, y)30 return bitlib.bxor(x, y)31 ret.lshift = (x,y)->32 if mt_x = getmetatable(x)33 if mt_x.__shl then return mt_x.__shl(x, y)34 if mt_y = getmetatable(x)35 if mt_y.__shl then return mt_y.__shl(x, y)36 return bitlib.lshift(x, y)37 ret.rshift = (x,y)->38 if mt_x = getmetatable(x)39 if mt_x.__shr then return mt_x.__shr(x, y)40 if mt_y = getmetatable(x)41 if mt_y.__shr then return mt_y.__shr(x, y)42 return bitlib.rshift(x, y)44 return ret