lua-users home
lua-l archive

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


Hallo,

On 3/13/07, Peter Odding <xolox@home.nl> wrote:
Hi all! (again)

I'm writing a binding to the Apache Portable Runtime[1] for Lua. This is
my first real C module for Lua and I've run into some `idiom
mismatches', so I want to know what other potential users prefer:

*Bitfields*
APR uses bitfield extensively. Some of this functionality is too low
level for Lua, but other stuff I really want to make available. For
example when calling stat[2] some properties (13 total) might be
expensive to retrieve, so they must be explicitly requested with a
bitfield of property flags. Up till now I've used a string where each
character maps to a flag. This is concise but feels obfuscated. The
other option is to let the user create a bitfield from Lua numbers,
which are `constants' my library makes available. This is clear and
readable, but very verbose, and the operators | and + are not really
compatible, i.e. adding the same flag twice basically destroys the
bitfield... Any other suggestions?


    Make your function accept a list of strings, each one describing
an option. Strings are interned and comparing for equality is fast.
Then, in your C code, convert each string to an integer and "or" them.
The conversion can be done using an upvalue table.

*Microseconds*
The `Time Routines' module[3] uses microseconds as it's basic unit of
time. Lua uses the C runtime, which most of the time uses seconds (on
Windows and Linux at least AFAIK). Should my binding convert between the
two, or let users do [T / 1000000] when they want to use os.date or
os.difftime? If my reasoning is correct, the division my library makes
should not lose precision when Lua uses floating point numbers... Right?
Which would you prefer?


    Use fractional seconds.

--
-alex
http://www.ventonegro.org/