lua-users home
lua-l archive

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

It was thus said that the Great Andrew Starks once stated:
> We have our nanomsg binding that we recently remade. It was very C heavy
> and now it's the opposite. We're making an "nml.core" that looks *pretty
> much* exactly like the C API. The higher level binding wraps it in a
> Lua-esque API.
> If you have made bindings this way, how far do you take the idea of
> sticking to the C API in your core layer?
> Do you strive for perfect adherence to the original C documentation? Do you
> change things like "-1" or "0" to "false", where appropriate? Do you use
> multiple returns where their API is using pointers?

  If I want to program in C, I know where it is.

  Basically, I don't constrain myself to slavisly follow the C API.  For
instance, when I wrapped the Berzerkly sockets API [1], at first I followed
the C API but hated it.  I then changed it to make it nicer to use (existing
Unix networking tutorials be damned).

  So, wereas in C, you do:

	struct sockaddr_in addr;
	int                reuse = 1;

	addr.sin_family = AF_INET;
	addr.sin_port   = htons(8080);
	sock = socket(AF_INET,SOCK_STREAM,0);
	bind(sock,(struct sockaddr *)&addr,sizeof(addr));

which is grind-grind-grind-grind, I worked the Lua API to be:

	addr ="","tcp",8080)
	sock =,"tcp")
	sock.reuseaddr = true

  You can see I wrapped calls to setsockopt() in the handler for __index()
(view a list of all such "variables" here [2]).

	addr ="","any","tcp",8080)
	sock =[1].family,"tcp")

where can return an IPv4 *OR* an IPv6 address, and
it'll just work (FYI: returns a list of

  In the general case, the more familiar I am with a particular C API, the
more likely I'll diverge wildly from it in a Lua wrapper.

  -spc (Again, because if I want to program in C, I know where to find it)