lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Asko Kauppi wrote:
> n.b. normally the only place where enums are defined (within LuaX)  
> are the C modules. This comes from the simple reason that any  
> bitfields normally are used only in contact with C code.  In Lua only  
> world, there are other ways.

IMHO writing a C function for every bitfield I want to use
is not the way to go. Most of the bitfields I have are really
N bit integers and not enumerations. Of course bitfields are
a low-level approach, but they _are_ more generic. :-)

A simple example:
  local function wputd(n)
    if n < 0 then
      n = n + 4294967296
    local r = n % 256
    n = (n - r) / 256
    r = n % 256
    n = (n - r) / 256
    r = n % 256
    n = (n - r) / 256
    wputb(r, n)

Why oh why can't I simply write:

  local function wputd(n)
    wputb(bitfield(n, 0,8, 8,8, 16,8, 24,8))

I spare you the code for some of the more uglier examples
like splitting the ModRM and SIB bytes of an x86 opcode. :-)

And I really don't want to write things like this anymore:

  local opcode = op == "movzx" and 4023 or 4031

But rather:

  local opcode = op == "movzx" and 0x0fb7 or 0x0fbf

Just so this doesn't sound like a rant about a specific
application: dealing with some network protocols (like DNS
or TLS) is really, really ugly without hex numbers and
bitfields. Don't tell me I have to write this in C ...